| 1 | /* |
| 2 | * cstring.i |
| 3 | * $Header: /cvsroot/swig/SWIG/Lib/python/cstrbase.swg,v 1.3 2005/09/06 07:33:16 marcelomatus Exp $ |
| 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 <pytuplehlp.swg> |
| 14 | |
| 15 | %define %typemap_cstrings(Name, Char, |
| 16 | SWIG_AsCharPtr, |
| 17 | SWIG_AsCharPtrAndSize, |
| 18 | SWIG_FromCharPtr, |
| 19 | SWIG_FromCharArray) |
| 20 | |
| 21 | /* %cstring_input_binary(TYPEMAP, SIZE) |
| 22 | * |
| 23 | * Macro makes a function accept binary string data along with |
| 24 | * a size. For example: |
| 25 | * |
| 26 | * %cstring_input_binary(Char *buff, int size); |
| 27 | * void foo(Char *buff, int size) { |
| 28 | * } |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | %define Name ## _input_binary(TYPEMAP, SIZE) |
| 33 | %typemap(in, fragment=#SWIG_AsCharPtrAndSize) (TYPEMAP, SIZE) |
| 34 | (Char *buf, size_t size) |
| 35 | { |
| 36 | SWIG_AsCharPtrAndSize($input, &buf, &size); |
| 37 | if (SWIG_arg_fail($argnum)) SWIG_fail; |
| 38 | $1 = ($1_ltype) buf; |
| 39 | $2 = ($2_ltype) size - 1; |
| 40 | } |
| 41 | %enddef |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | * %cstring_bounded_output(TYPEMAP, MAX) |
| 46 | * |
| 47 | * This macro is used to return a NULL-terminated output string of |
| 48 | * some maximum length. For example: |
| 49 | * |
| 50 | * %cstring_bounded_output(Char *outx, 512); |
| 51 | * void foo(Char *outx) { |
| 52 | * sprintf(outx,"blah blah\n"); |
| 53 | * } |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | %define Name ## _bounded_output(TYPEMAP,MAX) |
| 58 | %typemap(in,numinputs=0) TYPEMAP(Char temp[MAX+1]) |
| 59 | "$1 = ($1_ltype) temp;"; |
| 60 | |
| 61 | %typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr ) TYPEMAP |
| 62 | "$1[MAX] = 0; $result = t_output_helper($result, SWIG_FromCharPtr($1));"; |
| 63 | %enddef |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * %cstring_chunk_output(TYPEMAP, SIZE) |
| 68 | * |
| 69 | * This macro is used to return a chunk of binary string data. |
| 70 | * Embedded NULLs are okay. For example: |
| 71 | * |
| 72 | * %cstring_chunk_output(Char *outx, 512); |
| 73 | * void foo(Char *outx) { |
| 74 | * memmove(outx, somedata, 512); |
| 75 | * } |
| 76 | * |
| 77 | */ |
| 78 | |
| 79 | %define Name ## _chunk_output(TYPEMAP,SIZE) |
| 80 | %typemap(in,numinputs=0) TYPEMAP(Char temp[SIZE]) |
| 81 | "$1 = ($1_ltype) temp;"; |
| 82 | |
| 83 | %typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray) TYPEMAP |
| 84 | "$result = t_output_helper($result, SWIG_FromCharArray($1,SIZE));"; |
| 85 | %enddef |
| 86 | |
| 87 | |
| 88 | /* |
| 89 | * %cstring_bounded_mutable(TYPEMAP, SIZE) |
| 90 | * |
| 91 | * This macro is used to wrap a string that's going to mutate. |
| 92 | * |
| 93 | * %cstring_bounded_mutable(Char *in, 512); |
| 94 | * void foo(in *x) { |
| 95 | * while (*x) { |
| 96 | * *x = toupper(*x); |
| 97 | * x++; |
| 98 | * } |
| 99 | * } |
| 100 | * |
| 101 | */ |
| 102 | |
| 103 | |
| 104 | %define Name ## _bounded_mutable(TYPEMAP,MAX) |
| 105 | %typemap(in,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP(Char temp[MAX+1]) { |
| 106 | Char *t = 0; size_t n; |
| 107 | SWIG_AsCharPtrAndSize($input, &t, &n); |
| 108 | if (SWIG_arg_fail($argnum)) SWIG_fail; |
| 109 | if ( n > (size_t)MAX ) n = (size_t)MAX; |
| 110 | memcpy(temp, t, sizeof(Char)*n); |
| 111 | temp[n] = 0; |
| 112 | $1 = ($1_ltype) temp; |
| 113 | } |
| 114 | %typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP |
| 115 | "$1[MAX] = 0; $result = t_output_helper($result, SWIG_FromCharPtr($1));"; |
| 116 | %enddef |
| 117 | |
| 118 | |
| 119 | /* |
| 120 | * %cstring_mutable(TYPEMAP [, expansion]) |
| 121 | * |
| 122 | * This macro is used to wrap a string that will mutate in place. |
| 123 | * It may change size up to a user-defined expansion. |
| 124 | * |
| 125 | * %cstring_mutable(Char *in); |
| 126 | * void foo(in *x) { |
| 127 | * while (*x) { |
| 128 | * *x = toupper(*x); |
| 129 | * x++; |
| 130 | * } |
| 131 | * } |
| 132 | * |
| 133 | */ |
| 134 | |
| 135 | %define Name ## _mutable(TYPEMAP,EXP...) |
| 136 | %typemap(in,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP { |
| 137 | #if #EXP == "" |
| 138 | const size_t expansion = 1; |
| 139 | #else |
| 140 | const size_t expansion = 1 + EXP; |
| 141 | #endif |
| 142 | Char* t = 0; size_t n = 0; |
| 143 | SWIG_AsCharPtrAndSize($input, &t, &n); |
| 144 | if (SWIG_arg_fail($argnum)) SWIG_fail; |
| 145 | $1 = SWIG_new_array(n+expansion, $*1_ltype); |
| 146 | memcpy($1,t,sizeof(Char)*n); |
| 147 | $1[n] = 0; |
| 148 | } |
| 149 | |
| 150 | %typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP { |
| 151 | $result = t_output_helper($result,SWIG_FromCharPtr($1)); |
| 152 | SWIG_delete_array($1); |
| 153 | } |
| 154 | %enddef |
| 155 | |
| 156 | /* |
| 157 | * %cstring_output_maxsize(TYPEMAP, SIZE) |
| 158 | * |
| 159 | * This macro returns data in a string of some user-defined size. |
| 160 | * |
| 161 | * %cstring_output_maxsize(Char *outx, int max) { |
| 162 | * void foo(Char *outx, int max) { |
| 163 | * sprintf(outx,"blah blah\n"); |
| 164 | * } |
| 165 | */ |
| 166 | |
| 167 | %define Name ## _output_maxsize(TYPEMAP, SIZE) |
| 168 | %typemap(in,fragment=SWIG_As_frag(unsigned long)) (TYPEMAP, SIZE) { |
| 169 | $2 = ($2_ltype) SWIG_As(unsigned long)($input); |
| 170 | if (SWIG_arg_fail($argnum)) SWIG_fail; |
| 171 | $1 = SWIG_new_array($2+1, $*1_ltype); |
| 172 | } |
| 173 | %typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) (TYPEMAP,SIZE) { |
| 174 | $result = t_output_helper($result,SWIG_FromCharPtr($1)); |
| 175 | SWIG_delete_array($1); |
| 176 | } |
| 177 | %enddef |
| 178 | |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | * %cstring_output_withsize(TYPEMAP, SIZE) |
| 183 | * |
| 184 | * This macro is used to return Character data along with a size |
| 185 | * parameter. |
| 186 | * |
| 187 | * %cstring_output_maxsize(Char *outx, int *max) { |
| 188 | * void foo(Char *outx, int *max) { |
| 189 | * sprintf(outx,"blah blah\n"); |
| 190 | * *max = strlen(outx); |
| 191 | * } |
| 192 | */ |
| 193 | |
| 194 | %define Name ## _output_withsize(TYPEMAP, SIZE) |
| 195 | %typemap(in,fragment=SWIG_As_frag(unsigned long)) (TYPEMAP, SIZE) { |
| 196 | size_t n = SWIG_As(unsigned long)($input); |
| 197 | if (SWIG_arg_fail($argnum)) SWIG_fail; |
| 198 | $1 = SWIG_new_array(n+1, $*1_ltype); |
| 199 | $2 = SWIG_new($*2_ltype); |
| 200 | *$2 = n; |
| 201 | } |
| 202 | %typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray) (TYPEMAP,SIZE) { |
| 203 | $result = t_output_helper($result, SWIG_FromCharArray($1,*$2)); |
| 204 | SWIG_delete_array($1); |
| 205 | SWIG_delete($2); |
| 206 | } |
| 207 | %enddef |
| 208 | |
| 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 Name ## _output_allocate(TYPEMAP, RELEASE) |
| 224 | %typemap(in,numinputs=0) TYPEMAP($*1_ltype temp = 0) |
| 225 | "$1 = &temp;"; |
| 226 | |
| 227 | %typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP { |
| 228 | if (*$1) { |
| 229 | $result = t_output_helper($result,SWIG_FromCharPtr(*$1)); |
| 230 | RELEASE; |
| 231 | } |
| 232 | } |
| 233 | %enddef |
| 234 | |
| 235 | |
| 236 | /* |
| 237 | * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) |
| 238 | * |
| 239 | * This macro is used to return Character data that was |
| 240 | * allocated with new or malloc. |
| 241 | * |
| 242 | * %cstring_output_allocated(Char **outx, int *sz, free($1)); |
| 243 | * void foo(Char **outx, int *sz) { |
| 244 | * *outx = (Char *) malloc(512); |
| 245 | * sprintf(outx,"blah blah\n"); |
| 246 | * *sz = strlen(outx); |
| 247 | * } |
| 248 | */ |
| 249 | |
| 250 | %define Name ## _output_allocate_size(TYPEMAP, SIZE, RELEASE) |
| 251 | %typemap(in,numinputs=0) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) |
| 252 | "$1 = &temp; $2 = &tempn;"; |
| 253 | |
| 254 | %typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray)(TYPEMAP,SIZE) { |
| 255 | if (*$1) { |
| 256 | $result = t_output_helper($result,SWIG_FromCharArray(*$1,*$2)); |
| 257 | RELEASE; |
| 258 | } |
| 259 | } |
| 260 | %enddef |
| 261 | |
| 262 | %enddef |
| 263 | |