primitives and corrections/arrays?

Ned Konz ned at bike-nomad.com
Thu Oct 16 16:09:49 UTC 2003


On Thursday 16 October 2003 7:53 am, Ragnar Hojland Espinosa wrote:
> I'm trying to pass a collection/array of strings through a primitive,
> but its not working.. I'm having trouble at accessing the data in the
> collection.  Example code pointers welcome.. I'm stuck :(

I assume you've read my "Writing Plugins" document, and Andy Greenberg's 
plugins tutorial. If not, look at:
http://minnow.cc.gatech.edu/squeak/370

And that you're using the SmartSyntaxInterpreterPlugin.

> I guess I'll find the same problem when trying to populate an array
> from C back to squeak..
>
> BTW, I'm putting together some rather noobish notes on plugin writings
> on http://minnow.cc.gatech.edu/squeak/3408 FWIW..
>
>
> primitiveLDAPSearch: someAttrs
>
> 	| i buffer buflen attrs mystr |
>
> 	self var: #buffer declareC: 'char buffer[8192]'.
> 	self var: #buflen declareC: 'int buflen'.
> 	self var: #attrs declareC: 'char** attrs'.
> 	self var: #i declareC: 'int i'.
>
> 	self primitive: 'primitiveLDAPSearch' parameters: #(ByteArray).

Is someAttrs *really* a ByteArray? That is, did you pack the attrs into it? Or 
is it an Array of String or something?

Seems like you want to use Array instead, and then index into it.

It is simpler to pack the strings into a ByteArray and then strdup them, but 
your code isn't doing that (apparently). Instead, it's reading bytes out of a 
byte array and then trying to duplicate them.

>
> 	buflen _ someAttrs size.

	buflen := interpreterProxy sizeOfSTArrayFromCPrimitive: someAttrs.

> 	self cCode: 'attrs = (char**) malloc (buflen)'.

No. I would think you'd need to:
	attrs = (char**) malloc(buflen * sizeof(char*));
or
	attrs = (char**) calloc(buflen, sizeof(char*));

> 	i _ 0.
>
> 	1 to: (someAttrs size) do: [ :idx |
> 		mystr _ someAttrs at: idx.
> 		buflen _ interpreterProxy byteSizeOf: mystr  cPtrAsOop.

Yeah, but it's not a string; it's a character if you have a ByteArray.

> 		self cCode: 'strncpy (buffer, mystr, buflen)'.
> 		buffer at: buflen put: 0.
> 		self cCode: 'attrs[i++] = strdup(buffer)'.
> 	].
>
> 	self cCode: 'LDAP_Search  (attrs)'.
> 	^ true asOop: Boolean
>
> I also tried the following, as the do: wasnt vorking very well..
>
> 	self primitive: 'primitiveLDAPSearch' parameters: #(Oop  Array).
>
> 	buflen _ someAttrs size.
> 	self cCode: 'attrs = (char**) malloc (buflen)'.
> 	i _ 0.
> 	someAttrs do: [:each |
> 		buflen _ interpreterProxy byteSizeOf: each cPtrAsOop.
> 		self cCode: 'strncpy (buffer, each, buflen)'.
> 		buffer at: buflen put: 0.
> 		self cCode: 'attrs[i++] = strdup(each)'.
> 	].
>
> 	self cCode: 'LDAP_Search (attrs)'.

-- 
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE



More information about the Squeak-dev mailing list