Swig question


#1

I’ve made a swig wrapper for some code that needs a char pointer passed
in, how do I do such a thing?


#2

On May 24, 2007, at 8:07 PM, Brett L. Trotter wrote:

Traceback (most recent call last):
File “./test.py”, line 18, in ?
main()
File “./test.py”, line 15, in main
encode_rs_char(rs_handle, teststr, paritystr)
TypeError: in method ‘encode_rs_char’, argument 2 of type ‘unsigned
char *’

so what can I pass it?

I think you’ll need to pass “const std::vector &” since C++,
SWIG, and Python all know what this is [a reference to a std::vector
of chars, that cannot be modified by the receiving function]. That
works for other blocks used in GNU Radio, IIRC. - MLD


#3

Brett L. Trotter wrote:

I’ve made a swig wrapper for some code that needs a char pointer passed
in, how do I do such a thing?

Thinking about this more, maybe I need to give more information.

I’ve wrapped Phil Karn’s FEC library and I’m trying to call
encode_rs_char:
void encode_rs_char(void *rs,unsigned char *data, unsigned char
*parity);
I passed it a string of a correct length, like I did for data, and it
only complained about the one for parity

my python looks like this

from fec import *

def main():
# make a test string with a predictable value
teststr = ‘’
for i in range(127):
teststr = teststr + chr(i)
# make a 127 byte long string to fill with parity
paritystr = chr(0) * 127
# create a handle for the reed-solomon function with the right
params
rs_handle = init_rs_char(8, 0x187, 1, 1, 128, 0)
# do the actual encoding
encode_rs_char(rs_handle, teststr, paritystr)

main()

and I get:
Traceback (most recent call last):
File “./test.py”, line 18, in ?
main()
File “./test.py”, line 15, in main
encode_rs_char(rs_handle, teststr, paritystr)
TypeError: in method ‘encode_rs_char’, argument 2 of type ‘unsigned char
*’

so what can I pass it?


#4

On Thu, May 24, 2007 at 07:07:09PM -0500, Brett L. Trotter wrote:

Brett L. Trotter wrote:

I’ve made a swig wrapper for some code that needs a char pointer passed
in, how do I do such a thing?

Thinking about this more, maybe I need to give more information.

Read up on typemaps in the swig documentation.

Eric


#5

On Thu, May 24, 2007 at 06:43:21PM -0500, Brett L. Trotter wrote:

I’ve made a swig wrapper for some code that needs a char pointer passed
in, how do I do such a thing?

Use const std::string &foo instead of char *.

For a longer answer, see the swig docs.

Eric


#6

Brett-

I am also very interested in wrapping a C-based FEC library. Could you
please keep us updated with your progress?
Thanks.

-Steven