2013-05-21 93 views
3

我看遍了所有的地方。我曾嘗試過這個網站的一些技巧。無濟於事。SWIG無符號字符和字節[]

我有一個C++全局函數

char* squid(char* buff, int len); 

我創建了一個.i文件

%module Crabby 

%include "arrays_java.i" 

%{ 
/* headers here are included in the wrapper code */ 
#include "sponge.h" 
%} 



%typemap(jtype) (const signed char *arr, size_t sz) "byte[]" 
%typemap(jstype) (const signed char *arr, size_t sz) "byte[]" 
%typemap(jni) (const signed char *arr, size_t sz) "jbyteArray" 
%typemap(javain) (const signed char *arr, size_t sz) "$javainput" 

%typemap(in) (const signed char* arr, size_t sz) { 
    $1 = JCALL2(GetByteArrayElements, jenv, $input, NULL); 
    const size_t sz = JCALL1(GetArrayLength, jenv, $input); 
    $2 = $1 + sz; 
} 

%typemap(freearg) (const signed char *arr, size_t sz) { 
    // Or use 0 instead of ABORT to keep changes if it was a copy 
    JCALL3(ReleaseByteArrayElements, jenv, $input, $1, JNI_ABORT); 
} 

%apply (const signed char* arr, size_t sz) { (const unsigned char* buff, int len) } 
%apply (const signed char* arr, size_t sz) { (const unsigned char* query, int queryLen) } 

%include "sponge.h" 

不管我做什麼的界面總是

public static String Squid(String buff, int len) 

如果刪除無符號的我在cxx包裝中得到非法轉換

這是痛飲2.0.1

回答

2

你的界面接近,但有以下問題:爲%apply

  • 您需要的符號/無符號限定詞精確匹配爲buff

    1. const事項(有在你展示的聲明中沒有限定詞。
    2. 你在類型映射需要numinputs=1壓縮它只有一個Java輸入。
    3. 將大小設置爲計算指針沒有多大意義。

    所以固定接口的樣子:

    %module Crabby 
    
    %include "arrays_java.i" 
    
    %{ 
    /* headers here are included in the wrapper code */ 
    #include "sponge.h" 
    %} 
    
    %typemap(jtype) (const signed char *arr, size_t sz) "byte[]" 
    %typemap(jstype) (const signed char *arr, size_t sz) "byte[]" 
    %typemap(jni) (const signed char *arr, size_t sz) "jbyteArray" 
    %typemap(javain) (const signed char *arr, size_t sz) "$javainput" 
    
    %typemap(in,numinputs=1) (const signed char* arr, size_t sz) { 
        $1 = JCALL2(GetByteArrayElements, jenv, $input, NULL); 
        const size_t sz = JCALL1(GetArrayLength, jenv, $input); 
        $2 = sz; 
    } 
    
    %typemap(freearg) (const signed char *arr, size_t sz) { 
        // Or use 0 instead of ABORT to keep changes if it was a copy 
        JCALL3(ReleaseByteArrayElements, jenv, $input, $1, JNI_ABORT); 
    } 
    
    %apply (const signed char* arr, size_t sz) { (char* buff, int len) } 
    
    %include "sponge.h" 
    
  • +0

    有沒有辦法解決函數簽名,這樣只需要一個字符串? – soandos

    +0

    @soandos是的,但問題在於如何將字符串映射到'char *',因爲'jchar'與字符不同。你想要的字節數作爲長度,或字符的數量? – Flexo

    +0

    字節數 – soandos

    相關問題