- Timestamp:
- Sep 21, 2011 2:28:26 PM (12 years ago)
- Location:
- branches/blt4/src/core
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/blt4/src/core/RpBuffer.cc
r1527 r2544 112 112 113 113 bool 114 Buffer::load (Outcome &status, const char * filePath)114 Buffer::load (Outcome &status, const char *path) 115 115 { 116 116 status.addContext("Rappture::Buffer::load()"); 117 117 118 118 FILE *f; 119 f = fopen( filePath, "rb");119 f = fopen(path, "rb"); 120 120 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 124 125 struct stat stat; 125 126 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) { 134 138 status.addError("can't allocate %d bytes for file \"%s\": %s", 135 size, filePath, strerror(errno));139 stat.st_size, path, strerror(errno)); 136 140 fclose(f); 137 141 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 158 bool 159 Buffer::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); 146 171 fclose(f); // Close the file. 147 172 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 bool168 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 182 173 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)); 185 176 return false; 186 177 } … … 288 279 { 289 280 int ret=0, flush=0; 290 unsigned have=0;291 281 z_stream strm; 292 282 293 283 char in[CHUNK]; 294 284 char out[CHUNK]; 295 296 int bytesWritten = 0;297 285 298 286 /* allocate deflate state */ … … 330 318 ret = deflate(&strm, flush); /* no bad return value */ 331 319 assert(ret != Z_STREAM_ERROR); /* state not clobbered */ 320 321 int have; 332 322 have = CHUNK - strm.avail_out; 333 323 /* 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)) { 336 325 (void)deflateEnd(&strm); 337 326 bout.clear(); 338 327 // 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); 340 329 return false; 341 330 } -
branches/blt4/src/core/RpLibrary.cc
r1824 r2544 1735 1735 1736 1736 /**********************************************************************/ 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 1743 size_t 1744 RpLibrary::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 /**********************************************************************/ 1737 1765 // METHOD: put() 1738 1766 /// Put a string value into the xml. … … 1813 1841 // library doesn't exist, do nothing; 1814 1842 status.error("invalid library object"); 1815 status.addContext("RpLibrary::put() ");1843 status.addContext("RpLibrary::put() - putDouble"); 1816 1844 return *this; 1817 1845 } … … 1820 1848 1821 1849 put(path,valStr.str(),id,append); 1822 status.addContext("RpLibrary::put() - putDouble");1823 1850 return *this; 1824 1851 } … … 2007 2034 Rappture::Buffer buf; 2008 2035 Rappture::Buffer fileBuf; 2009 Rappture::Outcome err;2010 2036 2011 2037 if (!this->root) { … … 2014 2040 } 2015 2041 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()"); 2018 2045 return *this; 2019 2046 } … … 2025 2052 put(path, fileBuf.bytes(), "", append, RPLIB_TRANSLATE); 2026 2053 } 2027 status.addContext("RpLibrary::putFile()");2028 2054 return *this; 2029 2055 } -
branches/blt4/src/core/RpLibrary.h
r1041 r2544 81 81 bool getBool ( std::string path = "") const; 82 82 Rappture::Buffer getData ( std::string path = "") const; 83 size_t getFile ( std::string path, 84 std::string fileName) const; 83 85 84 86 /* -
branches/blt4/src/core/RpLibraryFInterface.cc
r1105 r2544 211 211 } 212 212 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_len257 );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_len315 );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_len373 );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_len395 )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 nodes417 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 442 213 /**********************************************************************/ 443 214 // FUNCTION: rp_lib_get() … … 445 216 /** 446 217 */ 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 ) 218 void 219 rp_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 */ 453 225 { 454 226 std::string xmlText = ""; … … 478 250 /** 479 251 */ 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 ) 252 void 253 rp_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 */ 486 259 { 487 260 std::string xmlText = ""; … … 512 285 /** 513 286 */ 514 double rp_lib_get_double( int* handle, /* integer handle of library */ 515 char* path, /* null terminated path */ 516 int path_len 517 ) 287 double 288 rp_lib_get_double( 289 int* handle, /* integer handle of library */ 290 char* path, /* null terminated path */ 291 int path_len) 518 292 { 519 293 double retVal = 0.0; … … 602 376 return retVal; 603 377 } 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 */ 386 int 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 604 414 605 415 … … 678 488 /** 679 489 */ 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 ) 490 void 491 rp_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) 688 499 { 689 500 std::string inPath = ""; … … 718 529 /** 719 530 */ 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 ) 531 void 532 rp_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) 728 540 { 729 541 std::string inPath = ""; … … 747 559 748 560 749 void rp_lib_put_obj( int* handle, 750 char* path, 751 int* valHandle, 752 int* append, 753 int path_len 754 ) 561 void 562 rp_lib_put_obj( 563 int* handle, 564 char* path, 565 int* valHandle, 566 int* append, 567 int path_len) 755 568 { 756 569 std::string inPath = ""; … … 781 594 } 782 595 } 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_len793 )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 object811 // whose contents must not be modified812 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 pointer881 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 */894 596 895 597 /**********************************************************************/ -
branches/blt4/src/core/RpLibraryFInterface.h
r1086 r2544 78 78 char* path, 79 79 int path_len); 80 81 extern int rp_lib_get_file(int* handle, char* path, char* fileName, 82 int path_len, int fileName_len); 80 83 81 84 void rp_lib_put_str ( int* handle, -
branches/blt4/src/core/RpLibraryFStubs.c
r1018 r2544 340 340 } 341 341 342 int 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 351 int 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 360 int 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 342 369 void 343 370 rp_lib_put_str_ ( int* handle, … … 353 380 354 381 void 355 rp_lib_put_str__ ( 382 rp_lib_put_str__ ( int* handle, 356 383 char* path, 357 384 char* value, -
branches/blt4/src/core/RpLibraryFStubs.h
r1018 r2544 82 82 int path_len); 83 83 84 int rp_lib_get_file_ ( int* handle, 85 char* path, 86 char* fileName, 87 int path_len, 88 int fileName_len); 89 84 90 void rp_lib_put_str_ ( int* handle, 85 91 char* path, … … 223 229 int path_len); 224 230 231 int rp_lib_get_file__ ( int* handle, 232 char* path, 233 char* fileName, 234 int path_len, 235 int fileName_len); 236 225 237 void rp_lib_put_str__ ( int* handle, 226 238 char* path, … … 365 377 int path_len); 366 378 379 int RP_LIB_GET_FILE ( int* handle, 380 char* path, 381 char* fileName, 382 int path_len, 383 int fileName_len); 384 367 385 void RP_LIB_PUT_STR ( int* handle, 368 386 char* path, -
branches/blt4/src/core/RpOutcome.cc
r1571 r2544 83 83 _remark.append(bufPtr); 84 84 } 85 /* fprintf(stderr, "Outcome: %s\n", _remark.c_str()); */ 85 86 _status = 1; /* Set to error */ 86 87 if (bufPtr != stackSpace) { -
branches/blt4/src/core/RpSimpleBuffer.h
r1897 r2544 1 1 2 /* 2 3 * ====================================================================== … … 69 70 #include <cstdarg> 70 71 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 */ 71 88 namespace Rappture { 72 89 … … 74 91 class SimpleBuffer { 75 92 public: 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 } 77 109 SimpleBuffer(size_t nmemb); 78 SimpleBuffer(const T* bytes, int nmemb=-1); 110 111 /** 112 * Copy constructor 113 * @param SimpleBuffer object to copy 114 */ 79 115 SimpleBuffer(const SimpleBuffer& b); 116 80 117 SimpleBuffer<T>& operator=(const SimpleBuffer<T>& b); 81 118 SimpleBuffer operator+(const SimpleBuffer& b) const; 82 119 SimpleBuffer<T>& operator+=(const SimpleBuffer<T>& b); 83 120 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); 91 177 int append(const T* bytes, int nmemb=-1); 92 178 int appendf(const char *format, ...); … … 98 184 SimpleBuffer<T>& rewind(); 99 185 SimpleBuffer<T>& show(); 100 186 101 187 bool good() const; 102 188 bool bad() const; 103 189 bool eof() const; 104 190 105 191 SimpleBuffer<T>& move(SimpleBuffer<T>& b); 106 192 107 193 protected: 108 109 void bufferInit();110 void bufferFree();111 194 195 void Initialize(); 196 void Release(); 197 112 198 private: 113 199 114 200 /// Pointer to the memory that holds our buffer's data 115 201 T* _buf; 116 202 117 203 /// Position offset within the buffer's memory 118 204 size_t _pos; 119 205 120 206 /// Number of members stored in the buffer 121 size_t _n MembStored;122 207 size_t _numElemsUsed; 208 123 209 /// Total number of members available in the buffer 124 size_t _n MembAvl;125 210 size_t _numElemsAllocated; 211 126 212 /// State of the last file like operation. 127 213 bool _fileState; 128 214 129 215 /// 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 132 218 size_t __guesslen(const T* bytes); 133 219 134 220 }; 135 221 136 222 typedef SimpleBuffer<char> SimpleCharBuffer; 137 223 typedef SimpleBuffer<float> SimpleFloatBuffer; … … 139 225 140 226 /** 141 * Construct an empty SimpleBuffer.142 */143 template<class T>144 SimpleBuffer<T>::SimpleBuffer()145 {146 bufferInit();147 }148 149 150 /**151 227 * Construct an empty SimpleBuffer of specified size. 152 228 */ 153 229 template<class T> 154 SimpleBuffer<T>::SimpleBuffer(size_t n memb)155 { 156 bufferInit();157 158 if (n memb== 0) {230 SimpleBuffer<T>::SimpleBuffer(size_t numElems) 231 { 232 Initialize(); 233 234 if (numElems == 0) { 159 235 // ignore requests for sizes equal to zero 160 236 return; … … 162 238 163 239 // buffer sizes less than min_size are set to min_size 164 if (n memb < (size_t) _minMembCnt) {165 n memb = _minMembCnt;166 } 167 168 if (set(n memb) != nmemb) {240 if (numElems < (size_t) _minNumElems) { 241 numElems = _minNumElems; 242 } 243 244 if (set(numElems) != numElems) { 169 245 return; 170 246 } 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; 186 248 } 187 249 … … 194 256 SimpleBuffer<T>::SimpleBuffer(const SimpleBuffer<T>& b) 195 257 { 196 bufferInit();197 append(b.bytes(), b.nmemb());258 Initialize(); 259 append(b.bytes(), b.nmemb()); 198 260 } 199 261 … … 207 269 SimpleBuffer<T>::operator=(const SimpleBuffer<T>& b) 208 270 { 209 bufferFree(); 210 bufferInit(); 271 Release(); 211 272 append(b.bytes(),b.nmemb()); 212 273 return *this; … … 247 308 template<class T> 248 309 T 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 } 310 SimpleBuffer<T>::operator[](size_t index) 311 { 312 return (_buf + index); // Rely on pointer arithmetic 313 } 314 315 317 316 318 317 /** … … 327 326 return strlen(bytes); 328 327 } 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 */ 329 336 330 337 /** … … 348 355 template<class T> 349 356 int 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; 357 SimpleBuffer<T>::append(const T* bytes, int numElems) 358 { 358 359 359 360 // 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. 362 364 } 363 365 … … 367 369 // bytes in *bytes. 368 370 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 */ 421 template<class T> 422 size_t 423 SimpleBuffer<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 */ 457 template<class T> 458 int 459 SimpleBuffer<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) { 378 485 // 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) { 385 499 386 500 // buffer sizes less than min_size are set to min_size 387 if (newMembCnt < (size_t) _min MembCnt) {388 newMembCnt = (size_t) _min MembCnt;501 if (newMembCnt < (size_t) _minNumElems) { 502 newMembCnt = (size_t) _minNumElems; 389 503 } 390 504 … … 395 509 */ 396 510 size_t membAvl; 397 membAvl = (_n MembAvl > 0) ? _nMembAvl : _minMembCnt;511 membAvl = (_numElemsAllocated > 0) ? _numElemsAllocated : _minNumElems; 398 512 while (newMembCnt > membAvl) { 399 513 membAvl += membAvl; … … 408 522 } 409 523 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); 495 526 496 527 va_start(arg,format); … … 511 542 512 543 // FIXME: round the new size up to the nearest multiple of 256? 513 set(_n MembStored+nmemb);544 set(_numElemsUsed+nmemb); 514 545 515 546 // reset dest because it may have moved during reallocation 516 dest = (char*) (_buf + _n MembStored);547 dest = (char*) (_buf + _numElemsUsed); 517 548 size = bytesAdded; 518 549 … … 528 559 } 529 560 530 _n MembStored += nmemb;561 _numElemsUsed += nmemb; 531 562 532 563 // remove the null character added by vsnprintf() … … 549 580 SimpleBuffer<T>::remove(int nmemb) 550 581 { 551 if ((_n MembStored - nmemb) < 0){552 _n MembStored = 0;582 if ((_numElemsUsed - nmemb) < 0){ 583 _numElemsUsed = 0; 553 584 _pos = 0; 554 585 } else { 555 _n MembStored -= nmemb;556 if (_pos >= _n MembStored) {586 _numElemsUsed -= nmemb; 587 if (_pos >= _numElemsUsed) { 557 588 // move _pos back to the new end of the buffer. 558 _pos = _n MembStored-1;589 _pos = _numElemsUsed-1; 559 590 } 560 591 } … … 563 594 return nmemb; 564 595 } 565 566 596 567 597 template<class T> … … 585 615 } 586 616 _buf = buf; 587 _nMembAvl = nmemb; 588 return _nMembAvl; 589 } 590 617 _numElemsAllocated = nmemb; 618 return _numElemsAllocated; 619 } 591 620 592 621 template<> inline … … 596 625 size_t curMemb = 0; 597 626 598 while (curMemb != _n MembStored) {627 while (curMemb != _numElemsUsed) { 599 628 fprintf(stdout,"_buf[%lu] = :%c:\n", (long unsigned int)curMemb, 600 629 _buf[curMemb]); 601 630 curMemb += 1; 602 631 } 603 fprintf(stdout,"_nMembAvl = :%lu:\n", (long unsigned int)_nMembAvl); 632 fprintf(stdout,"_numElemsAllocated = :%lu:\n", 633 (long unsigned int)_numElemsAllocated); 604 634 605 635 return *this; … … 613 643 size_t curMemb = 0; 614 644 615 while (curMemb != _n MembStored) {645 while (curMemb != _numElemsUsed) { 616 646 fprintf(stdout,"_buf[%lu] = :%#x:\n", (long unsigned int)curMemb, 617 647 (unsigned long)_buf[curMemb]); 618 648 curMemb += 1; 619 649 } 620 fprintf(stdout,"_n MembAvl = :%lu:\n", (long unsigned int)_nMembAvl);650 fprintf(stdout,"_numElemsAllocated = :%lu:\n", (long unsigned int)_numElemsAllocated); 621 651 622 652 return *this; … … 651 681 652 682 // make sure we don't read off the end of our buffer 653 if ( (_pos + nmemb) > _n MembStored ) {654 nMembRead = _n MembStored - _pos;683 if ( (_pos + nmemb) > _numElemsUsed ) { 684 nMembRead = _numElemsUsed - _pos; 655 685 } 656 686 else { … … 694 724 _pos = 0; 695 725 } 696 else if (offset >= (long)_n MembStored) {726 else if (offset >= (long)_numElemsUsed) { 697 727 /* dont go off the end of data */ 698 _pos = _n MembStored - 1;728 _pos = _numElemsUsed - 1; 699 729 } 700 730 else { … … 707 737 _pos = 0; 708 738 } 709 else if ((_pos + offset) >= _n MembStored) {739 else if ((_pos + offset) >= _numElemsUsed) { 710 740 /* dont go off the end of data */ 711 _pos = _n MembStored - 1;741 _pos = _numElemsUsed - 1; 712 742 } 713 743 else { … … 716 746 } 717 747 else if (whence == SEEK_END) { 718 if (offset <= (long)(-1*_n MembStored)) {748 if (offset <= (long)(-1*_numElemsUsed)) { 719 749 /* dont go off the beginning of data */ 720 750 _pos = 0; … … 722 752 else if (offset >= 0) { 723 753 /* dont go off the end of data */ 724 _pos = _n MembStored - 1;754 _pos = _numElemsUsed - 1; 725 755 } 726 756 else { 727 _pos = (size_t)((_n MembStored - 1) + offset);757 _pos = (size_t)((_numElemsUsed - 1) + offset); 728 758 } 729 759 } … … 794 824 SimpleBuffer<T>::eof() const 795 825 { 796 return (_pos >= _n MembStored);826 return (_pos >= _numElemsUsed); 797 827 } 798 828 … … 801 831 * Move the data from this SimpleBuffer to the SimpleBuffer provided by 802 832 * the caller. All data except the _pos is moved and this SimpleBuffer is 803 * re-initialized with bufferInit().833 * re-initialized with Initialize(). 804 834 * @param SimpleBuffer to move the information to 805 835 * @return reference to this SimpleBuffer object. … … 809 839 SimpleBuffer<T>::move(SimpleBuffer<T>& b) 810 840 { 811 bufferFree();841 Release(); 812 842 813 843 _buf = b._buf; 814 844 _pos = b._pos; 815 845 _fileState = b._fileState; 816 _n MembStored = b._nMembStored;817 _n MembAvl = b._nMembAvl;818 819 b. bufferInit();846 _numElemsUsed = b._numElemsUsed; 847 _numElemsAllocated = b._numElemsAllocated; 848 849 b.Initialize(); 820 850 821 851 return *this; … … 825 855 /** 826 856 * Initializes a dynamic buffer, discarding any previous contents 827 * of the buffer. bufferFree() should have been called already857 * of the buffer. Release() should have been called already 828 858 * if the dynamic buffer was previously in use. 829 859 */ 830 860 template<class T> 831 861 void 832 SimpleBuffer<T>:: bufferInit()862 SimpleBuffer<T>::Initialize() 833 863 { 834 864 _buf = NULL; 835 865 _pos = 0; 836 866 _fileState = true; 837 _n MembStored = 0;838 _n MembAvl= 0;867 _numElemsUsed = 0; 868 _numElemsAllocated = 0; 839 869 } 840 870 … … 846 876 template<class T> 847 877 void 848 SimpleBuffer<T>:: bufferFree()878 SimpleBuffer<T>::Release() 849 879 { 850 880 if (_buf != NULL) { … … 852 882 _buf = NULL; 853 883 } 854 bufferInit();884 Initialize(); 855 885 } 856 886 -
branches/blt4/src/core/scew/xhandler.c
r1559 r2544 193 193 } 194 194 if (bytes == NULL) { 195 fprintf(stderr, "can't allocated % dbytes for character data\n",195 fprintf(stderr, "can't allocated %lu bytes for character data\n", 196 196 size); 197 197 return;
Note: See TracChangeset
for help on using the changeset viewer.