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:
- %array_functions(char, SWIGArrayUtility); – not sure if char is correct
- inline char *getCharArray() – not sure if C function signature is correct
- String result = getCharArray(); – String return seems odd, but that’s what is generated by SWIG
- unsure if inline char *getCharArray() creates an array that is of proper structure for wrapping.
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;
}
}
Two changes are necessary:
char * getCharArray()must bechar ** getCharArray(), because the function returns an array (C pointer) of pointers tochar *. After this change a newSWIGTYPE_p_p_charJava 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 tochar *(StringJava class).I have tested the given solution using this include file:
This interface file:
And this tester class: