Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.4 KiB

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