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()
58 MemStream::MemStream(size_type nLen)
70 FileStream::FileStream()
73 m_bCloseOnDelete = false;
76 MemStream::~MemStream()
87 FileStream::~FileStream()
89 if (m_hFile != NULL && m_bCloseOnDelete)
93 /////////////////////////////////////////////////////////////////////////////
96 char* MemStream::ReadString(char* pBuf, size_type nMax)
103 if (m_nPosition >= m_nFileSize)
108 if (m_nPosition == m_nFileSize)
111 ch = m_pBuffer[m_nPosition];
123 char* FileStream::ReadString(char* pBuf, size_type nMax)
125 return fgets(pBuf, static_cast<int>(nMax), m_hFile);
128 MemStream::size_type MemStream::read(byte_type* buffer, size_type length)
133 if (m_nPosition > m_nFileSize)
137 if (m_nPosition + length > m_nFileSize)
138 nRead = m_nFileSize - m_nPosition;
142 memcpy((unsigned char*)buffer, (unsigned char*)m_pBuffer + m_nPosition, nRead);
143 m_nPosition += nRead;
148 FileStream::size_type FileStream::read(byte_type* buffer, size_type length)
150 return fread(buffer, 1, length, m_hFile);
153 int MemStream::GetChar()
155 if (m_nPosition > m_nFileSize)
158 unsigned char* ret = (unsigned char*)m_pBuffer + m_nPosition;
164 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);
177 memcpy((unsigned char*)m_pBuffer + m_nPosition, (unsigned char*)buffer, length);
179 m_nPosition += size_type(length);
181 if (m_nPosition > m_nFileSize)
182 m_nFileSize = m_nPosition;
187 FileStream::size_type FileStream::write(const byte_type* buffer, size_type length)
189 return fwrite(buffer, 1, length, m_hFile);
192 int MemStream::PutChar(int c)
194 if (m_nPosition + 1 > m_nBufferSize)
195 GrowFile(m_nPosition + 1);
197 unsigned char* bt = (unsigned char*)m_pBuffer + m_nPosition;
202 if (m_nPosition > m_nFileSize)
203 m_nFileSize = m_nPosition;
208 /*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
209 void FileStream::printf(const char* s, ...)
214 vfprintf(m_hFile, s, args);
218 /*!\todo SPoG suggestion: replace printf with operator >> using c++ iostream and strstream */
219 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)
232 return fputc(c, m_hFile);
235 bool FileStream::Open(const char *filename, const char *mode)
237 m_hFile = fopen(filename, mode);
238 m_bCloseOnDelete = true;
240 return (m_hFile != NULL);
243 void MemStream::Close()
249 if (m_pBuffer && m_bAutoDelete)
254 void FileStream::Close()
260 m_bCloseOnDelete = false;
263 int MemStream::Seek(offset_type lOff, int nFrom)
265 size_type lNewPos = m_nPosition;
267 if (nFrom == SEEK_SET)
269 else if (nFrom == SEEK_CUR)
271 else if (nFrom == SEEK_END)
272 lNewPos = m_nFileSize + lOff;
274 return (position_type)-1;
276 m_nPosition = lNewPos;
278 return static_cast<int>(m_nPosition);
281 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
293 FileStream::position_type FileStream::GetPosition() const
295 return ftell(m_hFile);
298 void MemStream::GrowFile(size_type nNewLen)
300 if (nNewLen > m_nBufferSize)
303 size_type nNewBufferSize = m_nBufferSize;
305 // determine new buffer size
306 while (nNewBufferSize < nNewLen)
307 nNewBufferSize += m_nGrowBytes;
309 // allocate new buffer
310 unsigned char* lpNew;
311 if (m_pBuffer == NULL)
312 lpNew = static_cast<unsigned char*>(malloc(nNewBufferSize));
314 lpNew = static_cast<unsigned char*>(realloc(m_pBuffer, nNewBufferSize));
317 m_nBufferSize = nNewBufferSize;
321 void MemStream::Flush()
323 // Nothing to be done
326 void FileStream::Flush()
334 void MemStream::Abort()
339 void FileStream::Abort()
343 // close but ignore errors
344 if (m_bCloseOnDelete)
347 m_bCloseOnDelete = false;
351 void MemStream::SetLength(size_type nNewLen)
353 if (nNewLen > m_nBufferSize)
356 if (nNewLen < m_nPosition)
357 m_nPosition = nNewLen;
359 m_nFileSize = nNewLen;
362 void FileStream::SetLength(size_type nNewLen)
364 fseek(m_hFile, static_cast<long>(nNewLen), SEEK_SET);
367 MemStream::size_type MemStream::GetLength() const
372 FileStream::size_type FileStream::GetLength() const
374 size_type 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, static_cast<long>(nCur), SEEK_SET);