c12
c12

Reputation: 9827

SWIG & Java Use of carrays.i and array_functions for C Array of Strings

I have the below configuration where I'm trying to create a test C function that returns a pointer to an Array of Strings and then wrap that using SWIG's carrays.i and array_functions so that I can access the Array elements in Java. I'm unsure which of %array_class or %array_functions is most appropriate for this situation. This example is a building block towards wrapping a C function that returns a dynamically created array.

Uncertainties:

SWIG.i:

%module Test

%{
#include "test.h"
%}

%include <carrays.i>
%array_functions(char, SWIGArrayUtility);
%include "test.h"

%pragma(java) modulecode=%{
  public static char[] getCharArrayImpl() {
    final int num = numFoo();
    char ret[] = new char[num];
    String result = getCharArray();
    for (int i = 0; i < num; ++i) {
      ret[i] = SWIGArrayUtility_getitem(result, i);
    }
    return ret;
  } 

%}

Inline Header C Function:

#ifndef TEST_H
#define TEST_H

inline static unsigned short numFoo() {
  return 3;
}

inline char *getCharArray(){
    static char* foo[3];
    foo[0]="ABC";
    foo[1]="5CDE";
    foo[2]="EEE6";
    return foo;
}

#endif

Java Main Tester:

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

}

Java Main Tester Output:

length=3
?
?
,

SWIG Generated Java APIs:

public class Test {
  public static String new_SWIGArrayUtility(int nelements) {
    return TestJNI.new_SWIGArrayUtility(nelements);
  }

  public static void delete_SWIGArrayUtility(String ary) {
    TestJNI.delete_SWIGArrayUtility(ary);
  }

  public static char SWIGArrayUtility_getitem(String ary, int index) {
    return TestJNI.SWIGArrayUtility_getitem(ary, index);
  }

  public static void SWIGArrayUtility_setitem(String ary, int index, char value) {
    TestJNI.SWIGArrayUtility_setitem(ary, index, value);
  }

  public static int numFoo() {
    return TestJNI.numFoo();
  }

  public static String getCharArray() {
    return TestJNI.getCharArray();
  }


  public static char[] getCharArrayImpl() {
    final int num = numFoo();
    char ret[] = new char[num];
    String result = getCharArray();
    System.out.println("result=" + result);
    for (int i = 0; i < num; ++i) {
      ret[i] = SWIGArrayUtility_getitem(result, i);
      System.out.println("ret[" + i + "]=" + ret[i]);
    }
    return ret;
  } 


}

Upvotes: 2

Views: 2007

Answers (1)

Juan Mellado
Juan Mellado

Reputation: 15113

Two changes are necessary:

  • char * getCharArray() must be char ** getCharArray(), because the function returns an array (C pointer) of pointers to char *. After this change a new SWIGTYPE_p_p_char Java class appears, and to get it an %include "various.i" must be added into the interface file.

  • %array_functions(char, SWIGArrayUtility) must be %array_functions(char *, SWIGArrayUtility), because the array contains pointers to char * (String Java class).

I have tested the given solution using this include file:

#ifndef TEST2_H
#define TEST2_H

unsigned short numFoo() {
  return 3;
}

char ** getCharArray(){
    static char* foo[3];
    foo[0]="ABC";
    foo[1]="5CDE";
    foo[2]="EEE6";
    return foo;
}

#endif

This interface file:

%module Test2

%{
#include "test2.h"
%}
%include "test2.h"

%include "various.i"

%include "carrays.i"
%array_functions(char *, SWIGArrayUtility);

%pragma(java) modulecode=%{
  public static String[] getCharArrayImpl() {
    final int num = numFoo();
    String ret[] = new String[num];
    SWIGTYPE_p_p_char result = getCharArray();
    for (int i = 0; i < num; ++i) {
        ret[i] = SWIGArrayUtility_getitem(result, i);
    }
    return ret;
  } 
%}

And this tester class:

public static void main(String[] args) {
    System.loadLibrary("test2");
    String res[] = Test2.getCharArrayImpl();
    System.out.println("length=" + res.length);
    for(int i=0; i < res.length; i++){
        System.out.println(res[i]);
    }
}

Upvotes: 2

Related Questions