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 
00053         mData = new uchar[size];
00054         mSize = size;
00055         mPos = mData;
00056         mEnd = mData + size;
00057 
00058         if( ptr )
00059             memcpy( mData, ptr, mSize );
00060 
00061         return mData;
00062     }
00063 
00064     //-----------------------------------------------------------------------
00065     DataChunk & DataChunk::clear(void)
00066     {
00067         if (mData)
00068         {
00069             delete [] mData;
00070             mData = 0;
00071             mSize = 0;
00072         }
00073 
00074         return *this;
00075     }
00076     //-----------------------------------------------------------------------
00077     size_t DataChunk::getSize(void) const
00078     {
00079         return mSize;
00080     }
00081 
00082     //-----------------------------------------------------------------------
00083     uchar * DataChunk::getPtr(void)
00084     {
00085         return mData;
00086     }
00087 
00088     //-----------------------------------------------------------------------
00089     const uchar * DataChunk::getPtr() const
00090     {
00091         return mData;
00092     }
00093 
00094     //-----------------------------------------------------------------------
00095     size_t DataChunk::read(void* buffer, size_t size)
00096     {
00097         size_t cnt = size;
00098         // Read over end of memory?
00099         if (mPos + size > mEnd)
00100             cnt = mEnd - mPos;
00101         if (cnt == 0)
00102             return 0;
00103 
00104         memcpy(buffer, (const void*)mPos, cnt);
00105         mPos += cnt;
00106         return cnt;
00107     }
00108 
00109     //-----------------------------------------------------------------------
00110     DataChunk & DataChunk::seek( size_t pos )
00111     {
00112         if( pos <= mSize )
00113             mPos = mData + pos;
00114 
00115         return *this;
00116     }
00117     //-----------------------------------------------------------------------
00118     DataChunk & DataChunk::skip( long offset )
00119     {
00120         size_t newpos = (size_t)( ( mPos - mData ) + offset );
00121         assert( mData <= mData + newpos && mData + newpos <= mEnd );        
00122 
00123         mPos = mData + newpos;
00124 
00125         return *this;
00126     }
00127     //-----------------------------------------------------------------------
00128     size_t DataChunk::readUpTo( void* buffer, size_t size, const char *delim )
00129     {
00130         size_t pos = strcspn((const char*)mPos, delim);
00131         if (pos > size)
00132             pos = size;
00133 
00134         // Make sure pos can never go past the end of the data 
00135         if(mPos + pos > mEnd) pos = mEnd - mPos; 
00136 
00137         if (pos > 0)
00138         {
00139             memcpy(buffer, (const void*)mPos, pos);
00140         }
00141         mPos += pos + 1;
00142 
00143         return pos;
00144     }
00145     //-----------------------------------------------------------------------
00146     size_t DataChunk::skipUpTo( const char *delim )
00147     {
00148 
00149         size_t pos = strcspn( (const char*)mPos, delim );
00150 
00151         // Make sure pos can never go past the end of the data 
00152         if(mPos + pos > mEnd) pos = mEnd - mPos; 
00153 
00154         mPos += pos + 1;
00155 
00156         return pos;
00157     }
00158     //-----------------------------------------------------------------------
00159     bool DataChunk::isEOF(void) const
00160     {
00161         if (mPos >= mEnd)
00162             return true;
00163         else
00164             return false;
00165 
00166     }
00167     //-----------------------------------------------------------------------
00168     String DataChunk::getLine(bool trimAfter)
00169     {
00170         static char buf[512]; // prevent continuous allocation / deallocation - not thread safe!
00171 
00172         size_t count = readUpTo(buf, 511);
00173         buf[count] = '\0';
00174         String ret = buf;
00175         if (trimAfter)
00176             ret.trim();
00177         return ret;
00178 
00179     }
00180     //-----------------------------------------------------------------------
00181     String DataChunk::getAsString(void) const
00182     {
00183         String s;
00184         // Insert n characters since we can't expect mData to be null-terminated
00185         s.insert(0, (const char*)mData, mSize);
00186         return s;
00187     }
00188 
00189 }

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:09 2004