Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreDataChunk.cpp

Go to the documentation of this file.
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     //-----------------------------------------------------------------------
00099     size_t DataChunk::read(void* buffer, size_t size)
00100     {
00101         size_t cnt = size;
00102         // Read over end of memory?
00103         if (mPos + size > mEnd)
00104             cnt = mEnd - mPos;
00105         if (cnt == 0)
00106             return 0;
00107 
00108         memcpy(buffer, (const void*)mPos, cnt);
00109         mPos += cnt;
00110         return cnt;
00111     }
00112 
00113     //-----------------------------------------------------------------------
00114     DataChunk & DataChunk::seek( size_t pos )
00115     {
00116         if( pos <= mSize )
00117             mPos = mData + pos;
00118 
00119         return *this;
00120     }
00121     //-----------------------------------------------------------------------
00122     DataChunk & DataChunk::skip( long offset )
00123     {
00124         size_t newpos = (size_t)( ( mPos - mData ) + offset );
00125         assert( mData <= mData + newpos && mData + newpos <= mEnd );        
00126 
00127         mPos = mData + newpos;
00128 
00129         return *this;
00130     }
00131     //-----------------------------------------------------------------------
00132     size_t DataChunk::readUpTo( void* buffer, size_t size, const char *delim )
00133     {
00134         size_t pos = strcspn((const char*)mPos, delim);
00135         if (pos > size)
00136             pos = size;
00137 
00138         // Make sure pos can never go past the end of the data 
00139         if(mPos + pos > mEnd) pos = mEnd - mPos; 
00140 
00141         if (pos > 0)
00142         {
00143             memcpy(buffer, (const void*)mPos, pos);
00144         }
00145         mPos += pos + 1;
00146 
00147         return pos;
00148     }
00149     //-----------------------------------------------------------------------
00150     size_t DataChunk::skipUpTo( const char *delim )
00151     {
00152 
00153         size_t pos = strcspn( (const char*)mPos, delim );
00154 
00155         // Make sure pos can never go past the end of the data 
00156         if(mPos + pos > mEnd) pos = mEnd - mPos; 
00157 
00158         mPos += pos + 1;
00159 
00160         return pos;
00161     }
00162     //-----------------------------------------------------------------------
00163     bool DataChunk::isEOF(void) const
00164     {
00165         if (mPos >= mEnd)
00166             return true;
00167         else
00168             return false;
00169 
00170     }
00171     //-----------------------------------------------------------------------
00172     String DataChunk::getLine(bool trimAfter)
00173     {
00174         static char buf[512]; // prevent continuous allocation / deallocation - not thread safe!
00175 
00176         size_t count = readUpTo(buf, 511);
00177         buf[count] = '\0';
00178         String ret = buf;
00179         if (trimAfter)
00180             ret.trim();
00181         return ret;
00182 
00183     }
00184     //-----------------------------------------------------------------------
00185     String DataChunk::getAsString(void) const
00186     {
00187         String s;
00188         // Insert n characters since we can't expect mData to be null-terminated
00189         s.insert(0, (const char*)mData, mSize);
00190         return s;
00191     }
00192 
00193 }

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:06 2004