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



Georgios Petasis wrote:
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.

Hi,

That's not entirely accurate and overly general.

Here's an example:

#include <stdio.h>

int main () {
char c = 'a';
char *cp = &c;
char **cpp = &cp;

printf ("%c\n", **cpp);

return 0;
}

This explains more about the differences between arrays and pointers:
http://c-faq.com/aryptr/index.html

Arrays decay to pointers in some cases.

It's important to know the distinction because:

int main() {
char *a = "Hello World";
char b[] = "Hello World";

a[0] = 'a'; /*faults usually with read-only string literals/constants*/
b[0] = 'b'; /*works*/

printf ("%s %s\n", a, b);

return 0;
}


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;

Tcl_Alloc() will panic if it's unable to allocate memory. It never
returns if the request can't be met, so the "if (array)" isn't
necessary.

}
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):

I don't mean to be overly critical, but I believe your code is violating
the C++ standard and possibly will break.

http://developer.mozilla.org/en/docs/C%2B%2B_Portability_Guide#Don.27t_use_reserved_words_as_identifiers

And I quote from that:
"According to the C++ Standard, 17.4.3.1.2 Global Names
[lib.global.names], paragraph 1:

Certain sets of names and function signatures are always reserved to the
implementation:

* Each name that contains a double underscore (__) or begins with an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.
* Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace."


So I guess you could wrap it in a namespace, or rewrite it to not use an
underscore prefix for _size. C has a similar restriction with
underscore prefixes. Sometimes as an alternative programmers use a _
suffix for instance variables.


I hope this constructive criticism helps.

You know more about swig than I do. :-)


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: 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)
  • Re: Window Management
    ... (* prompt is a string resource identifier specifying the string ... PROCEDURE YesNoCancel(prompt: ARRAY OF CHAR; ... VAR INOUT response: ARRAY OF CHAR): BOOLEAN; ...
    (comp.lang.ada)