On 06/03/12 15:55, colin gray wrote:
> I'm trying to figure out how to appropriately wrap The unsigned char*
> getBytes C Function below using SWIG (Java).
>
> The getBytes function returns a buffer of sender_id_t's:
> typedef unsigned char id_t[32];
> struct sender_id_t {
> id_t id;
> uint32_t phy_idx;
> };
>
> On the Java side I have an API which gives me the count of sender_id_t's
> and the getBytes API returns the length of the sender_it_t byte buffer.
> When I take the length/count, I'm able to know that every *N* bytes is a
> new sender_id_t.
>
> How would I wrap the unsigned char * getBytes() function so that I can
> loop over the byte buffer on the Java side and parse out the bytes and
> convert them to Java Strings? If there is a better approach, feel free
> to suggest. Thanks!
>
> _*C function:*_
> unsigned char *
> getBytes(resultset_t* resultset, int32_t *length)
> {
> int n;
> int count = getCount(resultset);
> unsigned char *ackbyte_ptr;
> unsigned char buffer[count * sizeof(sender_id_t)];
> sender_id_t *acks = getAcks(resultset);
> ackbyte_ptr = buffer;
> for (n=0; n<count; n++) {
> memcpy(ackbyte_ptr, acks->id, sizeof(sender_id_t));
> acks++;
> ackbyte_ptr += sizeof(sender_id_t);
> }
> ackbyte_ptr = buffer;
> return ackbyte_ptr;
> }
This lack of information in the C 'unsigned char *' type is problematic.
SWIG has no real mechanism for linking an output type (unsigned char *)
and an input type (resultset_t*) in your function above. Depending on
how many funtions that are similar to the one above, you might just want
to write a new C function, say getBytesSWIG, that wraps getBytes and use
SWIG to wrap getBytesSWIG. Pseudo code:
%ignore string_info_t;
%rename(getBytes) getBytesSWIG;
typedef struct {
int count;
char *string;
} string_info_t;
string_info_t getBytesSWIG(resultset_t* resultset, int32*length) {
string_info_t si;
si.count = getCount(resultset);
si.string = getBytes(resultset, length);
return si;
}
Also add %ignore getBytes if you can't avoid wrapping getBytes.
Then write a set of typemaps for returning string_info_t to marshal this
into a Java String.
William
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Swig-user mailing list
Swig...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user