| 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 | #include "StdAfx.h" |
|---|
| 27 | |
|---|
| 28 | CDataStream::CDataStream(unsigned iInitSize, unsigned iGrow) |
|---|
| 29 | { |
|---|
| 30 | // Initialize data |
|---|
| 31 | m_iAllocSize=iInitSize; |
|---|
| 32 | m_iGrowSize=iGrow; |
|---|
| 33 | m_iPosition=0; |
|---|
| 34 | m_pData=new char[m_iAllocSize]; |
|---|
| 35 | m_bOwnData=true; |
|---|
| 36 | m_pDataOwner=NULL; |
|---|
| 37 | #ifdef _DEBUG |
|---|
| 38 | m_iReallocCount=0; |
|---|
| 39 | #endif |
|---|
| 40 | } |
|---|
| 41 | CDataStream::CDataStream(const CDataStream &other) |
|---|
| 42 | { |
|---|
| 43 | // Setup data from other instance (reference only, data will be copied if needed) |
|---|
| 44 | m_iAllocSize=other.m_iAllocSize; |
|---|
| 45 | m_iGrowSize=other.m_iGrowSize; |
|---|
| 46 | m_iPosition=other.m_iPosition; |
|---|
| 47 | m_pData=other.m_pData; |
|---|
| 48 | m_bOwnData=false; |
|---|
| 49 | m_pDataOwner=&other; |
|---|
| 50 | other.AddRef(); |
|---|
| 51 | #ifdef _DEBUG |
|---|
| 52 | m_iReallocCount=0; |
|---|
| 53 | #endif |
|---|
| 54 | } |
|---|
| 55 | CDataStream::CDataStream(const void *pOther, unsigned iSize, CRefCount *pDataOwner) |
|---|
| 56 | { |
|---|
| 57 | // Setup data, optionally including data owner |
|---|
| 58 | m_iAllocSize=iSize; |
|---|
| 59 | m_iGrowSize=512; |
|---|
| 60 | m_iPosition=0; |
|---|
| 61 | m_pData=(char*)pOther; |
|---|
| 62 | m_bOwnData=false; |
|---|
| 63 | m_pDataOwner=pDataOwner; |
|---|
| 64 | if(m_pDataOwner) m_pDataOwner->AddRef(); |
|---|
| 65 | #ifdef _DEBUG |
|---|
| 66 | m_iReallocCount=0; |
|---|
| 67 | #endif |
|---|
| 68 | } |
|---|
| 69 | CDataStream::CDataStream(void *pOther, unsigned iSize) |
|---|
| 70 | { |
|---|
| 71 | // Setup data, optionally including data owner |
|---|
| 72 | m_iAllocSize=iSize; |
|---|
| 73 | m_iGrowSize=512; |
|---|
| 74 | m_iPosition=0; |
|---|
| 75 | m_pData=(char*)pOther; |
|---|
| 76 | m_bOwnData=true; |
|---|
| 77 | m_pDataOwner=0; |
|---|
| 78 | #ifdef _DEBUG |
|---|
| 79 | m_iReallocCount=0; |
|---|
| 80 | #endif |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | CDataStream::~CDataStream(void) |
|---|
| 84 | { |
|---|
| 85 | if(m_bOwnData) delete[] m_pData; |
|---|
| 86 | else if(m_pDataOwner) m_pDataOwner->Release(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | // Insert integer at a position |
|---|
| 90 | void CDataStream::InsertInt(int iData, int iPosition) |
|---|
| 91 | { |
|---|
| 92 | if(m_iPosition+4>m_iAllocSize) Realloc(4); |
|---|
| 93 | memmove(m_pData+iPosition+4, m_pData+iPosition, m_iPosition-iPosition); |
|---|
| 94 | *((int*)(m_pData+iPosition))=iData; |
|---|
| 95 | m_iPosition+=4; |
|---|
| 96 | } |
|---|
| 97 | void CDataStream::InsertString(const char *pszData, int iPosition) |
|---|
| 98 | { |
|---|
| 99 | int iLen=strlen(pszData)+1; |
|---|
| 100 | |
|---|
| 101 | if(m_iPosition+iLen>m_iAllocSize) Realloc(iLen); |
|---|
| 102 | memmove(m_pData+iPosition+iLen, m_pData+iPosition, m_iPosition-iPosition); |
|---|
| 103 | memcpy(m_pData+iPosition, pszData, iLen); |
|---|
| 104 | m_iPosition+=iLen; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | // Add |
|---|
| 108 | void CDataStream::AddByte(unsigned char bData) |
|---|
| 109 | { |
|---|
| 110 | if(m_iPosition+1>m_iAllocSize || !m_bOwnData) Realloc(1); |
|---|
| 111 | *((unsigned char*)(m_pData+m_iPosition))=bData; |
|---|
| 112 | m_iPosition+=1; |
|---|
| 113 | } |
|---|
| 114 | void CDataStream::AddInt(int iData) |
|---|
| 115 | { |
|---|
| 116 | if(m_iPosition+4>m_iAllocSize || !m_bOwnData) Realloc(4); |
|---|
| 117 | *((int*)(m_pData+m_iPosition))=iData; |
|---|
| 118 | m_iPosition+=4; |
|---|
| 119 | } |
|---|
| 120 | void CDataStream::AddFloat(float fData) |
|---|
| 121 | { |
|---|
| 122 | if(m_iPosition+4>m_iAllocSize || !m_bOwnData) Realloc(4); |
|---|
| 123 | *((float*)(m_pData+m_iPosition))=fData; |
|---|
| 124 | m_iPosition+=4; |
|---|
| 125 | } |
|---|
| 126 | /*void CDataStream::AddString(const faststring &sData) |
|---|
| 127 | { |
|---|
| 128 | int iLen=sData.size()+1; |
|---|
| 129 | if(m_iPosition+iLen>m_iAllocSize || !m_bOwnData) Realloc(iLen); |
|---|
| 130 | memcpy(m_pData+m_iPosition, sData.c_str(), iLen); |
|---|
| 131 | m_iPosition+=iLen; |
|---|
| 132 | }*/ |
|---|
| 133 | void CDataStream::AddString(const char *pszData) |
|---|
| 134 | { |
|---|
| 135 | int iLen=strlen(pszData)+1; |
|---|
| 136 | if(m_iPosition+iLen>m_iAllocSize || !m_bOwnData) Realloc(iLen); |
|---|
| 137 | memcpy(m_pData+m_iPosition, pszData, iLen); |
|---|
| 138 | m_iPosition+=iLen; |
|---|
| 139 | } |
|---|
| 140 | void CDataStream::AddBinary(const void *pData, unsigned iLen) |
|---|
| 141 | { |
|---|
| 142 | AddInt(iLen); |
|---|
| 143 | if(m_iPosition+iLen>m_iAllocSize) Realloc(iLen); |
|---|
| 144 | memcpy(m_pData+m_iPosition, pData, iLen); |
|---|
| 145 | m_iPosition+=iLen; |
|---|
| 146 | } |
|---|
| 147 | void CDataStream::AddRAW(const void *pData, unsigned iLen) |
|---|
| 148 | { |
|---|
| 149 | if(m_iPosition+iLen>m_iAllocSize) Realloc(iLen); |
|---|
| 150 | memcpy(m_pData+m_iPosition, pData, iLen); |
|---|
| 151 | m_iPosition+=iLen; |
|---|
| 152 | } |
|---|
| 153 | void CDataStream::AddBool(bool bData) |
|---|
| 154 | { |
|---|
| 155 | if(m_iPosition+1>m_iAllocSize || !m_bOwnData) Realloc(1); |
|---|
| 156 | *((bool*)(m_pData+m_iPosition))=bData; |
|---|
| 157 | m_iPosition+=1; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | // Retrieve |
|---|
| 161 | unsigned char CDataStream::GetByte(void) const |
|---|
| 162 | { |
|---|
| 163 | m_iPosition+=1; |
|---|
| 164 | return *(unsigned char*)((char*)(m_pData+(m_iPosition-1))); |
|---|
| 165 | } |
|---|
| 166 | int CDataStream::GetInt(void) const |
|---|
| 167 | { |
|---|
| 168 | m_iPosition+=4; |
|---|
| 169 | return *(int*)((char*)(m_pData+(m_iPosition-4))); |
|---|
| 170 | } |
|---|
| 171 | float CDataStream::GetFloat(void) const |
|---|
| 172 | { |
|---|
| 173 | m_iPosition+=4; |
|---|
| 174 | return *(float*)((char*)(m_pData+(m_iPosition-4))); |
|---|
| 175 | } |
|---|
| 176 | const char *CDataStream::GetString(void) const |
|---|
| 177 | { |
|---|
| 178 | const char *pStr=m_pData+m_iPosition; |
|---|
| 179 | m_iPosition+=strlen(pStr)+1; |
|---|
| 180 | return pStr; |
|---|
| 181 | } |
|---|
| 182 | const void *CDataStream::GetBinary(unsigned &iLen) const |
|---|
| 183 | { |
|---|
| 184 | iLen=GetInt(); |
|---|
| 185 | const void *pRet=m_pData+m_iPosition; |
|---|
| 186 | m_iPosition+=iLen; |
|---|
| 187 | return pRet; |
|---|
| 188 | } |
|---|
| 189 | const void* CDataStream::GetRAW(unsigned int iLen) const |
|---|
| 190 | { |
|---|
| 191 | const void *pRet=m_pData+m_iPosition; |
|---|
| 192 | m_iPosition+=iLen; |
|---|
| 193 | return pRet; |
|---|
| 194 | } |
|---|
| 195 | bool CDataStream::GetBool() const |
|---|
| 196 | { |
|---|
| 197 | m_iPosition+=1; |
|---|
| 198 | return *(bool*)((char*)(m_pData+(m_iPosition-1))); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | // Reset position |
|---|
| 202 | void CDataStream::SetPosition(unsigned iPos) const |
|---|
| 203 | { |
|---|
| 204 | if(iPos<=m_iAllocSize && iPos>=0) m_iPosition=iPos; |
|---|
| 205 | } |
|---|
| 206 | int CDataStream::GetPosition(void) const |
|---|
| 207 | { |
|---|
| 208 | return m_iPosition; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | // |
|---|
| 212 | void CDataStream::Reserve(unsigned int iNewAlloc) |
|---|
| 213 | { |
|---|
| 214 | if(m_iAllocSize<iNewAlloc) Realloc(iNewAlloc-m_iAllocSize); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | void CDataStream::Realloc(int iSpaceNeeded) |
|---|
| 218 | { |
|---|
| 219 | // Allocate new chunk |
|---|
| 220 | char *pNewData=new char[m_iAllocSize+iSpaceNeeded+m_iGrowSize]; |
|---|
| 221 | memcpy(pNewData, m_pData, m_iAllocSize); |
|---|
| 222 | if(m_bOwnData) delete[] m_pData; |
|---|
| 223 | m_pData=pNewData; |
|---|
| 224 | m_iAllocSize+=iSpaceNeeded+m_iGrowSize; |
|---|
| 225 | |
|---|
| 226 | // Did we previously own data? |
|---|
| 227 | if(!m_bOwnData) |
|---|
| 228 | { |
|---|
| 229 | // Nope, did we have a data owner reference? |
|---|
| 230 | if(m_pDataOwner) |
|---|
| 231 | { |
|---|
| 232 | // Yep, release data owner |
|---|
| 233 | m_pDataOwner->Release(); |
|---|
| 234 | m_pDataOwner=NULL; |
|---|
| 235 | } |
|---|
| 236 | // We own the data now |
|---|
| 237 | m_bOwnData=true; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | #ifdef _DEBUG |
|---|
| 241 | if(++m_iReallocCount>5) LOGWARNING "Performance warning, datastream realloccount>5 %X, size=%d", (int)this, m_iAllocSize); |
|---|
| 242 | #endif |
|---|
| 243 | } |
|---|