Changeset 655 for trunk


Ignore:
Timestamp:
Apr 3, 2007, 12:03:26 PM (17 years ago)
Author:
dkearney
Message:

small updates to tests, but they are still incomplete
added -no-header flag to encode function to produce data that does not have a @@RP-ENC header

Location:
trunk/src/tcl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tcl/src/RpEncodeTclInterface.cc

    r653 r655  
    162162 *
    163163 * Full function call:
    164  * ::Rappture::encoding::encode ?-as z|b64|zb64? <string>
     164 * ::Rappture::encoding::encode ?-as z|b64|zb64? ?-no-header? <string>
    165165 */
    166166
     
    183183    int compress              = 1;
    184184    int base64                = 1;
     185    int addHeader             = 1;
     186
     187    Tcl_Obj *result           = NULL;
     188
     189    Tcl_ResetResult(interp);
     190
     191    cmdName = Tcl_GetString(objv[nextarg++]);
     192
     193    // parse through command line options
     194    if ((objc <= 2) && (objc >= 5)) {
     195        Tcl_AppendResult(interp,
     196                "wrong # args: should be \"", cmdName,
     197                " ?-as z|b64|zb64? ?-no-header? <string>\"", (char*)NULL);
     198        return TCL_ERROR;
     199    }
     200
     201    while ((objc - nextarg) > 0) {
     202        option = Tcl_GetStringFromObj(objv[nextarg], &optionLen);
     203        if (*option == '-') {
     204            if ( strncmp(option,"-as",optionLen) == 0 ) {
     205                nextarg++;
     206                typeLen = 0;
     207                if (nextarg < objc) {
     208                    encodeType = Tcl_GetStringFromObj(objv[nextarg],&typeLen);
     209                    nextarg++;
     210                }
     211                if (        (typeLen == 1) &&
     212                            (strncmp(encodeType,"z",typeLen) == 0) ) {
     213                    compress = 1;
     214                    base64 = 0;
     215                }
     216                else if (   (typeLen == 3) &&
     217                            (strncmp(encodeType,"b64",typeLen) == 0) ) {
     218                    compress = 0;
     219                    base64 = 1;
     220                }
     221                else if (   (typeLen == 4) &&
     222                            (strncmp(encodeType,"zb64",typeLen) == 0) ) {
     223                    compress = 1;
     224                    base64 = 1;
     225                }
     226                else {
     227                    // user did not specify recognized wishes for this option,
     228                    Tcl_AppendResult(interp, "bad value \"",(char*)NULL);
     229                    if (encodeType != NULL) {
     230                        Tcl_AppendResult(interp, encodeType,(char*)NULL);
     231                    }
     232                    Tcl_AppendResult(interp,
     233                            "\": should be one of z, b64, zb64",
     234                            (char*)NULL);
     235                    return TCL_ERROR;
     236                }
     237            }
     238            else if ( strncmp(option,"-no-header",optionLen) == 0 ) {
     239                nextarg++;
     240                addHeader = 0;
     241            }
     242            else {
     243                break;
     244            }
     245        }
     246        else {
     247            break;
     248        }
     249    }
     250
     251    if ((objc - nextarg) != 1) {
     252        Tcl_AppendResult(interp,
     253                "wrong # args: should be \"", cmdName,
     254                " ?-as z|b64|zb64? ?-no-header? <string>\"", (char*)NULL);
     255        return TCL_ERROR;
     256    }
     257
     258    option = (const char*) Tcl_GetByteArrayFromObj(objv[nextarg++], &optionLen);
     259
     260    if (strncmp(option,"@@RP-ENC:z\n",11) == 0) {
     261        buf = Rappture::Buffer(option+11,optionLen-11);
     262        buf.decode(1,0);
     263    }
     264    else if (strncmp(option,"@@RP-ENC:b64\n",13) == 0) {
     265        buf = Rappture::Buffer(option+13,optionLen-13);
     266        buf.decode(0,1);
     267    }
     268    else if (strncmp(option,"@@RP-ENC:zb64\n",14) == 0) {
     269        buf = Rappture::Buffer(option+14,optionLen-14);
     270        buf.decode(1,1);
     271    }
     272    else {
     273        // no special recognized tags
     274        buf = Rappture::Buffer(option,optionLen);
     275    }
     276
     277    buf.encode(compress,base64);
     278    result = Tcl_GetObjResult(interp);
     279
     280    if (addHeader == 1) {
     281        if ((compress == 1) && (base64 == 0)) {
     282            Tcl_AppendToObj(result,"@@RP-ENC:z\n",11);
     283        }
     284        else if ((compress == 0) && (base64 == 1)) {
     285            Tcl_AppendToObj(result,"@@RP-ENC:b64\n",13);
     286        }
     287        else if ((compress == 1) && (base64 == 1)) {
     288            Tcl_AppendToObj(result,"@@RP-ENC:zb64\n",14);
     289        }
     290        else {
     291            // do nothing
     292        }
     293    }
     294    else {
     295        // do nothing
     296    }
     297
     298    Tcl_AppendToObj(result,buf.bytes(),buf.size());
     299
     300    return TCL_OK;
     301}
     302
     303/**********************************************************************/
     304// FUNCTION: RpTclEncodingDecode()
     305/// Rappture::encoding::decode function in Tcl, decodes provided string
     306/**
     307 * Decode a string by uncompressing it with zlib and base64 decoding it.
     308 * If binary data is provided, the data is base64 decoded and uncompressed.
     309 * RpTclEncodingIs is used to qualify binary data.
     310 *
     311 * Full function call:
     312 * ::Rappture::encoding::decode ?-as z|b64|zb64? <string>
     313 */
     314
     315int
     316RpTclEncodingDecode (   ClientData cdata,
     317                        Tcl_Interp *interp,
     318                        int objc,
     319                        Tcl_Obj *const objv[]  )
     320{
     321
     322    const char* encodeType    = NULL; // name of the units provided by user
     323    const char* option        = NULL;
     324    const char* cmdName       = NULL;
     325    Rappture::Buffer buf      = ""; // name of the units provided by user
     326
     327    int optionLen             = 0;
     328    int typeLen               = 0;
     329    int nextarg               = 0; // start parsing using the '2'th argument
     330
     331    int decompress            = 0;
     332    int base64                = 0;
    185333
    186334    Tcl_Obj *result           = NULL;
     
    209357            if (        (typeLen == 1) &&
    210358                        (strncmp(encodeType,"z",typeLen) == 0) ) {
    211                 compress = 1;
     359                decompress = 1;
    212360                base64 = 0;
    213361            }
    214362            else if (   (typeLen == 3) &&
    215363                        (strncmp(encodeType,"b64",typeLen) == 0) ) {
    216                 compress = 0;
     364                decompress = 0;
    217365                base64 = 1;
    218366            }
    219367            else if (   (typeLen == 4) &&
    220368                        (strncmp(encodeType,"zb64",typeLen) == 0) ) {
    221                 compress = 1;
     369                decompress = 1;
    222370                base64 = 1;
    223371            }
     
    247395    if (strncmp(option,"@@RP-ENC:z\n",11) == 0) {
    248396        buf = Rappture::Buffer(option+11,optionLen-11);
    249         buf.decode(1,0);
    250     }
    251     else if (strncmp(option,"@@RP-ENC:b64\n",13) == 0) {
    252         buf = Rappture::Buffer(option+13,optionLen-13);
    253         buf.decode(0,1);
    254     }
    255     else if (strncmp(option,"@@RP-ENC:zb64\n",14) == 0) {
    256         buf = Rappture::Buffer(option+14,optionLen-14);
    257         buf.decode(1,1);
    258     }
    259     else {
    260         // no special recognized tags
    261         buf = Rappture::Buffer(option,optionLen);
    262     }
    263 
    264     buf.encode(compress,base64);
    265     result = Tcl_GetObjResult(interp);
    266 
    267     if ((compress == 1) && (base64 == 0)) {
    268         Tcl_AppendToObj(result,"@@RP-ENC:z\n",11);
    269     }
    270     else if ((compress == 0) && (base64 == 1)) {
    271         Tcl_AppendToObj(result,"@@RP-ENC:b64\n",13);
    272     }
    273     else if ((compress == 1) && (base64 == 1)) {
    274         Tcl_AppendToObj(result,"@@RP-ENC:zb64\n",14);
    275     }
    276     else {
    277         // do nothing
    278     }
    279 
    280     Tcl_AppendToObj(result,buf.bytes(),buf.size());
    281 
    282     return TCL_OK;
    283 }
    284 
    285 /**********************************************************************/
    286 // FUNCTION: RpTclEncodingDecode()
    287 /// Rappture::encoding::decode function in Tcl, decodes provided string
    288 /**
    289  * Decode a string by uncompressing it with zlib and base64 decoding it.
    290  * If binary data is provided, the data is base64 decoded and uncompressed.
    291  * RpTclEncodingIs is used to qualify binary data.
    292  *
    293  * Full function call:
    294  * ::Rappture::encoding::decode ?-as z|b64|zb64? <string>
    295  */
    296 
    297 int
    298 RpTclEncodingDecode (   ClientData cdata,
    299                         Tcl_Interp *interp,
    300                         int objc,
    301                         Tcl_Obj *const objv[]  )
    302 {
    303 
    304     const char* encodeType    = NULL; // name of the units provided by user
    305     const char* option        = NULL;
    306     const char* cmdName       = NULL;
    307     Rappture::Buffer buf      = ""; // name of the units provided by user
    308 
    309     int optionLen             = 0;
    310     int typeLen               = 0;
    311     int nextarg               = 0; // start parsing using the '2'th argument
    312 
    313     int decompress            = 0;
    314     int base64                = 0;
    315 
    316     Tcl_Obj *result           = NULL;
    317 
    318     Tcl_ResetResult(interp);
    319 
    320     cmdName = Tcl_GetString(objv[nextarg++]);
    321 
    322     // parse through command line options
    323     if ((objc != 2) && (objc != 4)) {
    324         Tcl_AppendResult(interp,
    325                 "wrong # args: should be \"", cmdName,
    326                 " ?-as z|b64|zb64? <string>\"", (char*)NULL);
    327         return TCL_ERROR;
    328     }
    329 
    330     option = Tcl_GetStringFromObj(objv[nextarg], &optionLen);
    331     if (*option == '-') {
    332         if ( strncmp(option,"-as",optionLen) == 0 ) {
    333             nextarg++;
    334             typeLen = 0;
    335             if (nextarg < objc) {
    336                 encodeType = Tcl_GetStringFromObj(objv[nextarg],&typeLen);
    337                 nextarg++;
    338             }
    339             if (        (typeLen == 1) &&
    340                         (strncmp(encodeType,"z",typeLen) == 0) ) {
    341                 decompress = 1;
    342                 base64 = 0;
    343             }
    344             else if (   (typeLen == 3) &&
    345                         (strncmp(encodeType,"b64",typeLen) == 0) ) {
    346                 decompress = 0;
    347                 base64 = 1;
    348             }
    349             else if (   (typeLen == 4) &&
    350                         (strncmp(encodeType,"zb64",typeLen) == 0) ) {
    351                 decompress = 1;
    352                 base64 = 1;
    353             }
    354             else {
    355                 // user did not specify recognized wishes for this option,
    356                 Tcl_AppendResult(interp, "bad value \"",(char*)NULL);
    357                 if (encodeType != NULL) {
    358                     Tcl_AppendResult(interp, encodeType,(char*)NULL);
    359                 }
    360                 Tcl_AppendResult(interp,
    361                         "\": should be one of z, b64, zb64",
    362                         (char*)NULL);
    363                 return TCL_ERROR;
    364             }
    365         }
    366     }
    367 
    368     if ((objc - nextarg) != 1) {
    369         Tcl_AppendResult(interp,
    370                 "wrong # args: should be \"", cmdName,
    371                 " ?-as z|b64|zb64? <string>\"", (char*)NULL);
    372         return TCL_ERROR;
    373     }
    374 
    375     option = (const char*) Tcl_GetByteArrayFromObj(objv[nextarg++], &optionLen);
    376 
    377     if (strncmp(option,"@@RP-ENC:z\n",11) == 0) {
    378         buf = Rappture::Buffer(option+11,optionLen-11);
    379397        if ( (decompress == 0) && (base64 == 0) ) {
    380398            decompress = 1;
  • trunk/src/tcl/tests/encode.test

    r652 r655  
    5656#----------------------------------------------------------
    5757# encode command
    58 # Rappture::encoding::encode ?-as z|b64? <string>
     58# Rappture::encoding::encode ?-as z|b64? ?-no-header? <string>
    5959#----------------------------------------------------------
    6060
    6161test encode-2.0.0 {Rappture::encoding::encode, 0 arguments} {
    6262    list [catch {Rappture::encoding::encode} msg] $msg
    63 } {1 {wrong # args: should be "Rappture::encoding::encode ?-as z|b64|zb64? <string>"}}
     63} {1 {wrong # args: should be "Rappture::encoding::encode ?-as z|b64|zb64? ?-no-header? <string>"}}
    6464
    6565test encode-2.1.0 {Rappture::encoding::encode, ascii string argument} {
     
    8787test encode-2.2.2 {Rappture::encoding::encode, -as flag correct value z} {
    8888    list [catch {Rappture::encoding::encode -as z} msg] $msg
    89 } {1 {wrong # args: should be "Rappture::encoding::encode ?-as z|b64|zb64? <string>"}}
     89} {1 {wrong # args: should be "Rappture::encoding::encode ?-as z|b64|zb64? ?-no-header? <string>"}}
    9090
    9191test encode-2.2.3 {Rappture::encoding::encode, -as z w/ string} {
     
    130130test decode-3.2.1 {Rappture::encoding::decode, -as flag, bad value} {
    131131    list [catch {Rappture::encoding::decode -as zz} msg] $msg
    132 } {1 {bad value "zz": should be one of z, b64, zb64}}
     132} {1 {wrong # args: should be "Rappture::encoding::decode ?-as z|b64|zb64? <string>"}}
    133133
    134134test decode-3.2.2 {Rappture::encoding::decode, -as flag, zb64 w/ string} {
Note: See TracChangeset for help on using the changeset viewer.