00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright © 2000-2002 The OGRE Team 00008 Also see acknowledgements in Readme.html 00009 00010 This program is free software; you can redistribute it and/or modify it under 00011 the terms of the GNU Lesser General Public License as published by the Free Software 00012 Foundation; either version 2 of the License, or (at your option) any later 00013 version. 00014 00015 This program is distributed in the hope that it will be useful, but WITHOUT 00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public License along with 00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00022 http://www.gnu.org/copyleft/lesser.txt. 00023 ----------------------------------------------------------------------------- 00024 */ 00025 #include "OgreStableHeaders.h" 00026 #include "OgreDataChunk.h" 00027 #include "OgreException.h" 00028 00029 namespace Ogre { 00030 00031 //----------------------------------------------------------------------- 00032 DataChunk::DataChunk() 00033 : mData(NULL), mPos(NULL), mEnd(NULL), mSize(0) 00034 { 00035 } 00036 00037 DataChunk::DataChunk( void *pData, size_t size ) 00038 { 00039 mData = reinterpret_cast< uchar* >( pData ); 00040 mEnd = mData + size; 00041 mPos = mData; 00042 mSize = size; 00043 } 00044 00045 //----------------------------------------------------------------------- 00046 uchar* DataChunk::allocate( size_t size, const uchar * ptr ) 00047 { 00048 assert (size > 0); 00049 00050 if( mData ) 00051 delete [] mData; 00052 mSize = size; 00053 // Always allocate 1 additional byte, which we set to zero 00054 // This is to ensure that string chunks are always null-terminated 00055 mData = new uchar[mSize + 1]; 00056 mPos = mData; 00057 mEnd = mData + mSize; 00058 00059 if( ptr ) 00060 memcpy( mData, ptr, size ); 00061 00062 // Add null terminator to the end (just after where chunk reports that it ends) 00063 mData[mSize] = '\0'; 00064 00065 return mData; 00066 } 00067 00068 //----------------------------------------------------------------------- 00069 DataChunk & DataChunk::clear(void) 00070 { 00071 if (mData) 00072 { 00073 delete [] mData; 00074 mData = 0; 00075 mSize = 0; 00076 } 00077 00078 return *this; 00079 } 00080 //----------------------------------------------------------------------- 00081 size_t DataChunk::getSize(void) const 00082 { 00083 return mSize; 00084 } 00085 00086 //----------------------------------------------------------------------- 00087 uchar * DataChunk::getPtr(void) 00088 { 00089 return mData; 00090 } 00091 00092 //----------------------------------------------------------------------- 00093 const uchar * DataChunk::getPtr() const 00094 { 00095 return mData; 00096 } 00097 //----------------------------------------------------------------------- 00098 uchar * DataChunk::getCurrentPtr(void) 00099 { 00100 return mPos; 00101 } 00102 00103 //----------------------------------------------------------------------- 00104 const uchar * DataChunk::getCurrentPtr() const 00105 { 00106 return mPos; 00107 } 00108 00109 //----------------------------------------------------------------------- 00110 size_t DataChunk::read(void* buffer, size_t size) 00111 { 00112 size_t cnt = size; 00113 // Read over end of memory? 00114 if (mPos + size > mEnd) 00115 cnt = mEnd - mPos; 00116 if (cnt == 0) 00117 return 0; 00118 00119 memcpy(buffer, (const void*)mPos, cnt); 00120 mPos += cnt; 00121 return cnt; 00122 } 00123 00124 //----------------------------------------------------------------------- 00125 DataChunk & DataChunk::seek( size_t pos ) 00126 { 00127 if( pos <= mSize ) 00128 mPos = mData + pos; 00129 00130 return *this; 00131 } 00132 //----------------------------------------------------------------------- 00133 DataChunk & DataChunk::skip( long offset ) 00134 { 00135 size_t newpos = (size_t)( ( mPos - mData ) + offset ); 00136 assert( mData <= mData + newpos && mData + newpos <= mEnd ); 00137 00138 mPos = mData + newpos; 00139 00140 return *this; 00141 } 00142 //----------------------------------------------------------------------- 00143 size_t DataChunk::readUpTo( void* buffer, size_t size, const char *delim ) 00144 { 00145 size_t pos = strcspn((const char*)mPos, delim); 00146 if (pos > size) 00147 pos = size; 00148 00149 // Make sure pos can never go past the end of the data 00150 if(mPos + pos > mEnd) pos = mEnd - mPos; 00151 00152 if (pos > 0) 00153 { 00154 memcpy(buffer, (const void*)mPos, pos); 00155 } 00156 mPos += pos + 1; 00157 00158 return pos; 00159 } 00160 //----------------------------------------------------------------------- 00161 size_t DataChunk::skipUpTo( const char *delim ) 00162 { 00163 00164 size_t pos = strcspn( (const char*)mPos, delim ); 00165 00166 // Make sure pos can never go past the end of the data 00167 if(mPos + pos > mEnd) pos = mEnd - mPos; 00168 00169 mPos += pos + 1; 00170 00171 return pos; 00172 } 00173 //----------------------------------------------------------------------- 00174 bool DataChunk::isEOF(void) const 00175 { 00176 if (mPos >= mEnd) 00177 return true; 00178 else 00179 return false; 00180 00181 } 00182 //----------------------------------------------------------------------- 00183 String DataChunk::getLine(bool trimAfter) 00184 { 00185 static char buf[512]; // prevent continuous allocation / deallocation - not thread safe! 00186 00187 size_t count = readUpTo(buf, 511); 00188 buf[count] = '\0'; 00189 String ret = buf; 00190 if (trimAfter) 00191 StringUtil::trim(ret); 00192 return ret; 00193 00194 } 00195 //----------------------------------------------------------------------- 00196 String DataChunk::getAsString(void) const 00197 { 00198 String s; 00199 // Insert n characters since we can't expect mData to be null-terminated 00200 s.insert(0, (const char*)mData, mSize); 00201 return s; 00202 } 00203 00204 }
Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:22 2004