From: Hendrik M. <qwi...@sn...> - 2008-01-24 11:43:40
|
Hi all, And another question about the SWIG Java interface. I have struct mgTreeNode. For debugging purposes, I wrote a function in = C, printTreeNode, which returns a string giving some information on the=20 struct. A little testing program in C shows me the function is doing fin= e. However, after wrapping it, strange stuff gets through on the Java side: Correct version in C program: a: 000000, id: 0, state: 0, left: [a: 100000, id: 0, state: 0, left: [a: = 010001, id: 0, state: 0, left: [(null)], right: [(null)]], right: [a:=20 001000, id: 0, state: 0, left: [(null)], right: [(null)]]], right: [(null= )] What comes through in Java: a: =C3=B0=C2=A2=C2=8F=D3=AA*, id: 0, state: 0, left: [a: =C3=B0Hj=D3=AA*,= id: 0, state: 0, left:=20 [(null)], right: [(null)]], right: [(null So two things are going wrong here: first of all the bit vector is=20 converted to some strange signs. This points to some encoding problem.=20 Fact is that the 00000 is generated on the fly in the printTreeNode=20 function, since the original is a char* which contains 0 and 1 (not the=20 characters, but the numbers). I convert this to a readable string as=20 follows: for (i =3D 0; i < labelLength; ++i) { label[i] =3D node->a[i] + '0'; } Note that what renders here as =C3=B0=C2=A2=C2=8F=D3=AA* is supposed to b= e only one=20 character: a 0 or a 1 (the two outputs above are not from the same tree).= Second problem: the output seems chopped off somewhere: the closing=20 parens are missing at the end. The function is the following: char *printTreeNode(mgTreeNode * node, int labelLength) { int i; char *buffer; char *leftDaughter =3D 0; char *rightDaughter =3D 0; char *label =3D malloc(labelLength); for (i =3D 0; i < labelLength; ++i) { label[i] =3D node->a[i] + '0'; } if (node->left) { leftDaughter =3D printTreeNode(node->left, labelLength); } if (node->right) { rightDaughter =3D printTreeNode(node->right, labelLength); } buffer =3D malloc(500); sprintf(buffer, "a: %s, id: %d, state: %d, left: [%s], right: [%s]",=20 label, node->id, node->state, leftDaughter, rightDaughter); free(label); if (leftDaughter) { free(leftDaughter); } if (rightDaughter) { free(rightDaughter); } return buffer; } Since I am very new to C, any improvement suggestions are welcome. The struct is the following: typedef struct mgTreeNode { /* tree node */ mA a; /* alphabet element */ struct mgTreeNode *left, *right; /* successors */ mgId id; /* state space id */ mgState state; /* automaton state */ } mgTreeNode; And mona.i declares /* print a tree node with labels of the given length, recursively into a = string*/ char *printTreeNode(mgTreeNode * node, int labelLength); Notice that I have to give the length of the char* to the function,=20 since the struct contains no information about this. Very grateful for any help, H. --=20 Hendrik Maryns http://tcl.sfs.uni-tuebingen.de/~hendrik/ =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D http://aouw.org Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html |