[Swig-user] SWIG carrays.i and array_functions wrapping of C Char Array

406 views
Skip to first unread message

colin gray

unread,
Mar 19, 2012, 4:10:57 PM3/19/12
to swig...@lists.sourceforge.net
Hello, 

I'm trying to utilize the below C function, charArray in Java using SWIG, but it looks like I'm not able to get all of the C Array element's data.  If I change the C function to use just a single digit or letter then its fine.  Anyone see any issues with my implementation?
 

   **SWIG.i:**

    %module Test
    
    %{
    #include "test.h"
    %}
    
    %include <carrays.i>
    %array_functions(char, char_array);
    %include "test.h"
    
    %pragma(java) modulecode=%{
      public static char[] getChars() {
        final int num = numFoo();
        char[] ret = new char[num];
        String result = charArray();
        for (int i = 0; i < num; ++i) {
          ret[i] = char_array_getitem(result, i);
        }
        return ret;
      }  
    %}

**C Header & Inline Function:**

    #ifndef TEST_H
    #define TEST_H
    
    inline static unsigned short numFoo() {
      return 3;
    }
    
    inline char *charArray(){
    static char foo[3];
    foo[0]='ABC';
        foo[1]='5CDE';
        foo[2]='EEE6';
        return foo;
    }
    
    #endif

**Java Test Main:**

    public class TestMain {
    
    public static void main(String[] args) {
       System.loadLibrary("Test");
       for(int i=0; i < test.length; i++){
        char chr = (char) test[i];
        System.out.println(chr);
       }
    }
    
    }

**Output from Java Main:**

    C
    E
    6

Bob Hood

unread,
Mar 19, 2012, 4:48:49 PM3/19/12
to swig...@lists.sourceforge.net
On 3/19/2012 2:10 PM, colin gray wrote:
Hello, 

I'm trying to utilize the below C function, charArray in Java using SWIG, but it looks like I'm not able to get all of the C Array element's data.  If I change the C function to use just a single digit or letter then its fine.  Anyone see any issues with my implementation?
 
  
    inline char *charArray(){
    static char foo[3];
    foo[0]='ABC';
    foo[1]='5CDE';
    foo[2]='EEE6';
    return foo;
    }


For one thing, you're stuffing single characters into a character array (I'm kinda surprised you're not getting compile errors with multiple characters in single quotes).  If you want character strings, then change your C function to something like this:

     inline char** charArray()
     {
         static char** foo[] = { "ABC", "5CDE", "EEE6", 0 };
         return foo;
     }

Note that your return type is now different that what it was before.


Render me gone,                       |||
Bob                                 ^(===)^
---------------------------------oOO--(_)--OOo---------------------------------
    I'm not so good with advice...can I interest you in a sarcastic comment?

colin gray

unread,
Mar 19, 2012, 5:28:47 PM3/19/12
to Bob Hood, swig...@lists.sourceforge.net
Thanks for the advice Bob, much appreciated.  If I change the return type to a char** then the C function (charArray() returns a SWIGTYPE_p_p_char, but the char_array_getitem takes in a String to represent the Array parameter.  Any ideas?

//C Function Wrapper:

public static SWIGTYPE_p_p_char charArray() {

    long cPtr = TestJNI.charArray();

    return (cPtr == 0) ? null : new SWIGTYPE_p_p_char(cPtr, false);

  }

public static char char_array_getitem(String ary, int index) {

    return TestJNI.char_array_getitem(ary, index);

  }


------------------------------------------------------------------------------
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




--
Colin Gray

Bob Hood

unread,
Mar 19, 2012, 6:40:36 PM3/19/12
to swig...@lists.sourceforge.net
The problem here is that I don't know the Java API, so I don't know how to tell Java what its got.  Your C++ function is now returning char**, which is an array of character pointers (with each pointer pointing to the string "ABC", "5CDE", etc.).

In other words, charArray() is returning an array of pointers (each to its own char array) instead of just a single array of char's.  You need to let Java know that what it is handling is no longer array[char], but is now array[array[char], array[char], array[char]].  You may need to set up a typemap to handle this conversion.

Hopefully, I'm not further confusing you.  But somebody who knows the SWIG/Java interfaces might do better.  I only know Python.
Reply all
Reply to author
Forward
0 new messages