Changeset 1527 for trunk/src/core
- Timestamp:
- Jun 22, 2009, 12:38:49 PM (15 years ago)
- Location:
- trunk/src/core
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpBuffer.cc
r1384 r1527 120 120 if (f == NULL) { 121 121 status.addError("can't open \"%s\": %s", filePath, strerror(errno)); 122 122 return false; 123 123 } 124 124 struct stat stat; 125 125 if (fstat(fileno(f), &stat) < 0) { 126 126 status.addError("can't stat \"%s\": %s", filePath, strerror(errno)); 127 128 } 127 return false; 128 } 129 129 off_t size; 130 130 size = stat.st_size; … … 133 133 if (memblock == NULL) { 134 134 status.addError("can't allocate %d bytes for file \"%s\": %s", 135 135 size, filePath, strerror(errno)); 136 136 fclose(f); 137 137 return false; … … 139 139 140 140 // FIXME: better yet, create an "extend" method in the buffer and returns 141 // 142 // 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 143 144 144 size_t nRead; 145 145 nRead = fread(memblock, sizeof(char), size, f); 146 fclose(f); 146 fclose(f); // Close the file. 147 147 148 148 if (nRead != (size_t)size) { 149 149 status.addError("can't read %d bytes from \"%s\": %s", size, filePath, 150 151 return false; 152 } 150 strerror(errno)); 151 return false; 152 } 153 153 154 154 int nBytes; … … 158 158 if (nBytes != size) { 159 159 status.addError("can't append %d bytes from \"%s\" to buffer: %s", 160 161 return false; 162 } 160 size, filePath, strerror(errno)); 161 return false; 162 } 163 163 return true; 164 164 } … … 174 174 if (f != NULL) { 175 175 status.addError("can't open \"%s\": %s\n", filePath, strerror(errno)); 176 176 return false; 177 177 } 178 178 ssize_t nWritten; 179 179 nWritten = fwrite(bytes(), size(), sizeof(char), f); 180 fclose(f); 181 180 fclose(f); // Close the file. 181 182 182 if (nWritten != (ssize_t)size()) { 183 183 status.addError("can't write %d bytes to \"%s\": %s\n", size(), 184 185 184 filePath, strerror(errno)); 185 return false; 186 186 } 187 187 return true; … … 198 198 switch (flags & (RPENC_Z | RPENC_B64)) { 199 199 case 0: 200 201 202 case RPENC_Z: 203 204 205 206 207 208 209 case RPENC_B64: 200 break; 201 202 case RPENC_Z: // Compress only 203 if (!do_compress(status, *this, bout)) { 204 return false; 205 } 206 move(bout); 207 break; 208 209 case RPENC_B64: // Encode only 210 210 if (!do_base64_enc(status, *this, bout)) { 211 211 return false; 212 212 } 213 214 213 move(bout); 214 break; 215 215 216 216 case (RPENC_B64 | RPENC_Z): 217 218 219 220 221 217 218 // It's always compress then encode 219 if (!do_compress(status, *this, bout)) { 220 return false; 221 } 222 222 if (!do_base64_enc(status, bout, *this)) { 223 223 return false; 224 224 } 225 225 break; 226 226 } 227 227 return true; … … 238 238 switch (flags & (RPENC_Z | RPENC_B64)) { 239 239 case 0: 240 241 242 243 244 245 246 247 248 249 250 251 252 253 break; 254 255 case RPENC_Z: 256 257 258 259 260 261 262 case RPENC_B64: 240 if (encoding::isBase64(bytes(), size())) { 241 if (!do_base64_dec(status, *this, bout)) { 242 return false; 243 } 244 move(bout); 245 } 246 bout.clear(); 247 if (encoding::isBinary(bytes(), size())) { 248 if (!do_decompress(status, *this, bout)) { 249 return false; 250 } 251 move(bout); 252 } 253 break; 254 255 case RPENC_Z: // Decompress only 256 if (!do_decompress(status, *this, bout)) { 257 return false; 258 } 259 move(bout); 260 break; 261 262 case RPENC_B64: // Decode only 263 263 if (!do_base64_dec(status, *this, bout)) { 264 264 return false; 265 265 } 266 267 266 move(bout); 267 break; 268 268 269 269 case (RPENC_B64 | RPENC_Z): 270 271 270 271 // It's always decode then decompress 272 272 if (!do_base64_dec(status, *this, bout)) { 273 273 return false; 274 274 } 275 276 277 278 279 275 clear(); 276 if (!do_decompress(status, bout, *this)) { 277 return false; 278 } 279 break; 280 280 } 281 281 return true; … … 285 285 bool 286 286 Buffer::do_compress(Outcome& status, SimpleCharBuffer& bin, 287 287 SimpleCharBuffer& bout) 288 288 { 289 289 int ret=0, flush=0; … … 357 357 bool 358 358 Buffer::do_decompress(Outcome& status, SimpleCharBuffer& bin, 359 359 SimpleCharBuffer& bout) 360 360 { 361 361 int ret; … … 433 433 bool 434 434 Buffer::do_base64_enc(Outcome& status, const SimpleCharBuffer& bin, 435 435 SimpleCharBuffer& bout ) 436 436 { 437 437 int tBufSize = 0; … … 453 453 bool 454 454 Buffer::do_base64_dec(Outcome& status, const SimpleCharBuffer& bin, 455 455 SimpleCharBuffer& bout ) 456 456 { 457 457 int tBufSize = 0; -
trunk/src/core/RpBuffer.h
r1384 r1527 72 72 73 73 enum { CHUNK = 4096 }; 74 74 75 75 bool do_compress(Outcome& status, SimpleCharBuffer& bin, 76 76 SimpleCharBuffer& bout ); 77 77 bool do_decompress( Outcome& status, SimpleCharBuffer& bin, 78 78 SimpleCharBuffer& bout ); 79 79 bool do_base64_enc(Outcome& status, const SimpleCharBuffer& bin, 80 80 SimpleCharBuffer& bout ); 81 81 bool do_base64_dec(Outcome& status, const SimpleCharBuffer& bin, 82 82 SimpleCharBuffer& bout ); 83 83 }; 84 84 85 85 } // namespace Rappture 86 86 87 87 #ifdef __cplusplus 88 88 } -
trunk/src/core/RpLibrary.cc
r1384 r1527 1177 1177 if (retNode) { 1178 1178 // allocate a new rappture library object for the node 1179 retLib = new RpLibrary( retNode,this->tree ); 1179 retLib = new RpLibrary( retNode,this->tree ); 1180 1180 } 1181 1181 } … … 1545 1545 // because base64 character set does not include xml entity chars 1546 1546 if (!Rappture::encoding::decode(status, inData, 0)) { 1547 1548 1547 return retStr; 1548 } 1549 1549 retStr = std::string(inData.bytes(),inData.size()); 1550 1550 } else { … … 1552 1552 if (translateFlag == RPLIB_TRANSLATE) { 1553 1553 translatedContents = ERTranslator.decode(inData.bytes(), 1554 1554 inData.size()); 1555 1555 if (translatedContents == NULL) { 1556 1556 // translation failed 1557 1557 if (!status) { 1558 1558 status.error("Error while translating entity references"); 1559 1559 return retStr; 1560 1560 } 1561 1561 } else { … … 1738 1738 RpLibrary& 1739 1739 RpLibrary::put (std::string path, std::string value, std::string id, 1740 1740 unsigned int append, unsigned int translateFlag) 1741 1741 { 1742 1742 Rappture::EntityRef ERTranslator; … … 1756 1756 // check for binary data 1757 1757 // FIXME: I've already appended a NUL-byte of this assuming that 1758 // 1758 // it's a ASCII string. This test must come before. 1759 1759 if (Rappture::encoding::isBinary(value.c_str(), value.length())) { 1760 1760 putData(path, value.c_str(), value.length(), append); … … 1775 1775 if (!status) { 1776 1776 status.error("Error while translating entity references"); 1777 1777 return *this; 1778 1778 } 1779 1779 } … … 1801 1801 1802 1802 RpLibrary& 1803 RpLibrary::put (std::string path, double value, std::string id, 1804 1803 RpLibrary::put (std::string path, double value, std::string id, 1804 unsigned int append) 1805 1805 { 1806 1806 std::stringstream valStr; … … 1962 1962 1963 1963 if (retNode == NULL) { 1964 1965 1964 status.addError("can't create node from path \"%s\"", path.c_str()); 1965 return *this; 1966 1966 } 1967 1967 if (append == RPLIB_APPEND) { 1968 1969 1970 1971 1972 1973 1974 1968 if ( (contents = scew_element_contents(retNode)) ) { 1969 inData.append(contents); 1970 // base64 decode and un-gzip the data 1971 if (!Rappture::encoding::decode(status, inData, 0)) { 1972 return *this; 1973 } 1974 } 1975 1975 } 1976 1976 if (inData.append(bytes, nbytes) != nbytes) { 1977 1978 1979 } 1977 status.addError("can't append %d bytes", nbytes); 1978 return *this; 1979 } 1980 1980 // gzip and base64 encode the data 1981 1981 flags = RPENC_Z|RPENC_B64|RPENC_HDR; 1982 1982 if (!Rappture::encoding::encode(status, inData,flags)) { 1983 1983 return *this; 1984 1984 } 1985 1985 bytesWritten = (unsigned int) inData.size(); … … 1993 1993 /// Put data from a file into the xml. 1994 1994 /** 1995 * Append flag adds additional nodes, it does not merge same 1995 * Append flag adds additional nodes, it does not merge same 1996 1996 * named nodes together 1997 1997 */ 1998 1998 1999 1999 RpLibrary& 2000 RpLibrary::putFile(std::string path, std::string fileName, 2001 2000 RpLibrary::putFile(std::string path, std::string fileName, 2001 unsigned int compress, unsigned int append) 2002 2002 { 2003 2003 Rappture::Buffer buf; … … 2011 2011 2012 2012 if (!fileBuf.load(err, fileName.c_str())) { 2013 2014 2013 fprintf(stderr, "error loading file: %s\n", err.remark()); 2014 return *this; 2015 2015 } 2016 2016 if (compress == RPLIB_COMPRESS) { 2017 2017 putData(path, fileBuf.bytes(), fileBuf.size(), append); 2018 2018 } else { 2019 2019 /* Always append a NUL-byte to the end of ASCII strings. */ 2020 2020 fileBuf.append("\0", 1); 2021 2021 put(path, fileBuf.bytes(), "", append, RPLIB_TRANSLATE); -
trunk/src/core/RpSimpleBuffer.h
r1398 r1527 67 67 #include <cstring> 68 68 #include <cstdlib> 69 #include <cstdarg> 69 70 70 71 namespace Rappture { … … 88 89 SimpleBuffer<T>& clear(); 89 90 int append(const T* bytes, int nmemb=-1); 91 int appendf(const char *format, ...); 92 int remove(int nmemb); 90 93 size_t read(const T* bytes, size_t nmemb); 91 94 int seek(long offset, int whence); 92 95 int tell() const; 93 size_t set(size_t n bytes);96 size_t set(size_t nmemb); 94 97 SimpleBuffer<T>& rewind(); 95 98 SimpleBuffer<T>& show(); … … 345 348 return 0; 346 349 } 350 351 // FIXME: i think this needs to be division, 352 // need to create test case with array of ints 353 // i'm not sure we can even guess the number 354 // bytes in *bytes. 347 355 348 356 if (nmemb == -1) { … … 351 359 // at the length of the object. 352 360 nbytes = __guesslen(bytes); 353 nmemb = nbytes *sizeof(T);361 nmemb = nbytes/sizeof(T); 354 362 } 355 363 … … 397 405 } 398 406 407 /** 408 * Append formatted bytes to the end of this buffer 409 * @param pointer to bytes to be added 410 * @param number of bytes to be added 411 * @return number of bytes appended. 412 */ 413 template<class T> 414 int 415 SimpleBuffer<T>::appendf(const char *format, ...) 416 { 417 size_t newMembCnt = 0; 418 size_t nbytes = 0; 419 int nmemb = 0; 420 421 char* dest = NULL; 422 size_t size = 0; 423 size_t bytesAdded = 0; 424 va_list arg; 425 426 // User specified NULL format 427 if (format == NULL) { 428 return 0; 429 } 430 431 // FIXME: i think this needs to be division, 432 // need to create test case with array of ints 433 // i'm not sure we can even guess the number 434 // bytes in *bytes. 435 436 437 // add one for terminating null character 438 nbytes = strlen(format) + 1; 439 440 if (nbytes <= 0) { 441 // no data written, invalid option 442 return nbytes; 443 } 444 445 // FIXME: we need ceil of nbytes/sizeof(T), instead we add 1 for safety 446 447 nmemb = nbytes/sizeof(T); 448 if (nmemb == 0) { 449 nmemb++; 450 } 451 452 newMembCnt = (size_t)(_nMembStored + nmemb); 453 454 if (newMembCnt > _nMembAvl) { 455 456 // buffer sizes less than min_size are set to min_size 457 if (newMembCnt < (size_t) _minMembCnt) { 458 newMembCnt = (size_t) _minMembCnt; 459 } 460 461 /* 462 * Allocate a larger buffer for the string if the current one isn't 463 * large enough. Allocate extra space in the new buffer so that there 464 * will be room to grow before we have to allocate again. 465 */ 466 size_t membAvl; 467 membAvl = (_nMembAvl > 0) ? _nMembAvl : _minMembCnt; 468 while (newMembCnt > membAvl) { 469 membAvl += membAvl; 470 } 471 472 /* 473 * reallocate to a larger buffer 474 */ 475 if (set(membAvl) != membAvl) { 476 return 0; 477 } 478 } 479 480 dest = (char*) (_buf + _nMembStored); 481 size = (_nMembAvl-_nMembStored)*sizeof(T); 482 483 va_start(arg,format); 484 bytesAdded = vsnprintf(dest,size,format,arg); 485 va_end(arg); 486 487 // bytesAdded contains the number of bytes that would have 488 // been placed in the buffer if the call was successful. 489 // this value does not include the trailing null character. 490 // so we add one to account for it. 491 492 bytesAdded++; 493 nmemb = bytesAdded/sizeof(T); 494 495 if (bytesAdded > size) { 496 // we did not fit everything in the original buffer 497 // resize and try again. 498 499 // FIXME: round the new size up to the nearest multiple of 256? 500 set(_nMembStored+nmemb); 501 502 // reset dest because it may have moved during reallocation 503 dest = (char*) (_buf + _nMembStored); 504 size = bytesAdded; 505 506 va_start(arg,format); 507 bytesAdded = vsnprintf(dest,size,format,arg); 508 va_end(arg); 509 510 if (bytesAdded > size) { 511 // crystals grow, people grow, data doesn't grow... 512 // issue error 513 fprintf(stderr,"error in appendf while appending data"); 514 } 515 } 516 517 _nMembStored += nmemb; 518 519 // remove the null character added by vsnprintf() 520 // we do this because if we are appending strings, 521 // the embedded null acts as a terminating null char. 522 // this is a generic buffer so if user wants a 523 // terminating null, they should append it. 524 remove(1); 525 526 return nmemb; 527 } 528 529 /** 530 * Remove bytes from the end of this buffer 531 * @param number of bytes to be removed 532 * @return number of bytes removed. 533 */ 534 template<class T> 535 int 536 SimpleBuffer<T>::remove(int nmemb) 537 { 538 if ((_nMembStored - nmemb) < 0){ 539 _nMembStored = 0; 540 _pos = 0; 541 } else { 542 _nMembStored -= nmemb; 543 if (_pos >= _nMembStored) { 544 // move _pos back to the new end of the buffer. 545 _pos = _nMembStored-1; 546 } 547 } 548 549 550 return nmemb; 551 } 552 399 553 400 554 template<class T> … … 412 566 413 567 if (buf == NULL) { 414 fprintf(stderr,"Can't allocate %zu bytes of memory\n",nbytes); 568 fprintf(stderr,"Can't allocate %lu bytes of memory\n", 569 (long unsigned int)nbytes); 415 570 _fileState = false; 416 571 return 0; … … 429 584 430 585 while (curMemb != _nMembStored) { 431 fprintf(stdout,"_buf[%zu] = :%c:\n", curMemb, _buf[curMemb]); 586 fprintf(stdout,"_buf[%lu] = :%c:\n", (long unsigned int)curMemb, 587 _buf[curMemb]); 432 588 curMemb += 1; 433 589 } 434 fprintf(stdout,"_nMembAvl = :% zu:\n",_nMembAvl);590 fprintf(stdout,"_nMembAvl = :%lu:\n", (long unsigned int)_nMembAvl); 435 591 436 592 return *this; … … 445 601 446 602 while (curMemb != _nMembStored) { 447 fprintf(stdout,"_buf[% zu] = :%#lx:\n",curMemb,448 (long unsigned)_buf[curMemb]);603 fprintf(stdout,"_buf[%lu] = :%#x:\n", (long unsigned int)curMemb, 604 (unsigned long)_buf[curMemb]); 449 605 curMemb += 1; 450 606 } 451 fprintf(stdout,"_nMembAvl = :% zu:\n",_nMembAvl);607 fprintf(stdout,"_nMembAvl = :%lu:\n", (long unsigned int)_nMembAvl); 452 608 453 609 return *this; -
trunk/src/core/RpUnits.cc
r1432 r1527 2395 2395 retVal = RpUnitsPreset::addPresetMisc(); 2396 2396 } 2397 else if (group.compare(RP_TYPE_POWER) == 0) { 2398 retVal = RpUnitsPreset::addPresetPower(); 2399 } 2397 2400 2398 2401 return retVal; … … 2426 2429 result += addPresetMagnetic(); 2427 2430 result += addPresetMisc(); 2431 result += addPresetPower(); 2428 2432 2429 2433 return 0; … … 2940 2944 RpUnits* amu = NULL; 2941 2945 RpUnits* bel = NULL; 2946 RpUnits* amp = NULL; 2947 RpUnits* ohm = NULL; 2942 2948 2943 2949 volt = RpUnits::define("V", NULL, RP_TYPE_EPOT, RPUNITS_METRIC); … … 2947 2953 amu = RpUnits::define("amu", NULL, "mass_unit", !RPUNITS_METRIC); 2948 2954 bel = RpUnits::define("B", NULL, "audio_transmission", RPUNITS_METRIC); 2955 amp = RpUnits::define("amp", NULL, "electric_current", RPUNITS_METRIC); 2956 ohm = RpUnits::define("ohm", NULL, "electric_resistance", RPUNITS_METRIC); 2949 2957 2950 2958 // RpUnits* percent = RpUnits::define("%", NULL, RP_TYPE_MISC); 2959 2960 return 0; 2961 } 2962 2963 /**********************************************************************/ 2964 // METHOD: addPresetPower() 2965 /// Add power related units to the dictionary 2966 /** 2967 * Defines the following units: 2968 * watt (W) 2969 * 2970 * Return codes: 0 success, anything else is error 2971 */ 2972 2973 int 2974 RpUnitsPreset::addPresetPower () { 2975 2976 RpUnits* watt = NULL; 2977 2978 // watts are derived units = J/s = kg*m2/s3 = Newton*m/s and Amps*Volt 2979 watt = RpUnits::define("W", NULL, RP_TYPE_POWER, RPUNITS_METRIC); 2951 2980 2952 2981 return 0; … … 2998 3027 return &RpUnitsTypes::hintTypeMisc; 2999 3028 } 3029 else if (type.compare(RP_TYPE_POWER) == 0) { 3030 return &RpUnitsTypes::hintTypePower; 3031 } 3000 3032 else { 3001 3033 return NULL; … … 3177 3209 3178 3210 if ( (unitObj->getType()).compare(RP_TYPE_MISC) == 0 ) { 3211 retVal = true; 3212 } 3213 3214 return retVal; 3215 } 3216 3217 bool 3218 RpUnitsTypes::hintTypePower ( RpUnits* unitObj ) { 3219 3220 bool retVal = false; 3221 3222 if ( (unitObj->getType()).compare(RP_TYPE_POWER) == 0 ) { 3179 3223 retVal = true; 3180 3224 } -
trunk/src/core/RpUnits.h
r1427 r1527 48 48 #define RP_TYPE_MAGNETIC "magnetic" 49 49 #define RP_TYPE_MISC "misc" 50 #define RP_TYPE_POWER "power" 50 51 51 52 … … 87 88 static int addPresetMagnetic(); 88 89 static int addPresetMisc(); 90 static int addPresetPower(); 89 91 }; 90 92 … … 110 112 static bool hintTypeMagnetic ( RpUnits* unitObj ); 111 113 static bool hintTypeMisc ( RpUnits* unitObj ); 114 static bool hintTypePower ( RpUnits* unitObj ); 112 115 113 116 private: … … 520 523 // RP_TYPE_MAGNETIC "magnetic" load units related to magnetics 521 524 // RP_TYPE_MISC "misc" load units related to everything else 525 // RP_TYPE_POWER "power" load units related to power 522 526 // (no other groups have been created) 523 527
Note: See TracChangeset
for help on using the changeset viewer.