SWIG & Java 使用 carrays.i 和 array_functions for C 字符串数组



我有下面的配置,我试图创建一个测试C函数,该函数返回一个指向字符串数组的指针,然后使用SWIG的carray.I和Array_functions包装它,这样我就可以访问Java中的Array元素。我不确定%array_class和%array_functions中哪一个最适合这种情况。此示例是一个构建块,用于包装返回动态创建的数组的C函数。

不确定性:

  • %array_functions(char,SWIGArrayUtility);-不确定char是否正确
  • inline char*getCharArray()-不确定C函数签名是否正确
  • 字符串结果=getCharArray();-字符串返回看起来很奇怪,但这是SWIG生成的
  • 不确定inline char*getCharArray()是否创建了一个具有适当结构的数组来进行包装

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;
  } 
%}

内联标题C功能:

#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主测试仪:

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主测试仪输出:

length=3
?
?
,

SWIG生成的Java API:

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

}

需要进行两项更改:

  • char * getCharArray()必须是char ** getCharArray(),因为函数返回指向char *的指针数组(C指针)。更改后,会出现一个新的SWIGTYPE_p_p_charJava类,要获得它,必须在接口文件中添加一个%include "various.i"

  • %array_functions(char, SWIGArrayUtility)必须是%array_functions(char *, SWIGArrayUtility),因为数组包含指向char *String Java类)的指针。

我已经使用这个包含文件测试了给定的解决方案:

#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

此接口文件:

%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;
  } 
%}

这个测试类别:

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]);
    }
}

最新更新