| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of LEXIExporter |
|---|
| 4 | |
|---|
| 5 | Copyright 2006 NDS Limited |
|---|
| 6 | |
|---|
| 7 | Author(s): |
|---|
| 8 | Lasse Tassing |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | ----------------------------------------------------------------------------- |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #ifndef __DATA_STREAM__ |
|---|
| 27 | #define __DATA_STREAM__ |
|---|
| 28 | |
|---|
| 29 | // Helper class. Binary packages can be build and serialized using this class |
|---|
| 30 | class CDataStream : public CRefCount |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | CDataStream(unsigned iInitSize=512, unsigned iGrow=512); |
|---|
| 34 | CDataStream(const CDataStream &other); |
|---|
| 35 | CDataStream(const void *pData, unsigned iSize, CRefCount *pDataOwner=NULL); |
|---|
| 36 | CDataStream(void *pData, unsigned iSize); // NOTE! This takes ownership of data |
|---|
| 37 | ~CDataStream(void); |
|---|
| 38 | |
|---|
| 39 | // |
|---|
| 40 | void Reserve(unsigned int iNewAlloc); |
|---|
| 41 | |
|---|
| 42 | // Insertion |
|---|
| 43 | void InsertInt(int iData, int iPosition=0); |
|---|
| 44 | void InsertString(const char *pszData, int iPosition=0); |
|---|
| 45 | |
|---|
| 46 | // Add |
|---|
| 47 | void AddByte(unsigned char bDate); |
|---|
| 48 | void AddInt(int iData); |
|---|
| 49 | void AddFloat(float fData); |
|---|
| 50 | void AddString(const char *pszData); |
|---|
| 51 | void AddBinary(const void *pData, unsigned iLen); |
|---|
| 52 | void AddRAW(const void *pData, unsigned iLen); // Adds raw data to the stream |
|---|
| 53 | void AddBool(bool bData); |
|---|
| 54 | |
|---|
| 55 | // Retrieve |
|---|
| 56 | unsigned char GetByte() const; |
|---|
| 57 | int GetInt(void) const; |
|---|
| 58 | float GetFloat(void) const; |
|---|
| 59 | const char *GetString(void) const; |
|---|
| 60 | const void *GetBinary(unsigned &iLen) const; |
|---|
| 61 | const void *GetRAW(unsigned iLen) const; // Get raw data from the stream (from current position) |
|---|
| 62 | bool GetBool() const; |
|---|
| 63 | |
|---|
| 64 | // Reset position |
|---|
| 65 | void Reset(void) { SetPosition(0); } |
|---|
| 66 | void SetPosition(unsigned iPos) const; |
|---|
| 67 | int GetPosition(void) const; |
|---|
| 68 | |
|---|
| 69 | // Retrieve base data (mostly used when serializing the stream) |
|---|
| 70 | const void* GetBaseData(void) const { return m_pData; } |
|---|
| 71 | |
|---|
| 72 | unsigned GetAllocSize(void) const { return m_iAllocSize; } |
|---|
| 73 | |
|---|
| 74 | private: |
|---|
| 75 | void Realloc(int iSpaceNeeded); |
|---|
| 76 | |
|---|
| 77 | // Released, if valid |
|---|
| 78 | const CRefCount *m_pDataOwner; |
|---|
| 79 | |
|---|
| 80 | // Data |
|---|
| 81 | bool m_bOwnData; |
|---|
| 82 | char *m_pData; |
|---|
| 83 | mutable unsigned m_iPosition; |
|---|
| 84 | unsigned m_iAllocSize; |
|---|
| 85 | unsigned m_iGrowSize; |
|---|
| 86 | |
|---|
| 87 | #ifdef _DEBUG |
|---|
| 88 | unsigned m_iReallocCount; |
|---|
| 89 | #endif |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | #endif |
|---|