//----------------------------------------------------------------------------- // File: ossimjni.i // // License: See top level LICENSE.txt file. // // Author: David Burken // // Description: SWIG Module to build ossim-jni java bindings. // //----------------------------------------------------------------------------- // $Id$ // Start ossimJniWrappers %module ossimJniWrappers %{ #include #include #include #include #include #include #include #include #include #include #include #include #include %} //--- // Handle any std::exception including ossimException. // Note: Must be placed before any methods or libraries to be wrapped: //--- %include %exception { try { $action } catch (const std::exception& e) { SWIG_exception(SWIG_RuntimeError, e.what()); } } //--- // This tells SWIG to treat char ** as a special case when used as a parameter //in a function call. //--- %typemap(in) char ** (jint size) { int i = 0; size = (*jenv)->GetArrayLength(jenv, $input); $1 = (char **) malloc((size+1)*sizeof(char *)); /* make a copy of each string */ for (i = 0; iGetObjectArrayElement(jenv, $input, i); const char * c_string = (*jenv)->GetStringUTFChars(jenv, j_string, 0); $1[i] = malloc((strlen(c_string)+1)*sizeof(char)); strcpy($1[i], c_string); (*jenv)->ReleaseStringUTFChars(jenv, j_string, c_string); (*jenv)->DeleteLocalRef(jenv, j_string); } $1[i] = 0; } // This cleans up the memory we malloc'd before the function call. %typemap(freearg) char ** { int i; for (i=0; iFindClass(jenv, "java/lang/String"); while ($1[len]) len++; jresult = (*jenv)->NewObjectArray(jenv, len, clazz, NULL); /* exception checking omitted */ for (i=0; iNewStringUTF(jenv, *result++); (*jenv)->SetObjectArrayElement(jenv, jresult, i, temp_string); (*jenv)->DeleteLocalRef(jenv, temp_string); } } // These 3 typemaps tell SWIG what JNI and Java types to use. %typemap(jni) char ** "jobjectArray" %typemap(jtype) char ** "String[]" %typemap(jstype) char ** "String[]" // These 2 typemaps handle the conversion of the jtype to jstype typemap type and vice versa. %typemap(javain) char ** "$javainput" %typemap(javaout) char ** { return $jnicall; } %include %include %include %include %include // Handle Init::initialize arg: %apply char **STRING_ARRAY { char* argv[] }; // Basic primatives: %include %apply short[] {ossimjni_sint16 *}; %apply unsigned short[] {ossimjni_uint16 *}; %apply int[] {ossimjni_sint32 *}; %apply int[] {ossimjni_int32 *}; %apply int[] {int *}; %apply unsigned int[] {ossimjni_uint32 *}; %apply float[] {ossimjni_float32 *}; %apply double[] {ossimjni_float64 *}; %apply unsigned char[] { ossimjni_uint8 * } %apply char* BYTE { ossimjni_int8 * } // Rename overloaded operators to squash swig warnings : %rename(assign) operator=; namespace std { %template(StringVector) vector; %template(UintVector) vector; } // Used for java class generation. %include %include %include %include %include %include %include %include %include %include %pragma(java) jniclasscode= %{ static { try { System.loadLibrary("ossimjni-swig"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. \n" + e); System.exit(1); } } %}