- Timestamp:
- Jun 18, 2008, 6:51:34 AM (16 years ago)
- Location:
- trunk/src/core
- Files:
-
- 2 added
- 1 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/Makefile.in
r1041 r1051 49 49 RpBufferCInterface.h \ 50 50 RpDict.h \ 51 RpDXWriter.h \ 51 52 RpEncode.h \ 52 53 RpEntityRef.h \ … … 114 115 RpBuffer.o \ 115 116 RpBufferCInterface.o \ 117 RpDXWriter.o \ 116 118 RpEncode.o \ 117 119 RpEntityRef.o \ … … 124 126 RpPtr.o \ 125 127 RpResult.o \ 126 RpSimpleBuffer.o \127 128 RpUnits.o \ 128 129 RpUnitsCInterface.o \ … … 133 134 RpUtilsFInterface.o \ 134 135 $(SCEW_OBJS) \ 135 $(B64_OBJS) 136 $(B64_OBJS) 136 137 137 138 name = rappture … … 144 145 $(lib): $(OBJS) 145 146 $(RM) $@ 146 $(AR) -r $@ $(OBJS) 147 $(AR) -r $@ $(OBJS) 147 148 $(RANLIB) $@ 148 149 149 150 $(shared_lib): $(OBJS) 150 151 $(SHLIB_LD) $(SHLIB_LDFLAGS) -o $@ $(OBJS) \ 151 $(LIB_SEARCH_DIRS) $(LIBS) 152 $(LIB_SEARCH_DIRS) $(LIBS) 152 153 153 154 install: install_libs install_headers … … 155 156 install_libs: $(lib) $(shared_lib) 156 157 $(INSTALL) -m 444 $(lib) $(libdir) 157 $(INSTALL) -m 555 $(shared_lib) $(libdir) 158 $(INSTALL) -m 555 $(shared_lib) $(libdir) 158 159 159 install_headers: 160 install_headers: 160 161 $(INSTALL) -d $(incdir) 161 162 for i in $(HEADERS) ; do \ … … 163 164 done 164 165 165 .cc.o: 166 $(CXX) $(CXX_SWITCHES) -c $? 166 .cc.o: 167 $(CXX) $(CXX_SWITCHES) -c $? 167 168 .c.o: 168 $(CC) $(CC_SWITCHES)-c $? 169 $(CC) $(CC_SWITCHES)-c $? 169 170 170 clean: 171 clean: 171 172 $(RM) $(OBJS) $(lib) $(shared_lib) 172 173 -
trunk/src/core/RpBuffer.cc
r1038 r1051 30 30 */ 31 31 Buffer::Buffer() 32 : Simple Buffer(),32 : SimpleCharBuffer(), 33 33 _level(6), 34 34 _compressionType(RPCOMPRESS_GZIP), … … 41 41 */ 42 42 Buffer::Buffer(int nbytes) 43 : Simple Buffer(nbytes),43 : SimpleCharBuffer(nbytes), 44 44 _level(6), 45 45 _compressionType(RPCOMPRESS_GZIP), … … 55 55 */ 56 56 Buffer::Buffer(const char* bytes, int nbytes) 57 : Simple Buffer(bytes,nbytes),57 : SimpleCharBuffer(bytes,nbytes), 58 58 _level(6), 59 59 _compressionType(RPCOMPRESS_GZIP), … … 66 66 */ 67 67 Buffer::Buffer(const Buffer& b) 68 : Simple Buffer(b),68 : SimpleCharBuffer(b), 69 69 _level(b._level), 70 70 _compressionType(b._compressionType), … … 79 79 Buffer::operator=(const Buffer& b) 80 80 { 81 Simple Buffer::operator=(b);81 SimpleCharBuffer::operator=(b); 82 82 83 83 _level = b._level; … … 101 101 Buffer::operator+=(const Buffer& b) 102 102 { 103 Simple Buffer::operator+=(b);103 SimpleCharBuffer::operator+=(b); 104 104 return *this; 105 105 } … … 176 176 { 177 177 Outcome err; 178 Simple Buffer bin;179 Simple Buffer bout;178 SimpleCharBuffer bin; 179 SimpleCharBuffer bout; 180 180 181 181 if ((base64 == 0) && (compress == 0)) { … … 216 216 { 217 217 Outcome err; 218 Simple Buffer bin;219 Simple Buffer bout;218 SimpleCharBuffer bin; 219 SimpleCharBuffer bout; 220 220 221 221 if ((base64 == 0) && (decompress == 0)) { … … 254 254 void 255 255 Buffer::do_compress( Outcome& status, 256 Simple Buffer& bin,257 Simple Buffer& bout )256 SimpleCharBuffer& bin, 257 SimpleCharBuffer& bout ) 258 258 { 259 259 int ret=0, flush=0; … … 328 328 void 329 329 Buffer::do_decompress( Outcome& status, 330 Simple Buffer& bin,331 Simple Buffer& bout )330 SimpleCharBuffer& bin, 331 SimpleCharBuffer& bout ) 332 332 { 333 333 int ret; … … 409 409 void 410 410 Buffer::do_base64_enc( Outcome& status, 411 const Simple Buffer& bin,412 Simple Buffer& bout )411 const SimpleCharBuffer& bin, 412 SimpleCharBuffer& bout ) 413 413 { 414 414 int tBufSize = 0; … … 421 421 tBufSize += E.encode_end(tBuf+tBufSize); 422 422 423 bout = Simple Buffer(tBuf,tBufSize);423 bout = SimpleCharBuffer(tBuf,tBufSize); 424 424 delete [] tBuf; 425 425 … … 430 430 void 431 431 Buffer::do_base64_dec( Outcome& status, 432 const Simple Buffer& bin,433 Simple Buffer& bout )432 const SimpleCharBuffer& bin, 433 SimpleCharBuffer& bout ) 434 434 { 435 435 int tBufSize = 0; … … 441 441 tBufSize = D.decode(bin.bytes(),bin.size(),tBuf); 442 442 443 bout = Simple Buffer(tBuf,tBufSize);443 bout = SimpleCharBuffer(tBuf,tBufSize); 444 444 delete [] tBuf; 445 445 -
trunk/src/core/RpBuffer.h
r1038 r1051 18 18 #include <RpSimpleBuffer.h> 19 19 20 20 21 #ifdef __cplusplus 21 22 extern "C" { … … 29 30 namespace Rappture { 30 31 31 32 32 /** 33 33 * Block of memory that dynamically resizes itself as needed. … … 37 37 */ 38 38 39 class Buffer : public Simple Buffer{39 class Buffer : public SimpleCharBuffer{ 40 40 public: 41 41 Buffer(); … … 67 67 68 68 void do_compress( Outcome& status, 69 Simple Buffer& bin,70 Simple Buffer& bout );69 SimpleCharBuffer& bin, 70 SimpleCharBuffer& bout ); 71 71 void do_decompress( Outcome& status, 72 Simple Buffer& bin,73 Simple Buffer& bout );72 SimpleCharBuffer& bin, 73 SimpleCharBuffer& bout ); 74 74 void do_base64_enc( Outcome& status, 75 const Simple Buffer& bin,76 Simple Buffer& bout );75 const SimpleCharBuffer& bin, 76 SimpleCharBuffer& bout ); 77 77 void do_base64_dec( Outcome& status, 78 const Simple Buffer& bin,79 Simple Buffer& bout );78 const SimpleCharBuffer& bin, 79 SimpleCharBuffer& bout ); 80 80 }; 81 81 -
trunk/src/core/RpEncode.cc
r1018 r1051 13 13 */ 14 14 #include "RpEncode.h" 15 #include <cstring> 15 16 16 17 #ifdef __cplusplus -
trunk/src/core/RpOutcome.cc
r1018 r1051 12 12 #include "RpOutcome.h" 13 13 #include <stdarg.h> 14 #include <stdlib.h> 14 15 using namespace Rappture; 15 16 -
trunk/src/core/RpSimpleBuffer.h
r1038 r1051 65 65 66 66 #include <fstream> 67 68 #define RP_SIMPLEBUFFER_MIN_SIZE 256 69 70 #ifdef __cplusplus 71 extern "C" { 72 #endif // ifdef __cplusplus 73 67 #include <cstring> 68 #include <cstdlib> 74 69 75 70 namespace Rappture { 76 71 72 template <class T> 77 73 class SimpleBuffer { 78 74 public: 79 75 SimpleBuffer(); 80 SimpleBuffer( int nbytes);81 SimpleBuffer(const char* bytes, int nbytes=-1);76 SimpleBuffer(size_t nmemb); 77 SimpleBuffer(const T* bytes, int nmemb=-1); 82 78 SimpleBuffer(const SimpleBuffer& b); 83 SimpleBuffer & operator=(const SimpleBuffer& b);84 SimpleBuffer operator+(const SimpleBuffer& b) const;85 SimpleBuffer & operator+=(const SimpleBuffer& b);79 SimpleBuffer<T>& operator=(const SimpleBuffer<T>& b); 80 SimpleBuffer operator+(const SimpleBuffer& b) const; 81 SimpleBuffer<T>& operator+=(const SimpleBuffer<T>& b); 86 82 virtual ~SimpleBuffer(); 87 83 88 const char* bytes() const; 89 unsigned int size() const; 90 91 SimpleBuffer& clear(); 92 int append(const char* bytes, int nbytes=-1); 93 int read(const char* bytes, int nbytes); 94 int seek(int offset, int whence); 84 const T* bytes() const; 85 size_t size() const; 86 size_t nmemb() const; 87 88 SimpleBuffer<T>& clear(); 89 int append(const T* bytes, int nmemb=-1); 90 size_t read(const T* bytes, size_t nmemb); 91 int seek(long offset, int whence); 95 92 int tell() const; 96 SimpleBuffer& rewind(); 93 size_t set(size_t nbytes); 94 SimpleBuffer<T>& rewind(); 95 SimpleBuffer<T>& show(); 97 96 98 97 bool good() const; … … 100 99 bool eof() const; 101 100 102 SimpleBuffer & move(SimpleBuffer& b);101 SimpleBuffer<T>& move(SimpleBuffer<T>& b); 103 102 104 103 protected: … … 110 109 111 110 /// Pointer to the memory that holds our buffer's data 112 char* _buf;111 T* _buf; 113 112 114 113 /// Position offset within the buffer's memory 115 unsigned int _pos;116 117 /// Size of the used memoryin the buffer118 unsigned int _size;119 120 /// Total spaceavailable in the buffer121 unsigned int _spaceAvl;114 size_t _pos; 115 116 /// Number of members stored in the buffer 117 size_t _nMembStored; 118 119 /// Total number of members available in the buffer 120 size_t _nMembAvl; 122 121 123 122 /// State of the last file like operation. 124 123 bool _fileState; 124 125 /// Minimum number of members is set to the number you can fit in 256 bytes 126 const static int _minMembCnt=(256/sizeof(T)); 127 128 size_t __guesslen(const T* bytes); 129 125 130 }; 126 131 132 typedef SimpleBuffer<char> SimpleCharBuffer; 133 typedef SimpleBuffer<float> SimpleFloatBuffer; 134 typedef SimpleBuffer<double> SimpleDoubleBuffer; 135 136 /** 137 * Construct an empty SimpleBuffer. 138 */ 139 template<class T> 140 SimpleBuffer<T>::SimpleBuffer() 141 { 142 bufferInit(); 143 } 144 145 146 /** 147 * Construct an empty SimpleBuffer of specified size. 148 */ 149 template<class T> 150 SimpleBuffer<T>::SimpleBuffer(size_t nmemb) 151 { 152 bufferInit(); 153 154 if (nmemb == 0) { 155 // ignore requests for sizes equal to zero 156 return; 157 } 158 159 // buffer sizes less than min_size are set to min_size 160 if (nmemb < (size_t) _minMembCnt) { 161 nmemb = _minMembCnt; 162 } 163 164 if (set(nmemb) != nmemb) { 165 return; 166 } 167 _nMembStored = nmemb; 168 } 169 170 171 /** 172 * Construct a SimpleBuffer loaded with initial data. 173 * 174 * @param bytes pointer to bytes being stored. 175 * @param nbytes number of bytes being stored. 176 */ 177 template<class T> 178 SimpleBuffer<T>::SimpleBuffer(const T* bytes, int nmemb) 179 { 180 bufferInit(); 181 append(bytes,nmemb); 182 } 183 184 185 /** 186 * Copy constructor 187 * @param SimpleBuffer object to copy 188 */ 189 template<class T> 190 SimpleBuffer<T>::SimpleBuffer(const SimpleBuffer<T>& b) 191 { 192 bufferInit(); 193 append(b.bytes(),b.nmemb()); 194 } 195 196 197 /** 198 * Assignment operator 199 * @param SimpleBuffer object to copy 200 */ 201 template<class T> 202 SimpleBuffer<T>& 203 SimpleBuffer<T>::operator=(const SimpleBuffer<T>& b) 204 { 205 bufferFree(); 206 bufferInit(); 207 append(b.bytes(),b.nmemb()); 208 return *this; 209 } 210 211 212 /** 213 * Operator + 214 * @param SimpleBuffer object to add 215 */ 216 template<class T> 217 SimpleBuffer<T> 218 SimpleBuffer<T>::operator+(const SimpleBuffer<T>& b) const 219 { 220 SimpleBuffer<T> newBuffer(*this); 221 newBuffer.operator+=(b); 222 return newBuffer; 223 } 224 225 226 /** 227 * Operator += 228 * @param SimpleBuffer object to add 229 */ 230 template<class T> 231 SimpleBuffer<T>& 232 SimpleBuffer<T>::operator+=(const SimpleBuffer<T>& b) 233 { 234 append(b.bytes(),b.nmemb()); 235 return *this; 236 } 237 238 239 /** 240 * Destructor 241 */ 242 template<class T> 243 SimpleBuffer<T>::~SimpleBuffer() 244 { 245 bufferFree(); 246 } 247 248 249 /** 250 * Get the bytes currently stored in the buffer. These bytes can 251 * be stored, and used later to construct another Buffer to 252 * decode the information. 253 * 254 * @return Pointer to the bytes in the buffer. 255 */ 256 template<class T> 257 const T* 258 SimpleBuffer<T>::bytes() const 259 { 260 return _buf; 261 } 262 263 264 /** 265 * Get the number of bytes currently stored in the buffer. 266 * @return Number of the bytes used in the buffer. 267 */ 268 template<class T> 269 size_t 270 SimpleBuffer<T>::size() const 271 { 272 return _nMembStored*sizeof(T); 273 } 274 275 276 /** 277 * Get the number of members currently stored in the buffer. 278 * @return Number of the members used in the buffer. 279 */ 280 template<class T> 281 size_t 282 SimpleBuffer<T>::nmemb() const 283 { 284 return _nMembStored; 285 } 286 287 288 /** 289 * Clear the buffer, making it empty. 290 * @return Reference to this buffer. 291 */ 292 template<class T> 293 SimpleBuffer<T>& 294 SimpleBuffer<T>::clear() 295 { 296 bufferFree(); 297 bufferInit(); 298 299 return *this; 300 } 301 302 /** 303 * guess the length of a null terminated character buffer 304 * @param pointer to the buffer to guess the length of 305 * @return a guess of the length of the buffer. 306 */ 307 template<> inline 308 size_t 309 SimpleBuffer<char>::__guesslen(const char* bytes) 310 { 311 return strlen(bytes); 312 } 313 314 /** 315 * guess the length of a non-null terminated character buffer 316 * @param pointer to the buffer to guess the length of 317 * @return a guess of the length of the buffer. 318 */ 319 template<class T> 320 size_t 321 SimpleBuffer<T>::__guesslen(const T* bytes) 322 { 323 return (sizeof(T)); 324 } 325 326 /** 327 * Append bytes to the end of this buffer 328 * @param pointer to bytes to be added 329 * @param number of bytes to be added 330 * @return number of bytes appended. 331 */ 332 template<class T> 333 int 334 SimpleBuffer<T>::append(const T* bytes, int nmemb) 335 { 336 size_t newMembCnt = 0; 337 size_t nbytes = 0; 338 339 void* dest = NULL; 340 void const* src = NULL; 341 size_t size = 0; 342 343 // User specified NULL buffer to append 344 if ( (bytes == NULL) && (nmemb < 1) ) { 345 return 0; 346 } 347 348 if (nmemb == -1) { 349 // user signaled null terminated string 350 // or that we should make an educated guess 351 // at the length of the object. 352 nbytes = __guesslen(bytes); 353 nmemb = nbytes*sizeof(T); 354 } 355 356 if (nmemb <= 0) { 357 // no data written, invalid option 358 return nmemb; 359 } 360 361 newMembCnt = (size_t)(_nMembStored + nmemb); 362 363 if (newMembCnt > _nMembAvl) { 364 365 // buffer sizes less than min_size are set to min_size 366 if (newMembCnt < (size_t) _minMembCnt) { 367 newMembCnt = (size_t) _minMembCnt; 368 } 369 370 /* 371 * Allocate a larger buffer for the string if the current one isn't 372 * large enough. Allocate extra space in the new buffer so that there 373 * will be room to grow before we have to allocate again. 374 */ 375 size_t membAvl; 376 membAvl = (_nMembAvl > 0) ? _nMembAvl : _minMembCnt; 377 while (newMembCnt > membAvl) { 378 membAvl += membAvl; 379 } 380 381 /* 382 * reallocate to a larger buffer 383 */ 384 if (set(membAvl) != membAvl) { 385 return 0; 386 } 387 } 388 389 dest = (void*) (_buf + _nMembStored); 390 src = (void const*) bytes; 391 size = (size_t) (nmemb*sizeof(T)); 392 memcpy(dest,src,size); 393 394 _nMembStored += nmemb; 395 396 return nmemb; 397 } 398 399 400 template<class T> 401 size_t 402 SimpleBuffer<T>::set(size_t nmemb) 403 { 404 T *buf; 405 size_t nbytes = nmemb*sizeof(T); 406 407 if (_buf == NULL) { 408 buf = (T*) malloc(nbytes); 409 } else { 410 buf = (T*) realloc((void*) _buf, nbytes); 411 } 412 413 if (buf == NULL) { 414 fprintf(stderr,"Can't allocate %i bytes of memory\n",nbytes); 415 _fileState = false; 416 return 0; 417 } 418 _buf = buf; 419 _nMembAvl = nmemb; 420 return _nMembAvl; 421 } 422 423 424 template<class T> 425 SimpleBuffer<T>& 426 SimpleBuffer<T>::show() 427 { 428 size_t curMemb = 0; 429 430 while (curMemb != _nMembStored) { 431 fprintf(stdout,"_buf[%d] = :%#x:\n",curMemb,_buf[curMemb]); 432 curMemb += 1; 433 } 434 fprintf(stdout,"_nMembAvl = :%d:\n",_nMembAvl); 435 436 return *this; 437 } 438 439 440 /** 441 * Read data from the buffer into a memory location provided by caller 442 * @param Pointer locating where to place read bytes. 443 * @param Size of the memory location. 444 * @return Number of bytes written to memory location 445 */ 446 template<class T> 447 size_t 448 SimpleBuffer<T>::read(const T* bytes, size_t nmemb) 449 { 450 size_t nMembRead = 0; 451 452 void* dest = NULL; 453 void const* src = NULL; 454 size_t size = 0; 455 456 // SimpleBuffer is empty. 457 if (_buf == NULL) { 458 return 0; 459 } 460 461 // User specified NULL buffer. 462 if (bytes == NULL) { 463 return 0; 464 } 465 466 // make sure we don't read off the end of our buffer 467 if ( (_pos + nmemb) > _nMembStored ) { 468 nMembRead = _nMembStored - _pos; 469 } 470 else { 471 nMembRead = nmemb; 472 } 473 474 if (nMembRead <= 0) { 475 return 0; 476 } 477 478 dest = (void*) bytes; 479 src = (void const*) (_buf + _pos); 480 size = nMembRead*sizeof(T); 481 memcpy(dest,src,size); 482 483 _pos = (_pos + nMembRead); 484 485 return nMembRead; 486 } 487 488 489 /** 490 * Set buffer position indicator to spot within the buffer 491 * @param Offset from whence location in buffer. 492 * @param Place from where offset is added or subtracted. 493 * @return 0 on success, anything else is failure 494 */ 495 template<class T> 496 int 497 SimpleBuffer<T>::seek(long offset, int whence) 498 { 499 int retVal = 0; 500 501 if (_buf == NULL) { 502 return -1 ; 503 } 504 505 if (whence == SEEK_SET) { 506 if (offset < 0) { 507 /* dont go off the beginning of data */ 508 _pos = 0; 509 } 510 else if (offset >= (long)_nMembStored) { 511 /* dont go off the end of data */ 512 _pos = _nMembStored - 1; 513 } 514 else { 515 _pos = (size_t)(offset); 516 } 517 } 518 else if (whence == SEEK_CUR) { 519 if ( (_pos + offset) < 0) { 520 /* dont go off the beginning of data */ 521 _pos = 0; 522 } 523 else if ((_pos + offset) >= _nMembStored) { 524 /* dont go off the end of data */ 525 _pos = _nMembStored - 1; 526 } 527 else { 528 _pos = (size_t)(_pos + offset); 529 } 530 } 531 else if (whence == SEEK_END) { 532 if (offset <= (long)(-1*_nMembStored)) { 533 /* dont go off the beginning of data */ 534 _pos = 0; 535 } 536 else if (offset >= 0) { 537 /* dont go off the end of data */ 538 _pos = _nMembStored - 1; 539 } 540 else { 541 _pos = (size_t)((_nMembStored - 1) + offset); 542 } 543 } 544 else { 545 retVal = -1; 546 } 547 548 return retVal; 549 } 550 551 552 /** 553 * Tell caller the offset of the position indicator from the start of buffer 554 * @return Number of bytes the position indicator is from start of buffer 555 */ 556 template<class T> 557 int 558 SimpleBuffer<T>::tell() const 559 { 560 return (int)_pos; 561 } 562 563 564 /** 565 * Read data from the buffer into a memory location provided by caller 566 */ 567 template<class T> 568 SimpleBuffer<T>& 569 SimpleBuffer<T>::rewind() 570 { 571 _pos = 0; 572 return *this; 573 } 574 575 576 /** 577 * Tell if the last file like operation (ie. read()) was successful 578 * or if there was a failure like eof, or bad memory 579 * @return True or false boolean value 580 */ 581 template<class T> 582 bool 583 SimpleBuffer<T>::good() const 584 { 585 return (_fileState); 586 } 587 588 589 /** 590 * Tell if the last file like operation (ie. read()) failed 591 * Opposite of good() 592 * @return True or false boolean value 593 */ 594 template<class T> 595 bool 596 SimpleBuffer<T>::bad() const 597 { 598 return (!_fileState); 599 } 600 601 602 /** 603 * Tell if the position flag is at the end of the buffer 604 * @return True or false boolean value 605 */ 606 template<class T> 607 bool 608 SimpleBuffer<T>::eof() const 609 { 610 return (_pos >= _nMembStored); 611 } 612 613 614 /** 615 * Move the data from this SimpleBuffer to the SimpleBuffer provided by 616 * the caller. All data except the _pos is moved and this SimpleBuffer is 617 * re-initialized with bufferInit(). 618 * @param SimpleBuffer to move the information to 619 * @return reference to this SimpleBuffer object. 620 */ 621 template<class T> 622 SimpleBuffer<T>& 623 SimpleBuffer<T>::move(SimpleBuffer<T>& b) 624 { 625 bufferFree(); 626 627 _buf = b._buf; 628 _pos = b._pos; 629 _fileState = b._fileState; 630 _nMembStored = b._nMembStored; 631 _nMembAvl = b._nMembAvl; 632 633 b.bufferInit(); 634 635 return *this; 636 } 637 638 639 /** 640 * Initializes a dynamic buffer, discarding any previous contents 641 * of the buffer. bufferFree() should have been called already 642 * if the dynamic buffer was previously in use. 643 */ 644 template<class T> 645 void 646 SimpleBuffer<T>::bufferInit() 647 { 648 _buf = NULL; 649 _pos = 0; 650 _fileState = true; 651 _nMembStored = 0; 652 _nMembAvl = 0; 653 } 654 655 656 /** 657 * Frees up any memory allocated for the dynamic buffer and 658 * reinitializes the buffer to an empty state. 659 */ 660 template<class T> 661 void 662 SimpleBuffer<T>::bufferFree() 663 { 664 if (_buf != NULL) { 665 free(_buf); 666 _buf = NULL; 667 } 668 bufferInit(); 669 } 670 127 671 } // namespace Rappture 128 129 #ifdef __cplusplus130 }131 #endif // ifdef __cplusplus132 672 133 673 #endif // RAPPTURE_SIMPLEBUFFER_H
Note: See TracChangeset
for help on using the changeset viewer.