2 Copyright (c) 2001, Loki software, inc.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
15 Neither the name of Loki software nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // File class, can be a memory file or a regular disk file.
33 // Originally from LeoCAD, used with permission from the author. :)
35 // Leonardo Zide (leo@lokigames.com)
46 IDataStream::IDataStream()
49 IDataStream::~IDataStream()
52 /////////////////////////////////////////////////////////////////////////////
53 // File construction/destruction
55 MemStream::MemStream(){
64 MemStream::MemStream( unsigned long nLen ){
75 FileStream::FileStream(){
77 m_bCloseOnDelete = false;
80 MemStream::~MemStream(){
91 FileStream::~FileStream(){
92 if ( m_hFile != NULL && m_bCloseOnDelete ) {
97 /////////////////////////////////////////////////////////////////////////////
100 char* MemStream::ReadString( char* pBuf, unsigned long nMax ){
107 if ( m_nPosition >= m_nFileSize ) {
113 if ( m_nPosition == m_nFileSize ) {
117 ch = m_pBuffer[m_nPosition];
130 char* FileStream::ReadString( char* pBuf, unsigned long nMax ){
131 return fgets( pBuf, nMax, m_hFile );
134 unsigned long MemStream::Read( void* pBuf, unsigned long nCount ){
139 if ( m_nPosition > m_nFileSize ) {
144 if ( m_nPosition + nCount > m_nFileSize ) {
145 nRead = (unsigned long)( m_nFileSize - m_nPosition );
151 memcpy( (unsigned char*)pBuf, (unsigned char*)m_pBuffer + m_nPosition, nRead );
152 m_nPosition += nRead;
157 unsigned long FileStream::Read( void* pBuf, unsigned long nCount ){
158 return fread( pBuf, 1, nCount, m_hFile );
161 int MemStream::GetChar(){
162 if ( m_nPosition > m_nFileSize ) {
166 unsigned char* ret = (unsigned char*)m_pBuffer + m_nPosition;
172 int FileStream::GetChar(){
173 return fgetc( m_hFile );
176 unsigned long MemStream::Write( const void* pBuf, unsigned long nCount ){
181 if ( m_nPosition + nCount > m_nBufferSize ) {
182 GrowFile( m_nPosition + nCount );
185 memcpy( (unsigned char*)m_pBuffer + m_nPosition, (unsigned char*)pBuf, nCount );
187 m_nPosition += nCount;
189 if ( m_nPosition > m_nFileSize ) {
190 m_nFileSize = m_nPosition;
196 unsigned long FileStream::Write( const void* pBuf, unsigned long nCount ){
197 return fwrite( pBuf, 1, nCount, m_hFile );
200 int MemStream::PutChar( int c ){
201 if ( m_nPosition + 1 > m_nBufferSize ) {
202 GrowFile( m_nPosition + 1 );
205 unsigned char* bt = (unsigned char*)m_pBuffer + m_nPosition;
210 if ( m_nPosition > m_nFileSize ) {
211 m_nFileSize = m_nPosition;
217 /*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
218 void FileStream::printf( const char* s, ... ){
222 vfprintf( m_hFile, s, args );
226 /*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
227 void MemStream::printf( const char* s, ... ){
232 vsprintf( buffer, s, args );
234 Write( buffer, strlen( buffer ) );
237 int FileStream::PutChar( int c ){
238 return fputc( c, m_hFile );
241 bool FileStream::Open( const char *filename, const char *mode ){
242 m_hFile = fopen( filename, mode );
243 m_bCloseOnDelete = true;
245 return m_hFile != NULL;
248 void MemStream::Close(){
253 if ( m_pBuffer && m_bAutoDelete ) {
259 void FileStream::Close(){
260 if ( m_hFile != NULL ) {
265 m_bCloseOnDelete = false;
268 unsigned long MemStream::Seek( long lOff, int nFrom ){
269 unsigned long lNewPos = m_nPosition;
271 if ( nFrom == SEEK_SET ) {
274 else if ( nFrom == SEEK_CUR ) {
277 else if ( nFrom == SEEK_END ) {
278 lNewPos = m_nFileSize + lOff;
281 return (unsigned long)-1;
284 m_nPosition = lNewPos;
289 unsigned long FileStream::Seek( long lOff, int nFrom ){
290 fseek( m_hFile, lOff, nFrom );
292 return ftell( m_hFile );
295 unsigned long MemStream::GetPosition() const {
299 unsigned long FileStream::GetPosition() const {
300 return ftell( m_hFile );
303 void MemStream::GrowFile( unsigned long nNewLen ){
304 if ( nNewLen > m_nBufferSize ) {
306 unsigned long nNewBufferSize = m_nBufferSize;
308 // determine new buffer size
309 while ( nNewBufferSize < nNewLen )
310 nNewBufferSize += m_nGrowBytes;
312 // allocate new buffer
313 unsigned char* lpNew;
314 if ( m_pBuffer == NULL ) {
315 lpNew = static_cast<unsigned char*>( malloc( nNewBufferSize ) );
318 lpNew = static_cast<unsigned char*>( realloc( m_pBuffer, nNewBufferSize ) );
322 m_nBufferSize = nNewBufferSize;
326 void MemStream::Flush(){
327 // Nothing to be done
330 void FileStream::Flush(){
331 if ( m_hFile == NULL ) {
338 void MemStream::Abort(){
342 void FileStream::Abort(){
343 if ( m_hFile != NULL ) {
344 // close but ignore errors
345 if ( m_bCloseOnDelete ) {
349 m_bCloseOnDelete = false;
353 void MemStream::SetLength( unsigned long nNewLen ){
354 if ( nNewLen > m_nBufferSize ) {
358 if ( nNewLen < m_nPosition ) {
359 m_nPosition = nNewLen;
362 m_nFileSize = nNewLen;
365 void FileStream::SetLength( unsigned long nNewLen ){
366 fseek( m_hFile, nNewLen, SEEK_SET );
369 unsigned long MemStream::GetLength() const {
373 unsigned long FileStream::GetLength() const {
374 unsigned long nLen, nCur;
376 // Seek is a non const operation
377 nCur = ftell( m_hFile );
378 fseek( m_hFile, 0, SEEK_END );
379 nLen = ftell( m_hFile );
380 fseek( m_hFile, nCur, SEEK_SET );