[Swig-user] Populate Java Client Array Using SWIG array_functions Macro and C Function Return Value

39 views
Skip to first unread message

colin gray

unread,
Mar 8, 2012, 10:00:04 AM3/8/12
to swig...@lists.sourceforge.net
Hello,

When reading the SWIG documentation for Java, I see examples on how to pass an array from Java to a C function using the %array_functions(char, chararray), but I don't understand how to create an array in my Java client from a C function returning a char array using SWIG. 

Secondly, why does SWIG generate a newchararray that returns a String, shouldn't it be a char[]?  See generated code below:

SWIG.i
    %module Test
    %include "carrays.i"
   
    %array_functions(char, chararray)
   
    %{
    #include "Sample.h"
   
    %}
    %include "Sample.h"

SWIG Generated Java Code:
     public static String new_chararray(int nelements) {
        return TestJNI.new_chararray(nelements);
      }
   
      public static void delete_chararray(String ary) {
        TestJNI.delete_chararray(ary);
      }
   
      public static char chararray_getitem(String ary, int index) {
        return TestJNI.chararray_getitem(ary, index);
      }
   
      public static void chararray_setitem(String ary, int index, char value) {
        TestJNI.chararray_setitem(ary, index, value);
      }

C Function Returning a char[]:
char *
getValuesAsString(resultset_t* resultset, int32_t *length)
    {
        int n, len;
        int count = getCount(resultset); //calls another api to get count for return array
        len = sizeof(sender_id_t)+1;
        char *list_ptr = NULL;
        char ackbyte_list[count][len];
   
        sender_id_t *acks = getAcks(resultset);
   
        for (n=0; n<count; n++) {
            memcpy(ackbyte_list, acks->id, sizeof(sender_id_t));
            acks++;
        }
        *length = sizeof(ackbyte_list);
        list_ptr = *ackbyte_list;
        return list_ptr;
    }

--
Colin

William S Fulton

unread,
Mar 15, 2012, 4:55:08 PM3/15/12
to colin gray, swig...@lists.sourceforge.net
On 08/03/12 15:00, colin gray wrote:
> Hello,
>
> When reading the SWIG documentation for Java, I see examples on how to
> pass an array from Java to a C function using the %array_functions(char,
> chararray), but I don't understand how to create an array in my Java
> client from a C function returning a char array using SWIG.
>
> Secondly, why does SWIG generate a newchararray that returns a String,
> shouldn't it be a char[]? See generated code below:
>
What it should return cannot be deduced by purely returning the type, so
an assumption is made that it is a string. That is documented and the
idea is to customise it if you don't like the default behaviour. Look at
arrays_java.i for some more ideas on array handling.

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

Reply all
Reply to author
Forward
0 new messages