ctp交易平台转java接口转换

首先感谢倪材@csdn的博客,给了我很大帮助。

http://blog.csdn.net/pjjing/article/details/53186394

http://blog.csdn.net/pjjing/article/details/53187469

这里着重说一下转换中遇到的问题。

首先swig中转换 char *str[] 和char **时会转换成SWIGTYPE_p_p_char类型,这里需要在编写.i文件之前,先编写various.i文件,内容如下

/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* various.i
*
* SWIG Typemap library for Java.
* Various useful typemaps.
* ----------------------------------------------------------------------------- */ /*
* char **STRING_ARRAY typemaps.
* These typemaps are for C String arrays which are NULL terminated.
* char *values[] = { "one", "two", "three", NULL }; // note NULL
* char ** is mapped to a Java String[].
*
* Example usage wrapping:
* %apply char **STRING_ARRAY { char **input };
* char ** foo(char **input);
*
* Java usage:
* String numbers[] = { "one", "two", "three" };
* String[] ret = modulename.foo( numbers };
*/
%typemap(jni) char **STRING_ARRAY "jobjectArray"
%typemap(jtype) char **STRING_ARRAY "String[]"
%typemap(jstype) char **STRING_ARRAY "String[]"
%typemap(in) char **STRING_ARRAY (jint size) {
int i = ;
size = JCALL1(GetArrayLength, jenv, $input);
#ifdef __cplusplus
$ = new char*[size+];
#else
$ = (char **)calloc(size+, sizeof(char *));
#endif
for (i = ; i<size; i++) {
jstring j_string = (jstring)JCALL2(GetObjectArrayElement, jenv, $input, i);
const char *c_string = JCALL2(GetStringUTFChars, jenv, j_string, );
#ifdef __cplusplus
$[i] = new char [strlen(c_string)+];
#else
$[i] = (char *)calloc(strlen(c_string)+, sizeof(const char *));
#endif
strcpy($[i], c_string);
JCALL2(ReleaseStringUTFChars, jenv, j_string, c_string);
JCALL1(DeleteLocalRef, jenv, j_string);
}
$[i] = ;
} %typemap(freearg) char **STRING_ARRAY {
int i;
for (i=; i<size$argnum-; i++)
#ifdef __cplusplus
delete[] $[i];
delete[] $;
#else
free($[i]);
free($);
#endif
} %typemap(out) char **STRING_ARRAY {
int i;
int len=;
jstring temp_string;
const jclass clazz = JCALL1(FindClass, jenv, "java/lang/String"); while ($[len]) len++;
jresult = JCALL3(NewObjectArray, jenv, len, clazz, NULL);
/* exception checking omitted */ for (i=; i<len; i++) {
temp_string = JCALL1(NewStringUTF, jenv, *result++);
JCALL3(SetObjectArrayElement, jenv, jresult, i, temp_string);
JCALL1(DeleteLocalRef, jenv, temp_string);
}
} %typemap(javain) char **STRING_ARRAY "$javainput"
%typemap(javaout) char **STRING_ARRAY {
return $jnicall;
} /*
* char **STRING_OUT typemaps.
* These are typemaps for returning strings when using a C char ** parameter type.
* The returned string appears in the 1st element of the passed in Java String array.
*
* Example usage wrapping:
* void foo(char **string_out);
*
* Java usage:
* String stringOutArray[] = { "" };
* modulename.foo(stringOutArray);
* System.out.println( stringOutArray[0] );
*/
%typemap(jni) char **STRING_OUT "jobjectArray"
%typemap(jtype) char **STRING_OUT "String[]"
%typemap(jstype) char **STRING_OUT "String[]"
%typemap(javain) char **STRING_OUT "$javainput" %typemap(in) char **STRING_OUT($*1_ltype temp) {
if (!$input) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null");
return $null;
}
if (JCALL1(GetArrayLength, jenv, $input) == ) {
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element");
return $null;
}
$ = &temp;
} %typemap(argout) char **STRING_OUT {
jstring jnewstring = NULL;
if($) {
jnewstring = JCALL1(NewStringUTF, jenv, *$);
}
JCALL3(SetObjectArrayElement, jenv, $input, , jnewstring);
} /*
* char *BYTE typemaps.
* These are input typemaps for mapping a Java byte[] array to a C char array.
* Note that as a Java array is used and thus passeed by reference, the C routine
* can return data to Java via the parameter.
*
* Example usage wrapping:
* void foo(char *array);
*
* Java usage:
* byte b[] = new byte[20];
* modulename.foo(b);
*/
%typemap(jni) char *BYTE "jbyteArray"
%typemap(jtype) char *BYTE "byte[]"
%typemap(jstype) char *BYTE "byte[]"
%typemap(in) char *BYTE {
$ = (char *) JCALL2(GetByteArrayElements, jenv, $input, );
} %typemap(argout) char *BYTE {
JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $, );
} %typemap(javain) char *BYTE "$javainput" /* Prevent default freearg typemap from being used */
%typemap(freearg) char *BYTE ""

然后将various.i引入自己编写的.i文件:

%module(directors="") thosttradeapi
%include "various.i"
%apply char **STRING_ARRAY { char *ppInstrumentID[] } %{
#include "ThostFtdcTraderApi.h"
#include "ThostFtdcMdApi.h"
%}
%feature("director") CThostFtdcTraderSpi;
%include "ThostFtdcUserApiDataType.h"
%include "ThostFtdcUserApiStruct.h"
%include "ThostFtdcTraderApi.h"
%feature("director") CThostFtdcMdSpi;
%include "ThostFtdcMdApi.h"

中文乱码的问题,按照博客上所述的,需要把thosttraderapi_wrap.cpp中所有的带有中文的方法替换,可以用newStringUTF快速定位,替换完之后make,makefile如下

OBJS=thosttraderapi_wrap.o
INCLUDE=-I./ -I/home/user/jdk/include -I/home/user/jdk/include/linux
TARGET=libthosttraderapi_wrap.so
CPPFLAG=-shared -fPIC
CC=g++
LDLIB=-liconv -L.-Lthosttraderapi
$(TARGET) : $(OBJS)
$(CC) $(CPPFLAG) $(INCLUDE) -o $(TARGET) $(OBJS) $(LDLIB)
$(OBJS) : %.o : %.cpp
$(CC) -c -fPIC $(INCLUDE) $< -o $@
clean:
-rm -f $(OBJS)
   -rm -f $(TARGET)
install:
   cp $(TARGET) /usr/lib

 make之后直接load libthosttradeapi_wrap.so,中文乱码的问题就解决了。

上一篇:⭐❤️BOA服务器的安装,开发与移植❤️⭐


下一篇:019、字符串 upper()、capitalize()、title() 区别