Changeset 1527 for trunk/src


Ignore:
Timestamp:
Jun 22, 2009, 12:38:49 PM (15 years ago)
Author:
dkearney
Message:

various code cleanups, mainly tabs. adding units ohms and amps, adding appendf interface to the simple buffer so we can append formatted strings.

Location:
trunk/src/core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpBuffer.cc

    r1384 r1527  
    120120    if (f == NULL) {
    121121        status.addError("can't open \"%s\": %s", filePath, strerror(errno));
    122         return false;
     122        return false;
    123123    }
    124124    struct stat stat;
    125125    if (fstat(fileno(f), &stat) < 0) {
    126126        status.addError("can't stat \"%s\": %s", filePath, strerror(errno));
    127         return false;
    128     }   
     127        return false;
     128    }
    129129    off_t size;
    130130    size = stat.st_size;
     
    133133    if (memblock == NULL) {
    134134        status.addError("can't allocate %d bytes for file \"%s\": %s",
    135                         size, filePath, strerror(errno));
     135                        size, filePath, strerror(errno));
    136136        fclose(f);
    137137        return false;
     
    139139
    140140    // FIXME: better yet, create an "extend" method in the buffer and returns
    141     //       the address of the char buffer so I can read the data directly
    142     //       into the buffer.  This eliminates memory new/copy/delete ops.
     141    //             the address of the char buffer so I can read the data directly
     142    //             into the buffer.  This eliminates memory new/copy/delete ops.
    143143
    144144    size_t nRead;
    145145    nRead = fread(memblock, sizeof(char), size, f);
    146     fclose(f);                  // Close the file.
     146    fclose(f);                        // Close the file.
    147147
    148148    if (nRead != (size_t)size) {
    149149        status.addError("can't read %d bytes from \"%s\": %s", size, filePath,
    150                         strerror(errno));
    151         return false;
    152     }   
     150                        strerror(errno));
     151        return false;
     152    }
    153153
    154154    int nBytes;
     
    158158    if (nBytes != size) {
    159159        status.addError("can't append %d bytes from \"%s\" to buffer: %s",
    160                 size, filePath, strerror(errno));
    161         return false;
    162     }   
     160                size, filePath, strerror(errno));
     161        return false;
     162    }
    163163    return true;
    164164}
     
    174174    if (f != NULL) {
    175175        status.addError("can't open \"%s\": %s\n", filePath, strerror(errno));
    176         return false;
     176        return false;
    177177    }
    178178    ssize_t nWritten;
    179179    nWritten = fwrite(bytes(), size(), sizeof(char), f);
    180     fclose(f);                  // Close the file.
    181    
     180    fclose(f);                        // Close the file.
     181
    182182    if (nWritten != (ssize_t)size()) {
    183183        status.addError("can't write %d bytes to \"%s\": %s\n", size(),
    184                         filePath, strerror(errno));
    185         return false;
     184                        filePath, strerror(errno));
     185        return false;
    186186    }
    187187    return true;
     
    198198    switch (flags & (RPENC_Z | RPENC_B64)) {
    199199    case 0:
    200         break;
    201 
    202     case RPENC_Z:               // Compress only
    203         if (!do_compress(status, *this, bout)) {
    204             return false;
    205         }
    206         move(bout);
    207         break;
    208 
    209     case RPENC_B64:             // Encode only
     200        break;
     201
     202    case RPENC_Z:                // Compress only
     203        if (!do_compress(status, *this, bout)) {
     204            return false;
     205        }
     206        move(bout);
     207        break;
     208
     209    case RPENC_B64:                // Encode only
    210210        if (!do_base64_enc(status, *this, bout)) {
    211211            return false;
    212212        }
    213         move(bout);
    214         break;
     213        move(bout);
     214        break;
    215215
    216216    case (RPENC_B64 | RPENC_Z):
    217        
    218         // It's always compress then encode
    219         if (!do_compress(status, *this, bout)) {
    220             return false;
    221         }
     217
     218        // It's always compress then encode
     219        if (!do_compress(status, *this, bout)) {
     220            return false;
     221        }
    222222        if (!do_base64_enc(status, bout, *this)) {
    223223            return false;
    224224        }
    225         break;
     225        break;
    226226    }
    227227    return true;
     
    238238    switch (flags & (RPENC_Z | RPENC_B64)) {
    239239    case 0:
    240         if (encoding::isBase64(bytes(), size())) {
    241             if (!do_base64_dec(status, *this, bout)) {
    242                 return false;
    243             }
    244             move(bout);
    245         }
    246         bout.clear();
    247         if (encoding::isBinary(bytes(), size())) {
    248             if (!do_decompress(status, *this, bout)) {
    249                 return false;
    250             }
    251             move(bout);
    252         }
    253         break;
    254 
    255     case RPENC_Z:               // Decompress only
    256         if (!do_decompress(status, *this, bout)) {
    257             return false;
    258         }
    259         move(bout);
    260         break;
    261 
    262     case RPENC_B64:             // Decode only
     240        if (encoding::isBase64(bytes(), size())) {
     241            if (!do_base64_dec(status, *this, bout)) {
     242                return false;
     243            }
     244            move(bout);
     245        }
     246        bout.clear();
     247        if (encoding::isBinary(bytes(), size())) {
     248            if (!do_decompress(status, *this, bout)) {
     249                return false;
     250            }
     251            move(bout);
     252        }
     253        break;
     254
     255    case RPENC_Z:                // Decompress only
     256        if (!do_decompress(status, *this, bout)) {
     257            return false;
     258        }
     259        move(bout);
     260        break;
     261
     262    case RPENC_B64:                // Decode only
    263263        if (!do_base64_dec(status, *this, bout)) {
    264264            return false;
    265265        }
    266         move(bout);
    267         break;
     266        move(bout);
     267        break;
    268268
    269269    case (RPENC_B64 | RPENC_Z):
    270        
    271         // It's always decode then decompress
     270
     271        // It's always decode then decompress
    272272        if (!do_base64_dec(status, *this, bout)) {
    273273            return false;
    274274        }
    275         clear();
    276         if (!do_decompress(status, bout, *this)) {
    277             return false;
    278         }
    279         break;
     275        clear();
     276        if (!do_decompress(status, bout, *this)) {
     277            return false;
     278        }
     279        break;
    280280    }
    281281    return true;
     
    285285bool
    286286Buffer::do_compress(Outcome& status, SimpleCharBuffer& bin,
    287                     SimpleCharBuffer& bout)
     287                    SimpleCharBuffer& bout)
    288288{
    289289    int ret=0, flush=0;
     
    357357bool
    358358Buffer::do_decompress(Outcome& status, SimpleCharBuffer& bin,
    359                       SimpleCharBuffer& bout)
     359                      SimpleCharBuffer& bout)
    360360{
    361361    int ret;
     
    433433bool
    434434Buffer::do_base64_enc(Outcome& status, const SimpleCharBuffer& bin,
    435                       SimpleCharBuffer& bout )
     435                      SimpleCharBuffer& bout )
    436436{
    437437    int tBufSize = 0;
     
    453453bool
    454454Buffer::do_base64_dec(Outcome& status, const SimpleCharBuffer& bin,
    455                       SimpleCharBuffer& bout )
     455                      SimpleCharBuffer& bout )
    456456{
    457457    int tBufSize = 0;
  • trunk/src/core/RpBuffer.h

    r1384 r1527  
    7272
    7373    enum { CHUNK = 4096 };
    74    
     74
    7575    bool do_compress(Outcome& status, SimpleCharBuffer& bin,
    76                      SimpleCharBuffer& bout  );
     76                     SimpleCharBuffer& bout  );
    7777    bool do_decompress( Outcome& status, SimpleCharBuffer& bin,
    7878                        SimpleCharBuffer& bout  );
    7979    bool do_base64_enc(Outcome& status, const SimpleCharBuffer& bin,
    80                        SimpleCharBuffer& bout  );
     80                       SimpleCharBuffer& bout  );
    8181    bool do_base64_dec(Outcome& status, const SimpleCharBuffer& bin,
    82                        SimpleCharBuffer& bout  );
     82                       SimpleCharBuffer& bout  );
    8383};
    8484
    8585} // namespace Rappture
    86  
     86
    8787#ifdef __cplusplus
    8888    }
  • trunk/src/core/RpLibrary.cc

    r1384 r1527  
    11771177        if (retNode) {
    11781178            // allocate a new rappture library object for the node
    1179             retLib = new RpLibrary( retNode,this->tree ); 
     1179            retLib = new RpLibrary( retNode,this->tree );
    11801180        }
    11811181    }
     
    15451545        // because base64 character set does not include xml entity chars
    15461546        if (!Rappture::encoding::decode(status, inData, 0)) {
    1547             return retStr;
    1548         }
     1547            return retStr;
     1548        }
    15491549        retStr = std::string(inData.bytes(),inData.size());
    15501550    } else {
     
    15521552        if (translateFlag == RPLIB_TRANSLATE) {
    15531553            translatedContents = ERTranslator.decode(inData.bytes(),
    1554                                                      inData.size());
     1554                                                     inData.size());
    15551555            if (translatedContents == NULL) {
    15561556                // translation failed
    15571557                if (!status) {
    15581558                    status.error("Error while translating entity references");
    1559                     return retStr;
     1559                    return retStr;
    15601560                }
    15611561            } else {
     
    17381738RpLibrary&
    17391739RpLibrary::put (std::string path, std::string value, std::string id,
    1740                 unsigned int append, unsigned int translateFlag)
     1740                unsigned int append, unsigned int translateFlag)
    17411741{
    17421742    Rappture::EntityRef ERTranslator;
     
    17561756    // check for binary data
    17571757    // FIXME: I've already appended a NUL-byte of this assuming that
    1758     //        it's a ASCII string. This test must come before.
     1758    //        it's a ASCII string. This test must come before.
    17591759    if (Rappture::encoding::isBinary(value.c_str(), value.length())) {
    17601760        putData(path, value.c_str(), value.length(), append);
     
    17751775            if (!status) {
    17761776                status.error("Error while translating entity references");
    1777                 return *this;
     1777                return *this;
    17781778            }
    17791779        }
     
    18011801
    18021802RpLibrary&
    1803 RpLibrary::put (std::string path, double value, std::string id, 
    1804                 unsigned int append)
     1803RpLibrary::put (std::string path, double value, std::string id,
     1804                unsigned int append)
    18051805{
    18061806    std::stringstream valStr;
     
    19621962
    19631963    if (retNode == NULL) {
    1964         status.addError("can't create node from path \"%s\"", path.c_str());
    1965         return *this;
     1964        status.addError("can't create node from path \"%s\"", path.c_str());
     1965        return *this;
    19661966    }
    19671967    if (append == RPLIB_APPEND) {
    1968         if ( (contents = scew_element_contents(retNode)) ) {
    1969             inData.append(contents);
    1970             // base64 decode and un-gzip the data
    1971             if (!Rappture::encoding::decode(status, inData, 0)) {
    1972                 return *this;
    1973             }
    1974         }
     1968        if ( (contents = scew_element_contents(retNode)) ) {
     1969            inData.append(contents);
     1970            // base64 decode and un-gzip the data
     1971            if (!Rappture::encoding::decode(status, inData, 0)) {
     1972                return *this;
     1973            }
     1974        }
    19751975    }
    19761976    if (inData.append(bytes, nbytes) != nbytes) {
    1977         status.addError("can't append %d bytes", nbytes);
    1978         return *this;
    1979     }   
     1977        status.addError("can't append %d bytes", nbytes);
     1978        return *this;
     1979    }
    19801980    // gzip and base64 encode the data
    19811981    flags = RPENC_Z|RPENC_B64|RPENC_HDR;
    19821982    if (!Rappture::encoding::encode(status, inData,flags)) {
    1983         return *this;
     1983        return *this;
    19841984    }
    19851985    bytesWritten = (unsigned int) inData.size();
     
    19931993/// Put data from a file into the xml.
    19941994/**
    1995  *  Append flag adds additional nodes, it does not merge same 
     1995 *  Append flag adds additional nodes, it does not merge same
    19961996 *  named nodes together
    19971997 */
    19981998
    19991999RpLibrary&
    2000 RpLibrary::putFile(std::string path, std::string fileName, 
    2001                    unsigned int compress, unsigned int append)
     2000RpLibrary::putFile(std::string path, std::string fileName,
     2001                   unsigned int compress, unsigned int append)
    20022002{
    20032003    Rappture::Buffer buf;
     
    20112011
    20122012    if (!fileBuf.load(err, fileName.c_str())) {
    2013         fprintf(stderr, "error loading file: %s\n", err.remark());
    2014         return *this;
     2013        fprintf(stderr, "error loading file: %s\n", err.remark());
     2014        return *this;
    20152015    }
    20162016    if (compress == RPLIB_COMPRESS) {
    20172017        putData(path, fileBuf.bytes(), fileBuf.size(), append);
    20182018    } else {
    2019         /* Always append a NUL-byte to the end of ASCII strings. */
     2019        /* Always append a NUL-byte to the end of ASCII strings. */
    20202020        fileBuf.append("\0", 1);
    20212021        put(path, fileBuf.bytes(), "", append, RPLIB_TRANSLATE);
  • trunk/src/core/RpSimpleBuffer.h

    r1398 r1527  
    6767#include <cstring>
    6868#include <cstdlib>
     69#include <cstdarg>
    6970
    7071namespace Rappture {
     
    8889    SimpleBuffer<T>& clear();
    8990    int append(const T* bytes, int nmemb=-1);
     91    int appendf(const char *format, ...);
     92    int remove(int nmemb);
    9093    size_t read(const T* bytes, size_t nmemb);
    9194    int seek(long offset, int whence);
    9295    int tell() const;
    93     size_t set(size_t nbytes);
     96    size_t set(size_t nmemb);
    9497    SimpleBuffer<T>& rewind();
    9598    SimpleBuffer<T>& show();
     
    345348        return 0;
    346349    }
     350
     351    // FIXME: i think this needs to be division,
     352    // need to create test case with array of ints
     353    // i'm not sure we can even guess the number
     354    // bytes in *bytes.
    347355
    348356    if (nmemb == -1) {
     
    351359        // at the length of the object.
    352360        nbytes = __guesslen(bytes);
    353         nmemb = nbytes*sizeof(T);
     361        nmemb = nbytes/sizeof(T);
    354362    }
    355363
     
    397405}
    398406
     407/**
     408 * Append formatted bytes to the end of this buffer
     409 * @param pointer to bytes to be added
     410 * @param number of bytes to be added
     411 * @return number of bytes appended.
     412 */
     413template<class T>
     414int
     415SimpleBuffer<T>::appendf(const char *format, ...)
     416{
     417    size_t newMembCnt = 0;
     418    size_t nbytes = 0;
     419    int nmemb = 0;
     420
     421    char* dest = NULL;
     422    size_t size = 0;
     423    size_t bytesAdded = 0;
     424    va_list arg;
     425
     426    // User specified NULL format
     427    if (format == NULL) {
     428        return 0;
     429    }
     430
     431    // FIXME: i think this needs to be division,
     432    // need to create test case with array of ints
     433    // i'm not sure we can even guess the number
     434    // bytes in *bytes.
     435
     436
     437    // add one for terminating null character
     438    nbytes = strlen(format) + 1;
     439
     440    if (nbytes <= 0) {
     441        // no data written, invalid option
     442        return nbytes;
     443    }
     444
     445    // FIXME: we need ceil of nbytes/sizeof(T), instead we add 1 for safety
     446
     447    nmemb = nbytes/sizeof(T);
     448    if (nmemb == 0) {
     449        nmemb++;
     450    }
     451
     452    newMembCnt = (size_t)(_nMembStored + nmemb);
     453
     454    if (newMembCnt > _nMembAvl) {
     455
     456        // buffer sizes less than min_size are set to min_size
     457        if (newMembCnt < (size_t) _minMembCnt) {
     458            newMembCnt = (size_t) _minMembCnt;
     459        }
     460
     461        /*
     462         * Allocate a larger buffer for the string if the current one isn't
     463         * large enough. Allocate extra space in the new buffer so that there
     464         * will be room to grow before we have to allocate again.
     465         */
     466        size_t membAvl;
     467        membAvl = (_nMembAvl > 0) ? _nMembAvl : _minMembCnt;
     468        while (newMembCnt > membAvl) {
     469            membAvl += membAvl;
     470        }
     471
     472        /*
     473         * reallocate to a larger buffer
     474         */
     475        if (set(membAvl) != membAvl) {
     476            return 0;
     477        }
     478    }
     479
     480    dest = (char*) (_buf + _nMembStored);
     481    size = (_nMembAvl-_nMembStored)*sizeof(T);
     482
     483    va_start(arg,format);
     484    bytesAdded = vsnprintf(dest,size,format,arg);
     485    va_end(arg);
     486
     487    // bytesAdded contains the number of bytes that would have
     488    // been placed in the buffer if the call was successful.
     489    // this value does not include the trailing null character.
     490    // so we add one to account for it.
     491
     492    bytesAdded++;
     493    nmemb = bytesAdded/sizeof(T);
     494
     495    if (bytesAdded > size) {
     496        // we did not fit everything in the original buffer
     497        // resize and try again.
     498
     499        // FIXME: round the new size up to the nearest multiple of 256?
     500        set(_nMembStored+nmemb);
     501
     502        // reset dest because it may have moved during reallocation
     503        dest = (char*) (_buf + _nMembStored);
     504        size = bytesAdded;
     505
     506        va_start(arg,format);
     507        bytesAdded = vsnprintf(dest,size,format,arg);
     508        va_end(arg);
     509
     510        if (bytesAdded > size) {
     511            // crystals grow, people grow, data doesn't grow...
     512            // issue error
     513            fprintf(stderr,"error in appendf while appending data");
     514        }
     515    }
     516
     517    _nMembStored += nmemb;
     518
     519    // remove the null character added by vsnprintf()
     520    // we do this because if we are appending strings,
     521    // the embedded null acts as a terminating null char.
     522    // this is a generic buffer so if user wants a
     523    // terminating null, they should append it.
     524    remove(1);
     525
     526    return nmemb;
     527}
     528
     529/**
     530 * Remove bytes from the end of this buffer
     531 * @param number of bytes to be removed
     532 * @return number of bytes removed.
     533 */
     534template<class T>
     535int
     536SimpleBuffer<T>::remove(int nmemb)
     537{
     538    if ((_nMembStored - nmemb) < 0){
     539        _nMembStored = 0;
     540        _pos = 0;
     541    } else {
     542        _nMembStored -= nmemb;
     543        if (_pos >= _nMembStored) {
     544            // move _pos back to the new end of the buffer.
     545            _pos = _nMembStored-1;
     546        }
     547    }
     548
     549
     550    return nmemb;
     551}
     552
    399553
    400554template<class T>
     
    412566
    413567    if (buf == NULL) {
    414         fprintf(stderr,"Can't allocate %zu bytes of memory\n",nbytes);
     568        fprintf(stderr,"Can't allocate %lu bytes of memory\n",
     569            (long unsigned int)nbytes);
    415570        _fileState = false;
    416571        return 0;
     
    429584
    430585    while (curMemb != _nMembStored) {
    431         fprintf(stdout,"_buf[%zu] = :%c:\n", curMemb, _buf[curMemb]);
     586        fprintf(stdout,"_buf[%lu] = :%c:\n", (long unsigned int)curMemb,
     587                _buf[curMemb]);
    432588        curMemb += 1;
    433589    }
    434     fprintf(stdout,"_nMembAvl = :%zu:\n", _nMembAvl);
     590    fprintf(stdout,"_nMembAvl = :%lu:\n", (long unsigned int)_nMembAvl);
    435591
    436592    return *this;
     
    445601
    446602    while (curMemb != _nMembStored) {
    447         fprintf(stdout,"_buf[%zu] = :%#lx:\n", curMemb,
    448             (long unsigned)_buf[curMemb]);
     603        fprintf(stdout,"_buf[%lu] = :%#x:\n", (long unsigned int)curMemb,
     604                (unsigned long)_buf[curMemb]);
    449605        curMemb += 1;
    450606    }
    451     fprintf(stdout,"_nMembAvl = :%zu:\n", _nMembAvl);
     607    fprintf(stdout,"_nMembAvl = :%lu:\n", (long unsigned int)_nMembAvl);
    452608
    453609    return *this;
  • trunk/src/core/RpUnits.cc

    r1432 r1527  
    23952395        retVal = RpUnitsPreset::addPresetMisc();
    23962396    }
     2397    else if (group.compare(RP_TYPE_POWER) == 0) {
     2398        retVal = RpUnitsPreset::addPresetPower();
     2399    }
    23972400
    23982401    return retVal;
     
    24262429    result += addPresetMagnetic();
    24272430    result += addPresetMisc();
     2431    result += addPresetPower();
    24282432
    24292433    return 0;
     
    29402944    RpUnits* amu       = NULL;
    29412945    RpUnits* bel       = NULL;
     2946    RpUnits* amp       = NULL;
     2947    RpUnits* ohm       = NULL;
    29422948
    29432949    volt      = RpUnits::define("V",  NULL, RP_TYPE_EPOT, RPUNITS_METRIC);
     
    29472953    amu       = RpUnits::define("amu", NULL, "mass_unit", !RPUNITS_METRIC);
    29482954    bel       = RpUnits::define("B", NULL, "audio_transmission", RPUNITS_METRIC);
     2955    amp       = RpUnits::define("amp", NULL, "electric_current", RPUNITS_METRIC);
     2956    ohm       = RpUnits::define("ohm", NULL, "electric_resistance", RPUNITS_METRIC);
    29492957
    29502958    // RpUnits* percent   = RpUnits::define("%",  NULL, RP_TYPE_MISC);
     2959
     2960    return 0;
     2961}
     2962
     2963/**********************************************************************/
     2964// METHOD: addPresetPower()
     2965/// Add power related units to the dictionary
     2966/**
     2967 * Defines the following units:
     2968 *   watt  (W)
     2969 *
     2970 * Return codes: 0 success, anything else is error
     2971 */
     2972
     2973int
     2974RpUnitsPreset::addPresetPower () {
     2975
     2976    RpUnits* watt      = NULL;
     2977
     2978    // watts are derived units = J/s = kg*m2/s3 = Newton*m/s and Amps*Volt
     2979    watt      = RpUnits::define("W",  NULL, RP_TYPE_POWER, RPUNITS_METRIC);
    29512980
    29522981    return 0;
     
    29983027        return &RpUnitsTypes::hintTypeMisc;
    29993028    }
     3029    else if (type.compare(RP_TYPE_POWER) == 0) {
     3030        return &RpUnitsTypes::hintTypePower;
     3031    }
    30003032    else {
    30013033        return NULL;
     
    31773209
    31783210    if ( (unitObj->getType()).compare(RP_TYPE_MISC) == 0 ) {
     3211        retVal = true;
     3212    }
     3213
     3214    return retVal;
     3215}
     3216
     3217bool
     3218RpUnitsTypes::hintTypePower   (   RpUnits* unitObj    ) {
     3219
     3220    bool retVal = false;
     3221
     3222    if ( (unitObj->getType()).compare(RP_TYPE_POWER) == 0 ) {
    31793223        retVal = true;
    31803224    }
  • trunk/src/core/RpUnits.h

    r1427 r1527  
    4848#define RP_TYPE_MAGNETIC    "magnetic"
    4949#define RP_TYPE_MISC        "misc"
     50#define RP_TYPE_POWER       "power"
    5051
    5152
     
    8788        static int  addPresetMagnetic();
    8889        static int  addPresetMisc();
     90        static int  addPresetPower();
    8991};
    9092
     
    110112        static bool hintTypeMagnetic  ( RpUnits* unitObj );
    111113        static bool hintTypeMisc      ( RpUnits* unitObj );
     114        static bool hintTypePower     ( RpUnits* unitObj );
    112115
    113116    private:
     
    520523        //  RP_TYPE_MAGNETIC  "magnetic"      load units related to magnetics
    521524        //  RP_TYPE_MISC      "misc"          load units related to everything else
     525        //  RP_TYPE_POWER     "power"         load units related to power
    522526        //  (no other groups have been created)
    523527
Note: See TracChangeset for help on using the changeset viewer.