Changeset 2544 for branches


Ignore:
Timestamp:
Sep 21, 2011 2:28:26 PM (13 years ago)
Author:
gah
Message:

update from trunk

Location:
branches/blt4/src/core
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/blt4/src/core/RpBuffer.cc

    r1527 r2544  
    112112
    113113bool
    114 Buffer::load (Outcome &status, const char* filePath)
     114Buffer::load (Outcome &status, const char *path)
    115115{
    116116    status.addContext("Rappture::Buffer::load()");
    117117
    118118    FILE *f;
    119     f = fopen(filePath, "rb");
     119    f = fopen(path, "rb");
    120120    if (f == NULL) {
    121         status.addError("can't open \"%s\": %s", filePath, strerror(errno));
    122         return false;
    123     }
     121        status.addError("can't open \"%s\": %s", path, strerror(errno));
     122        return false;
     123    }
     124
    124125    struct stat stat;
    125126    if (fstat(fileno(f), &stat) < 0) {
    126         status.addError("can't stat \"%s\": %s", filePath, strerror(errno));
    127         return false;
    128     }
    129     off_t size;
    130     size = stat.st_size;
    131     char* memblock;
    132     memblock = new char [size];
    133     if (memblock == NULL) {
     127        status.addError("can't stat \"%s\": %s", path, strerror(errno));
     128        return false;
     129    }
     130
     131    size_t oldSize, numBytesRead;
     132
     133    // Save the # of elements in the current buffer.
     134    oldSize = count();
     135
     136    // Extend the buffer to accomodate the file contents.
     137    if (extend(stat.st_size) == 0) {
    134138        status.addError("can't allocate %d bytes for file \"%s\": %s",
    135                         size, filePath, strerror(errno));
     139                stat.st_size, path, strerror(errno));
    136140        fclose(f);
    137141        return false;
    138     }
    139 
    140     // 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.
    143 
    144     size_t nRead;
    145     nRead = fread(memblock, sizeof(char), size, f);
     142    }   
     143    // Read the file contents directly onto the end of the old buffer.
     144    numBytesRead = fread((char *)bytes() + oldSize, sizeof(char),
     145        stat.st_size, f);
     146    fclose(f);
     147    if (numBytesRead != (size_t)stat.st_size) {
     148        status.addError("can't read %ld bytes from \"%s\": %s", stat.st_size,
     149                        path, strerror(errno));
     150        return false;
     151    }
     152    // Reset the # of elements in the buffer to the new count.
     153    count(stat.st_size + oldSize);
     154    return true;
     155}
     156
     157
     158bool
     159Buffer::dump (Outcome &status, const char* path)
     160{
     161    status.addContext("Rappture::Buffer::dump()");
     162
     163    FILE *f;
     164    f = fopen(path, "wb");
     165    if (f == NULL) {
     166        status.addError("can't open \"%s\": %s\n", path, strerror(errno));
     167        return false;
     168    }
     169    ssize_t nWritten;
     170    nWritten = fwrite(bytes(), sizeof(char), size(), f);
    146171    fclose(f);                        // Close the file.
    147172
    148     if (nRead != (size_t)size) {
    149         status.addError("can't read %d bytes from \"%s\": %s", size, filePath,
    150                         strerror(errno));
    151         return false;
    152     }
    153 
    154     int nBytes;
    155     nBytes = append(memblock, size);
    156     delete [] memblock;
    157 
    158     if (nBytes != size) {
    159         status.addError("can't append %d bytes from \"%s\" to buffer: %s",
    160                 size, filePath, strerror(errno));
    161         return false;
    162     }
    163     return true;
    164 }
    165 
    166 
    167 bool
    168 Buffer::dump (Outcome &status, const char* filePath)
    169 {
    170     status.addContext("Rappture::Buffer::dump()");
    171 
    172     FILE *f;
    173     f = fopen(filePath, "wb");
    174     if (f != NULL) {
    175         status.addError("can't open \"%s\": %s\n", filePath, strerror(errno));
    176         return false;
    177     }
    178     ssize_t nWritten;
    179     nWritten = fwrite(bytes(), size(), sizeof(char), f);
    180     fclose(f);                        // Close the file.
    181 
    182173    if (nWritten != (ssize_t)size()) {
    183         status.addError("can't write %d bytes to \"%s\": %s\n", size(), 
    184                         filePath, strerror(errno));
     174        status.addError("can't write %d bytes to \"%s\": %s\n", size(),
     175                        path, strerror(errno));
    185176        return false;
    186177    }
     
    288279{
    289280    int ret=0, flush=0;
    290     unsigned have=0;
    291281    z_stream strm;
    292282
    293283    char in[CHUNK];
    294284    char out[CHUNK];
    295 
    296     int bytesWritten = 0;
    297285
    298286    /* allocate deflate state */
     
    330318            ret = deflate(&strm, flush);    /* no bad return value */
    331319            assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
     320
     321            int have;
    332322            have = CHUNK - strm.avail_out;
    333323            /* write to file and check for error */
    334             bytesWritten = bout.append(out, have);
    335             if ( ( (unsigned) bytesWritten != have ) ) {
     324            if ((have > 0) && (bout.append(out, have) == 0)) {
    336325                (void)deflateEnd(&strm);
    337326                bout.clear();
    338327                // return Z_ERRNO;
    339                 status.addError("error writing compressed data to temp buffer");
     328                status.addError("error writing compressed data to temp buffer numBytes=%d", have);
    340329                return false;
    341330            }
  • branches/blt4/src/core/RpLibrary.cc

    r1824 r2544  
    17351735
    17361736/**********************************************************************/
     1737// METHOD: getFile()
     1738/// Get the value at path and write it to the file at fileName
     1739/**
     1740 * Return the number of bytes written
     1741 */
     1742
     1743size_t
     1744RpLibrary::getFile (std::string path, std::string fileName) const
     1745{
     1746    Rappture::Buffer buf;
     1747
     1748    buf = getData(path);
     1749
     1750    if (buf.bad()) {
     1751        status.addContext("RpLibrary::getFile()");
     1752        return 0;
     1753    }
     1754
     1755    if (!buf.dump(status, fileName.c_str())) {
     1756        status.addContext("RpLibrary::getFile()");
     1757        return 0;
     1758    }
     1759
     1760    return buf.size();
     1761}
     1762
     1763
     1764/**********************************************************************/
    17371765// METHOD: put()
    17381766/// Put a string value into the xml.
     
    18131841        // library doesn't exist, do nothing;
    18141842        status.error("invalid library object");
    1815         status.addContext("RpLibrary::put()");
     1843        status.addContext("RpLibrary::put() - putDouble");
    18161844        return *this;
    18171845    }
     
    18201848
    18211849    put(path,valStr.str(),id,append);
    1822     status.addContext("RpLibrary::put() - putDouble");
    18231850    return *this;
    18241851}
     
    20072034    Rappture::Buffer buf;
    20082035    Rappture::Buffer fileBuf;
    2009     Rappture::Outcome err;
    20102036
    20112037    if (!this->root) {
     
    20142040    }
    20152041
    2016     if (!fileBuf.load(err, fileName.c_str())) {
    2017         fprintf(stderr, "error loading file: %s\n", err.remark());
     2042    if (!fileBuf.load(status, fileName.c_str())) {
     2043        fprintf(stderr, "error loading file: %s\n", status.remark());
     2044        status.addContext("RpLibrary::putFile()");
    20182045        return *this;
    20192046    }
     
    20252052        put(path, fileBuf.bytes(), "", append, RPLIB_TRANSLATE);
    20262053    }
    2027     status.addContext("RpLibrary::putFile()");
    20282054    return *this;
    20292055}
  • branches/blt4/src/core/RpLibrary.h

    r1041 r2544  
    8181        bool        getBool   ( std::string path = "") const;
    8282        Rappture::Buffer getData ( std::string path = "") const;
     83        size_t      getFile   ( std::string path,
     84                                std::string fileName) const;
    8385
    8486        /*
  • branches/blt4/src/core/RpLibraryFInterface.cc

    r1105 r2544  
    211211}
    212212
    213 
    214 //int rp_lib_child_comp (   int* handle,    /* integer handle of library */
    215 //                            char* path,     /* DOM path of requested object */
    216 //                            char* type,     /* specific name of element */
    217 //                            int* childNum,  /* child number for iteration */
    218 //                            char* retText,  /* buffer to store return text */
    219 //                            int path_len,   /* length of path */
    220 //                            int type_len,   /* length of type */
    221 //                            int retText_len /* length of return text buffer */
    222 //                       )
    223 /*
    224 {
    225     int retVal = 0;
    226 
    227     char* inPath = NULL;
    228     char* inType = NULL;
    229 
    230     inPath = null_terminate(path,path_len);
    231     inType = null_terminate(type,type_len);
    232 
    233     if (inPath) {
    234         path_len = strlen(inPath) + 1;
    235     }
    236     else {
    237         path_len = 0;
    238     }
    239 
    240     if (inType) {
    241         type_len = strlen(inType) + 1;
    242     }
    243     else {
    244         type_len = 0;
    245     }
    246 
    247     retVal = rp_lib_child_str( handle,
    248                                  inPath,
    249                                  "component",
    250                                  inType,
    251                                  childNum,
    252                                  retText,
    253                                  path_len,
    254                                  10,
    255                                  type_len,
    256                                  retText_len
    257                                );
    258 
    259     if (inPath) {
    260         free(inPath);
    261         inPath = NULL;
    262     }
    263 
    264     if (inType) {
    265         free(inType);
    266         inType = NULL;
    267     }
    268 
    269     return retVal;
    270 }
    271 */
    272 
    273 //int rp_lib_child_id(   int* handle,    /* integer handle of library */
    274 //                          char* path,     /* DOM path of requested object */
    275 //                          char* type,     /* specific name of element */
    276 //                          int* childNum,  /* child number for iteration */
    277 //                          char* retText,  /* buffer to store return text */
    278 //                          int path_len,   /* length of path */
    279 //                          int type_len,   /* length of type */
    280 //                          int retText_len /* length of return text buffer */
    281 //                       )
    282 /*
    283 {
    284     int retVal = 0;
    285     char* inPath = NULL;
    286     char* inType = NULL;
    287 
    288     inPath = null_terminate(path,path_len);
    289     inType = null_terminate(type,type_len);
    290 
    291     if (inPath) {
    292         path_len = strlen(inPath) + 1;
    293     }
    294     else {
    295         path_len = 0;
    296     }
    297 
    298     if (inType) {
    299         type_len = strlen(inType) + 1;
    300     }
    301     else {
    302         type_len = 0;
    303     }
    304 
    305     retVal = rp_lib_child_str( handle,
    306                                  inPath,
    307                                  "id",
    308                                  inType,
    309                                  childNum,
    310                                  retText,
    311                                  path_len,
    312                                  3,
    313                                  type_len,
    314                                  retText_len
    315                                );
    316 
    317     if (inPath) {
    318         free(inPath);
    319         inPath = NULL;
    320     }
    321 
    322     if (inType) {
    323         free(inType);
    324         inType = NULL;
    325     }
    326 
    327     return retVal;
    328 }
    329 */
    330 
    331 //int rp_lib_child_type (   int* handle,    /* integer handle of library */
    332 //                            char* path,     /* DOM path of requested object */
    333 //                            char* type,     /* specific name of element */
    334 //                            int* childNum,  /* child number for iteration */
    335 //                            char* retText,  /* buffer to store return text */
    336 //                            int path_len,   /* length of path */
    337 //                            int type_len,   /* length of type */
    338 //                            int retText_len /* length of return text buffer */
    339 //                       )
    340 /*
    341 {
    342     int retVal = 0;
    343     char* inPath = NULL;
    344     char* inType = NULL;
    345 
    346     inPath = null_terminate(path,path_len);
    347     inType = null_terminate(type,type_len);
    348 
    349     if (inPath) {
    350         path_len = strlen(inPath) + 1;
    351     }
    352     else {
    353         path_len = 0;
    354     }
    355 
    356     if (inType) {
    357         type_len = strlen(inType) + 1;
    358     }
    359     else {
    360         type_len = 0;
    361     }
    362 
    363     retVal = rp_lib_child_str( handle,
    364                                  inPath,
    365                                  "type",
    366                                  inType,
    367                                  childNum,
    368                                  retText,
    369                                  path_len,
    370                                  5,
    371                                  type_len,
    372                                  retText_len
    373                                );
    374 
    375     if (inPath) {
    376         free(inPath);
    377         inPath = NULL;
    378     }
    379 
    380     if (inType) {
    381         free(inType);
    382         inType = NULL;
    383     }
    384 
    385     return retVal;
    386 }
    387 */
    388 
    389 /*
    390 int rp_lib_child_obj ( int* handle,
    391                             char* path,
    392                             char* type,
    393                             int path_len,
    394                             int type_len
    395                           )
    396 {
    397     int newObjHandle = -1;
    398 
    399     PyObject* lib = NULL;
    400     PyObject* list = NULL;
    401 
    402     char* inPath = NULL;
    403     char* inType = NULL;
    404 
    405     inPath = null_terminate(path,path_len);
    406     inType = null_terminate(type,type_len);
    407 
    408     if (rapptureStarted) {
    409         if ((handle) && (*handle != 0)) {
    410             lib = (RpLibrary*) getObject_Void(*handle);
    411 
    412             if (lib) {
    413                 list = rpChildren_f(lib, inPath, "object");
    414                 if (list) {
    415                     // store the whole list,
    416                     // need to make a way to read the nodes
    417                     newObjHandle = storeObject_Void((void*)list);
    418                     // Py_DECREF(list);
    419                 }
    420                 else {
    421 
    422                 }
    423             }
    424         }
    425     }
    426 
    427     if (inPath) {
    428         free(inPath);
    429         inPath = NULL;
    430     }
    431 
    432     if (inType) {
    433         free(inType);
    434         inType = NULL;
    435     }
    436 
    437     return newObjHandle;
    438 
    439 }
    440 */
    441 
    442213/**********************************************************************/
    443214// FUNCTION: rp_lib_get()
     
    445216/**
    446217 */
    447 void rp_lib_get( int* handle, /* integer handle of library */
    448                    char* path,      /* null terminated path */
    449                    char* retText,   /* return text buffer for fortran*/
    450                    int path_len,
    451                    int retText_len /* length of return text buffer */
    452                  )
     218void
     219rp_lib_get(
     220    int* handle,                        /* integer handle of library */
     221    char* path,                         /* null terminated path */
     222    char* retText,                      /* return text buffer for fortran*/
     223
     224    int retText_len)                    /* length of return text buffer */
    453225{
    454226    std::string xmlText = "";
     
    478250/**
    479251 */
    480 void rp_lib_get_str( int* handle, /* integer handle of library */
    481                    char* path,      /* null terminated path */
    482                    char* retText,   /* return text buffer for fortran*/
    483                    int path_len,
    484                    int retText_len /* length of return text buffer */
    485                  )
     252void
     253rp_lib_get_str(
     254    int* handle,                        /* integer handle of library */
     255    char* path,                         /* null terminated path */
     256    char* retText,                      /* return text buffer for fortran*/
     257    int path_len,
     258    int retText_len)                    /* length of return text buffer */
    486259{
    487260    std::string xmlText = "";
     
    512285/**
    513286 */
    514 double rp_lib_get_double( int* handle,   /* integer handle of library */
    515                             char* path,    /* null terminated path */
    516                             int path_len
    517                           )
     287double
     288rp_lib_get_double(
     289    int* handle,                        /* integer handle of library */
     290    char* path,                         /* null terminated path */
     291    int path_len)
    518292{
    519293    double retVal = 0.0;
     
    602376    return retVal;
    603377}
     378
     379
     380/**********************************************************************/
     381// FUNCTION: rp_lib_get_file()
     382/// Get data located at 'path' and write it to the file 'fileName'.
     383/**
     384 * Returns if any bytes were written to the file
     385 */
     386int rp_lib_get_file (   int* handle,     /* integer handle of library */
     387                        char* path,      /* null terminated path */
     388                        char* fileName,  /* name of file to write data to */
     389                        int path_len,    /* length of the path variable */
     390                        int fileName_len /* length of the fileName variable */
     391                     )
     392{
     393    size_t nbytes = 0;
     394
     395    RpLibrary* lib = NULL;
     396
     397    std::string inPath = "";
     398    std::string filePath = "";
     399
     400    inPath = null_terminate_str(path,path_len);
     401    filePath = null_terminate_str(fileName,fileName_len);
     402
     403    if ((handle) && (*handle != 0)) {
     404        lib = (RpLibrary*) getObject_Void(*handle);
     405
     406        if (lib) {
     407            nbytes = lib->getFile(inPath, filePath);
     408        }
     409    }
     410
     411    return nbytes;
     412}
     413
    604414
    605415
     
    678488/**
    679489 */
    680 void rp_lib_put_data( int* handle,
    681                         char* path,
    682                         char* bytes,
    683                         int* nbytes,
    684                         int* append,
    685                         int path_len,
    686                         int bytes_len
    687                       )
     490void
     491rp_lib_put_data(
     492    int* handle,
     493    char* path,
     494    char* bytes,
     495    int* nbytes,
     496    int* append,
     497    int path_len,
     498    int bytes_len)
    688499{
    689500    std::string inPath = "";
     
    718529/**
    719530 */
    720 void rp_lib_put_file( int* handle,
    721                         char* path,
    722                         char* fileName,
    723                         int* compress,
    724                         int* append,
    725                         int path_len,
    726                         int fileName_len
    727                       )
     531void
     532rp_lib_put_file(
     533    int* handle,
     534    char* path,
     535    char* fileName,
     536    int* compress,
     537    int* append,
     538    int path_len,
     539    int fileName_len)
    728540{
    729541    std::string inPath = "";
     
    747559
    748560
    749 void rp_lib_put_obj( int* handle,
    750                         char* path,
    751                         int* valHandle,
    752                         int* append,
    753                         int path_len
    754                       )
     561void
     562rp_lib_put_obj(
     563    int* handle,
     564    char* path,
     565    int* valHandle,
     566    int* append,
     567    int path_len)
    755568{
    756569    std::string inPath = "";
     
    781594    }
    782595}
    783 
    784 
    785 /*
    786 void rp_lib_put_id_obj ( int* handle,
    787                         char* path,
    788                         int* valHandle,
    789                         char* id,
    790                         int* append,
    791                         int path_len,
    792                         int id_len
    793                       )
    794 {
    795     PyObject* lib = NULL;
    796     PyObject* value = NULL;
    797 
    798     char* inPath = NULL;
    799     char* inId = NULL;
    800 
    801     inPath = null_terminate(path,path_len);
    802     inId = null_terminate(id,id_len);
    803 
    804     if (rapptureStarted) {
    805         if ((handle) && (*handle != 0)) {
    806             lib = (RpLibrary*) getObject_Void(*handle);
    807             value = (RpLibrary*) getObject_Void(*valHandle);
    808 
    809             if (lib && value) {
    810                 // retObj is a borrowed object
    811                 // whose contents must not be modified
    812                 rpPutObj(lib, inPath, value, inId, *append);
    813             }
    814         }
    815     }
    816 
    817     if (inPath) {
    818         free(inPath);
    819         inPath = NULL;
    820     }
    821 
    822     if (inId) {
    823         free(inId);
    824         inId = NULL;
    825     }
    826 
    827 }
    828 */
    829 
    830 /*
    831 int rp_lib_remove (int* handle, char* path, int path_len)
    832 {
    833     int newObjHandle = -1;
    834 
    835     PyObject* lib = NULL;
    836     PyObject* removedObj = NULL;
    837 
    838     char* inPath = NULL;
    839 
    840     inPath = null_terminate(path,path_len);
    841 
    842     if (rapptureStarted) {
    843         if ((handle) && (*handle != 0)) {
    844             lib = (RpLibrary*) getObject_Void(*handle);
    845 
    846             if (lib) {
    847                 removedObj = rpRemove(lib, inPath);
    848 
    849                 if (removedObj) {
    850                     newObjHandle = storeObject_Void((void*)removedObj);
    851                     // Py_DECREF(removedObj);
    852                 }
    853 
    854             }
    855         }
    856     }
    857 
    858     if (inPath) {
    859         free(inPath);
    860         inPath = NULL;
    861     }
    862 
    863     return newObjHandle;
    864 }
    865 */
    866 
    867 /*
    868 int rp_lib_xml_len (int* handle)
    869 {
    870     int length = -1;
    871     char* xmlText = NULL;
    872 
    873     PyObject* lib = NULL;
    874 
    875     if (rapptureStarted) {
    876         if ((handle) && (*handle != 0)) {
    877             lib = (RpLibrary*) getObject_Void(*handle);
    878 
    879             if (lib) {
    880                 // dont modify xmlText, borrowed pointer
    881                 xmlText = rpXml(lib);
    882 
    883                 if (xmlText) {
    884                     length = strlen(xmlText) + 1;
    885                     free(xmlText);
    886                     // printf("len = :%d:\n",length);
    887                 }
    888             }
    889         }
    890     }
    891     return length;
    892 }
    893 */
    894596
    895597/**********************************************************************/
  • branches/blt4/src/core/RpLibraryFInterface.h

    r1086 r2544  
    7878                            char* path,
    7979                            int path_len);
     80
     81extern int rp_lib_get_file(int* handle, char* path, char* fileName,
     82        int path_len, int fileName_len);
    8083
    8184void rp_lib_put_str (       int* handle,
  • branches/blt4/src/core/RpLibraryFStubs.c

    r1018 r2544  
    340340}
    341341
     342int rp_lib_get_file_ (  int* handle,
     343                        char* path,
     344                        char* fileName,
     345                        int path_len,
     346                        int fileName_len) {
     347
     348    return rp_lib_get_file(handle,path,fileName,path_len,fileName_len);
     349}
     350
     351int rp_lib_get_file__ ( int* handle,
     352                        char* path,
     353                        char* fileName,
     354                        int path_len,
     355                        int fileName_len) {
     356
     357    return rp_lib_get_file(handle,path,fileName,path_len,fileName_len);
     358}
     359
     360int RP_LIB_GET_FILE (   int* handle,
     361                        char* path,
     362                        char* fileName,
     363                        int path_len,
     364                        int fileName_len) {
     365
     366    return rp_lib_get_file(handle,path,fileName,path_len,fileName_len);
     367}
     368
    342369void
    343370rp_lib_put_str_ (       int* handle,
     
    353380
    354381void
    355 rp_lib_put_str__ (       int* handle,
     382rp_lib_put_str__ (      int* handle,
    356383                        char* path,
    357384                        char* value,
  • branches/blt4/src/core/RpLibraryFStubs.h

    r1018 r2544  
    8282                            int path_len);
    8383
     84int rp_lib_get_file_ (      int* handle,
     85                            char* path,
     86                            char* fileName,
     87                            int path_len,
     88                            int fileName_len);
     89
    8490void rp_lib_put_str_ (      int* handle,
    8591                            char* path,
     
    223229                            int path_len);
    224230
     231int rp_lib_get_file__ (     int* handle,
     232                            char* path,
     233                            char* fileName,
     234                            int path_len,
     235                            int fileName_len);
     236
    225237void rp_lib_put_str__ (     int* handle,
    226238                            char* path,
     
    365377                            int path_len);
    366378
     379int RP_LIB_GET_FILE (       int* handle,
     380                            char* path,
     381                            char* fileName,
     382                            int path_len,
     383                            int fileName_len);
     384
    367385void RP_LIB_PUT_STR (       int* handle,
    368386                            char* path,
  • branches/blt4/src/core/RpOutcome.cc

    r1571 r2544  
    8383        _remark.append(bufPtr);
    8484    }
     85    /* fprintf(stderr, "Outcome: %s\n", _remark.c_str()); */
    8586    _status = 1;                /* Set to error */
    8687    if (bufPtr != stackSpace) {
  • branches/blt4/src/core/RpSimpleBuffer.h

    r1897 r2544  
     1
    12/*
    23 * ======================================================================
     
    6970#include <cstdarg>
    7071
     72/*
     73 * Note: I think I would redo this class to deal only with unsigned byte
     74 *       arrays, instead of arrays of <T>.  I would create other classes
     75 *       that arrays of <T> that used the lower level byte array.
     76 *
     77 *       It would make it cleaner if the class only dealt with bytes
     78 *       instead of elements (of various sizes). 
     79 *
     80 *       Specific implementations of <T> could perform data alignment on
     81 *       word/double word/quad word boundaries.  This is an optimization
     82 *       that should could done for double arrays.
     83 *
     84 * Note: The signed int argument on append() should be replaced with a
     85 *       size_t.  This limits the length of strings to appended.  It
     86 *       silently truncates bigger sizes to the lower 32-bits. 
     87 */
    7188namespace Rappture {
    7289
     
    7491class SimpleBuffer {
    7592public:
    76     SimpleBuffer();
     93    /**
     94     * Construct an empty SimpleBuffer.
     95     */
     96    SimpleBuffer() {
     97        Initialize();
     98    }
     99    /**
     100     * Construct a SimpleBuffer loaded with initial data.
     101     *
     102     * @param bytes pointer to bytes being stored.
     103     * @param nbytes number of bytes being stored.
     104     */
     105    SimpleBuffer(const T* bytes, int numElems=-1) {
     106        Initialize();
     107        append(bytes, numElems);
     108    }
    77109    SimpleBuffer(size_t nmemb);
    78     SimpleBuffer(const T* bytes, int nmemb=-1);
     110   
     111    /**
     112     * Copy constructor
     113     * @param SimpleBuffer object to copy
     114     */
    79115    SimpleBuffer(const SimpleBuffer& b);
     116   
    80117    SimpleBuffer<T>& operator=(const SimpleBuffer<T>& b);
    81118    SimpleBuffer     operator+(const SimpleBuffer& b) const;
    82119    SimpleBuffer<T>& operator+=(const SimpleBuffer<T>& b);
    83120    T operator[](size_t offset);
    84     virtual ~SimpleBuffer();
    85 
    86     const T* bytes() const;
    87     size_t size() const;
    88     size_t nmemb() const;
    89 
    90     SimpleBuffer<T>& clear();
     121   
     122    /**
     123     * Destructor
     124     */
     125    virtual ~SimpleBuffer() {
     126        Release();
     127    }
     128   
     129    /**
     130     * Get the bytes currently stored in the buffer.  These bytes can
     131     * be stored, and used later to construct another Buffer to
     132     * decode the information.
     133     *
     134     * @return Pointer to the bytes in the buffer.
     135     */
     136    const T* bytes() const {
     137        return _buf;
     138    }
     139    /**
     140     * Get the number of bytes currently stored in the buffer.
     141     * @return Number of the bytes used in the buffer.
     142     */
     143    size_t size() const {
     144        return _numElemsUsed * sizeof(T);
     145    }
     146    /**
     147     * Get the number of members currently stored in the buffer.
     148     * @return Number of the members used in the buffer.
     149     */
     150    size_t nmemb() const {
     151        return _numElemsUsed;
     152    }
     153    /**
     154     * Get the number of members currently stored in the buffer.
     155     * @return Number of the members used in the buffer.
     156     */
     157    size_t count() const {
     158        return _numElemsUsed;
     159    }
     160    /**
     161     * Set the number of members currently stored in the buffer.
     162     * @return Number of the members used in the buffer.
     163     */
     164    size_t count(size_t newCount) {
     165        _numElemsUsed = newCount;
     166        return _numElemsUsed;
     167    }
     168    /**
     169     * Clear the buffer, making it empty.
     170     * @return Reference to this buffer.
     171     */
     172    SimpleBuffer<T>& clear() {
     173        Release();
     174        return *this;
     175    }
     176    size_t extend(size_t extraElems);
    91177    int append(const T* bytes, int nmemb=-1);
    92178    int appendf(const char *format, ...);
     
    98184    SimpleBuffer<T>& rewind();
    99185    SimpleBuffer<T>& show();
    100 
     186   
    101187    bool good() const;
    102188    bool bad() const;
    103189    bool eof() const;
    104 
     190   
    105191    SimpleBuffer<T>& move(SimpleBuffer<T>& b);
    106 
     192   
    107193protected:
    108 
    109     void bufferInit();
    110     void bufferFree();
    111 
     194   
     195    void Initialize();
     196    void Release();
     197   
    112198private:
    113 
     199   
    114200    /// Pointer to the memory that holds our buffer's data
    115201    T* _buf;
    116 
     202   
    117203    /// Position offset within the buffer's memory
    118204    size_t _pos;
    119 
     205   
    120206    /// Number of members stored in the buffer
    121     size_t _nMembStored;
    122 
     207    size_t _numElemsUsed;
     208   
    123209    /// Total number of members available in the buffer
    124     size_t _nMembAvl;
    125 
     210    size_t _numElemsAllocated;
     211   
    126212    /// State of the last file like operation.
    127213    bool _fileState;
    128 
     214   
    129215    /// Minimum number of members is set to the number you can fit in 256 bytes
    130     const static int _minMembCnt=(256/sizeof(T));
    131 
     216    const static size_t _minNumElems = (256/sizeof(T));
     217   
    132218    size_t __guesslen(const T* bytes);
    133 
     219   
    134220};
    135 
     221   
    136222typedef SimpleBuffer<char>   SimpleCharBuffer;
    137223typedef SimpleBuffer<float>  SimpleFloatBuffer;
     
    139225
    140226/**
    141  * Construct an empty SimpleBuffer.
    142  */
    143 template<class T>
    144 SimpleBuffer<T>::SimpleBuffer()
    145 {
    146     bufferInit();
    147 }
    148 
    149 
    150 /**
    151227 * Construct an empty SimpleBuffer of specified size.
    152228 */
    153229template<class T>
    154 SimpleBuffer<T>::SimpleBuffer(size_t nmemb)
    155 {
    156     bufferInit();
    157 
    158     if (nmemb == 0) {
     230SimpleBuffer<T>::SimpleBuffer(size_t numElems)
     231{
     232    Initialize();
     233
     234    if (numElems == 0) {
    159235        // ignore requests for sizes equal to zero
    160236        return;
     
    162238
    163239    // buffer sizes less than min_size are set to min_size
    164     if (nmemb < (size_t) _minMembCnt) {
    165         nmemb = _minMembCnt;
    166     }
    167 
    168     if (set(nmemb) != nmemb) {
     240    if (numElems < (size_t) _minNumElems) {
     241        numElems = _minNumElems;
     242    }
     243
     244    if (set(numElems) != numElems) {
    169245        return;
    170246    }
    171     _nMembStored = nmemb;
    172 }
    173 
    174 
    175 /**
    176  * Construct a SimpleBuffer loaded with initial data.
    177  *
    178  * @param bytes pointer to bytes being stored.
    179  * @param nbytes number of bytes being stored.
    180  */
    181 template<class T>
    182 SimpleBuffer<T>::SimpleBuffer(const T* bytes, int nmemb)
    183 {
    184     bufferInit();
    185     append(bytes,nmemb);
     247    _numElemsUsed = numElems;
    186248}
    187249
     
    194256SimpleBuffer<T>::SimpleBuffer(const SimpleBuffer<T>& b)
    195257{
    196     bufferInit();
    197     append(b.bytes(),b.nmemb());
     258    Initialize();
     259    append(b.bytes(), b.nmemb());
    198260}
    199261
     
    207269SimpleBuffer<T>::operator=(const SimpleBuffer<T>& b)
    208270{
    209     bufferFree();
    210     bufferInit();
     271    Release();
    211272    append(b.bytes(),b.nmemb());
    212273    return *this;
     
    247308template<class T>
    248309T
    249 SimpleBuffer<T>::operator[](size_t idx)
    250 {
    251     return (_buf+idx);
    252 }
    253 
    254 
    255 /**
    256  * Destructor
    257  */
    258 template<class T>
    259 SimpleBuffer<T>::~SimpleBuffer()
    260 {
    261     bufferFree();
    262 }
    263 
    264 
    265 /**
    266  * Get the bytes currently stored in the buffer.  These bytes can
    267  * be stored, and used later to construct another Buffer to
    268  * decode the information.
    269  *
    270  * @return Pointer to the bytes in the buffer.
    271  */
    272 template<class T>
    273 const T*
    274 SimpleBuffer<T>::bytes() const
    275 {
    276     return _buf;
    277 }
    278 
    279 
    280 /**
    281  * Get the number of bytes currently stored in the buffer.
    282  * @return Number of the bytes used in the buffer.
    283  */
    284 template<class T>
    285 size_t
    286 SimpleBuffer<T>::size() const
    287 {
    288     return _nMembStored*sizeof(T);
    289 }
    290 
    291 
    292 /**
    293  * Get the number of members currently stored in the buffer.
    294  * @return Number of the members used in the buffer.
    295  */
    296 template<class T>
    297 size_t
    298 SimpleBuffer<T>::nmemb() const
    299 {
    300     return _nMembStored;
    301 }
    302 
    303 
    304 /**
    305  * Clear the buffer, making it empty.
    306  * @return Reference to this buffer.
    307  */
    308 template<class T>
    309 SimpleBuffer<T>&
    310 SimpleBuffer<T>::clear()
    311 {
    312     bufferFree();
    313     bufferInit();
    314 
    315     return *this;
    316 }
     310SimpleBuffer<T>::operator[](size_t index)
     311{
     312    return (_buf + index);              // Rely on pointer arithmetic
     313}
     314
     315
    317316
    318317/**
     
    327326    return strlen(bytes);
    328327}
     328
     329/* FIXME:       Change the signed int to size_t.  Move the -1 copy string
     330 *              up to the SimpleCharBuffer class.   There needs to be both
     331 *              a heavy duty class that can take a string bigger than
     332 *              2^31-1 in length, and a convenient class that doesn't make
     333 *              you add call strlen(s).
     334 *             
     335 */
    329336
    330337/**
     
    348355template<class T>
    349356int
    350 SimpleBuffer<T>::append(const T* bytes, int nmemb)
    351 {
    352     size_t newMembCnt = 0;
    353     size_t nbytes = 0;
    354 
    355     void* dest = NULL;
    356     void const* src  = NULL;
    357     size_t size = 0;
     357SimpleBuffer<T>::append(const T* bytes, int numElems)
     358{
    358359
    359360    // User specified NULL buffer to append
    360     if ( (bytes == NULL) && (nmemb < 1) ) {
    361         return 0;
     361    if (bytes == NULL) {
     362        return 0;                       // Should not give append a NULL
     363                                        // buffer pointer.
    362364    }
    363365
     
    367369    // bytes in *bytes.
    368370
    369     if (nmemb == -1) {
    370         // user signaled null terminated string
    371         // or that we should make an educated guess
    372         // at the length of the object.
    373         nbytes = __guesslen(bytes);
    374         nmemb = nbytes/sizeof(T);
    375     }
    376 
    377     if (nmemb <= 0) {
     371    size_t numBytes = 0;
     372    if (numElems == -1) {
     373        /* This should be moved into the SimpleCharBuffer.  It doesn't make
     374         * sense to look for a NUL byte unless it's a string buffer. We
     375         * can then change numElems to be size_t. */
     376        numBytes = __guesslen(bytes);
     377        numElems = numBytes / sizeof(T);
     378    }
     379    if (numElems <= 0) {
     380        return numElems;
     381    }
     382
     383    size_t newSize;
     384    newSize = _numElemsUsed + numElems;
     385    if (newSize > _numElemsAllocated) {
     386
     387        // buffer sizes less than min_size are set to min_size
     388        if (newSize < _minNumElems) {
     389            newSize = _minNumElems;
     390        }
     391
     392        /*
     393         * Allocate a larger buffer for the string if the current one isn't
     394         * large enough. Allocate extra space in the new buffer so that there
     395         * will be room to grow before we have to allocate again.
     396         */
     397        size_t size;
     398        size = (_numElemsAllocated > 0) ? _numElemsAllocated : _minNumElems;
     399        while (newSize > size) {
     400            size += size;
     401        }
     402
     403        /*
     404         * reallocate to a larger buffer
     405         */
     406        if (set(size) != size) {
     407            return 0;
     408        }
     409    }
     410    memcpy(_buf + _numElemsUsed, bytes, numElems * sizeof(T));
     411    _numElemsUsed += numElems;
     412    return numElems;
     413}
     414
     415/**
     416 * Append bytes to the end of this buffer
     417 * @param pointer to bytes to be added
     418 * @param number of bytes to be added
     419 * @return number of bytes appended.
     420 */
     421template<class T>
     422size_t
     423SimpleBuffer<T>::extend(size_t numExtraElems)
     424{
     425    size_t newSize;
     426
     427    newSize = _numElemsUsed + numExtraElems;
     428    if (newSize > _numElemsAllocated) {
     429
     430        /* Enforce a minimum buffer size. */
     431        if (newSize < (size_t) _minNumElems) {
     432            newSize = (size_t) _minNumElems;
     433        }
     434
     435        size_t size;
     436        size = (_numElemsAllocated > 0) ? _numElemsAllocated : _minNumElems;
     437
     438        /* Keep doubling the size of the buffer until we have enough space to
     439         * hold the extra elements. */
     440        while (newSize > size) {
     441            size += size;
     442        }
     443        /* Reallocate to a larger buffer. */
     444        if (set(size) != size) {
     445            return 0;
     446        }
     447    }
     448    return _numElemsAllocated;
     449}
     450
     451/**
     452 * Append formatted bytes to the end of this buffer
     453 * @param pointer to bytes to be added
     454 * @param number of bytes to be added
     455 * @return number of bytes appended.
     456 */
     457template<class T>
     458int
     459SimpleBuffer<T>::appendf(const char *format, ...)
     460{
     461    size_t newMembCnt = 0;
     462    size_t nbytes = 0;
     463    int nmemb = 0;
     464
     465    char* dest = NULL;
     466    size_t size = 0;
     467    size_t bytesAdded = 0;
     468    va_list arg;
     469
     470    // User specified NULL format
     471    if (format == NULL) {
     472        return 0;
     473    }
     474
     475    // FIXME: i think this needs to be division,
     476    // need to create test case with array of ints
     477    // i'm not sure we can even guess the number
     478    // bytes in *bytes.
     479
     480
     481    // add one for terminating null character
     482    nbytes = strlen(format) + 1;
     483
     484    if (nbytes <= 0) {
    378485        // no data written, invalid option
    379         return nmemb;
    380     }
    381 
    382     newMembCnt = (size_t)(_nMembStored + nmemb);
    383 
    384     if (newMembCnt > _nMembAvl) {
     486        return nbytes;
     487    }
     488
     489    // FIXME: we need ceil of nbytes/sizeof(T), instead we add 1 for safety
     490
     491    nmemb = nbytes/sizeof(T);
     492    if (nmemb == 0) {
     493        nmemb++;
     494    }
     495
     496    newMembCnt = (size_t)(_numElemsUsed + nmemb);
     497
     498    if (newMembCnt > _numElemsAllocated) {
    385499
    386500        // buffer sizes less than min_size are set to min_size
    387         if (newMembCnt < (size_t) _minMembCnt) {
    388             newMembCnt = (size_t) _minMembCnt;
     501        if (newMembCnt < (size_t) _minNumElems) {
     502            newMembCnt = (size_t) _minNumElems;
    389503        }
    390504
     
    395509         */
    396510        size_t membAvl;
    397         membAvl = (_nMembAvl > 0) ? _nMembAvl : _minMembCnt;
     511        membAvl = (_numElemsAllocated > 0) ? _numElemsAllocated : _minNumElems;
    398512        while (newMembCnt > membAvl) {
    399513            membAvl += membAvl;
     
    408522    }
    409523
    410     dest = (void*) (_buf + _nMembStored);
    411     src  = (void const*) bytes;
    412     size = (size_t) (nmemb*sizeof(T));
    413     memcpy(dest,src,size);
    414 
    415     _nMembStored += nmemb;
    416 
    417     return nmemb;
    418 }
    419 
    420 /**
    421  * Append formatted bytes to the end of this buffer
    422  * @param pointer to bytes to be added
    423  * @param number of bytes to be added
    424  * @return number of bytes appended.
    425  */
    426 template<class T>
    427 int
    428 SimpleBuffer<T>::appendf(const char *format, ...)
    429 {
    430     size_t newMembCnt = 0;
    431     size_t nbytes = 0;
    432     int nmemb = 0;
    433 
    434     char* dest = NULL;
    435     size_t size = 0;
    436     size_t bytesAdded = 0;
    437     va_list arg;
    438 
    439     // User specified NULL format
    440     if (format == NULL) {
    441         return 0;
    442     }
    443 
    444     // FIXME: i think this needs to be division,
    445     // need to create test case with array of ints
    446     // i'm not sure we can even guess the number
    447     // bytes in *bytes.
    448 
    449 
    450     // add one for terminating null character
    451     nbytes = strlen(format) + 1;
    452 
    453     if (nbytes <= 0) {
    454         // no data written, invalid option
    455         return nbytes;
    456     }
    457 
    458     // FIXME: we need ceil of nbytes/sizeof(T), instead we add 1 for safety
    459 
    460     nmemb = nbytes/sizeof(T);
    461     if (nmemb == 0) {
    462         nmemb++;
    463     }
    464 
    465     newMembCnt = (size_t)(_nMembStored + nmemb);
    466 
    467     if (newMembCnt > _nMembAvl) {
    468 
    469         // buffer sizes less than min_size are set to min_size
    470         if (newMembCnt < (size_t) _minMembCnt) {
    471             newMembCnt = (size_t) _minMembCnt;
    472         }
    473 
    474         /*
    475          * Allocate a larger buffer for the string if the current one isn't
    476          * large enough. Allocate extra space in the new buffer so that there
    477          * will be room to grow before we have to allocate again.
    478          */
    479         size_t membAvl;
    480         membAvl = (_nMembAvl > 0) ? _nMembAvl : _minMembCnt;
    481         while (newMembCnt > membAvl) {
    482             membAvl += membAvl;
    483         }
    484 
    485         /*
    486          * reallocate to a larger buffer
    487          */
    488         if (set(membAvl) != membAvl) {
    489             return 0;
    490         }
    491     }
    492 
    493     dest = (char*) (_buf + _nMembStored);
    494     size = (_nMembAvl-_nMembStored)*sizeof(T);
     524    dest = (char*) (_buf + _numElemsUsed);
     525    size = (_numElemsAllocated-_numElemsUsed)*sizeof(T);
    495526
    496527    va_start(arg,format);
     
    511542
    512543        // FIXME: round the new size up to the nearest multiple of 256?
    513         set(_nMembStored+nmemb);
     544        set(_numElemsUsed+nmemb);
    514545
    515546        // reset dest because it may have moved during reallocation
    516         dest = (char*) (_buf + _nMembStored);
     547        dest = (char*) (_buf + _numElemsUsed);
    517548        size = bytesAdded;
    518549
     
    528559    }
    529560
    530     _nMembStored += nmemb;
     561    _numElemsUsed += nmemb;
    531562
    532563    // remove the null character added by vsnprintf()
     
    549580SimpleBuffer<T>::remove(int nmemb)
    550581{
    551     if ((_nMembStored - nmemb) < 0){
    552         _nMembStored = 0;
     582    if ((_numElemsUsed - nmemb) < 0){
     583        _numElemsUsed = 0;
    553584        _pos = 0;
    554585    } else {
    555         _nMembStored -= nmemb;
    556         if (_pos >= _nMembStored) {
     586        _numElemsUsed -= nmemb;
     587        if (_pos >= _numElemsUsed) {
    557588            // move _pos back to the new end of the buffer.
    558             _pos = _nMembStored-1;
     589            _pos = _numElemsUsed-1;
    559590        }
    560591    }
     
    563594    return nmemb;
    564595}
    565 
    566596
    567597template<class T>
     
    585615    }
    586616    _buf = buf;
    587     _nMembAvl = nmemb;
    588     return _nMembAvl;
    589 }
    590 
     617    _numElemsAllocated = nmemb;
     618    return _numElemsAllocated;
     619}
    591620
    592621template<> inline
     
    596625    size_t curMemb = 0;
    597626
    598     while (curMemb != _nMembStored) {
     627    while (curMemb != _numElemsUsed) {
    599628        fprintf(stdout,"_buf[%lu] = :%c:\n", (long unsigned int)curMemb,
    600629                _buf[curMemb]);
    601630        curMemb += 1;
    602631    }
    603     fprintf(stdout,"_nMembAvl = :%lu:\n", (long unsigned int)_nMembAvl);
     632    fprintf(stdout,"_numElemsAllocated = :%lu:\n",
     633            (long unsigned int)_numElemsAllocated);
    604634
    605635    return *this;
     
    613643    size_t curMemb = 0;
    614644
    615     while (curMemb != _nMembStored) {
     645    while (curMemb != _numElemsUsed) {
    616646        fprintf(stdout,"_buf[%lu] = :%#x:\n", (long unsigned int)curMemb,
    617647                (unsigned long)_buf[curMemb]);
    618648        curMemb += 1;
    619649    }
    620     fprintf(stdout,"_nMembAvl = :%lu:\n", (long unsigned int)_nMembAvl);
     650    fprintf(stdout,"_numElemsAllocated = :%lu:\n", (long unsigned int)_numElemsAllocated);
    621651
    622652    return *this;
     
    651681
    652682    // make sure we don't read off the end of our buffer
    653     if ( (_pos + nmemb) > _nMembStored ) {
    654         nMembRead = _nMembStored - _pos;
     683    if ( (_pos + nmemb) > _numElemsUsed ) {
     684        nMembRead = _numElemsUsed - _pos;
    655685    }
    656686    else {
     
    694724            _pos = 0;
    695725        }
    696         else if (offset >= (long)_nMembStored) {
     726        else if (offset >= (long)_numElemsUsed) {
    697727            /* dont go off the end of data */
    698             _pos = _nMembStored - 1;
     728            _pos = _numElemsUsed - 1;
    699729        }
    700730        else {
     
    707737            _pos = 0;
    708738        }
    709         else if ((_pos + offset) >= _nMembStored) {
     739        else if ((_pos + offset) >= _numElemsUsed) {
    710740            /* dont go off the end of data */
    711             _pos = _nMembStored - 1;
     741            _pos = _numElemsUsed - 1;
    712742        }
    713743        else {
     
    716746    }
    717747    else if (whence == SEEK_END) {
    718         if (offset <= (long)(-1*_nMembStored)) {
     748        if (offset <= (long)(-1*_numElemsUsed)) {
    719749            /* dont go off the beginning of data */
    720750            _pos = 0;
     
    722752        else if (offset >= 0) {
    723753            /* dont go off the end of data */
    724             _pos = _nMembStored - 1;
     754            _pos = _numElemsUsed - 1;
    725755        }
    726756        else {
    727             _pos = (size_t)((_nMembStored - 1) + offset);
     757            _pos = (size_t)((_numElemsUsed - 1) + offset);
    728758        }
    729759    }
     
    794824SimpleBuffer<T>::eof() const
    795825{
    796     return (_pos >= _nMembStored);
     826    return (_pos >= _numElemsUsed);
    797827}
    798828
     
    801831 * Move the data from this SimpleBuffer to the SimpleBuffer provided by
    802832 * the caller. All data except the _pos is moved and this SimpleBuffer is
    803  * re-initialized with bufferInit().
     833 * re-initialized with Initialize().
    804834 * @param SimpleBuffer to move the information to
    805835 * @return reference to this SimpleBuffer object.
     
    809839SimpleBuffer<T>::move(SimpleBuffer<T>& b)
    810840{
    811     bufferFree();
     841    Release();
    812842
    813843    _buf = b._buf;
    814844    _pos = b._pos;
    815845    _fileState = b._fileState;
    816     _nMembStored = b._nMembStored;
    817     _nMembAvl = b._nMembAvl;
    818 
    819     b.bufferInit();
     846    _numElemsUsed = b._numElemsUsed;
     847    _numElemsAllocated = b._numElemsAllocated;
     848
     849    b.Initialize();
    820850
    821851    return *this;
     
    825855 /**
    826856  *  Initializes a dynamic buffer, discarding any previous contents
    827   *  of the buffer. bufferFree() should have been called already
     857  *  of the buffer. Release() should have been called already
    828858  *  if the dynamic buffer was previously in use.
    829859  */
    830860template<class T>
    831861void
    832 SimpleBuffer<T>::bufferInit()
     862SimpleBuffer<T>::Initialize()
    833863{
    834864    _buf = NULL;
    835865    _pos = 0;
    836866    _fileState = true;
    837     _nMembStored = 0;
    838     _nMembAvl = 0;
     867    _numElemsUsed = 0;
     868    _numElemsAllocated = 0;
    839869}
    840870
     
    846876template<class T>
    847877void
    848 SimpleBuffer<T>::bufferFree()
     878SimpleBuffer<T>::Release()
    849879{
    850880    if (_buf != NULL) {
     
    852882        _buf = NULL;
    853883    }
    854     bufferInit();
     884    Initialize();
    855885}
    856886
  • branches/blt4/src/core/scew/xhandler.c

    r1559 r2544  
    193193        }
    194194        if (bytes == NULL) {
    195             fprintf(stderr, "can't allocated %d bytes for character data\n",
     195            fprintf(stderr, "can't allocated %lu bytes for character data\n",
    196196                    size);
    197197            return;
Note: See TracChangeset for help on using the changeset viewer.