PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/typemaps/cstrings.swg

#
Unknown | 288 lines | 255 code | 33 blank | 0 comment | 0 complexity | b98cd376e46f9799f1d5ef73a07085e7 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * cstrings.swg
  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. %define %typemaps_cstring(Name, Char,
  10. SWIG_AsCharPtr,
  11. SWIG_AsCharPtrAndSize,
  12. SWIG_FromCharPtr,
  13. SWIG_FromCharPtrAndSize)
  14. /* %cstring_input_binary(TYPEMAP, SIZE)
  15. *
  16. * Macro makes a function accept binary string data along with
  17. * a size. For example:
  18. *
  19. * %cstring_input_binary(Char *buff, int size);
  20. * void foo(Char *buff, int size) {
  21. * }
  22. *
  23. */
  24. %define Name ## _input_binary(TYPEMAP, SIZE)
  25. %typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) (TYPEMAP, SIZE)
  26. (int res, Char *buf = 0, size_t size = 0, int alloc = 0) {
  27. res = SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc);
  28. if (!SWIG_IsOK(res)) {
  29. %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  30. }
  31. $1 = ($1_ltype) buf;
  32. $2 = ($2_ltype) size - 1;
  33. }
  34. %typemap(freearg,noblock=1,match="in") (TYPEMAP, SIZE) {
  35. if (alloc$argnum == SWIG_NEWOBJ) %delete_array(buf$argnum);
  36. }
  37. %enddef
  38. /*
  39. * %cstring_bounded_output(TYPEMAP, MAX)
  40. *
  41. * This macro is used to return a NULL-terminated output string of
  42. * some maximum length. For example:
  43. *
  44. * %cstring_bounded_output(Char *outx, 512);
  45. * void foo(Char *outx) {
  46. * sprintf(outx,"blah blah\n");
  47. * }
  48. *
  49. */
  50. %define Name ## _bounded_output(TYPEMAP,MAX)
  51. %typemap(in,noblock=1,numinputs=0) TYPEMAP (Char temp[MAX+1]) {
  52. $1 = ($1_ltype) temp;
  53. }
  54. %typemap(freearg,match="in") TYPEMAP "";
  55. %typemap(argout,noblock=1,fragment= #SWIG_FromCharPtr ) TYPEMAP {
  56. $1[MAX] = 0;
  57. %append_output(SWIG_FromCharPtr($1));
  58. }
  59. %enddef
  60. /*
  61. * %cstring_chunk_output(TYPEMAP, SIZE)
  62. *
  63. * This macro is used to return a chunk of binary string data.
  64. * Embedded NULLs are okay. For example:
  65. *
  66. * %cstring_chunk_output(Char *outx, 512);
  67. * void foo(Char *outx) {
  68. * memmove(outx, somedata, 512);
  69. * }
  70. *
  71. */
  72. %define Name ## _chunk_output(TYPEMAP,SIZE)
  73. %typemap(in,noblock=1,numinputs=0) TYPEMAP(Char temp[SIZE]) {
  74. $1 = ($1_ltype) temp;
  75. }
  76. %typemap(freearg,match="in") TYPEMAP "";
  77. %typemap(argout,noblock=1,fragment= #SWIG_FromCharPtrAndSize) TYPEMAP {
  78. %append_output(SWIG_FromCharPtrAndSize($1,SIZE));
  79. }
  80. %enddef
  81. /*
  82. * %cstring_bounded_mutable(TYPEMAP, SIZE)
  83. *
  84. * This macro is used to wrap a string that's going to mutate.
  85. *
  86. * %cstring_bounded_mutable(Char *in, 512);
  87. * void foo(in *x) {
  88. * while (*x) {
  89. * *x = toupper(*x);
  90. * x++;
  91. * }
  92. * }
  93. *
  94. */
  95. %define Name ## _bounded_mutable(TYPEMAP,MAX)
  96. %typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP
  97. (int res,Char temp[MAX+1], Char *t = 0, size_t n = 0, int alloc = 0) {
  98. res = SWIG_AsCharPtrAndSize($input, &t, &n, &alloc);
  99. if (!SWIG_IsOK(res)) {
  100. %argument_fail(res, "TYPEMAP", $symname, $argnum);
  101. }
  102. if ( n > (size_t) MAX ) n = (size_t) MAX;
  103. memcpy(temp, t, sizeof(Char)*n);
  104. if (alloc == SWIG_NEWOBJ) %delete_array(t);
  105. temp[n - 1] = 0;
  106. $1 = ($1_ltype) temp;
  107. }
  108. %typemap(freearg,match="in") TYPEMAP "";
  109. %typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) TYPEMAP {
  110. $1[MAX] = 0;
  111. %append_output(SWIG_FromCharPtr($1));
  112. }
  113. %enddef
  114. /*
  115. * %cstring_mutable(TYPEMAP [, expansion])
  116. *
  117. * This macro is used to wrap a string that will mutate in place.
  118. * It may change size up to a user-defined expansion.
  119. *
  120. * %cstring_mutable(Char *in);
  121. * void foo(in *x) {
  122. * while (*x) {
  123. * *x = toupper(*x);
  124. * x++;
  125. * }
  126. * }
  127. *
  128. */
  129. %define Name ## _mutable(TYPEMAP,EXP...)
  130. %typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP (int res, Char *t = 0, size_t n = 0, int alloc = 0, size_t expansion = 0) {
  131. #if #EXP != ""
  132. expansion += EXP;
  133. #endif
  134. res = SWIG_AsCharPtrAndSize($input, &t, &n, &alloc);
  135. if (!SWIG_IsOK(res)) {
  136. %argument_fail(res, "TYPEMAP", $symname, $argnum);
  137. }
  138. $1 = %new_array(n+expansion, $*1_ltype);
  139. memcpy($1,t,sizeof(Char)*n);
  140. if (alloc == SWIG_NEWOBJ) %delete_array(t);
  141. $1[n-1] = 0;
  142. }
  143. %typemap(freearg,match="in") TYPEMAP "";
  144. %typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) TYPEMAP {
  145. %append_output(SWIG_FromCharPtr($1));
  146. %delete_array($1);
  147. }
  148. %enddef
  149. /*
  150. * %cstring_output_maxsize(TYPEMAP, SIZE)
  151. *
  152. * This macro returns data in a string of some user-defined size.
  153. *
  154. * %cstring_output_maxsize(Char *outx, int max) {
  155. * void foo(Char *outx, int max) {
  156. * sprintf(outx,"blah blah\n");
  157. * }
  158. */
  159. %define Name ## _output_maxsize(TYPEMAP, SIZE)
  160. %typemap(in,noblock=1,fragment=SWIG_AsVal_frag(size_t)) (TYPEMAP, SIZE) (int res, size_t size, Char *buff = 0) {
  161. res = SWIG_AsVal(size_t)($input, &size);
  162. if (!SWIG_IsOK(res)) {
  163. %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  164. }
  165. buff= %new_array(size+1, Char);
  166. $2 = %numeric_cast(size, $2_ltype);
  167. $1 = %static_cast(buff, $1_ltype);
  168. }
  169. %typemap(freearg,noblock=1,match="in") (TYPEMAP,SIZE) {
  170. if (buff$argnum) %delete_array(buff$argnum);
  171. }
  172. %typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) (TYPEMAP,SIZE) {
  173. %append_output(SWIG_FromCharPtr($1));
  174. }
  175. %enddef
  176. /*
  177. * %cstring_output_withsize(TYPEMAP, SIZE)
  178. *
  179. * This macro is used to return Character data along with a size
  180. * parameter.
  181. *
  182. * %cstring_output_maxsize(Char *outx, int *max) {
  183. * void foo(Char *outx, int *max) {
  184. * sprintf(outx,"blah blah\n");
  185. * *max = strlen(outx);
  186. * }
  187. */
  188. %define Name ## _output_withsize(TYPEMAP, SIZE)
  189. %typemap(in,noblock=1,fragment=SWIG_AsVal_frag(size_t)) (TYPEMAP, SIZE) (int res, size_t n, Char *buff = 0, $*2_ltype size) {
  190. res = SWIG_AsVal(size_t)($input, &n);
  191. if (!SWIG_IsOK(res)) {
  192. %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  193. }
  194. buff= %new_array(n+1, Char);
  195. $1 = %static_cast(buff, $1_ltype);
  196. size = %numeric_cast(n,$*2_ltype);
  197. $2 = &size;
  198. }
  199. %typemap(freearg,noblock=1,match="in")(TYPEMAP,SIZE) {
  200. if (buff$argnum) %delete_array(buff$argnum);
  201. }
  202. %typemap(argout,noblock=1,fragment=#SWIG_FromCharPtrAndSize) (TYPEMAP,SIZE) {
  203. %append_output(SWIG_FromCharPtrAndSize($1,*$2));
  204. }
  205. %enddef
  206. /*
  207. * %cstring_output_allocate(TYPEMAP, RELEASE)
  208. *
  209. * This macro is used to return Character data that was
  210. * allocated with new or malloc.
  211. *
  212. * %cstring_output_allocated(Char **outx, free($1));
  213. * void foo(Char **outx) {
  214. * *outx = (Char *) malloc(512);
  215. * sprintf(outx,"blah blah\n");
  216. * }
  217. */
  218. %define Name ## _output_allocate(TYPEMAP, RELEASE)
  219. %typemap(in,noblock=1,numinputs=0) TYPEMAP($*1_ltype temp = 0) {
  220. $1 = &temp;
  221. }
  222. %typemap(freearg,match="in") TYPEMAP "";
  223. %typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) TYPEMAP {
  224. if (*$1) {
  225. %append_output(SWIG_FromCharPtr(*$1));
  226. RELEASE;
  227. }
  228. }
  229. %enddef
  230. /*
  231. * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
  232. *
  233. * This macro is used to return Character data that was
  234. * allocated with new or malloc.
  235. *
  236. * %cstring_output_allocated(Char **outx, int *sz, free($1));
  237. * void foo(Char **outx, int *sz) {
  238. * *outx = (Char *) malloc(512);
  239. * sprintf(outx,"blah blah\n");
  240. * *sz = strlen(outx);
  241. * }
  242. */
  243. %define Name ## _output_allocate_size(TYPEMAP, SIZE, RELEASE)
  244. %typemap(in,noblock=1,numinputs=0) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) {
  245. $1 = &temp; $2 = &tempn;
  246. }
  247. %typemap(freearg,match="in") (TYPEMAP,SIZE) "";
  248. %typemap(argout,noblock=1,fragment=#SWIG_FromCharPtrAndSize)(TYPEMAP,SIZE) {
  249. if (*$1) {
  250. %append_output(SWIG_FromCharPtrAndSize(*$1,*$2));
  251. RELEASE;
  252. }
  253. }
  254. %enddef
  255. %enddef