Re: need a tiny help with my SWIG'd program
- From: Georgios Petasis <petasis@xxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Feb 2008 18:55:10 +0200
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
.
- Follow-Ups:
- Re: need a tiny help with my SWIG'd program
- From: Paul@Tcl3D
- Re: need a tiny help with my SWIG'd program
- From: Robert Heller
- Re: need a tiny help with my SWIG'd program
- References:
- need a tiny help with my SWIG'd program
- From: Mel
- Re: need a tiny help with my SWIG'd program
- From: Georgios Petasis
- need a tiny help with my SWIG'd program
- Prev by Date: Re: need a tiny help with my SWIG'd program
- Next by Date: Re: fconfigure -translation binary conversion
- Previous by thread: Re: need a tiny help with my SWIG'd program
- Next by thread: Re: need a tiny help with my SWIG'd program
- Index(es):
Relevant Pages
|