|
String Arrays: msg#00158programming.swig
Hello, a few thoughts: Yes, as far as I know typemaps will fix you problem (sample later) > > #include <stdio.h> > > char (*test (void))[33] > { > static char output[2][33]; > > sprintf (output[0], "Test"); > sprintf (output[1], "More"); > > return (&output[0]); > } > I couldnt see a good way to deal with this one. You end up with a pointer, but not much info on how many strings you get. So I rewrote the code to take the array & fill it instead: void get_strings(char output[][33],int* len) { sprintf (output[0], "Test"); sprintf (output[1], "More"); *len=2; } This can we wrappered with the following typemap: /* this first typemap tells SWIG how to deal with the input side of the function. it declared 2 variables (char output[10][33], int len) prepares them for calling into the function and tells SWIG that it should not expect any parameters in */ %typemap(in, numinputs=0) (char output[][33],int* len) (char output[10][33], int len) { $1 = output; $2 = &len; } /* this second typemap is for the returning of the data after the fn call it takes the array & converts to a python array */ %typemap(argout) (char output[][33],int* len) (int i,PyObject *t) { Py_XDECREF($result); // throw away any previous result // (not such a good idea...) $result= PyList_New(*$2); // make a list of length *len for (i = 0; i < *$2; ++i) { t=PyString_FromString($1[i]); // c-string to py-string PyList_SetItem($result, i, t); // add to the list } } After swigging (I used Windows & Mingw to compile this & ran on python 2.2.1) >>> import test >>> print test.get_strings() ['Test', 'More'] >>> Hope this helps Mark <:8)~ _______________________________________________ Swig maillist - Swig@xxxxxxxxxxxxxxx http://mailman.cs.uchicago.edu/mailman/listinfo/swig |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: "inheritance" order in Perl proxies: 00158, Jason E. Stewart |
|---|---|
| Next by Date: | RE: renaming the .so in C->python: 00158, Volker Boerchers |
| Previous by Thread: | String Arraysi: 00158, Alastair Burnett |
| Next by Thread: | how to prevent objects from being garbage collected.: 00158, meinrad recheis |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |