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