Changeset 583 for trunk/src2
- Timestamp:
- Feb 22, 2007, 1:45:50 AM (18 years ago)
- Location:
- trunk/src2/core
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src2/core/Makefile.in
r576 r583 13 13 14 14 CXX = @CXX@ 15 # CXX = g++16 15 17 16 CFLAGS=-g -Wall … … 38 37 39 38 prefix = @prefix@ 40 # prefix = /apps/rappture/2007021641 39 exec_prefix = ${prefix} 42 40 libdir = ${exec_prefix}/lib … … 80 78 ln -s $@ $(SHAREDLIBM) 81 79 82 #test$(EXE): test.o $(LIBS)83 # $(CXX) $(CFLAGS) -o $@ test.o $(LDFLAGS)84 85 80 test(EXE): test.o Ptr.o Outcome.o Lookup.o 86 81 $(CXX) $(CFLAGS) -o $@ test.o Ptr.o Outcome.o Lookup.o … … 90 85 $(CXX) $(CFLAGS) -o $@ RpBuffer_test.o $(LDFLAGS) -L ${libdir} -lz -lb64 91 86 92 install: $(LIBS) 87 install: $(LIBS) $(SHAREDLIBV) 93 88 -@if [ ! -d $(exec_prefix) ]; then mkdir -p $(exec_prefix); fi 94 89 -@if [ ! -d $(includedir) ]; then mkdir -p $(includedir); fi … … 104 99 chmod 644 $(includedir)/RpBuffer.h 105 100 cp $(LIBS) $(libdir) 101 cp $(SHAREDLIBV) $(libdir) 106 102 cd $(libdir); chmod 755 $(LIBS) 107 103 -@(cd $(libdir); $(RANLIB) librappture2.a || true) >/dev/null 2>&1 -
trunk/src2/core/RpBuffer.cc
r575 r583 93 93 SimpleBuffer::SimpleBuffer(const SimpleBuffer& b) 94 94 { 95 _buf = NULL; 96 _pos =0; 97 _size = b._size; 98 _spaceAvl = b._spaceAvl; 99 _shallow = false; 100 _fileState = true; 101 102 _buf = new char[b._spaceAvl]; 103 memcpy((void*) _buf, (void*) b._buf, (size_t) b._spaceAvl); 95 bufferInit(); 96 append(b.bytes(),b.size()); 104 97 } 105 98 … … 114 107 bufferFree(); 115 108 bufferInit(); 116 117 _buf = new char[b._spaceAvl]; 118 memcpy((void*) _buf, (void*) b._buf, (size_t) b._spaceAvl); 119 120 _pos = b._pos; 121 _size = b._size; 122 _spaceAvl = b._spaceAvl; 123 109 append(b.bytes(),b.size()); 110 return *this; 111 } 112 113 114 /** 115 * Operator + 116 * @param SimpleBuffer object to add 117 * @param SimpleBuffer object to add 118 */ 119 SimpleBuffer 120 SimpleBuffer::operator+(const SimpleBuffer& b) const 121 { 122 SimpleBuffer newBuffer(*this); 123 newBuffer.operator+=(b); 124 return newBuffer; 125 } 126 127 128 /** 129 * Operator += 130 * @param SimpleBuffer object to add 131 */ 132 SimpleBuffer& 133 SimpleBuffer::operator+=(const SimpleBuffer& b) 134 { 135 append(b.bytes(),b.size()); 124 136 return *this; 125 137 } … … 185 197 char *newBuffer = NULL; 186 198 199 // User specified NULL buffer to append 200 if (bytes == NULL) { 201 return 0; 202 } 203 187 204 if (nbytes == -1) { 188 205 // user signaled null terminated string … … 195 212 } 196 213 214 // Empty internal buffer, make sure its properly initialized. 197 215 if (_buf == NULL) { 198 _size = 0; 199 _spaceAvl = 0; 216 bufferInit(); 200 217 } 201 218 … … 243 260 */ 244 261 int 245 SimpleBuffer::read(c har* bytes, int nbytes)262 SimpleBuffer::read(const char* bytes, int nbytes) 246 263 { 247 264 int bytesRead = 0; 248 265 266 // SimpleBuffer is empty. 249 267 if (_buf == NULL) { 250 268 return 0; 251 269 } 252 270 253 // make sure we dont read off the end of our buffer 271 // User specified NULL buffer. 272 if (bytes == NULL) { 273 return 0; 274 } 275 276 // make sure we don't read off the end of our buffer 254 277 if ( (_pos + nbytes) >= _size) { 255 278 bytesRead = (_size - _pos); … … 264 287 265 288 if (bytesRead > 0) { 266 memcpy((void*) bytes, (void*) _buf, (size_t) bytesRead);289 memcpy((void*) bytes, (void*) (_buf+_pos), (size_t) bytesRead); 267 290 } 268 291 … … 270 293 271 294 return bytesRead; 295 } 296 297 298 /** 299 * Set buffer position indicator to spot within the buffer 300 * @param Offset from whence location in buffer. 301 * @param Place from where offset is added or subtracted. 302 * @return 0 on success, anything else is failure 303 */ 304 int 305 SimpleBuffer::seek(int offset, int whence) 306 { 307 int retVal = 0; 308 309 if (_buf == NULL) { 310 return -1 ; 311 } 312 313 if (whence == SEEK_SET) { 314 if (offset < 0) { 315 /* dont go off the beginning of data */ 316 _pos = 0; 317 } 318 else if (offset >= _size) { 319 /* dont go off the end of data */ 320 _pos = _size - 1; 321 } 322 else { 323 _pos = _pos + offset; 324 } 325 } 326 else if (whence == SEEK_CUR) { 327 if ( (_pos + offset) < 0) { 328 /* dont go off the beginning of data */ 329 _pos = 0; 330 } 331 else if ((_pos + offset) >= _size) { 332 /* dont go off the end of data */ 333 _pos = _size - 1; 334 } 335 else { 336 _pos = _pos + offset; 337 } 338 } 339 else if (whence == SEEK_END) { 340 if (offset <= (-1*_size)) { 341 /* dont go off the beginning of data */ 342 _pos = 0; 343 } 344 else if (offset >= 0) { 345 /* dont go off the end of data */ 346 _pos = _size - 1; 347 } 348 else { 349 _pos = (_size - 1) + offset; 350 } 351 } 352 else { 353 retVal = -1; 354 } 355 356 return retVal; 357 } 358 359 360 /** 361 * Tell caller the offset of the position indicator from the start of buffer 362 * @return Number of bytes the position indicator is from start of buffer 363 */ 364 int 365 SimpleBuffer::tell() 366 { 367 return _pos; 368 } 369 370 371 /** 372 * Read data from the buffer into a memory location provided by caller 373 */ 374 SimpleBuffer& 375 SimpleBuffer::rewind() 376 { 377 _pos = 0; 378 return *this; 272 379 } 273 380 … … 305 412 { 306 413 return (_pos >= _size); 307 }308 309 310 /**311 * Use this SimpleBuffer as a temporary pointer to another SimpleBuffer312 * This SimpleBuffer does not own the data that it points to, thus313 * when clear() or operator=() is called, the data is not deleted,314 * but the _buf value is changed. This could lead to a memory leak if315 * used improperly.316 * @param Pointer location memory to temporarily own.317 * @param number of bytes used in the buffer.318 * @param number of total bytes in the allocated buffer.319 * @return True or false boolean value.320 */321 SimpleBuffer&322 SimpleBuffer::shallowCopy(char* bytes, int nBytes, int spaceAvl)323 {324 bufferFree();325 326 _buf = bytes;327 _pos = 0;328 _size = nBytes;329 _spaceAvl = spaceAvl;330 _shallow = true;331 _fileState = true;332 333 return *this;334 414 } 335 415 … … 345 425 SimpleBuffer::move(SimpleBuffer& b) 346 426 { 347 b .bufferFree();348 349 b._buf =_buf;350 b._pos = 0;351 b._size =_size;352 b._spaceAvl =_spaceAvl;353 b._shallow = _shallow;354 355 b ufferInit();427 bufferFree(); 428 429 _buf = b._buf; 430 _pos = b._pos; 431 _size = b._size; 432 _spaceAvl = b._spaceAvl; 433 _fileState = b._fileState; 434 435 b.bufferInit(); 356 436 357 437 return *this; … … 371 451 _size = 0; 372 452 _spaceAvl = 0; 373 _shallow = false;374 453 _fileState = true; 375 454 } … … 383 462 SimpleBuffer::bufferFree() 384 463 { 385 if (_shallow == false) { 386 if (_buf != NULL) { 387 delete [] _buf; 388 _buf = NULL; 389 } 464 if (_buf != NULL) { 465 delete [] _buf; 466 _buf = NULL; 390 467 } 391 468 bufferInit(); … … 422 499 */ 423 500 Buffer::Buffer(const Buffer& b) 424 : _level(b._level), 501 : SimpleBuffer(b), 502 _level(b._level), 425 503 _compressionType(b._compressionType), 426 504 _windowBits(b._windowBits) 427 { 428 bufferInit(); 429 430 _buf = new char[b._spaceAvl]; 431 memcpy((void*) _buf, (void*) b._buf, (size_t) b._spaceAvl); 432 _size = b._size; 433 _spaceAvl = b._spaceAvl; 434 } 505 {} 435 506 436 507 /** … … 441 512 Buffer::operator=(const Buffer& b) 442 513 { 443 bufferFree(); 444 bufferInit(); 445 446 _buf = new char[b._spaceAvl]; 447 memcpy((void*) _buf, (void*) b._buf, (size_t) b._spaceAvl); 448 _pos = b._pos; 449 _size = b._size; 450 _spaceAvl = b._spaceAvl; 514 SimpleBuffer::operator=(b); 451 515 452 516 _level = b._level; 453 517 _compressionType = b._compressionType; 454 518 _windowBits = b._windowBits; 455 _fileState = b._fileState;456 519 457 520 return *this; … … 459 522 460 523 524 Buffer 525 Buffer::operator+(const Buffer& b) const 526 { 527 Buffer newBuffer(*this); 528 newBuffer.operator+=(b); 529 return newBuffer; 530 } 531 532 461 533 Buffer& 462 Buffer::operator=(const SimpleBuffer& b) 463 { 464 bufferFree(); 465 bufferInit(); 466 467 _buf = new char[b.size()]; 468 memcpy((void*) _buf, (void*) b.bytes(), (size_t) b.size()); 469 _pos = 0; 470 _size = b.size(); 471 _spaceAvl = b.size(); 472 473 _level = 6; 474 _compressionType = RPCOMPRESS_GZIP; 475 _windowBits = 15; 476 _fileState = true; 477 534 Buffer::operator+=(const Buffer& b) 535 { 536 SimpleBuffer::operator+=(b); 478 537 return *this; 479 538 } … … 538 597 } 539 598 540 outFile.write( _buf,_size);599 outFile.write(bytes(),size()); 541 600 outFile.close(); 542 601 … … 555 614 err.addContext("Rappture::Buffer::encode()"); 556 615 557 bin.shallowCopy(_buf, _size, _spaceAvl);616 rewind(); 558 617 559 618 if (compress) { 560 do_compress(err, bin,bout);619 do_compress(err,*this,bout); 561 620 } 562 621 563 622 if (base64) { 564 623 if (compress) { 565 bout.move(bin); 566 } 567 do_base64_enc(err,bin,bout); 624 bin.move(bout); 625 do_base64_enc(err,bin,bout); 626 } 627 else { 628 do_base64_enc(err,*this,bout); 629 } 568 630 } 569 631 570 632 if (!err) { 571 633 // write the encoded data to the internal buffer 572 bufferFree(); 573 operator=(bout); 634 move(bout); 574 635 } 575 636 … … 585 646 SimpleBuffer bout; 586 647 587 bin.shallowCopy(_buf, _size, _spaceAvl);648 rewind(); 588 649 589 650 if (base64) { 590 do_base64_dec(err, bin,bout);651 do_base64_dec(err,*this,bout); 591 652 } 592 653 593 654 if (decompress) { 594 655 if (base64) { 595 bout.move(bin); 596 } 597 do_decompress(err,bin,bout); 656 bin.move(bout); 657 do_decompress(err,bin,bout); 658 } 659 else { 660 do_decompress(err,*this,bout); 661 } 598 662 } 599 663 600 664 if (!err) { 601 665 // write the decoded data to the internal buffer 602 bufferFree(); 603 operator=(bout); 666 move(bout); 604 667 } 605 668 … … 609 672 610 673 void 611 Buffer::do_compress(Outcome& status, SimpleBuffer& bin, SimpleBuffer& bout) 612 { 613 int ret, flush; 614 unsigned have; 674 Buffer::do_compress( Outcome& status, 675 SimpleBuffer& bin, 676 SimpleBuffer& bout ) 677 { 678 int ret=0, flush=0; 679 unsigned have=0; 615 680 z_stream strm; 616 681 … … 638 703 do { 639 704 strm.avail_in = bin.read(in, CHUNK); 640 if (b ad() == true) {705 if (bin.bad() == true) { 641 706 (void)deflateEnd(&strm); 642 707 // return Z_ERRNO; … … 646 711 } 647 712 flush = bin.eof() ? Z_FINISH : Z_NO_FLUSH; 648 strm.next_in = ( unsigned char*) in;713 strm.next_in = (Bytef*) in; 649 714 /* run deflate() on input until output buffer not full, finish 650 715 compression if all of source has been read in */ 651 716 do { 652 717 strm.avail_out = CHUNK; 653 strm.next_out = ( unsigned char*) out;718 strm.next_out = (Bytef*) out; 654 719 ret = deflate(&strm, flush); /* no bad return value */ 655 720 assert(ret != Z_STREAM_ERROR); /* state not clobbered */ … … 681 746 682 747 void 683 Buffer::do_decompress(Outcome& status, SimpleBuffer& bin, SimpleBuffer& bout) 748 Buffer::do_decompress( Outcome& status, 749 SimpleBuffer& bin, 750 SimpleBuffer& bout ) 684 751 { 685 752 int ret; … … 709 776 do { 710 777 strm.avail_in = bin.read(in, CHUNK); 711 if (b ad() == true) {778 if (bin.bad() == true) { 712 779 (void)inflateEnd(&strm); 713 780 // return Z_ERRNO; … … 761 828 void 762 829 Buffer::do_base64_enc( Outcome& status, 763 SimpleBuffer& bin,830 const SimpleBuffer& bin, 764 831 SimpleBuffer& bout ) 765 832 { … … 782 849 void 783 850 Buffer::do_base64_dec( Outcome& status, 784 SimpleBuffer& bin,851 const SimpleBuffer& bin, 785 852 SimpleBuffer& bout ) 786 853 { -
trunk/src2/core/RpBuffer.h
r575 r583 86 86 SimpleBuffer(const SimpleBuffer& b); 87 87 SimpleBuffer& operator=(const SimpleBuffer& b); 88 SimpleBuffer operator+(const SimpleBuffer& b) const; 89 SimpleBuffer& operator+=(const SimpleBuffer& b); 88 90 virtual ~SimpleBuffer(); 89 91 … … 93 95 SimpleBuffer& clear(); 94 96 int append(const char* bytes, int nbytes=-1); 95 int read(char* bytes, int nbytes); 97 int read(const char* bytes, int nbytes); 98 int seek(int offset, int whence); 99 int tell(); 100 SimpleBuffer& rewind(); 96 101 97 102 bool good() const; … … 99 104 bool eof() const; 100 105 101 SimpleBuffer& shallowCopy(char* bytes, int nBytes, int spaceAvl);102 106 SimpleBuffer& move(SimpleBuffer& b); 103 107 104 108 protected: 109 110 void bufferInit(); 111 void bufferFree(); 112 113 private: 114 105 115 /// Pointer to the memory that holds our buffer's data 106 116 char* _buf; … … 115 125 int _spaceAvl; 116 126 117 /// Shallow copy flag.118 bool _shallow;119 120 127 /// State of the last file like operation. 121 128 bool _fileState; 122 123 void bufferInit();124 void bufferFree();125 129 }; 126 130 … … 137 141 Buffer(const char* bytes, int nbytes=-1); 138 142 Buffer(const Buffer& buffer); 139 Buffer& operator=(const Buffer& buffer); 143 Buffer& operator=(const Buffer& b); 144 Buffer operator+(const Buffer& b) const; 145 Buffer& operator+=(const Buffer& b); 140 146 virtual ~Buffer(); 141 147 … … 158 164 enum { CHUNK = 4096 }; 159 165 160 Buffer& operator=(const SimpleBuffer& buffer); 161 void do_compress(Outcome& status,SimpleBuffer& bin,SimpleBuffer& bout); 162 void do_decompress(Outcome& status,SimpleBuffer& bin,SimpleBuffer& bout); 163 void do_base64_enc(Outcome& status,SimpleBuffer& bin,SimpleBuffer& bout); 164 void do_base64_dec(Outcome& status,SimpleBuffer& bin,SimpleBuffer& bout); 166 void do_compress( Outcome& status, 167 SimpleBuffer& bin, 168 SimpleBuffer& bout ); 169 void do_decompress( Outcome& status, 170 SimpleBuffer& bin, 171 SimpleBuffer& bout ); 172 void do_base64_enc( Outcome& status, 173 const SimpleBuffer& bin, 174 SimpleBuffer& bout ); 175 void do_base64_dec( Outcome& status, 176 const SimpleBuffer& bin, 177 SimpleBuffer& bout ); 165 178 }; 166 179 -
trunk/src2/core/RpBuffer_test.cc
r575 r583 449 449 return (tests == passed); 450 450 } 451 452 passed++; 453 /* =========================================================== */ 454 455 return (tests == passed); 456 } 457 458 int testFile() 459 { 460 int passed = 0; 461 int tests = 0; 462 463 464 /* =========================================================== */ 465 tests++; 466 const char* inFile = "out.dx"; 467 const char* outFile = "out.dx.gz"; 468 Rappture::Buffer buffer1; 469 buffer1.load(inFile); 470 471 // compress, dont encode 472 buffer1.encode(true,false); 473 std::remove(outFile); 474 buffer1.dump(outFile); 475 buffer1.decode(true,false); 476 477 /* 478 if (size1before != 26) { 479 std::cout << "Error testClear1" << std::endl; 480 std::cout << "incorrect buffer size" << std::endl; 481 return (tests == passed); 482 } 483 if (size1after != 0) { 484 std::cout << "Error testClear1" << std::endl; 485 std::cout << "clear failed buffer size" << std::endl; 486 return (tests == passed); 487 } 488 */ 451 489 452 490 passed++; … … 468 506 passed += testLoad(); 469 507 passed += testClear(); 470 471 if (passed != 8) { 508 passed += testFile(); 509 510 if (passed != 9) { 472 511 printf("failed: %d\n", passed); 473 512 }
Note: See TracChangeset
for help on using the changeset viewer.