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.
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
11 * ----------------------------------------------------------------------------- */
13 /* %cstring_input_binary(TYPEMAP, SIZE)
15 * Macro makes a function accept binary string data along with
19 %define %cstring_input_binary(TYPEMAP, SIZE)
20 %apply (char *STRING, int LENGTH) { (TYPEMAP, SIZE) };
24 * %cstring_bounded_output(TYPEMAP, MAX)
26 * This macro is used to return a NULL-terminated output string of
27 * some maximum length. For example:
29 * %cstring_bounded_output(char *outx, 512);
30 * void foo(char *outx) {
31 * sprintf(outx,"blah blah\n");
36 %define %cstring_bounded_output(TYPEMAP,MAX)
37 %typemap(ignore) TYPEMAP(char temp[MAX+1]) {
40 %typemap(argout) TYPEMAP {
42 $result = caml_list_append($result,caml_val_string(str));
47 * %cstring_chunk_output(TYPEMAP, SIZE)
49 * This macro is used to return a chunk of binary string data.
50 * Embedded NULLs are okay. For example:
52 * %cstring_chunk_output(char *outx, 512);
53 * void foo(char *outx) {
54 * memmove(outx, somedata, 512);
59 %define %cstring_chunk_output(TYPEMAP,SIZE)
60 %typemap(ignore) TYPEMAP(char temp[SIZE]) {
63 %typemap(argout) TYPEMAP {
64 $result = caml_list_append($result,caml_val_string_len($1,SIZE));
69 * %cstring_bounded_mutable(TYPEMAP, SIZE)
71 * This macro is used to wrap a string that's going to mutate.
73 * %cstring_bounded_mutable(char *in, 512);
84 %define %cstring_bounded_mutable(TYPEMAP,MAX)
85 %typemap(in) TYPEMAP(char temp[MAX+1]) {
86 char *t = (char *)caml_ptr_val($input);
90 %typemap(argout) TYPEMAP {
91 $result = caml_list_append($result,caml_val_string_len($1,MAX));
96 * %cstring_mutable(TYPEMAP [, expansion])
98 * This macro is used to wrap a string that will mutate in place.
99 * It may change size up to a user-defined expansion.
101 * %cstring_mutable(char *in);
111 %define %cstring_mutable(TYPEMAP,...)
112 %typemap(in) TYPEMAP {
113 char *t = String_val($input);
114 int n = string_length($input);
116 #if #__VA_ARGS__ == ""
118 $1 = ($1_ltype) new char[n+1];
120 $1 = ($1_ltype) malloc(n+1);
124 $1 = ($1_ltype) new char[n+1+__VA_ARGS__];
126 $1 = ($1_ltype) malloc(n+1+__VA_ARGS__);
133 %typemap(argout) TYPEMAP {
134 $result = caml_list_append($result,caml_val_string($1));
144 * %cstring_output_maxsize(TYPEMAP, SIZE)
146 * This macro returns data in a string of some user-defined size.
148 * %cstring_output_maxsize(char *outx, int max) {
149 * void foo(char *outx, int max) {
150 * sprintf(outx,"blah blah\n");
154 %define %cstring_output_maxsize(TYPEMAP, SIZE)
155 %typemap(in) (TYPEMAP, SIZE) {
156 $2 = caml_val_long($input);
158 $1 = ($1_ltype) new char[$2+1];
160 $1 = ($1_ltype) malloc($2+1);
163 %typemap(argout) (TYPEMAP,SIZE) {
164 $result = caml_list_append($result,caml_val_string($1));
174 * %cstring_output_withsize(TYPEMAP, SIZE)
176 * This macro is used to return character data along with a size
179 * %cstring_output_maxsize(char *outx, int *max) {
180 * void foo(char *outx, int *max) {
181 * sprintf(outx,"blah blah\n");
182 * *max = strlen(outx);
186 %define %cstring_output_withsize(TYPEMAP, SIZE)
187 %typemap(in) (TYPEMAP, SIZE) {
188 int n = caml_val_long($input);
190 $1 = ($1_ltype) new char[n+1];
191 $2 = ($2_ltype) new $*1_ltype;
193 $1 = ($1_ltype) malloc(n+1);
194 $2 = ($2_ltype) malloc(sizeof($*1_ltype));
198 %typemap(argout) (TYPEMAP,SIZE) {
199 $result = caml_list_append($result,caml_val_string_len($1,$2));
211 * %cstring_output_allocate(TYPEMAP, RELEASE)
213 * This macro is used to return character data that was
214 * allocated with new or malloc.
216 * %cstring_output_allocated(char **outx, free($1));
217 * void foo(char **outx) {
218 * *outx = (char *) malloc(512);
219 * sprintf(outx,"blah blah\n");
223 %define %cstring_output_allocate(TYPEMAP, RELEASE)
224 %typemap(ignore) TYPEMAP($*1_ltype temp = 0) {
228 %typemap(argout) TYPEMAP {
230 $result = caml_list_append($result,caml_val_string($1));
233 $result = caml_list_append($result,caml_val_ptr($1));
239 * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
241 * This macro is used to return character data that was
242 * allocated with new or malloc.
244 * %cstring_output_allocated(char **outx, int *sz, free($1));
245 * void foo(char **outx, int *sz) {
246 * *outx = (char *) malloc(512);
247 * sprintf(outx,"blah blah\n");
248 * *sz = strlen(outx);
252 %define %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE)
253 %typemap(ignore) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) {
258 %typemap(argout)(TYPEMAP,SIZE) {
260 $result = caml_list_append($result,caml_val_string_len($1,$2));
263 $result = caml_list_append($result,caml_val_ptr($1));