PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/ocaml/cstring.i

#
Swig | 268 lines | 127 code | 28 blank | 113 comment | 0 complexity | 93f0cc1d36cd511cf02ca0ec75a08751 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * cstring.i
  3. *
  4. * This file provides typemaps and macros for dealing with various forms
  5. * of C character string handling. The primary use of this module
  6. * is in returning character data that has been allocated or changed in
  7. * some way.
  8. * ----------------------------------------------------------------------------- */
  9. /* %cstring_input_binary(TYPEMAP, SIZE)
  10. *
  11. * Macro makes a function accept binary string data along with
  12. * a size.
  13. */
  14. %define %cstring_input_binary(TYPEMAP, SIZE)
  15. %apply (char *STRING, int LENGTH) { (TYPEMAP, SIZE) };
  16. %enddef
  17. /*
  18. * %cstring_bounded_output(TYPEMAP, MAX)
  19. *
  20. * This macro is used to return a NULL-terminated output string of
  21. * some maximum length. For example:
  22. *
  23. * %cstring_bounded_output(char *outx, 512);
  24. * void foo(char *outx) {
  25. * sprintf(outx,"blah blah\n");
  26. * }
  27. *
  28. */
  29. %define %cstring_bounded_output(TYPEMAP,MAX)
  30. %typemap(ignore) TYPEMAP(char temp[MAX+1]) {
  31. $1 = ($1_ltype) temp;
  32. }
  33. %typemap(argout) TYPEMAP {
  34. $1[MAX] = 0;
  35. $result = caml_list_append($result,caml_val_string(str));
  36. }
  37. %enddef
  38. /*
  39. * %cstring_chunk_output(TYPEMAP, SIZE)
  40. *
  41. * This macro is used to return a chunk of binary string data.
  42. * Embedded NULLs are okay. For example:
  43. *
  44. * %cstring_chunk_output(char *outx, 512);
  45. * void foo(char *outx) {
  46. * memmove(outx, somedata, 512);
  47. * }
  48. *
  49. */
  50. %define %cstring_chunk_output(TYPEMAP,SIZE)
  51. %typemap(ignore) TYPEMAP(char temp[SIZE]) {
  52. $1 = ($1_ltype) temp;
  53. }
  54. %typemap(argout) TYPEMAP {
  55. $result = caml_list_append($result,caml_val_string_len($1,SIZE));
  56. }
  57. %enddef
  58. /*
  59. * %cstring_bounded_mutable(TYPEMAP, SIZE)
  60. *
  61. * This macro is used to wrap a string that's going to mutate.
  62. *
  63. * %cstring_bounded_mutable(char *in, 512);
  64. * void foo(in *x) {
  65. * while (*x) {
  66. * *x = toupper(*x);
  67. * x++;
  68. * }
  69. * }
  70. *
  71. */
  72. %define %cstring_bounded_mutable(TYPEMAP,MAX)
  73. %typemap(in) TYPEMAP(char temp[MAX+1]) {
  74. char *t = (char *)caml_ptr_val($input);
  75. strncpy(temp,t,MAX);
  76. $1 = ($1_ltype) temp;
  77. }
  78. %typemap(argout) TYPEMAP {
  79. $result = caml_list_append($result,caml_val_string_len($1,MAX));
  80. }
  81. %enddef
  82. /*
  83. * %cstring_mutable(TYPEMAP [, expansion])
  84. *
  85. * This macro is used to wrap a string that will mutate in place.
  86. * It may change size up to a user-defined expansion.
  87. *
  88. * %cstring_mutable(char *in);
  89. * void foo(in *x) {
  90. * while (*x) {
  91. * *x = toupper(*x);
  92. * x++;
  93. * }
  94. * }
  95. *
  96. */
  97. %define %cstring_mutable(TYPEMAP,...)
  98. %typemap(in) TYPEMAP {
  99. char *t = String_val($input);
  100. int n = string_length($input);
  101. $1 = ($1_ltype) t;
  102. #if #__VA_ARGS__ == ""
  103. #ifdef __cplusplus
  104. $1 = ($1_ltype) new char[n+1];
  105. #else
  106. $1 = ($1_ltype) malloc(n+1);
  107. #endif
  108. #else
  109. #ifdef __cplusplus
  110. $1 = ($1_ltype) new char[n+1+__VA_ARGS__];
  111. #else
  112. $1 = ($1_ltype) malloc(n+1+__VA_ARGS__);
  113. #endif
  114. #endif
  115. memmove($1,t,n);
  116. $1[n] = 0;
  117. }
  118. %typemap(argout) TYPEMAP {
  119. $result = caml_list_append($result,caml_val_string($1));
  120. #ifdef __cplusplus
  121. delete[] $1;
  122. #else
  123. free($1);
  124. #endif
  125. }
  126. %enddef
  127. /*
  128. * %cstring_output_maxsize(TYPEMAP, SIZE)
  129. *
  130. * This macro returns data in a string of some user-defined size.
  131. *
  132. * %cstring_output_maxsize(char *outx, int max) {
  133. * void foo(char *outx, int max) {
  134. * sprintf(outx,"blah blah\n");
  135. * }
  136. */
  137. %define %cstring_output_maxsize(TYPEMAP, SIZE)
  138. %typemap(in) (TYPEMAP, SIZE) {
  139. $2 = caml_val_long($input);
  140. #ifdef __cplusplus
  141. $1 = ($1_ltype) new char[$2+1];
  142. #else
  143. $1 = ($1_ltype) malloc($2+1);
  144. #endif
  145. }
  146. %typemap(argout) (TYPEMAP,SIZE) {
  147. $result = caml_list_append($result,caml_val_string($1));
  148. #ifdef __cplusplus
  149. delete [] $1;
  150. #else
  151. free($1);
  152. #endif
  153. }
  154. %enddef
  155. /*
  156. * %cstring_output_withsize(TYPEMAP, SIZE)
  157. *
  158. * This macro is used to return character data along with a size
  159. * parameter.
  160. *
  161. * %cstring_output_maxsize(char *outx, int *max) {
  162. * void foo(char *outx, int *max) {
  163. * sprintf(outx,"blah blah\n");
  164. * *max = strlen(outx);
  165. * }
  166. */
  167. %define %cstring_output_withsize(TYPEMAP, SIZE)
  168. %typemap(in) (TYPEMAP, SIZE) {
  169. int n = caml_val_long($input);
  170. #ifdef __cplusplus
  171. $1 = ($1_ltype) new char[n+1];
  172. $2 = ($2_ltype) new $*1_ltype;
  173. #else
  174. $1 = ($1_ltype) malloc(n+1);
  175. $2 = ($2_ltype) malloc(sizeof($*1_ltype));
  176. #endif
  177. *$2 = n;
  178. }
  179. %typemap(argout) (TYPEMAP,SIZE) {
  180. $result = caml_list_append($result,caml_val_string_len($1,$2));
  181. #ifdef __cplusplus
  182. delete [] $1;
  183. delete $2;
  184. #else
  185. free($1);
  186. free($2);
  187. #endif
  188. }
  189. %enddef
  190. /*
  191. * %cstring_output_allocate(TYPEMAP, RELEASE)
  192. *
  193. * This macro is used to return character data that was
  194. * allocated with new or malloc.
  195. *
  196. * %cstring_output_allocated(char **outx, free($1));
  197. * void foo(char **outx) {
  198. * *outx = (char *) malloc(512);
  199. * sprintf(outx,"blah blah\n");
  200. * }
  201. */
  202. %define %cstring_output_allocate(TYPEMAP, RELEASE)
  203. %typemap(ignore) TYPEMAP($*1_ltype temp = 0) {
  204. $1 = &temp;
  205. }
  206. %typemap(argout) TYPEMAP {
  207. if (*$1) {
  208. $result = caml_list_append($result,caml_val_string($1));
  209. RELEASE;
  210. } else {
  211. $result = caml_list_append($result,caml_val_ptr($1));
  212. }
  213. }
  214. %enddef
  215. /*
  216. * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
  217. *
  218. * This macro is used to return character data that was
  219. * allocated with new or malloc.
  220. *
  221. * %cstring_output_allocated(char **outx, int *sz, free($1));
  222. * void foo(char **outx, int *sz) {
  223. * *outx = (char *) malloc(512);
  224. * sprintf(outx,"blah blah\n");
  225. * *sz = strlen(outx);
  226. * }
  227. */
  228. %define %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
  229. %typemap(ignore) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) {
  230. $1 = &temp;
  231. $2 = &tempn;
  232. }
  233. %typemap(argout)(TYPEMAP,SIZE) {
  234. if (*$1) {
  235. $result = caml_list_append($result,caml_val_string_len($1,$2));
  236. RELEASE;
  237. } else
  238. $result = caml_list_append($result,caml_val_ptr($1));
  239. }
  240. %enddef