1 /* ----------------------------------------------------------------------------- 2 * various.i 3 * 4 * SWIG Typemap library for Java. 5 * Various useful typemaps. 6 * ----------------------------------------------------------------------------- */ 7 8 /* 9 * char **STRING_ARRAY typemaps. 10 * These typemaps are for C String arrays which are NULL terminated. 11 * char *values[] = { "one", "two", "three", NULL }; // note NULL 12 * char ** is mapped to a Java String[]. 13 * 14 * Example usage wrapping: 15 * %apply char **STRING_ARRAY { char **input }; 16 * char ** foo(char **input); 17 * 18 * Java usage: 19 * String numbers[] = { "one", "two", "three" }; 20 * String[] ret = modulename.foo( numbers }; 21 */ 22 %typemap(jni) char **STRING_ARRAY "jobjectArray" 23 %typemap(jtype) char **STRING_ARRAY "String[]" 24 %typemap(jstype) char **STRING_ARRAY "String[]" 25 %typemap(in) char **STRING_ARRAY (jint size) { 26 int i = 0; 27 if ($input) { 28 size = JCALL1(GetArrayLength, jenv, $input); 29 #ifdef __cplusplus 30 $1 = new char*[size+1]; 31 #else 32 $1 = (char **)calloc(size+1, sizeof(char *)); 33 #endif 34 for (i = 0; i<size; i++) { 35 jstring j_string = (jstring)JCALL2(GetObjectArrayElement, jenv, $input, i); 36 const char *c_string = JCALL2(GetStringUTFChars, jenv, j_string, 0); 37 #ifdef __cplusplus 38 $1[i] = new char [strlen(c_string)+1]; 39 #else 40 $1[i] = (char *)calloc(strlen(c_string)+1, sizeof(const char *)); 41 #endif 42 strcpy($1[i], c_string); 43 JCALL2(ReleaseStringUTFChars, jenv, j_string, c_string); 44 JCALL1(DeleteLocalRef, jenv, j_string); 45 } 46 $1[i] = 0; 47 } else { 48 $1 = 0; 49 size = 0; 50 } 51 } 52 53 %typemap(freearg) char **STRING_ARRAY { 54 int i; 55 for (i=0; i<size$argnum-1; i++) 56 #ifdef __cplusplus 57 delete[] $1[i]; 58 delete[] $1; 59 #else 60 free($1[i]); 61 free($1); 62 #endif 63 } 64 65 %typemap(out) char **STRING_ARRAY { 66 if ($1) { 67 int i; 68 int len=0; 69 jstring temp_string; 70 const jclass clazz = JCALL1(FindClass, jenv, "java/lang/String"); 71 72 while ($1[len]) len++; 73 $result = JCALL3(NewObjectArray, jenv, len, clazz, NULL); 74 /* exception checking omitted */ 75 76 for (i=0; i<len; i++) { 77 temp_string = JCALL1(NewStringUTF, jenv, *$1++); 78 JCALL3(SetObjectArrayElement, jenv, $result, i, temp_string); 79 JCALL1(DeleteLocalRef, jenv, temp_string); 80 } 81 } 82 } 83 84 %typemap(javain) char **STRING_ARRAY "$javainput" 85 %typemap(javaout) char **STRING_ARRAY { 86 return $jnicall; 87 } 88 89 /* 90 * char **STRING_OUT typemaps. 91 * These are typemaps for returning strings when using a C char ** parameter type. 92 * The returned string appears in the 1st element of the passed in Java String array. 93 * 94 * Example usage wrapping: 95 * void foo(char **string_out); 96 * 97 * Java usage: 98 * String stringOutArray[] = { "" }; 99 * modulename.foo(stringOutArray); 100 * System.out.println( stringOutArray[0] ); 101 */ 102 %typemap(jni) char **STRING_OUT "jobjectArray" 103 %typemap(jtype) char **STRING_OUT "String[]" 104 %typemap(jstype) char **STRING_OUT "String[]" 105 %typemap(javain) char **STRING_OUT "$javainput" 106 107 %typemap(in) char **STRING_OUT($*1_ltype temp) { 108 if (!$input) { 109 SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null"); 110 return $null; 111 } 112 if (JCALL1(GetArrayLength, jenv, $input) == 0) { 113 SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element"); 114 return $null; 115 } 116 $1 = &temp; 117 *$1 = 0; 118 } 119 120 %typemap(argout) char **STRING_OUT { 121 jstring jnewstring = NULL; 122 if ($1) { 123 jnewstring = JCALL1(NewStringUTF, jenv, *$1); 124 } 125 JCALL3(SetObjectArrayElement, jenv, $input, 0, jnewstring); 126 } 127 128 /* 129 * char *BYTE typemaps. 130 * These are input typemaps for mapping a Java byte[] array to a C char array. 131 * Note that as a Java array is used and thus passeed by reference, the C routine 132 * can return data to Java via the parameter. 133 * 134 * Example usage wrapping: 135 * void foo(char *array); 136 * 137 * Java usage: 138 * byte b[] = new byte[20]; 139 * modulename.foo(b); 140 */ 141 %typemap(jni) char *BYTE "jbyteArray" 142 %typemap(jtype) char *BYTE "byte[]" 143 %typemap(jstype) char *BYTE "byte[]" 144 %typemap(in) char *BYTE { 145 $1 = (char *) JCALL2(GetByteArrayElements, jenv, $input, 0); 146 } 147 148 %typemap(argout) char *BYTE { 149 JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1, 0); 150 } 151 152 %typemap(javain) char *BYTE "$javainput" 153 154 /* Prevent default freearg typemap from being used */ 155 %typemap(freearg) char *BYTE "" 156 157