2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (c) 2001, Loki software, inc.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
Neither the name of Loki software nor the names of its contributors may be used
|
|
|
|
to endorse or promote products derived from this software without specific prior
|
|
|
|
written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// File class, can be a memory file or a regular disk file.
|
|
|
|
// Originally from LeoCAD, used with permission from the author. :)
|
|
|
|
//
|
|
|
|
// Leonardo Zide (leo@lokigames.com)
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
IDataStream::IDataStream()
|
|
|
|
{ }
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:51:54 +00:00
|
|
|
IDataStream::~IDataStream()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// File construction/destruction
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
MemStream::MemStream(){
|
|
|
|
m_nGrowBytes = 1024;
|
|
|
|
m_nPosition = 0;
|
|
|
|
m_nBufferSize = 0;
|
|
|
|
m_nFileSize = 0;
|
|
|
|
m_pBuffer = NULL;
|
|
|
|
m_bAutoDelete = true;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
MemStream::MemStream( unsigned long nLen ){
|
|
|
|
m_nGrowBytes = 1024;
|
|
|
|
m_nPosition = 0;
|
|
|
|
m_nBufferSize = 0;
|
|
|
|
m_nFileSize = 0;
|
|
|
|
m_pBuffer = NULL;
|
|
|
|
m_bAutoDelete = true;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GrowFile( nLen );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
FileStream::FileStream(){
|
|
|
|
m_hFile = NULL;
|
|
|
|
m_bCloseOnDelete = false;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
MemStream::~MemStream(){
|
|
|
|
if ( m_pBuffer ) {
|
|
|
|
Close();
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_nGrowBytes = 0;
|
|
|
|
m_nPosition = 0;
|
|
|
|
m_nBufferSize = 0;
|
|
|
|
m_nFileSize = 0;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
FileStream::~FileStream(){
|
|
|
|
if ( m_hFile != NULL && m_bCloseOnDelete ) {
|
|
|
|
Close();
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// File operations
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char* MemStream::ReadString( char* pBuf, unsigned long nMax ){
|
|
|
|
int nRead = 0;
|
|
|
|
unsigned char ch;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( nMax <= 0 ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ( m_nPosition >= m_nFileSize ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( ( --nMax ) )
|
|
|
|
{
|
|
|
|
if ( m_nPosition == m_nFileSize ) {
|
|
|
|
break;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ch = m_pBuffer[m_nPosition];
|
|
|
|
m_nPosition++;
|
|
|
|
pBuf[nRead++] = ch;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ch == '\n' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
pBuf[nRead] = '\0';
|
|
|
|
return pBuf;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char* FileStream::ReadString( char* pBuf, unsigned long nMax ){
|
|
|
|
return fgets( pBuf, nMax, m_hFile );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long MemStream::Read( void* pBuf, unsigned long nCount ){
|
|
|
|
if ( nCount == 0 ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_nPosition > m_nFileSize ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long nRead;
|
|
|
|
if ( m_nPosition + nCount > m_nFileSize ) {
|
|
|
|
nRead = (unsigned long)( m_nFileSize - m_nPosition );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
nRead = nCount;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( (unsigned char*)pBuf, (unsigned char*)m_pBuffer + m_nPosition, nRead );
|
|
|
|
m_nPosition += nRead;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return nRead;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long FileStream::Read( void* pBuf, unsigned long nCount ){
|
|
|
|
return fread( pBuf, 1, nCount, m_hFile );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int MemStream::GetChar(){
|
|
|
|
if ( m_nPosition > m_nFileSize ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned char* ret = (unsigned char*)m_pBuffer + m_nPosition;
|
|
|
|
m_nPosition++;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return *ret;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int FileStream::GetChar(){
|
|
|
|
return fgetc( m_hFile );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long MemStream::Write( const void* pBuf, unsigned long nCount ){
|
|
|
|
if ( nCount == 0 ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_nPosition + nCount > m_nBufferSize ) {
|
|
|
|
GrowFile( m_nPosition + nCount );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( (unsigned char*)m_pBuffer + m_nPosition, (unsigned char*)pBuf, nCount );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_nPosition += nCount;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_nPosition > m_nFileSize ) {
|
|
|
|
m_nFileSize = m_nPosition;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return nCount;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long FileStream::Write( const void* pBuf, unsigned long nCount ){
|
|
|
|
return fwrite( pBuf, 1, nCount, m_hFile );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int MemStream::PutChar( int c ){
|
|
|
|
if ( m_nPosition + 1 > m_nBufferSize ) {
|
|
|
|
GrowFile( m_nPosition + 1 );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned char* bt = (unsigned char*)m_pBuffer + m_nPosition;
|
|
|
|
*bt = c;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_nPosition++;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_nPosition > m_nFileSize ) {
|
|
|
|
m_nFileSize = m_nPosition;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return 1;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
|
2012-03-17 20:01:54 +00:00
|
|
|
void FileStream::printf( const char* s, ... ){
|
|
|
|
va_list args;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
va_start( args, s );
|
|
|
|
vfprintf( m_hFile, s, args );
|
|
|
|
va_end( args );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
|
2012-03-17 20:01:54 +00:00
|
|
|
void MemStream::printf( const char* s, ... ){
|
|
|
|
va_list args;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char buffer[4096];
|
|
|
|
va_start( args, s );
|
|
|
|
vsprintf( buffer, s, args );
|
|
|
|
va_end( args );
|
|
|
|
Write( buffer, strlen( buffer ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int FileStream::PutChar( int c ){
|
|
|
|
return fputc( c, m_hFile );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bool FileStream::Open( const char *filename, const char *mode ){
|
|
|
|
m_hFile = fopen( filename, mode );
|
|
|
|
m_bCloseOnDelete = true;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return m_hFile != NULL;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void MemStream::Close(){
|
|
|
|
m_nGrowBytes = 0;
|
|
|
|
m_nPosition = 0;
|
|
|
|
m_nBufferSize = 0;
|
|
|
|
m_nFileSize = 0;
|
|
|
|
if ( m_pBuffer && m_bAutoDelete ) {
|
|
|
|
free( m_pBuffer );
|
|
|
|
}
|
|
|
|
m_pBuffer = NULL;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void FileStream::Close(){
|
|
|
|
if ( m_hFile != NULL ) {
|
|
|
|
fclose( m_hFile );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_hFile = NULL;
|
|
|
|
m_bCloseOnDelete = false;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long MemStream::Seek( long lOff, int nFrom ){
|
|
|
|
unsigned long lNewPos = m_nPosition;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( nFrom == SEEK_SET ) {
|
|
|
|
lNewPos = lOff;
|
|
|
|
}
|
|
|
|
else if ( nFrom == SEEK_CUR ) {
|
|
|
|
lNewPos += lOff;
|
|
|
|
}
|
|
|
|
else if ( nFrom == SEEK_END ) {
|
|
|
|
lNewPos = m_nFileSize + lOff;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return (unsigned long)-1;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_nPosition = lNewPos;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return m_nPosition;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long FileStream::Seek( long lOff, int nFrom ){
|
|
|
|
fseek( m_hFile, lOff, nFrom );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return ftell( m_hFile );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long MemStream::GetPosition() const {
|
|
|
|
return m_nPosition;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long FileStream::GetPosition() const {
|
|
|
|
return ftell( m_hFile );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void MemStream::GrowFile( unsigned long nNewLen ){
|
|
|
|
if ( nNewLen > m_nBufferSize ) {
|
|
|
|
// grow the buffer
|
|
|
|
unsigned long nNewBufferSize = m_nBufferSize;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// determine new buffer size
|
|
|
|
while ( nNewBufferSize < nNewLen )
|
|
|
|
nNewBufferSize += m_nGrowBytes;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// allocate new buffer
|
|
|
|
unsigned char* lpNew;
|
|
|
|
if ( m_pBuffer == NULL ) {
|
|
|
|
lpNew = static_cast<unsigned char*>( malloc( nNewBufferSize ) );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
lpNew = static_cast<unsigned char*>( realloc( m_pBuffer, nNewBufferSize ) );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_pBuffer = lpNew;
|
|
|
|
m_nBufferSize = nNewBufferSize;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void MemStream::Flush(){
|
|
|
|
// Nothing to be done
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void FileStream::Flush(){
|
|
|
|
if ( m_hFile == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fflush( m_hFile );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void MemStream::Abort(){
|
|
|
|
Close();
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void FileStream::Abort(){
|
|
|
|
if ( m_hFile != NULL ) {
|
|
|
|
// close but ignore errors
|
|
|
|
if ( m_bCloseOnDelete ) {
|
|
|
|
fclose( m_hFile );
|
|
|
|
}
|
|
|
|
m_hFile = NULL;
|
|
|
|
m_bCloseOnDelete = false;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void MemStream::SetLength( unsigned long nNewLen ){
|
|
|
|
if ( nNewLen > m_nBufferSize ) {
|
|
|
|
GrowFile( nNewLen );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( nNewLen < m_nPosition ) {
|
|
|
|
m_nPosition = nNewLen;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_nFileSize = nNewLen;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void FileStream::SetLength( unsigned long nNewLen ){
|
|
|
|
fseek( m_hFile, nNewLen, SEEK_SET );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long MemStream::GetLength() const {
|
|
|
|
return m_nFileSize;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned long FileStream::GetLength() const {
|
|
|
|
unsigned long nLen, nCur;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// Seek is a non const operation
|
|
|
|
nCur = ftell( m_hFile );
|
|
|
|
fseek( m_hFile, 0, SEEK_END );
|
|
|
|
nLen = ftell( m_hFile );
|
|
|
|
fseek( m_hFile, nCur, SEEK_SET );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return nLen;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|