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)
45 /////////////////////////////////////////////////////////////////////////////
46 // File construction/destruction
48 MemStream::MemStream(){
57 MemStream::MemStream( size_type nLen ){
68 FileStream::FileStream(){
70 m_bCloseOnDelete = false;
73 MemStream::~MemStream(){
84 FileStream::~FileStream(){
85 if ( m_hFile != NULL && m_bCloseOnDelete ) {
90 /////////////////////////////////////////////////////////////////////////////
93 char* MemStream::ReadString( char* pBuf, size_type nMax ){
100 if ( m_nPosition >= m_nFileSize ) {
106 if ( m_nPosition == m_nFileSize ) {
110 ch = m_pBuffer[m_nPosition];
123 char* FileStream::ReadString( char* pBuf, size_type nMax ){
124 return fgets( pBuf, static_cast<int>( nMax ), m_hFile );
127 MemStream::size_type MemStream::read( byte_type* buffer, size_type length ){
132 if ( m_nPosition > m_nFileSize ) {
137 if ( m_nPosition + length > m_nFileSize ) {
138 nRead = m_nFileSize - m_nPosition;
144 memcpy( (unsigned char*)buffer, (unsigned char*)m_pBuffer + m_nPosition, nRead );
145 m_nPosition += nRead;
150 FileStream::size_type FileStream::read( byte_type* buffer, size_type length ){
151 return fread( buffer, 1, length, m_hFile );
154 int MemStream::GetChar(){
155 if ( m_nPosition > m_nFileSize ) {
159 unsigned char* ret = (unsigned char*)m_pBuffer + m_nPosition;
165 int FileStream::GetChar(){
166 return fgetc( m_hFile );
169 MemStream::size_type MemStream::write( const byte_type* buffer, size_type length ){
174 if ( m_nPosition + length > m_nBufferSize ) {
175 GrowFile( m_nPosition + length );
178 memcpy( (unsigned char*)m_pBuffer + m_nPosition, (unsigned char*)buffer, length );
180 m_nPosition += size_type( length );
182 if ( m_nPosition > m_nFileSize ) {
183 m_nFileSize = m_nPosition;
189 FileStream::size_type FileStream::write( const byte_type* buffer, size_type length ){
190 return fwrite( buffer, 1, length, m_hFile );
193 int MemStream::PutChar( int c ){
194 if ( m_nPosition + 1 > m_nBufferSize ) {
195 GrowFile( m_nPosition + 1 );
198 unsigned char* bt = (unsigned char*)m_pBuffer + m_nPosition;
203 if ( m_nPosition > m_nFileSize ) {
204 m_nFileSize = m_nPosition;
210 /*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
211 void FileStream::printf( const char* s, ... ){
215 vfprintf( m_hFile, s, args );
219 /*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
220 void MemStream::printf( const char* s, ... ){
225 vsprintf( buffer, s, args );
227 write( reinterpret_cast<byte_type*>( buffer ), strlen( buffer ) );
230 int FileStream::PutChar( int c ){
231 return fputc( c, m_hFile );
234 bool FileStream::Open( const char *filename, const char *mode ){
235 m_hFile = fopen( filename, mode );
236 m_bCloseOnDelete = true;
238 return ( m_hFile != NULL );
241 void MemStream::Close(){
246 if ( m_pBuffer && m_bAutoDelete ) {
252 void FileStream::Close(){
253 if ( m_hFile != NULL ) {
258 m_bCloseOnDelete = false;
261 int MemStream::Seek( offset_type lOff, int nFrom ){
262 size_type lNewPos = m_nPosition;
264 if ( nFrom == SEEK_SET ) {
267 else if ( nFrom == SEEK_CUR ) {
270 else if ( nFrom == SEEK_END ) {
271 lNewPos = m_nFileSize + lOff;
277 m_nPosition = lNewPos;
279 return static_cast<int>( m_nPosition );
282 int FileStream::Seek( offset_type lOff, int nFrom ){
283 fseek( m_hFile, lOff, nFrom );
285 return ftell( m_hFile );
288 MemStream::position_type MemStream::GetPosition() const {
292 FileStream::position_type FileStream::GetPosition() const {
293 return ftell( m_hFile );
296 void MemStream::GrowFile( size_type nNewLen ){
297 if ( nNewLen > m_nBufferSize ) {
299 size_type nNewBufferSize = m_nBufferSize;
301 // determine new buffer size
302 while ( nNewBufferSize < nNewLen )
303 nNewBufferSize += m_nGrowBytes;
305 // allocate new buffer
306 unsigned char* lpNew;
307 if ( m_pBuffer == NULL ) {
308 lpNew = static_cast<unsigned char*>( malloc( nNewBufferSize ) );
311 lpNew = static_cast<unsigned char*>( realloc( m_pBuffer, nNewBufferSize ) );
315 m_nBufferSize = nNewBufferSize;
319 void MemStream::Flush(){
320 // Nothing to be done
323 void FileStream::Flush(){
324 if ( m_hFile == NULL ) {
331 void MemStream::Abort(){
335 void FileStream::Abort(){
336 if ( m_hFile != NULL ) {
337 // close but ignore errors
338 if ( m_bCloseOnDelete ) {
342 m_bCloseOnDelete = false;
346 void MemStream::SetLength( size_type nNewLen ){
347 if ( nNewLen > m_nBufferSize ) {
351 if ( nNewLen < m_nPosition ) {
352 m_nPosition = nNewLen;
355 m_nFileSize = nNewLen;
358 void FileStream::SetLength( size_type nNewLen ){
359 fseek( m_hFile, static_cast<long>( nNewLen ), SEEK_SET );
362 MemStream::size_type MemStream::GetLength() const {
366 FileStream::size_type FileStream::GetLength() const {
367 size_type nLen, nCur;
369 // Seek is a non const operation
370 nCur = ftell( m_hFile );
371 fseek( m_hFile, 0, SEEK_END );
372 nLen = ftell( m_hFile );
373 fseek( m_hFile, static_cast<long>( nCur ), SEEK_SET );