Changeset 1038
- Timestamp:
- Jun 10, 2008, 7:53:44 AM (16 years ago)
- Location:
- trunk/src/core
- Files:
-
- 3 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/Makefile.in
r1036 r1038 58 58 RpOutcomeCHelper.h \ 59 59 RpOutcomeCInterface.h \ 60 RpSimpleBuffer.h \ 60 61 RpUnits.h \ 61 62 RpUnitsCInterface.h \ … … 101 102 RpPtr.o \ 102 103 RpResult.o \ 104 RpSimpleBuffer.o \ 103 105 RpUnits.o \ 104 106 RpUnitsCInterface.o \ -
trunk/src/core/RpBuffer.cc
r1018 r1038 5 5 * AUTHOR: Derrick Kearney, Purdue University 6 6 * 7 * Copyright (c) 2004-200 7Purdue Research Foundation7 * Copyright (c) 2004-2008 Purdue Research Foundation 8 8 * ---------------------------------------------------------------------- 9 9 * See the file "license.terms" for information on usage and 10 10 * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. 11 * ======================================================================12 *13 * This code is based on the Tcl_DString facility included in the14 * Tcl source release, which includes the following copyright:15 *16 * Copyright (c) 1987-1993 The Regents of the University of California.17 * Copyright (c) 1994-1998 Sun Microsystems, Inc.18 * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.19 *20 * This software is copyrighted by the Regents of the University of21 * California, Sun Microsystems, Inc., Scriptics Corporation,22 * and other parties. The following terms apply to all files associated23 * with the software unless explicitly disclaimed in individual files.24 *25 * The authors hereby grant permission to use, copy, modify, distribute,26 * and license this software and its documentation for any purpose, provided27 * that existing copyright notices are retained in all copies and that this28 * notice is included verbatim in any distributions. No written agreement,29 * license, or royalty fee is required for any of the authorized uses.30 * Modifications to this software may be copyrighted by their authors31 * and need not follow the licensing terms described here, provided that32 * the new terms are clearly indicated on the first page of each file where33 * they apply.34 *35 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY36 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES37 * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY38 * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE39 * POSSIBILITY OF SUCH DAMAGE.40 *41 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,42 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,43 * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE44 * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE45 * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR46 * MODIFICATIONS.47 *48 * GOVERNMENT USE: If you are acquiring this software on behalf of the49 * U.S. government, the Government shall have only "Restricted Rights"50 * in the software and related documentation as defined in the Federal·51 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you52 * are acquiring the software on behalf of the Department of Defense, the53 * software shall be classified as "Commercial Computer Software" and the54 * Government shall have only "Restricted Rights" as defined in Clause55 * 252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the56 * authors grant the U.S. Government and others acting in its behalf57 * permission to use and distribute the software in accordance with the58 * terms specified in this license.·59 *60 11 * ====================================================================== 61 12 */ … … 76 27 77 28 /** 78 * Construct an empty SimpleBuffer.79 */80 SimpleBuffer::SimpleBuffer()81 {82 bufferInit();83 }84 85 86 /**87 * Construct a SimpleBuffer loaded with initial data.88 *89 * @param bytes pointer to bytes being stored.90 * @param nbytes number of bytes being stored.91 */92 SimpleBuffer::SimpleBuffer(const char* bytes, int nbytes)93 {94 bufferInit();95 append(bytes,nbytes);96 }97 98 99 /**100 * Copy constructor101 * @param SimpleBuffer object to copy102 */103 SimpleBuffer::SimpleBuffer(const SimpleBuffer& b)104 {105 bufferInit();106 append(b.bytes(),b.size());107 }108 109 110 /**111 * Assignment operator112 * @param SimpleBuffer object to copy113 */114 SimpleBuffer&115 SimpleBuffer::operator=(const SimpleBuffer& b)116 {117 bufferFree();118 bufferInit();119 append(b.bytes(),b.size());120 return *this;121 }122 123 124 /**125 * Operator +126 * @param SimpleBuffer object to add127 */128 SimpleBuffer129 SimpleBuffer::operator+(const SimpleBuffer& b) const130 {131 SimpleBuffer newBuffer(*this);132 newBuffer.operator+=(b);133 return newBuffer;134 }135 136 137 /**138 * Operator +=139 * @param SimpleBuffer object to add140 */141 SimpleBuffer&142 SimpleBuffer::operator+=(const SimpleBuffer& b)143 {144 append(b.bytes(),b.size());145 return *this;146 }147 148 149 /**150 * Destructor151 */152 SimpleBuffer::~SimpleBuffer()153 {154 bufferFree();155 }156 157 158 /**159 * Get the bytes currently stored in the buffer. These bytes can160 * be stored, and used later to construct another Buffer to161 * decode the information.162 *163 * @return Pointer to the bytes in the buffer.164 */165 const char*166 SimpleBuffer::bytes() const167 {168 return _buf;169 }170 171 172 /**173 * Get the number of bytes currently stored in the buffer.174 * @return Number of the bytes used in the buffer.175 */176 unsigned int177 SimpleBuffer::size() const178 {179 return _size;180 }181 182 183 /**184 * Clear the buffer, making it empty.185 * @return Reference to this buffer.186 */187 SimpleBuffer&188 SimpleBuffer::clear()189 {190 bufferFree();191 bufferInit();192 193 return *this;194 }195 196 197 /**198 * Append bytes to the end of this buffer199 * @param Buffer object to copy200 * @return Reference to this buffer.201 */202 int203 SimpleBuffer::append(const char* bytes, int nbytes)204 {205 unsigned int newSize = 0;206 char *newBuffer = NULL;207 208 // User specified NULL buffer to append209 if ( (bytes == NULL) && (nbytes < 1) ) {210 return 0;211 }212 213 if (nbytes == -1) {214 // user signaled null terminated string215 nbytes = strlen(bytes);216 }217 218 if (nbytes <= 0) {219 // no data written, invalid option220 return nbytes;221 }222 223 // Empty internal buffer, make sure its properly initialized.224 if (_buf == NULL) {225 bufferInit();226 }227 228 newSize = (unsigned int)(_size + nbytes);229 230 // ensure that our smallest buffer is 200 bytes231 if (newSize < (RP_BUFFER_MIN_SIZE/2)) {232 newSize = (RP_BUFFER_MIN_SIZE/2);233 }234 235 /*236 * Allocate a larger buffer for the string if the current one isn't237 * large enough. Allocate extra space in the new buffer so that there238 * will be room to grow before we have to allocate again.239 */240 241 if (newSize >= _spaceAvl) {242 _spaceAvl = newSize * 2;243 newBuffer = new char[_spaceAvl];244 if (newBuffer == NULL) {245 // return memory error246 return -1;247 }248 if (_buf != NULL) {249 memcpy((void*) newBuffer, (void*) _buf, (size_t) _size);250 delete [] _buf;251 _buf = NULL;252 }253 _buf = newBuffer;254 }255 256 memcpy((void*) (_buf + _size), (void*) bytes, (size_t) nbytes);257 258 _size = _size + (unsigned int) nbytes;259 260 return nbytes;261 }262 263 264 /**265 * Read data from the buffer into a memory location provided by caller266 * @param Pointer locating where to place read bytes.267 * @param Size of the memory location.268 * @return Number of bytes written to memory location269 */270 int271 SimpleBuffer::read(const char* bytes, int nbytes)272 {273 unsigned int bytesRead = 0;274 275 // SimpleBuffer is empty.276 if (_buf == NULL) {277 return 0;278 }279 280 // User specified NULL buffer.281 if (bytes == NULL) {282 return 0;283 }284 285 // User specified negative buffer size286 if (nbytes <= 0) {287 return 0;288 }289 290 // make sure we don't read off the end of our buffer291 if ( (_pos + nbytes) >= _size ) {292 bytesRead = (_size - _pos);293 }294 else {295 bytesRead = (unsigned int) nbytes;296 }297 298 if (bytesRead <= 0) {299 return 0;300 }301 302 if (bytesRead > 0) {303 memcpy((void*) bytes, (void*) (_buf+_pos), (size_t) bytesRead);304 }305 306 _pos = (_pos + bytesRead);307 308 return (int)bytesRead;309 }310 311 312 /**313 * Set buffer position indicator to spot within the buffer314 * @param Offset from whence location in buffer.315 * @param Place from where offset is added or subtracted.316 * @return 0 on success, anything else is failure317 */318 int319 SimpleBuffer::seek(int offset, int whence)320 {321 int retVal = 0;322 323 if (_buf == NULL) {324 return -1 ;325 }326 327 if (whence == SEEK_SET) {328 if (offset < 0) {329 /* dont go off the beginning of data */330 _pos = 0;331 }332 else if (offset >= (int)_size) {333 /* dont go off the end of data */334 _pos = _size - 1;335 }336 else {337 _pos = (unsigned int)(_pos + offset);338 }339 }340 else if (whence == SEEK_CUR) {341 if ( (_pos + offset) < 0) {342 /* dont go off the beginning of data */343 _pos = 0;344 }345 else if ((_pos + offset) >= _size) {346 /* dont go off the end of data */347 _pos = _size - 1;348 }349 else {350 _pos = (unsigned int)(_pos + offset);351 }352 }353 else if (whence == SEEK_END) {354 if (offset <= (-1*((int)_size))) {355 /* dont go off the beginning of data */356 _pos = 0;357 }358 else if (offset >= 0) {359 /* dont go off the end of data */360 _pos = _size - 1;361 }362 else {363 _pos = (unsigned int)((_size - 1) + offset);364 }365 }366 else {367 retVal = -1;368 }369 370 return retVal;371 }372 373 374 /**375 * Tell caller the offset of the position indicator from the start of buffer376 * @return Number of bytes the position indicator is from start of buffer377 */378 int379 SimpleBuffer::tell()380 {381 return (int)_pos;382 }383 384 385 /**386 * Read data from the buffer into a memory location provided by caller387 */388 SimpleBuffer&389 SimpleBuffer::rewind()390 {391 _pos = 0;392 return *this;393 }394 395 396 /**397 * Tell if the last file like operation (ie. read()) was successful398 * or if there was a failure like eof, or bad memory399 * @return True or false boolean value400 */401 bool402 SimpleBuffer::good() const403 {404 return (_fileState);405 }406 407 408 /**409 * Tell if the last file like operation (ie. read()) failed410 * Opposite of good()411 * @return True or false boolean value412 */413 bool414 SimpleBuffer::bad() const415 {416 return (!_fileState);417 }418 419 420 /**421 * Tell if the position flag is at the end of the buffer422 * @return True or false boolean value423 */424 bool425 SimpleBuffer::eof() const426 {427 return (_pos >= _size);428 }429 430 431 /**432 * Move the data from this SimpleBuffer to the SimpleBuffer provided by433 * the caller. All data except the _pos is moved and this SimpleBuffer is434 * re-initialized with bufferInit().435 * @param SimpleBuffer to move the information to436 * @return reference to this SimpleBuffer object.437 */438 SimpleBuffer&439 SimpleBuffer::move(SimpleBuffer& b)440 {441 bufferFree();442 443 _buf = b._buf;444 _pos = b._pos;445 _size = b._size;446 _spaceAvl = b._spaceAvl;447 _fileState = b._fileState;448 449 b.bufferInit();450 451 return *this;452 }453 454 455 /**456 * Initializes a dynamic buffer, discarding any previous contents457 * of the buffer. bufferFree() should have been called already458 * if the dynamic buffer was previously in use.459 */460 void461 SimpleBuffer::bufferInit()462 {463 _buf = NULL;464 _pos = 0;465 _size = 0;466 _spaceAvl = 0;467 _fileState = true;468 }469 470 471 /**472 * Frees up any memory allocated for the dynamic buffer and473 * reinitializes the buffer to an empty state.474 */475 void476 SimpleBuffer::bufferFree()477 {478 if (_buf != NULL) {479 delete [] _buf;480 _buf = NULL;481 }482 bufferInit();483 }484 485 486 /**487 29 * Construct an empty Buffer. 488 30 */ 489 31 Buffer::Buffer() 490 32 : SimpleBuffer(), 33 _level(6), 34 _compressionType(RPCOMPRESS_GZIP), 35 _windowBits(15) 36 {} 37 38 39 /** 40 * Construct an empty Buffer of specified size. 41 */ 42 Buffer::Buffer(int nbytes) 43 : SimpleBuffer(nbytes), 491 44 _level(6), 492 45 _compressionType(RPCOMPRESS_GZIP), -
trunk/src/core/RpBuffer.h
r1030 r1038 5 5 * AUTHOR: Derrick Kearney, Purdue University 6 6 * 7 * Copyright (c) 2004-200 7Purdue Research Foundation7 * Copyright (c) 2004-2008 Purdue Research Foundation 8 8 * ---------------------------------------------------------------------- 9 9 * See the file "license.terms" for information on usage and 10 10 * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. 11 * ======================================================================12 *13 * This code is based on the Tcl_DString facility included in the14 * Tcl source release, which includes the following copyright:15 *16 * Copyright (c) 1987-1993 The Regents of the University of California.17 * Copyright (c) 1994-1998 Sun Microsystems, Inc.18 * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.19 *20 * This software is copyrighted by the Regents of the University of21 * California, Sun Microsystems, Inc., Scriptics Corporation,22 * and other parties. The following terms apply to all files associated23 * with the software unless explicitly disclaimed in individual files.24 *25 * The authors hereby grant permission to use, copy, modify, distribute,26 * and license this software and its documentation for any purpose, provided27 * that existing copyright notices are retained in all copies and that this28 * notice is included verbatim in any distributions. No written agreement,29 * license, or royalty fee is required for any of the authorized uses.30 * Modifications to this software may be copyrighted by their authors31 * and need not follow the licensing terms described here, provided that32 * the new terms are clearly indicated on the first page of each file where33 * they apply.34 *35 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY36 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES37 * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY38 * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE39 * POSSIBILITY OF SUCH DAMAGE.40 *41 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,42 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,43 * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE44 * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE45 * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR46 * MODIFICATIONS.47 *48 * GOVERNMENT USE: If you are acquiring this software on behalf of the49 * U.S. government, the Government shall have only "Restricted Rights"50 * in the software and related documentation as defined in the Federal·51 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you52 * are acquiring the software on behalf of the Department of Defense, the53 * software shall be classified as "Commercial Computer Software" and the54 * Government shall have only "Restricted Rights" as defined in Clause55 * 252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the56 * authors grant the U.S. Government and others acting in its behalf57 * permission to use and distribute the software in accordance with the58 * terms specified in this license.·59 *60 11 * ====================================================================== 61 12 */ … … 64 15 #define RAPPTURE_BUFFER_H 65 16 66 #define RP_BUFFER_MIN_SIZE 20067 68 17 #include <RpOutcome.h> 18 #include <RpSimpleBuffer.h> 69 19 70 20 #ifdef __cplusplus … … 79 29 namespace Rappture { 80 30 81 class SimpleBuffer {82 public:83 SimpleBuffer();84 SimpleBuffer(const char* bytes, int nbytes=-1);85 SimpleBuffer(const SimpleBuffer& b);86 SimpleBuffer& operator=(const SimpleBuffer& b);87 SimpleBuffer operator+(const SimpleBuffer& b) const;88 SimpleBuffer& operator+=(const SimpleBuffer& b);89 virtual ~SimpleBuffer();90 91 const char* bytes() const;92 unsigned int size() const;93 94 SimpleBuffer& clear();95 int append(const char* bytes, int nbytes=-1);96 int read(const char* bytes, int nbytes);97 int seek(int offset, int whence);98 int tell();99 SimpleBuffer& rewind();100 101 bool good() const;102 bool bad() const;103 bool eof() const;104 105 SimpleBuffer& move(SimpleBuffer& b);106 107 protected:108 109 void bufferInit();110 void bufferFree();111 112 private:113 114 /// Pointer to the memory that holds our buffer's data115 char* _buf;116 117 /// Position offset within the buffer's memory118 unsigned int _pos;119 120 /// Size of the used memory in the buffer121 unsigned int _size;122 123 /// Total space available in the buffer124 unsigned int _spaceAvl;125 126 /// State of the last file like operation.127 bool _fileState;128 };129 31 130 32 /** … … 138 40 public: 139 41 Buffer(); 42 Buffer(int nbytes); 140 43 Buffer(const char* bytes, int nbytes=-1); 141 44 Buffer(const Buffer& buffer);
Note: See TracChangeset
for help on using the changeset viewer.