Passing strings to a plugin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing strings to a plugin

Bob.Cowdery
Passing strings to a plugin

Hi

I have a plugin as follows:

run: hostApi inDev: inDeviceName outDev: outDeviceName rate: sampleRate frames: frames latency: latency

        self export: true.

        self    primitive: 'run'
                parameters:#(String String String SmallInteger SmallInteger SmallInteger).

        (self
                cCode: 'sdrOpenMainStream(hostApi, inDeviceName, outDeviceName, sampleRate, frames, latency)'
                inSmalltalk: [false])
                ifFalse: [^ interpreterProxy primitiveFail].

When I look at the parameters on the C side the strings are sometimes correct (depending on their length and content) and sometimes have rubbish after them like the terminator is missing. The integers are always ok. Do I have to explicitly terminate the strings?

Thanks for any help.

Bob

*** Confidentiality Notice *** Proprietary/Confidential
Information belonging to CGI Group Inc. and its affiliates
may be contained in this message. If you are not a recipient
indicated or intended in this message (or responsible for
delivery of this message to such person), or you think for
any reason that this message may have been addressed to you
in error, you may not use or copy or deliver this message
to anyone else.  In such case, you should destroy this
message and are asked to notify the sender by reply email.



Reply | Threaded
Open this post in threaded view
|

Re: Passing strings to a plugin

Ragnar Hojland Espinosa-2
On 4/2/06, [hidden email] <[hidden email]> wrote:
>         self    primitive: 'run'
>                 parameters:#(String String String SmallInteger SmallInteger
> SmallInteger).
> [...]
> When I look at the parameters on the C side the strings are sometimes
> correct (depending on their length and content) and sometimes have rubbish
> after them like the terminator is missing. The integers are always ok. Do I
> have to explicitly terminate the strings?

Either that or pass along their length.
--
Ragnar

Reply | Threaded
Open this post in threaded view
|

RE: Passing strings to a plugin

Bob.Cowdery
In reply to this post by Bob.Cowdery
RE: Passing strings to a plugin

>> Either that or pass along their length.

Thanks Ragnar. Is there an easy was to null terminate a string in Squeak?

Bob

*** Confidentiality Notice *** Proprietary/Confidential
Information belonging to CGI Group Inc. and its affiliates
may be contained in this message. If you are not a recipient
indicated or intended in this message (or responsible for
delivery of this message to such person), or you think for
any reason that this message may have been addressed to you
in error, you may not use or copy or deliver this message
to anyone else.  In such case, you should destroy this
message and are asked to notify the sender by reply email.



Reply | Threaded
Open this post in threaded view
|

Re: Passing strings to a plugin

David T. Lewis
On Sun, Apr 02, 2006 at 02:02:41PM +0100, [hidden email] wrote:
>
> >> Either that or pass along their length.
>
> Thanks Ragnar. Is there an easy was to null terminate a string in Squeak?

In the image you can of course do something like this:
    'hello', (Character value: 0) asString

If you want to pass a String parameter to your plugin then use it as
a C string, see the #cStringFromString and #transientCStringFromString:
methods in OSProcessPlugin (OSPP is on Squeak Map).

cStringFromString: aString
        "Answer a new null-terminated C string copied from aString. The C string
        is allocated from the C runtime heap. See transientCStringFromString for
        a version which allocates from object memory.
        Caution: This may invoke the garbage collector."

        | len sPtr cString |
        self returnTypeC: 'char *'.
        self var: 'sPtr' declareC: 'char *sPtr'.
        self var: 'cString' declareC: 'char *cString'.
        sPtr _ interpreterProxy arrayValueOf: aString.
        len _ interpreterProxy sizeOfSTArrayFromCPrimitive: sPtr.
        cString _ self callocWrapper: len + 1 size: 1. "Space for a null terminated C string."
        self cCode: '(char *) strncpy (cString, sPtr, len)'. "Copy the string."
        ^ cString


transientCStringFromString: aString
        "Answer a new null-terminated C string copied from aString.
        The string is allocated in object memory, and will be moved
        without warning by the garbage collector. Any C pointer
        reference the the result is valid only until the garbage
        collector next runs. Therefore, this method should only be used
        within a single primitive in a section of code in which the
        garbage collector is guaranteed not to run. Note also that
        this method may itself invoke the garbage collector prior
        to allocating the new C string.

        Warning: The result of this method will be invalidated by the
        next garbage collection, including a GC triggered by creation
        of a new object within a primitive. Do not call this method
        twice to obtain two string pointers."

        | len stringPtr newString cString |
        self returnTypeC: 'char *'.
        self var: 'stringPtr' declareC: 'char *stringPtr'.
        self var: 'cString' declareC: 'char *cString'.
        len _ interpreterProxy sizeOfSTArrayFromCPrimitive: (interpreterProxy arrayValueOf: aString).
        "Allocate space for a null terminated C string."
        interpreterProxy pushRemappableOop: aString.
        newString _ interpreterProxy
                instantiateClass: interpreterProxy classString
                indexableSize: len + 1.
        stringPtr _ interpreterProxy arrayValueOf: interpreterProxy popRemappableOop.
        cString _ interpreterProxy arrayValueOf: newString. "Point to the actual C string."
        self cCode: '(char *)strncpy(cString, stringPtr, len)'. "Make a copy of the string."
        cString at: (len) put: 0. "Null terminate the C string."
        ^ cString


Dave