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'
(self
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 |
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 |
In reply to this post by Bob.Cowdery
>> 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 |
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 |
Free forum by Nabble | Edit this page |