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