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