Re: need a tiny help with my SWIG'd program



O/H Georgios Petasis έγραψε:
O/H Mel έγραψε:
the C function i am calling requires a:
'char ** myvar"

what would I be passing as TCL var to this function ?

void junk(char **' myvar)

in tcl I would call something like:

junk <what should I put here>

many thanks for your help

It is not so easy :-)
char ** means an array of null terminated strings. There in nothing in tcl that can be converted to this type. So, you have to add to your header file some C code to generate such structures.

For example, in a similar situation I have added the following helper code:

#ifdef SWIG
%rename(String) array_string;
#endif
#if defined(ARRAY_STRING_CLASS)
class array_string {
char **array;
int _size;
public:
array_string(int size) {
_size=size;
array = (char **) Tcl_Alloc(sizeof(char*)*_size);
if (array) for(int i=0; i<_size;++i) array[i]=NULL;
}
void setitem(int i, char* value) {
if (!array || i < 0 || i >= _size) return;
if(array[i]) Tcl_Free(array[i]);
array[i]=Tcl_Alloc(sizeof(char)*(strlen(value)+2));
strcpy(array[i], value);
}
char *getitem(int i) {
if (!array || i<0 || i>=_size) return NULL;return array[i];
}
char** cast(void) {return array;}
~array_string() {
if (array) {
for(int i=0; i<_size; ++i) {
if (array[i]) Tcl_Free(array[i]);
}
Tcl_Free((char *)array);
}
}
};
#endif /* ARRAY_STRING_CLASS */

Them, from tcl you can do (assuming that you have enabled shadow classes):

String x 5
x setitem 0 "string 1"
x setitem 1 "string 2"
...
junk [x cast]
## delete x...
delete_String x

George


Of course, you could also try to convert a tcl list into this, but its easier to do this from the tcl level than at the C level :-)
The ultimate solution will be to do tcl list -> char ** conversion as an argument template, and just call your function with a tcl list as a parameter. Perhaps there is some ready code if you search at google.

George
.



Relevant Pages

  • Re: need a tiny help with my SWIGd program
    ... what would I be passing as TCL var to this function? ... char ** means an array of null terminated strings. ... int objcPtr,i; ... Type map for stringVector: convert from a Tcl list on input. ...
    (comp.lang.tcl)
  • Re: need a tiny help with my SWIGd program
    ... what would I be passing as TCL var to this function? ... junk ... char ** means an array of null terminated strings. ... x setitem 0 "string 1" ...
    (comp.lang.tcl)
  • Re: need a tiny help with my SWIGd program
    ... what would I be passing as TCL var to this function? ... char ** means an array of null terminated strings. ... void setitem{ ... x setitem 0 "string 1" ...
    (comp.lang.tcl)
  • Re: Unicode or ASCII - perhabs that a problem?
    ... I get the length of the unicode string with Tcl_GetCharLength ... I use this char array inside the attached library, ... In this context - please don't ask for using Tcl_Channels, ...
    (comp.lang.tcl)
  • Re: need a tiny help with my SWIGd program
    ... what would I be passing as TCL var to this function? ... char ** means an array of null terminated strings. ... returns if the request can't be met, so the "if (array)" isn't necessary. ... Each name that contains a double underscore or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use. ...
    (comp.lang.tcl)