2012-03-08 55 views
1

我有下面的配置,我試圖創建一個測試C函數返回一個指向字符串數組的指針,然後使用SWIG的carrays .i和array_functions,以便我可以訪問Java中的Array元素。我不確定哪種%array_class或%array_functions最適合這種情況。此示例是一個構建塊,用於封裝返回動態創建的數組的C函數。SWIG和Java使用carrays.i和array_functions爲C數組字符串

不確定性:

  • %array_functions(炭,SWIGArrayUtility); - 不確定字符是否正確
  • 內嵌字符* getCharArray() - 不確定C函數簽名是否正確
  • String result = getCharArray(); - 字符串返回看起來很奇怪,但這就是SWIG生成的內容
  • 不確定是否內聯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 

爪哇主測試儀:

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

痛飲生成的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; 
    } 


} 

回答

2

兩個變化是必要的:

  • char * getCharArray()必須char ** getCharArray(),因爲函數返回一個數組(C poi )指向char *的指針。在此更改之後,將出現一個新的SWIGTYPE_p_p_char Java類,並且必須將其添加到接口文件%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]); 
    } 
} 
+0

感謝幫幫我! – c12 2012-03-23 17:40:10