00001 #ifndef CKPTBYTEARRAY_H_
00002 #define CKPTBYTEARRAY_H_
00003
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string>
00007
00027 class CkptByteArray {
00028
00030 static const unsigned int MIN_BUFFER_SIZE=64;
00032 static const unsigned int GROWTH_SIZE=MIN_BUFFER_SIZE;
00033
00034 public:
00035 CkptByteArray() {
00036 _bufferSize = MIN_BUFFER_SIZE;
00037 _dataBuffer = (char*)malloc(_bufferSize);
00038 _dataSize = 0;
00039 resetPtr();
00040 }
00041
00046 CkptByteArray(unsigned int size) {
00047 _bufferSize = size;
00048 _dataBuffer = (char*)malloc(_bufferSize);
00049 _dataSize = 0;
00050 resetPtr();
00051 }
00052
00060 CkptByteArray(char* buf, unsigned int bufSize) {
00061 _bufferSize = bufSize;
00062 _dataBuffer = (char*)malloc(bufSize);
00063 _dataSize = bufSize;
00064 memcpy(_dataBuffer, buf, bufSize);
00065 resetPtr();
00066 }
00067
00068 ~CkptByteArray() {
00069 if (_dataBuffer != NULL) free(_dataBuffer);
00070 }
00071
00082 bool writeBytes(char* userBuffer, unsigned int numBytes) {
00083 if ( (_dataSize + numBytes) > _bufferSize) {
00084
00085 if (grow() == false) return false;
00086 }
00087
00088 memcpy(_currDataPtr, userBuffer, numBytes);
00089 _currDataPtr += numBytes;
00090 _dataSize += numBytes;
00091
00092 return true;
00093 }
00094
00101 int readBytes(char* userBuffer, unsigned int numBytes) {
00102 char* userPtr = userBuffer;
00103 numBytes = (numBytes > bytesRemaining() ) ? bytesRemaining() : numBytes;
00104 for (unsigned int i=0; i < numBytes; i++) *userPtr++ = *_currDataPtr++;
00105 return numBytes;
00106 }
00115 bool write(long &lValue) { return writeBytes( (char*)&lValue, sizeof(lValue)); }
00116 bool write(int &iValue) { return writeBytes( (char*)&iValue, sizeof(iValue)); }
00117 bool write(short &sValue) { return writeBytes( (char*)&sValue, sizeof(sValue)); }
00118 bool write(double &dValue) { return writeBytes( (char*)&dValue, sizeof(dValue)); }
00119 bool write(float &fValue) { return writeBytes( (char*)&fValue, sizeof(fValue)); }
00120 bool write(char &cValue) { return writeBytes( (char*)&cValue, sizeof(cValue)); }
00121
00122 bool write(unsigned long &ulValue) { return writeBytes( (char*)&ulValue, sizeof(ulValue)); }
00123 bool write(unsigned int &uiValue) { return writeBytes( (char*)&uiValue, sizeof(uiValue)); }
00124 bool write(unsigned short &usValue) { return writeBytes( (char*)&usValue, sizeof(usValue)); }
00125
00126
00127 bool write(unsigned char &ucValue) { return writeBytes( (char*)&ucValue, sizeof(ucValue)); }
00128
00129 bool write(std::string &str) {
00130 unsigned int strLen = str.length();
00131 if (write(strLen) == false) return false;
00132 return writeBytes((char*)str.c_str(), strLen);
00133 }
00143 long readLong() { long v = *(long*)_currDataPtr; _currDataPtr+=sizeof(long); return v; }
00144 int readInt() { int v = *(int*)_currDataPtr; _currDataPtr+=sizeof(int); return v; }
00145 short readShort() { short v = *(short*)_currDataPtr; _currDataPtr+=sizeof(short); return v; }
00146 double readDouble() { double v = *(double*)_currDataPtr; _currDataPtr+=sizeof(double); return v; }
00147 float readFloat() { float v = *(float*)_currDataPtr; _currDataPtr+=sizeof(float); return v; }
00148 char readChar() { char v = *_currDataPtr; _currDataPtr+=sizeof(char); return v; }
00149
00150 unsigned long readULong() { unsigned long v; readBytes((char*)&v, sizeof(unsigned long)); return v; }
00151 unsigned int readUInt() { unsigned int v; readBytes((char*)&v, sizeof(unsigned int)); return v; }
00152 unsigned short readUShort() { unsigned short v; readBytes((char*)&v, sizeof(unsigned short)); return v; }
00153 unsigned char readUChar() { unsigned char v; readBytes((char*)&v, sizeof(unsigned char)); return v; }
00154
00155 std::string readString() {
00156 unsigned int strLength = readUInt();
00157 char* buf = (char*)malloc(strLength+1);
00158 readBytes(buf, strLength);
00159 buf[strLength] = 0;
00160 std::string str(buf, strLength);
00161 free(buf);
00162 return str;
00163 }
00166 void resetPtr() { _currDataPtr = _dataBuffer; }
00167 char* getDataPtr() { return _dataBuffer; }
00168 unsigned int getDataSize() { return _dataSize; }
00169 unsigned int bytesRemaining() { return (_dataSize - (_currDataPtr - _dataBuffer)); }
00170
00171 private:
00172
00173 bool grow() {
00174 int dist = _currDataPtr - _dataBuffer;
00175 char* newBuffer = (char*)realloc(_dataBuffer, _bufferSize + GROWTH_SIZE);
00176 if (newBuffer == NULL) {
00177 return false;
00178 }
00179 _dataBuffer = newBuffer;
00180 _currDataPtr = newBuffer + dist;
00181 _bufferSize += GROWTH_SIZE;
00182 return true;
00183 }
00184
00185 unsigned int _bufferSize;
00186 unsigned int _dataSize;
00187 char* _dataBuffer;
00188 char* _currDataPtr;
00189 };
00190
00191 typedef CkptByteArray byte_array;
00192
00193 #endif