Changeset 1368 for trunk/lang/tcl/src


Ignore:
Timestamp:
Mar 27, 2009 8:57:34 PM (15 years ago)
Author:
gah
Message:

Reflect library encode/decode changes

File:
1 edited

Legend:

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

    r1326 r1368  
    1414#include <tcl.h>
    1515#include "RpEncode.h"
     16
     17#define TRUE    1
     18#define FALSE   0
    1619
    1720extern "C" Tcl_AppInitProc RpEncoding_Init;
     
    7679
    7780    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);
    8083    if (('b' == *type) && (strcmp(type,"binary") == 0)) {
    8184        if (Rappture::encoding::isbinary(buf,bufLen) != 0) {
    8285            // non-ascii character found, return yes
    83             Tcl_AppendResult(interp, "yes", (char*)NULL);
     86            Tcl_SetResult(interp, (char *)"yes", TCL_STATIC);
    8487        } else {
    85             Tcl_AppendResult(interp, "no",(char*)NULL);
     88            Tcl_SetResult(interp, (char *)"no", TCL_STATIC);
    8689        }
    8790        return TCL_OK;
    8891    } else if (('e' == *type) && (strcmp(type,"encoded") == 0)) {
    89         if (Rappture::encoding::isencoded(buf,bufLen) != 0) {
     92        if (Rappture::encoding::isencoded(buf, bufLen) != 0) {
    9093            // valid "@@RP-ENC:" header found, return yes
    91             Tcl_AppendResult(interp, "yes", (char*)NULL);
     94            Tcl_SetResult(interp, (char *)"yes", TCL_STATIC);
    9295        } else {
    93             Tcl_AppendResult(interp, "no",(char*)NULL);
     96            Tcl_SetResult(interp, (char *)"no", TCL_STATIC);
    9497        }
    9598        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    }
    100103    return TCL_ERROR;
    101104}
     
    124127
    125128    const char* encodeType    = NULL; // name of the units provided by user
    126     const char* option        = NULL;
    127129    const char* cmdName       = NULL;
    128     Rappture::Buffer buf; // name of the units provided by user
    129130    Rappture::Outcome err;
    130131
    131     int optionLen             = 0;
    132132    int typeLen               = 0;
    133133    int nextarg               = 0; // start parsing using the '2'th argument
    134134
    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;
    139138
    140139    Tcl_Obj *result           = NULL;
     
    153152
    154153    while ((objc - nextarg) > 0) {
     154        const char *option;
     155        int optionLen;
     156
    155157        option = Tcl_GetStringFromObj(objv[nextarg], &optionLen);
    156158        if (*option == '-') {
     
    164166                if (        (typeLen == 1) &&
    165167                            (strncmp(encodeType,"z",typeLen) == 0) ) {
    166                     compress = 1;
    167                     base64 = 0;
     168                    compress = TRUE;
     169                    base64 = FALSE;
    168170                }
    169171                else if (   (typeLen == 3) &&
    170172                            (strncmp(encodeType,"b64",typeLen) == 0) ) {
    171                     compress = 0;
    172                     base64 = 1;
     173                    compress = FALSE;
     174                    base64 = TRUE;
    173175                }
    174176                else if (   (typeLen == 4) &&
    175177                            (strncmp(encodeType,"zb64",typeLen) == 0) ) {
    176                     compress = 1;
    177                     base64 = 1;
     178                    compress = TRUE;
     179                    base64 = TRUE;
    178180                }
    179181                else {
     
    191193            else if ( strncmp(option,"-no-header",optionLen) == 0 ) {
    192194                nextarg++;
    193                 addHeader = 0;
     195                addHeader = FALSE;
    194196            }
    195197            else if ( strcmp(option,"--") == 0 ) {
     
    216218    }
    217219
     220    int optionLen;
     221    const char* option;
    218222    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)) {
    239236        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);
    241241    return TCL_OK;
    242242}
     
    262262
    263263    const char* encodeType    = NULL; // name of the units provided by user
    264     const char* option        = NULL;
    265264    const char* cmdName       = NULL;
    266     Rappture::Buffer buf      = ""; // name of the units provided by user
    267265    Rappture::Outcome err;
    268266
    269     int optionLen             = 0;
    270267    int typeLen               = 0;
    271268    int nextarg               = 0; // start parsing using the '2'th argument
    272269
    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;
    280272
    281273    cmdName = Tcl_GetString(objv[nextarg++]);
     
    290282
    291283    while ((objc - nextarg) > 0) {
     284        const char *option;
     285        int optionLen;
     286
    292287        option = Tcl_GetStringFromObj(objv[nextarg], &optionLen);
    293288        if (*option == '-') {
     
    301296                if (        (typeLen == 1) &&
    302297                            (strncmp(encodeType,"z",typeLen) == 0) ) {
    303                     decompress = 1;
    304                     base64 = 0;
     298                    decompress = true;
     299                    base64 = false;
    305300                }
    306301                else if (   (typeLen == 3) &&
    307302                            (strncmp(encodeType,"b64",typeLen) == 0) ) {
    308                     decompress = 0;
    309                     base64 = 1;
     303                    decompress = false;
     304                    base64 = true;
    310305                }
    311306                else if (   (typeLen == 4) &&
    312307                            (strncmp(encodeType,"zb64",typeLen) == 0) ) {
    313                     decompress = 1;
    314                     base64 = 1;
     308                    decompress = true;
     309                    base64 = true;
    315310                }
    316311                else {
     
    346341    }
    347342
     343    int optionLen;
     344    const char* option;
    348345    option = (const char*) Tcl_GetByteArrayFromObj(objv[nextarg++], &optionLen);
    349346
    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)) {
    367356        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);
    369362    return TCL_OK;
    370363}
Note: See TracChangeset for help on using the changeset viewer.