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