// *************************************************************** // Function: CompiledPacket.cpp // *************************************************************** #include "CompiledPacket.h" #include #include //Constructor CompiledPacket::CompiledPacket(bool ClearMem) { mpBuffer=0; mBufferLength=0; mClearMemUsed=ClearMem; mMixedMode=false; mpSegment=0; mSbPointer=0; } CompiledPacket::CompiledPacket() { mpBuffer=0; mBufferLength=0; mClearMemUsed=false; mMixedMode=false; mpSegment=0; mSbPointer=0; } //************************************ // Name: Clear // // Clean-up the allocated data. // // Parameters: // // Return: // //************************************ void CompiledPacket::Clear() { if (mpBuffer!=NULL){delete [] mpBuffer;} mpBuffer=0; mBufferLength=0; mClearMemUsed=false; mMixedMode=false; mpSegment=0; mSbPointer=0; } //************************************ // Name: AddByte // // Add a byte to the buffer. // // Parameters: // c - Byte // // Return: // //************************************ void CompiledPacket::AddByte(unsigned char c) { AddStatic((void*)&c, 1); } //************************************ // Name: AddShort // // Add a short to the buffer. // // Parameters: // s - Short // // Return: // //************************************ void CompiledPacket::AddShort(unsigned short s) { AddStatic((void*)&s, 2); } //************************************ // Name: AddLong // // Add a long to the buffer. // // Parameters: // l - Long // // Return: // //************************************ void CompiledPacket::AddLong(unsigned long l) { AddStatic((void*)&l, sizeof(long)); } //************************************ // Name: AddFloat // // Add a float to the buffer. // // Parameters: // f - Float // // Return: // //************************************ void CompiledPacket::AddFloat(float f) { AddStatic((void*)&f, sizeof(float)); } //************************************ // Name: AddBool // // Add a boolean to the buffer. // // Parameters: // b - Bool // // Return: // //************************************ void CompiledPacket::AddBool(bool b) { AddStatic((void*)&b, sizeof(bool)); } //************************************ // Name: Add // // Add an array of data to the buffer. // // Parameters: // pData - Source data // length - Source data size // deleteString - If true the source buffer will be deleted. // // Return: // //************************************ void CompiledPacket::Add(unsigned char* pData, unsigned long length, bool deleteString) { AddStatic(pData, length); if (deleteString) delete [] pData; } //************************************ // Name: Add // // Adds a MiniCompPck to the buffer. // // Parameters: // mcp - Mini compiled packet // // Return: // //************************************ void CompiledPacket::Add(MiniCompPck mcp) { Add(mcp.pData, mcp.dataSize, true); } //************************************ // Name: UpdateBuffer // // Updates the buffer. (Internal only) // // Parameters: // // Return: // //************************************ void CompiledPacket::UpdateBuffer() { if (mSbPointer>0) { Add(this->mStaticBuffer, mSbPointer); mSbPointer=0; } } //************************************ // Name: AddStatic // // Adds data to the buffer. (Internal only) // // Parameters: // pData - Document // size - Document // // Return: // //************************************ void CompiledPacket::AddStatic(void* pData, unsigned long size) { //Make sure the data won't over fill the buffer //If it does we must empty the buffer if (size+mSbPointer>=STATIC_BUFFER_SIZE && mSbPointer>0) { Add(mStaticBuffer, mSbPointer); mSbPointer=0; } //Add the current data. No overflow of buffer if (size<=STATIC_BUFFER_SIZE) { memcpy(mStaticBuffer+mSbPointer, pData, size); mSbPointer+=size; } //If the amount of data will overflow the buffer, just add it directly else { Add((unsigned char*)pData, size); } } //************************************ // Name: Fill // // Adds in /length/ bytes of data with /c/ to the buffer // // Parameters: // c - Byte value // length - How many bytes // // Return: // //************************************ void CompiledPacket::Fill(unsigned char c, unsigned long length) { for (unsigned long i=0; imBufferLength) { size=mBufferLength; } *pLength=size; unsigned char* pRet=mpSegment; mpSegment+=size; mBufferLength-=size; return pRet; } //Deconstructor CompiledPacket::~CompiledPacket() { if (mClearMemUsed && mpBuffer!=NULL) delete [] mpBuffer; } //************************************ // Name: Add // // Add to the dynamic buffer. (Internal only) // // Parameters: // pString - Source data // length - Size of source data // // Return: // - zero //************************************ int CompiledPacket::Add(unsigned char* pString, unsigned int length) { if (mMixedMode) { return -1; } assert(pString!=NULL); if (mBufferLength==0) { mpBuffer=new unsigned char[length]; assert(mpBuffer!=NULL); memcpy(mpBuffer, pString, length); mBufferLength=length; mpSegment=mpBuffer; return length; } else { unsigned char* pTemp_buffer=new unsigned char[length+mBufferLength]; assert(pTemp_buffer!=NULL); memcpy(pTemp_buffer, mpBuffer, mBufferLength); memcpy(pTemp_buffer+mBufferLength, pString, length); delete [] mpBuffer; mpBuffer=pTemp_buffer; mBufferLength=length+mBufferLength; mpSegment=mpBuffer; return mBufferLength; } return 0; } //Returns the data in the packet unsigned char* CompiledPacket::GetString(int* pLength) { UpdateBuffer(); if (mMixedMode){return 0;} assert(pLength!=NULL); *pLength=mBufferLength; return mpBuffer; } //Returns the data in the packet unsigned char* CompiledPacket::GetString(int* pLength, int size) { UpdateBuffer(); assert(pLength!=NULL); if (mBufferLength==0){return 0;} if ((unsigned int)size>mBufferLength) { size=mBufferLength; } *pLength=size; unsigned char* pRet=mpSegment; mpSegment+=size; mBufferLength-=size; return pRet; }