PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/java/typemap/example.i

#
Swig | 101 lines | 50 code | 23 blank | 28 comment | 0 complexity | a3f47a1771ee7452e497596747586d1d MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* File : example.i */
  2. %module example
  3. %{
  4. /*
  5. example of a function that returns a value in the char * argument
  6. normally used like:
  7. char buf[bigenough];
  8. f1(buf);
  9. */
  10. void f1(char *s) {
  11. if(s != NULL) {
  12. sprintf(s, "hello world");
  13. }
  14. }
  15. void f2(char *s) {
  16. f1(s);
  17. }
  18. void f3(char *s) {
  19. f1(s);
  20. }
  21. %}
  22. /* default behaviour is that of input arg, Java cannot return a value in a
  23. * string argument, so any changes made by f1(char*) will not be seen in the Java
  24. * string passed to the f1 function.
  25. */
  26. void f1(char *s);
  27. %include various.i
  28. /* use the BYTE argout typemap to get around this. Changes in the string by
  29. * f2 can be seen in Java. */
  30. void f2(char *BYTE);
  31. /* Alternative approach uses a StringBuffer typemap for argout */
  32. /* Define the types to use in the generated JNI C code and Java code */
  33. %typemap(jni) char *SBUF "jobject"
  34. %typemap(jtype) char *SBUF "StringBuffer"
  35. %typemap(jstype) char *SBUF "StringBuffer"
  36. /* How to convert Java(JNI) type to requested C type */
  37. %typemap(in) char *SBUF {
  38. $1 = NULL;
  39. if($input != NULL) {
  40. /* Get the String from the StringBuffer */
  41. jmethodID setLengthID;
  42. jclass sbufClass = (*jenv)->GetObjectClass(jenv, $input);
  43. jmethodID toStringID = (*jenv)->GetMethodID(jenv, sbufClass, "toString", "()Ljava/lang/String;");
  44. jstring js = (jstring) (*jenv)->CallObjectMethod(jenv, $input, toStringID);
  45. /* Convert the String to a C string */
  46. const char *pCharStr = (*jenv)->GetStringUTFChars(jenv, js, 0);
  47. /* Take a copy of the C string as the typemap is for a non const C string */
  48. jmethodID capacityID = (*jenv)->GetMethodID(jenv, sbufClass, "capacity", "()I");
  49. jint capacity = (*jenv)->CallIntMethod(jenv, $input, capacityID);
  50. $1 = (char *) malloc(capacity+1);
  51. strcpy($1, pCharStr);
  52. /* Release the UTF string we obtained with GetStringUTFChars */
  53. (*jenv)->ReleaseStringUTFChars(jenv, js, pCharStr);
  54. /* Zero the original StringBuffer, so we can replace it with the result */
  55. setLengthID = (*jenv)->GetMethodID(jenv, sbufClass, "setLength", "(I)V");
  56. (*jenv)->CallVoidMethod(jenv, $input, setLengthID, (jint) 0);
  57. }
  58. }
  59. /* How to convert the C type to the Java(JNI) type */
  60. %typemap(argout) char *SBUF {
  61. if($1 != NULL) {
  62. /* Append the result to the empty StringBuffer */
  63. jstring newString = (*jenv)->NewStringUTF(jenv, $1);
  64. jclass sbufClass = (*jenv)->GetObjectClass(jenv, $input);
  65. jmethodID appendStringID = (*jenv)->GetMethodID(jenv, sbufClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
  66. (*jenv)->CallObjectMethod(jenv, $input, appendStringID, newString);
  67. /* Clean up the string object, no longer needed */
  68. free($1);
  69. $1 = NULL;
  70. }
  71. }
  72. /* Prevent the default freearg typemap from being used */
  73. %typemap(freearg) char *SBUF ""
  74. /* Convert the jstype to jtype typemap type */
  75. %typemap(javain) char *SBUF "$javainput"
  76. /* apply the new typemap to our function */
  77. void f3(char *SBUF);