Changeset 1368 for trunk/lang/tcl/src
- Timestamp:
- Mar 27, 2009 8:57:34 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lang/tcl/src/RpEncodeTclInterface.cc
r1326 r1368 14 14 #include <tcl.h> 15 15 #include "RpEncode.h" 16 17 #define TRUE 1 18 #define FALSE 0 16 19 17 20 extern "C" Tcl_AppInitProc RpEncoding_Init; … … 76 79 77 80 int bufLen; 78 const char *buf = (const char*) Tcl_GetByteArrayFromObj(objv[2],&bufLen);79 81 const char *buf; 82 buf = (const char*) Tcl_GetByteArrayFromObj(objv[2], &bufLen); 80 83 if (('b' == *type) && (strcmp(type,"binary") == 0)) { 81 84 if (Rappture::encoding::isbinary(buf,bufLen) != 0) { 82 85 // non-ascii character found, return yes 83 Tcl_ AppendResult(interp, "yes", (char*)NULL);86 Tcl_SetResult(interp, (char *)"yes", TCL_STATIC); 84 87 } else { 85 Tcl_ AppendResult(interp, "no",(char*)NULL);88 Tcl_SetResult(interp, (char *)"no", TCL_STATIC); 86 89 } 87 90 return TCL_OK; 88 91 } else if (('e' == *type) && (strcmp(type,"encoded") == 0)) { 89 if (Rappture::encoding::isencoded(buf, bufLen) != 0) {92 if (Rappture::encoding::isencoded(buf, bufLen) != 0) { 90 93 // valid "@@RP-ENC:" header found, return yes 91 Tcl_ AppendResult(interp, "yes", (char*)NULL);94 Tcl_SetResult(interp, (char *)"yes", TCL_STATIC); 92 95 } else { 93 Tcl_ AppendResult(interp, "no",(char*)NULL);96 Tcl_SetResult(interp, (char *)"no", TCL_STATIC); 94 97 } 95 98 return TCL_OK; 96 } 97 Tcl_AppendResult(interp, "bad option \"", type, 98 "\": should be one of binary, encoded", 99 (char*)NULL);99 } else { 100 Tcl_AppendResult(interp, "bad option \"", type, 101 "\": should be binary or encoded", (char*)NULL); 102 } 100 103 return TCL_ERROR; 101 104 } … … 124 127 125 128 const char* encodeType = NULL; // name of the units provided by user 126 const char* option = NULL;127 129 const char* cmdName = NULL; 128 Rappture::Buffer buf; // name of the units provided by user129 130 Rappture::Outcome err; 130 131 131 int optionLen = 0;132 132 int typeLen = 0; 133 133 int nextarg = 0; // start parsing using the '2'th argument 134 134 135 int compress = 1; 136 int base64 = 1; 137 int addHeader = 1; 138 int flags = 0; 135 bool compress, base64, addHeader; 136 137 compress = base64 = addHeader = true; 139 138 140 139 Tcl_Obj *result = NULL; … … 153 152 154 153 while ((objc - nextarg) > 0) { 154 const char *option; 155 int optionLen; 156 155 157 option = Tcl_GetStringFromObj(objv[nextarg], &optionLen); 156 158 if (*option == '-') { … … 164 166 if ( (typeLen == 1) && 165 167 (strncmp(encodeType,"z",typeLen) == 0) ) { 166 compress = 1;167 base64 = 0;168 compress = TRUE; 169 base64 = FALSE; 168 170 } 169 171 else if ( (typeLen == 3) && 170 172 (strncmp(encodeType,"b64",typeLen) == 0) ) { 171 compress = 0;172 base64 = 1;173 compress = FALSE; 174 base64 = TRUE; 173 175 } 174 176 else if ( (typeLen == 4) && 175 177 (strncmp(encodeType,"zb64",typeLen) == 0) ) { 176 compress = 1;177 base64 = 1;178 compress = TRUE; 179 base64 = TRUE; 178 180 } 179 181 else { … … 191 193 else if ( strncmp(option,"-no-header",optionLen) == 0 ) { 192 194 nextarg++; 193 addHeader = 0;195 addHeader = FALSE; 194 196 } 195 197 else if ( strcmp(option,"--") == 0 ) { … … 216 218 } 217 219 220 int optionLen; 221 const char* option; 218 222 option = (const char*) Tcl_GetByteArrayFromObj(objv[nextarg++], &optionLen); 219 buf = Rappture::Buffer(option,optionLen); 220 221 if (compress == 1) { 222 flags = flags | RPENC_Z; 223 } 224 if (base64 == 1) { 225 flags = flags | RPENC_B64; 226 } 227 if (addHeader == 1) { 228 flags = flags | RPENC_HDR; 229 } 230 231 err &= Rappture::encoding::encode(buf,flags); 232 233 if (!err) { 234 result = Tcl_NewByteArrayObj( 235 (const unsigned char*)buf.bytes(), buf.size()); 236 Tcl_SetObjResult(interp, result); 237 } 238 else { 223 Rappture::Buffer buf(option,optionLen); 224 225 unsigned int flags = 0; 226 if (compress) { 227 flags |= RPENC_Z; 228 } 229 if (base64) { 230 flags |= RPENC_B64; 231 } 232 if (addHeader) { 233 flags |= RPENC_HDR; 234 } 235 if (!Rappture::encoding::encode(err, buf, flags)) { 239 236 Tcl_AppendResult(interp, err.remark(), "\n", err.context(), NULL); 240 } 237 return TCL_ERROR; 238 } 239 result = Tcl_NewByteArrayObj((const unsigned char*)buf.bytes(), buf.size()); 240 Tcl_SetObjResult(interp, result); 241 241 return TCL_OK; 242 242 } … … 262 262 263 263 const char* encodeType = NULL; // name of the units provided by user 264 const char* option = NULL;265 264 const char* cmdName = NULL; 266 Rappture::Buffer buf = ""; // name of the units provided by user267 265 Rappture::Outcome err; 268 266 269 int optionLen = 0;270 267 int typeLen = 0; 271 268 int nextarg = 0; // start parsing using the '2'th argument 272 269 273 int decompress = 0; 274 int base64 = 0; 275 int flags = 0; 276 277 Tcl_Obj *result = NULL; 278 279 Tcl_ResetResult(interp); 270 bool decompress, base64; 271 decompress = base64 = false; 280 272 281 273 cmdName = Tcl_GetString(objv[nextarg++]); … … 290 282 291 283 while ((objc - nextarg) > 0) { 284 const char *option; 285 int optionLen; 286 292 287 option = Tcl_GetStringFromObj(objv[nextarg], &optionLen); 293 288 if (*option == '-') { … … 301 296 if ( (typeLen == 1) && 302 297 (strncmp(encodeType,"z",typeLen) == 0) ) { 303 decompress = 1;304 base64 = 0;298 decompress = true; 299 base64 = false; 305 300 } 306 301 else if ( (typeLen == 3) && 307 302 (strncmp(encodeType,"b64",typeLen) == 0) ) { 308 decompress = 0;309 base64 = 1;303 decompress = false; 304 base64 = true; 310 305 } 311 306 else if ( (typeLen == 4) && 312 307 (strncmp(encodeType,"zb64",typeLen) == 0) ) { 313 decompress = 1;314 base64 = 1;308 decompress = true; 309 base64 = true; 315 310 } 316 311 else { … … 346 341 } 347 342 343 int optionLen; 344 const char* option; 348 345 option = (const char*) Tcl_GetByteArrayFromObj(objv[nextarg++], &optionLen); 349 346 350 buf = Rappture::Buffer(option,optionLen); 351 352 if (decompress == 1) { 353 flags = flags | RPENC_Z; 354 } 355 if (base64 == 1) { 356 flags = flags | RPENC_B64; 357 } 358 359 err &= Rappture::encoding::decode(buf,flags); 360 361 if (!err) { 362 result = Tcl_NewByteArrayObj( 363 (const unsigned char*)buf.bytes(), buf.size()); 364 Tcl_SetObjResult(interp, result); 365 } 366 else { 347 unsigned int flags = 0; 348 if (decompress) { 349 flags |= RPENC_Z; 350 } 351 if (base64) { 352 flags |= RPENC_B64; 353 } 354 Rappture::Buffer buf(option, optionLen); 355 if (!Rappture::encoding::decode(err, buf,flags)) { 367 356 Tcl_AppendResult(interp, err.remark(), "\n", err.context(), NULL); 368 } 357 return TCL_ERROR; 358 } 359 Tcl_Obj *objPtr; 360 objPtr = Tcl_NewByteArrayObj((const unsigned char*)buf.bytes(), buf.size()); 361 Tcl_SetObjResult(interp, objPtr); 369 362 return TCL_OK; 370 363 }
Note: See TracChangeset
for help on using the changeset viewer.