PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-15/SWIG/Lib/python/cstring.i

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