| 1 | /* |
| 2 | * cstring.i |
| 3 | * $Header: /cvsroot/swig/SWIG/Lib/tcl/cstring.i,v 1.5 2004/10/06 16:51:22 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 | /* %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,fragment="t_output_helper") TYPEMAP { |
| 41 | Tcl_Obj *o; |
| 42 | $1[MAX] = 0; |
| 43 | o = Tcl_NewStringObj($1,-1); |
| 44 | Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp), o); |
| 45 | } |
| 46 | %enddef |
| 47 | |
| 48 | /* |
| 49 | * %cstring_chunk_output(TYPEMAP, SIZE) |
| 50 | * |
| 51 | * This macro is used to return a chunk of binary string data. |
| 52 | * Embedded NULLs are okay. For example: |
| 53 | * |
| 54 | * %cstring_chunk_output(char *outx, 512); |
| 55 | * void foo(char *outx) { |
| 56 | * memmove(outx, somedata, 512); |
| 57 | * } |
| 58 | * |
| 59 | */ |
| 60 | |
| 61 | %define %cstring_chunk_output(TYPEMAP,SIZE) |
| 62 | %typemap(ignore) TYPEMAP(char temp[SIZE]) { |
| 63 | $1 = ($1_ltype) temp; |
| 64 | } |
| 65 | %typemap(argout,fragment="t_output_helper") TYPEMAP { |
| 66 | Tcl_Obj *o = Tcl_NewStringObj($1,SIZE); |
| 67 | Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp),o); |
| 68 | } |
| 69 | %enddef |
| 70 | |
| 71 | /* |
| 72 | * %cstring_bounded_mutable(TYPEMAP, SIZE) |
| 73 | * |
| 74 | * This macro is used to wrap a string that's going to mutate. |
| 75 | * |
| 76 | * %cstring_bounded_mutable(char *in, 512); |
| 77 | * void foo(in *x) { |
| 78 | * while (*x) { |
| 79 | * *x = toupper(*x); |
| 80 | * x++; |
| 81 | * } |
| 82 | * } |
| 83 | * |
| 84 | */ |
| 85 | |
| 86 | |
| 87 | %define %cstring_bounded_mutable(TYPEMAP,MAX) |
| 88 | %typemap(in) TYPEMAP(char temp[MAX+1]) { |
| 89 | char *t = Tcl_GetStringFromObj($input,NULL); |
| 90 | strncpy(temp,t,MAX); |
| 91 | $1 = ($1_ltype) temp; |
| 92 | } |
| 93 | %typemap(argout,fragment="t_output_helper") TYPEMAP { |
| 94 | Tcl_Obj *o; |
| 95 | $1[MAX] = 0; |
| 96 | o = Tcl_NewStringObj($1,-1); |
| 97 | Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp),o); |
| 98 | } |
| 99 | %enddef |
| 100 | |
| 101 | /* |
| 102 | * %cstring_mutable(TYPEMAP [, expansion]) |
| 103 | * |
| 104 | * This macro is used to wrap a string that will mutate in place. |
| 105 | * It may change size up to a user-defined expansion. |
| 106 | * |
| 107 | * %cstring_mutable(char *in); |
| 108 | * void foo(in *x) { |
| 109 | * while (*x) { |
| 110 | * *x = toupper(*x); |
| 111 | * x++; |
| 112 | * } |
| 113 | * } |
| 114 | * |
| 115 | */ |
| 116 | |
| 117 | %define %cstring_mutable(TYPEMAP,...) |
| 118 | %typemap(in) TYPEMAP { |
| 119 | int n; |
| 120 | char *t = Tcl_GetStringFromObj($input,&n); |
| 121 | $1 = ($1_ltype) t; |
| 122 | |
| 123 | #if #__VA_ARGS__ == "" |
| 124 | #ifdef __cplusplus |
| 125 | $1 = ($1_ltype) new char[n+1]; |
| 126 | #else |
| 127 | $1 = ($1_ltype) malloc(n+1); |
| 128 | #endif |
| 129 | #else |
| 130 | #ifdef __cplusplus |
| 131 | $1 = ($1_ltype) new char[n+1+__VA_ARGS__]; |
| 132 | #else |
| 133 | $1 = ($1_ltype) malloc(n+1+__VA_ARGS__); |
| 134 | #endif |
| 135 | #endif |
| 136 | memmove($1,t,n); |
| 137 | $1[n] = 0; |
| 138 | } |
| 139 | |
| 140 | %typemap(argout,fragment="t_output_helper") TYPEMAP { |
| 141 | Tcl_Obj *o; |
| 142 | o = Tcl_NewStringObj($1,-1); |
| 143 | Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), o); |
| 144 | #ifdef __cplusplus |
| 145 | delete[] $1; |
| 146 | #else |
| 147 | free($1); |
| 148 | #endif |
| 149 | } |
| 150 | %enddef |
| 151 | |
| 152 | /* |
| 153 | * %cstring_output_maxsize(TYPEMAP, SIZE) |
| 154 | * |
| 155 | * This macro returns data in a string of some user-defined size. |
| 156 | * |
| 157 | * %cstring_output_maxsize(char *outx, int max) { |
| 158 | * void foo(char *outx, int max) { |
| 159 | * sprintf(outx,"blah blah\n"); |
| 160 | * } |
| 161 | */ |
| 162 | |
| 163 | %define %cstring_output_maxsize(TYPEMAP, SIZE) |
| 164 | %typemap(in) (TYPEMAP, SIZE) { |
| 165 | long temp; |
| 166 | if (Tcl_GetLongFromObj(interp,$input,&temp) != TCL_OK) { |
| 167 | SWIG_fail; |
| 168 | } |
| 169 | $2 = ($2_ltype) temp; |
| 170 | #ifdef __cplusplus |
| 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 | Tcl_Obj *o; |
| 178 | o = Tcl_NewStringObj($1,-1); |
| 179 | Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),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 | long n; |
| 204 | if (Tcl_GetLongFromObj(interp,$input,&n) != TCL_OK) { |
| 205 | SWIG_fail; |
| 206 | } |
| 207 | #ifdef __cplusplus |
| 208 | $1 = ($1_ltype) new char[n+1]; |
| 209 | $2 = ($2_ltype) new $*1_ltype; |
| 210 | #else |
| 211 | $1 = ($1_ltype) malloc(n+1); |
| 212 | $2 = ($2_ltype) malloc(sizeof($*1_ltype)); |
| 213 | #endif |
| 214 | *$2 = n; |
| 215 | } |
| 216 | %typemap(argout,fragment="t_output_helper") (TYPEMAP,SIZE) { |
| 217 | Tcl_Obj *o; |
| 218 | o = Tcl_NewStringObj($1,*$2); |
| 219 | Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp), o); |
| 220 | #ifdef __cplusplus |
| 221 | delete [] $1; |
| 222 | delete $2; |
| 223 | #else |
| 224 | free($1); |
| 225 | free($2); |
| 226 | #endif |
| 227 | } |
| 228 | %enddef |
| 229 | |
| 230 | /* |
| 231 | * %cstring_output_allocate(TYPEMAP, RELEASE) |
| 232 | * |
| 233 | * This macro is used to return character data that was |
| 234 | * allocated with new or malloc. |
| 235 | * |
| 236 | * %cstring_output_allocated(char **outx, free($1)); |
| 237 | * void foo(char **outx) { |
| 238 | * *outx = (char *) malloc(512); |
| 239 | * sprintf(outx,"blah blah\n"); |
| 240 | * } |
| 241 | */ |
| 242 | |
| 243 | %define %cstring_output_allocate(TYPEMAP, RELEASE) |
| 244 | %typemap(ignore) TYPEMAP($*1_ltype temp = 0) { |
| 245 | $1 = &temp; |
| 246 | } |
| 247 | |
| 248 | %typemap(argout,fragment="t_output_helper") TYPEMAP { |
| 249 | if (*$1) { |
| 250 | Tcl_Obj *o = Tcl_NewStringObj(*$1,-1); |
| 251 | RELEASE; |
| 252 | Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), o); |
| 253 | } |
| 254 | } |
| 255 | %enddef |
| 256 | |
| 257 | /* |
| 258 | * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) |
| 259 | * |
| 260 | * This macro is used to return character data that was |
| 261 | * allocated with new or malloc. |
| 262 | * |
| 263 | * %cstring_output_allocated(char **outx, int *sz, free($1)); |
| 264 | * void foo(char **outx, int *sz) { |
| 265 | * *outx = (char *) malloc(512); |
| 266 | * sprintf(outx,"blah blah\n"); |
| 267 | * *sz = strlen(outx); |
| 268 | * } |
| 269 | */ |
| 270 | |
| 271 | %define %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) |
| 272 | %typemap(ignore) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) { |
| 273 | $1 = &temp; |
| 274 | $2 = &tempn; |
| 275 | } |
| 276 | |
| 277 | %typemap(argout,fragment="t_output_helper")(TYPEMAP,SIZE) { |
| 278 | if (*$1) { |
| 279 | Tcl_Obj *o = Tcl_NewStringObj(*$1,*$2); |
| 280 | RELEASE; |
| 281 | Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp), o); |
| 282 | } |
| 283 | } |
| 284 | %enddef |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |