2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2004-04-17 09:57:15 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-04-17 09:57:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-08-06 14:30:52 +00:00
|
|
|
#include "common/ptr.h"
|
2004-04-17 09:57:15 +00:00
|
|
|
#include "common/stream.h"
|
2010-11-19 17:03:07 +00:00
|
|
|
#include "common/memstream.h"
|
|
|
|
#include "common/substream.h"
|
2005-04-22 17:40:09 +00:00
|
|
|
#include "common/str.h"
|
2004-04-17 09:57:15 +00:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2005-04-22 17:40:09 +00:00
|
|
|
void WriteStream::writeString(const String &str) {
|
|
|
|
write(str.c_str(), str.size());
|
|
|
|
}
|
2004-04-17 09:57:15 +00:00
|
|
|
|
2010-11-19 17:03:07 +00:00
|
|
|
SeekableReadStream *ReadStream::readStream(uint32 dataSize) {
|
2007-02-20 21:41:01 +00:00
|
|
|
void *buf = malloc(dataSize);
|
|
|
|
dataSize = read(buf, dataSize);
|
|
|
|
assert(dataSize > 0);
|
2009-10-18 19:41:59 +00:00
|
|
|
return new MemoryReadStream((byte *)buf, dataSize, DisposeAfterUse::YES);
|
2007-02-20 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32 MemoryReadStream::read(void *dataPtr, uint32 dataSize) {
|
|
|
|
// Read at most as many bytes as are still available...
|
2008-09-14 22:28:53 +00:00
|
|
|
if (dataSize > _size - _pos) {
|
2007-02-20 21:41:01 +00:00
|
|
|
dataSize = _size - _pos;
|
2008-09-14 22:28:53 +00:00
|
|
|
_eos = true;
|
|
|
|
}
|
2007-02-20 21:41:01 +00:00
|
|
|
memcpy(dataPtr, _ptr, dataSize);
|
|
|
|
|
|
|
|
_ptr += dataSize;
|
|
|
|
_pos += dataSize;
|
|
|
|
|
|
|
|
return dataSize;
|
|
|
|
}
|
|
|
|
|
2008-09-13 16:51:46 +00:00
|
|
|
bool MemoryReadStream::seek(int32 offs, int whence) {
|
2004-12-11 23:16:36 +00:00
|
|
|
// Pre-Condition
|
2007-02-20 21:41:01 +00:00
|
|
|
assert(_pos <= _size);
|
2004-12-11 23:16:36 +00:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_END:
|
|
|
|
// SEEK_END works just like SEEK_SET, only 'reversed',
|
|
|
|
// i.e. from the end.
|
2009-03-05 20:37:53 +00:00
|
|
|
offs = _size + offs;
|
2004-12-11 23:16:36 +00:00
|
|
|
// Fall through
|
|
|
|
case SEEK_SET:
|
|
|
|
_ptr = _ptrOrig + offs;
|
|
|
|
_pos = offs;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
_ptr += offs;
|
|
|
|
_pos += offs;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Post-Condition
|
2007-02-20 21:41:01 +00:00
|
|
|
assert(_pos <= _size);
|
2008-09-14 22:28:53 +00:00
|
|
|
|
|
|
|
// Reset end-of-stream flag on a successful seek
|
2008-12-22 11:22:15 +00:00
|
|
|
_eos = false;
|
2008-09-13 16:51:46 +00:00
|
|
|
return true; // FIXME: STREAM REWRITE
|
2004-05-04 03:27:00 +00:00
|
|
|
}
|
|
|
|
|
2010-11-18 17:02:51 +00:00
|
|
|
bool MemoryWriteStreamDynamic::seek(int32 offs, int whence) {
|
|
|
|
// Pre-Condition
|
|
|
|
assert(_pos <= _size);
|
|
|
|
switch (whence) {
|
|
|
|
case SEEK_END:
|
|
|
|
// SEEK_END works just like SEEK_SET, only 'reversed',
|
|
|
|
// i.e. from the end.
|
|
|
|
offs = _size + offs;
|
|
|
|
// Fall through
|
|
|
|
case SEEK_SET:
|
|
|
|
_ptr = _data + offs;
|
|
|
|
_pos = offs;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
_ptr += offs;
|
|
|
|
_pos += offs;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Post-Condition
|
|
|
|
assert(_pos <= _size);
|
|
|
|
|
|
|
|
return true; // FIXME: STREAM REWRITE
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2008-12-03 22:55:30 +00:00
|
|
|
enum {
|
|
|
|
LF = 0x0A,
|
|
|
|
CR = 0x0D
|
|
|
|
};
|
2005-04-22 17:40:09 +00:00
|
|
|
|
2009-12-30 23:00:55 +00:00
|
|
|
char *SeekableReadStream::readLine(char *buf, size_t bufSize) {
|
2008-07-20 16:47:52 +00:00
|
|
|
assert(buf != 0 && bufSize > 1);
|
|
|
|
char *p = buf;
|
|
|
|
size_t len = 0;
|
2008-07-21 11:33:30 +00:00
|
|
|
char c = 0;
|
2008-07-20 16:47:52 +00:00
|
|
|
|
|
|
|
// If end-of-file occurs before any characters are read, return NULL
|
2008-12-22 11:22:15 +00:00
|
|
|
// and the buffer contents remain unchanged.
|
2008-09-14 22:28:53 +00:00
|
|
|
if (eos() || err()) {
|
2008-07-20 16:47:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-14 22:28:53 +00:00
|
|
|
// Loop as long as there is still free space in the buffer,
|
|
|
|
// and the line has not ended
|
|
|
|
while (len + 1 < bufSize && c != LF) {
|
2008-07-20 16:47:52 +00:00
|
|
|
c = readByte();
|
2008-09-14 22:28:53 +00:00
|
|
|
|
|
|
|
if (eos()) {
|
|
|
|
// If end-of-file occurs before any characters are read, return
|
|
|
|
// NULL and the buffer contents remain unchanged.
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an error occurs, return NULL and the buffer contents
|
|
|
|
// are indeterminate.
|
|
|
|
if (err())
|
2008-07-20 16:47:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Check for CR or CR/LF
|
|
|
|
// * DOS and Windows use CRLF line breaks
|
|
|
|
// * Unix and OS X use LF line breaks
|
|
|
|
// * Macintosh before OS X used CR line breaks
|
|
|
|
if (c == CR) {
|
|
|
|
// Look at the next char -- is it LF? If not, seek back
|
|
|
|
c = readByte();
|
2008-09-14 22:28:53 +00:00
|
|
|
|
|
|
|
if (err()) {
|
|
|
|
return 0; // error: the buffer contents are indeterminate
|
|
|
|
}
|
|
|
|
if (eos()) {
|
|
|
|
// The CR was the last character in the file.
|
|
|
|
// Reset the eos() flag since we successfully finished a line
|
|
|
|
clearErr();
|
|
|
|
} else if (c != LF) {
|
2008-07-20 16:47:52 +00:00
|
|
|
seek(-1, SEEK_CUR);
|
2008-09-14 22:28:53 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:47:52 +00:00
|
|
|
// Treat CR & CR/LF as plain LF
|
|
|
|
c = LF;
|
|
|
|
}
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-07-20 16:47:52 +00:00
|
|
|
*p++ = c;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
2010-06-15 12:33:20 +00:00
|
|
|
// We always terminate the buffer if no error occurred
|
2008-07-20 16:47:52 +00:00
|
|
|
*p = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-09-03 17:53:25 +00:00
|
|
|
String SeekableReadStream::readLine() {
|
|
|
|
// Read a line
|
|
|
|
String line;
|
|
|
|
while (line.lastChar() != '\n') {
|
|
|
|
char buf[256];
|
2009-12-30 23:00:55 +00:00
|
|
|
if (!readLine(buf, 256))
|
2008-09-03 17:53:25 +00:00
|
|
|
break;
|
|
|
|
line += buf;
|
|
|
|
}
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-09-03 18:07:31 +00:00
|
|
|
if (line.lastChar() == '\n')
|
|
|
|
line.deleteLastChar();
|
2008-09-03 17:53:25 +00:00
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-20 16:47:52 +00:00
|
|
|
|
2006-11-13 20:58:21 +00:00
|
|
|
uint32 SubReadStream::read(void *dataPtr, uint32 dataSize) {
|
2008-09-14 22:28:53 +00:00
|
|
|
if (dataSize > _end - _pos) {
|
|
|
|
dataSize = _end - _pos;
|
|
|
|
_eos = true;
|
|
|
|
}
|
2006-11-13 20:58:21 +00:00
|
|
|
|
|
|
|
dataSize = _parentStream->read(dataPtr, dataSize);
|
|
|
|
_pos += dataSize;
|
|
|
|
|
|
|
|
return dataSize;
|
|
|
|
}
|
|
|
|
|
2009-10-18 19:41:59 +00:00
|
|
|
SeekableSubReadStream::SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream)
|
2007-02-19 21:11:13 +00:00
|
|
|
: SubReadStream(parentStream, end, disposeParentStream),
|
2006-11-13 20:58:21 +00:00
|
|
|
_parentStream(parentStream),
|
|
|
|
_begin(begin) {
|
|
|
|
assert(_begin <= _end);
|
|
|
|
_pos = _begin;
|
|
|
|
_parentStream->seek(_pos);
|
2008-09-14 22:28:53 +00:00
|
|
|
_eos = false;
|
2006-11-13 20:58:21 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 16:51:46 +00:00
|
|
|
bool SeekableSubReadStream::seek(int32 offset, int whence) {
|
2006-11-13 20:58:21 +00:00
|
|
|
assert(_pos >= _begin);
|
|
|
|
assert(_pos <= _end);
|
|
|
|
|
2009-09-30 16:16:53 +00:00
|
|
|
switch (whence) {
|
2006-11-13 20:58:21 +00:00
|
|
|
case SEEK_END:
|
2009-03-05 20:37:53 +00:00
|
|
|
offset = size() + offset;
|
2006-11-13 20:58:21 +00:00
|
|
|
// fallthrough
|
|
|
|
case SEEK_SET:
|
|
|
|
_pos = _begin + offset;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
_pos += offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(_pos >= _begin);
|
|
|
|
assert(_pos <= _end);
|
|
|
|
|
2008-09-14 22:28:53 +00:00
|
|
|
bool ret = _parentStream->seek(_pos);
|
|
|
|
if (ret) _eos = false; // reset eos on successful seek
|
|
|
|
|
|
|
|
return ret;
|
2006-11-13 20:58:21 +00:00
|
|
|
}
|
2004-04-29 20:28:42 +00:00
|
|
|
|
2012-03-10 23:51:20 +00:00
|
|
|
uint32 SafeSeekableSubReadStream::read(void *dataPtr, uint32 dataSize) {
|
2011-05-05 19:37:48 +00:00
|
|
|
// Make sure the parent stream is at the right position
|
|
|
|
seek(0, SEEK_CUR);
|
|
|
|
|
|
|
|
return SeekableSubReadStream::read(dataPtr, dataSize);
|
|
|
|
}
|
|
|
|
|
2010-11-18 17:02:51 +00:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class which adds buffering to any given ReadStream.
|
|
|
|
* Users can specify how big the buffer should be, and whether the
|
|
|
|
* wrapped stream should be disposed when the wrapper is disposed.
|
|
|
|
*/
|
|
|
|
class BufferedReadStream : virtual public ReadStream {
|
|
|
|
protected:
|
2011-08-06 14:30:52 +00:00
|
|
|
DisposablePtr<ReadStream> _parentStream;
|
2010-11-18 17:02:51 +00:00
|
|
|
byte *_buf;
|
|
|
|
uint32 _pos;
|
|
|
|
bool _eos; // end of stream
|
|
|
|
uint32 _bufSize;
|
|
|
|
uint32 _realBufSize;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream);
|
|
|
|
virtual ~BufferedReadStream();
|
|
|
|
|
|
|
|
virtual bool eos() const { return _eos; }
|
|
|
|
virtual bool err() const { return _parentStream->err(); }
|
|
|
|
virtual void clearErr() { _eos = false; _parentStream->clearErr(); }
|
|
|
|
|
|
|
|
virtual uint32 read(void *dataPtr, uint32 dataSize);
|
|
|
|
};
|
|
|
|
|
2009-10-18 19:41:59 +00:00
|
|
|
BufferedReadStream::BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream)
|
2011-08-06 14:30:52 +00:00
|
|
|
: _parentStream(parentStream, disposeParentStream),
|
2008-07-30 07:39:41 +00:00
|
|
|
_pos(0),
|
2010-08-25 09:14:41 +00:00
|
|
|
_eos(false),
|
2008-07-30 07:39:41 +00:00
|
|
|
_bufSize(0),
|
|
|
|
_realBufSize(bufSize) {
|
2008-07-29 17:42:19 +00:00
|
|
|
|
|
|
|
assert(parentStream);
|
2010-08-24 11:18:48 +00:00
|
|
|
_buf = new byte[bufSize];
|
2010-09-14 14:57:30 +00:00
|
|
|
assert(_buf);
|
2010-08-24 11:18:48 +00:00
|
|
|
}
|
|
|
|
|
2008-07-29 17:42:19 +00:00
|
|
|
BufferedReadStream::~BufferedReadStream() {
|
2009-05-04 16:35:47 +00:00
|
|
|
delete[] _buf;
|
2008-07-29 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 BufferedReadStream::read(void *dataPtr, uint32 dataSize) {
|
|
|
|
uint32 alreadyRead = 0;
|
|
|
|
const uint32 bufBytesLeft = _bufSize - _pos;
|
|
|
|
|
|
|
|
// Check whether the data left in the buffer suffices....
|
|
|
|
if (dataSize > bufBytesLeft) {
|
|
|
|
// Nope, we need to read more data
|
|
|
|
|
|
|
|
// First, flush the buffer, if it is non-empty
|
|
|
|
if (0 < bufBytesLeft) {
|
|
|
|
memcpy(dataPtr, _buf + _pos, bufBytesLeft);
|
|
|
|
_pos = _bufSize;
|
|
|
|
alreadyRead += bufBytesLeft;
|
|
|
|
dataPtr = (byte *)dataPtr + bufBytesLeft;
|
|
|
|
dataSize -= bufBytesLeft;
|
|
|
|
}
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-07-29 17:42:19 +00:00
|
|
|
// At this point the buffer is empty. Now if the read request
|
|
|
|
// exceeds the buffer size, just satisfy it directly.
|
2010-08-25 14:43:42 +00:00
|
|
|
if (dataSize > _realBufSize) {
|
|
|
|
uint32 n = _parentStream->read(dataPtr, dataSize);
|
|
|
|
if (_parentStream->eos())
|
|
|
|
_eos = true;
|
|
|
|
return alreadyRead + n;
|
|
|
|
}
|
2008-07-29 17:42:19 +00:00
|
|
|
|
|
|
|
// Refill the buffer.
|
|
|
|
// If we didn't read as many bytes as requested, the reason
|
|
|
|
// is EOF or an error. In that case we truncate the buffer
|
|
|
|
// size, as well as the number of bytes we are going to
|
|
|
|
// return to the caller.
|
2008-07-30 07:39:41 +00:00
|
|
|
_bufSize = _parentStream->read(_buf, _realBufSize);
|
|
|
|
_pos = 0;
|
2010-08-25 14:43:42 +00:00
|
|
|
if (_bufSize < dataSize) {
|
|
|
|
// we didn't get enough data from parent
|
|
|
|
if (_parentStream->eos())
|
|
|
|
_eos = true;
|
2008-07-30 07:39:41 +00:00
|
|
|
dataSize = _bufSize;
|
2010-08-25 14:43:42 +00:00
|
|
|
}
|
2008-07-29 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 09:14:41 +00:00
|
|
|
if (dataSize) {
|
|
|
|
// Satisfy the request from the buffer
|
|
|
|
memcpy(dataPtr, _buf + _pos, dataSize);
|
|
|
|
_pos += dataSize;
|
2010-10-12 02:18:11 +00:00
|
|
|
}
|
2008-07-29 17:42:19 +00:00
|
|
|
return alreadyRead + dataSize;
|
|
|
|
}
|
|
|
|
|
2010-11-18 17:02:51 +00:00
|
|
|
} // End of nameless namespace
|
|
|
|
|
|
|
|
|
|
|
|
ReadStream *wrapBufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) {
|
|
|
|
if (parentStream)
|
|
|
|
return new BufferedReadStream(parentStream, bufSize, disposeParentStream);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class which adds buffering to any given SeekableReadStream.
|
|
|
|
* @see BufferedReadStream
|
|
|
|
*/
|
|
|
|
class BufferedSeekableReadStream : public BufferedReadStream, public SeekableReadStream {
|
|
|
|
protected:
|
|
|
|
SeekableReadStream *_parentStream;
|
|
|
|
public:
|
|
|
|
BufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
|
|
|
|
|
|
|
|
virtual int32 pos() const { return _parentStream->pos() - (_bufSize - _pos); }
|
|
|
|
virtual int32 size() const { return _parentStream->size(); }
|
|
|
|
|
|
|
|
virtual bool seek(int32 offset, int whence = SEEK_SET);
|
|
|
|
};
|
|
|
|
|
2009-10-18 19:41:59 +00:00
|
|
|
BufferedSeekableReadStream::BufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream)
|
2008-07-29 17:42:19 +00:00
|
|
|
: BufferedReadStream(parentStream, bufSize, disposeParentStream),
|
|
|
|
_parentStream(parentStream) {
|
|
|
|
}
|
|
|
|
|
2008-09-13 16:51:46 +00:00
|
|
|
bool BufferedSeekableReadStream::seek(int32 offset, int whence) {
|
2008-07-29 17:42:19 +00:00
|
|
|
// If it is a "local" seek, we may get away with "seeking" around
|
|
|
|
// in the buffer only.
|
|
|
|
// Note: We could try to handle SEEK_END and SEEK_SET, too, but
|
|
|
|
// since they are rarely used, it seems not worth the effort.
|
2010-08-25 09:14:41 +00:00
|
|
|
_eos = false; // seeking always cancels EOS
|
2010-10-12 02:18:11 +00:00
|
|
|
|
2008-07-29 17:42:19 +00:00
|
|
|
if (whence == SEEK_CUR && (int)_pos + offset >= 0 && _pos + offset <= _bufSize) {
|
|
|
|
_pos += offset;
|
2010-08-25 14:43:42 +00:00
|
|
|
|
|
|
|
// Note: we do not need to reset parent's eos flag here. It is
|
|
|
|
// sufficient that it is reset when actually seeking in the parent.
|
2008-07-29 17:42:19 +00:00
|
|
|
} else {
|
|
|
|
// Seek was not local enough, so we reset the buffer and
|
2010-08-25 14:43:42 +00:00
|
|
|
// just seek normally in the parent stream.
|
2008-07-29 17:42:19 +00:00
|
|
|
if (whence == SEEK_CUR)
|
|
|
|
offset -= (_bufSize - _pos);
|
|
|
|
_pos = _bufSize;
|
|
|
|
_parentStream->seek(offset, whence);
|
|
|
|
}
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2010-08-25 09:14:41 +00:00
|
|
|
return true;
|
2008-07-29 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2010-11-18 17:02:51 +00:00
|
|
|
} // End of nameless namespace
|
|
|
|
|
|
|
|
SeekableReadStream *wrapBufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream) {
|
|
|
|
if (parentStream)
|
|
|
|
return new BufferedSeekableReadStream(parentStream, bufSize, disposeParentStream);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class which adds buffering to any WriteStream.
|
|
|
|
*/
|
|
|
|
class BufferedWriteStream : public WriteStream {
|
|
|
|
protected:
|
|
|
|
WriteStream *_parentStream;
|
|
|
|
byte *_buf;
|
|
|
|
uint32 _pos;
|
|
|
|
const uint32 _bufSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write out the data in the buffer.
|
|
|
|
*
|
|
|
|
* @note This method is identical to flush() (which actually is
|
|
|
|
* implemented by calling this method), except that it is not
|
|
|
|
* virtual, hence there is less overhead calling it.
|
|
|
|
*/
|
2010-11-18 17:56:21 +00:00
|
|
|
bool flushBuffer() {
|
|
|
|
const uint32 bytesToWrite = _pos;
|
2010-11-18 17:02:51 +00:00
|
|
|
|
2010-11-18 17:56:21 +00:00
|
|
|
if (bytesToWrite) {
|
|
|
|
_pos = 0;
|
|
|
|
if (_parentStream->write(_buf, bytesToWrite) != bytesToWrite)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-24 11:18:48 +00:00
|
|
|
|
2010-11-18 17:56:21 +00:00
|
|
|
public:
|
|
|
|
BufferedWriteStream(WriteStream *parentStream, uint32 bufSize)
|
|
|
|
: _parentStream(parentStream),
|
|
|
|
_pos(0),
|
|
|
|
_bufSize(bufSize) {
|
|
|
|
|
|
|
|
assert(parentStream);
|
|
|
|
_buf = new byte[bufSize];
|
|
|
|
assert(_buf);
|
|
|
|
}
|
2010-08-24 11:18:48 +00:00
|
|
|
|
2010-11-18 17:56:21 +00:00
|
|
|
virtual ~BufferedWriteStream() {
|
|
|
|
const bool flushResult = flushBuffer();
|
|
|
|
assert(flushResult);
|
2010-10-12 02:18:11 +00:00
|
|
|
|
2010-11-18 20:27:15 +00:00
|
|
|
delete _parentStream;
|
2010-10-12 02:18:11 +00:00
|
|
|
|
2010-11-18 17:56:21 +00:00
|
|
|
delete[] _buf;
|
|
|
|
}
|
2010-08-24 11:18:48 +00:00
|
|
|
|
2010-11-18 17:56:21 +00:00
|
|
|
virtual uint32 write(const void *dataPtr, uint32 dataSize) {
|
|
|
|
// check if we have enough space for writing to the buffer
|
|
|
|
if (_bufSize - _pos >= dataSize) {
|
|
|
|
memcpy(_buf + _pos, dataPtr, dataSize);
|
|
|
|
_pos += dataSize;
|
|
|
|
} else if (_bufSize >= dataSize) { // check if we can flush the buffer and load the data
|
|
|
|
const bool flushResult = flushBuffer();
|
|
|
|
assert(flushResult);
|
|
|
|
memcpy(_buf, dataPtr, dataSize);
|
|
|
|
_pos += dataSize;
|
|
|
|
} else { // too big for our buffer
|
|
|
|
const bool flushResult = flushBuffer();
|
|
|
|
assert(flushResult);
|
|
|
|
return _parentStream->write(dataPtr, dataSize);
|
|
|
|
}
|
|
|
|
return dataSize;
|
2010-08-24 11:18:48 +00:00
|
|
|
}
|
|
|
|
|
2010-11-18 17:56:21 +00:00
|
|
|
virtual bool flush() { return flushBuffer(); }
|
2010-10-12 02:18:11 +00:00
|
|
|
|
2010-11-18 17:56:21 +00:00
|
|
|
};
|
2010-08-24 11:18:48 +00:00
|
|
|
|
2010-11-18 17:02:51 +00:00
|
|
|
} // End of nameless namespace
|
2010-06-15 10:34:14 +00:00
|
|
|
|
2010-11-18 20:27:15 +00:00
|
|
|
WriteStream *wrapBufferedWriteStream(WriteStream *parentStream, uint32 bufSize) {
|
|
|
|
if (parentStream)
|
|
|
|
return new BufferedWriteStream(parentStream, bufSize);
|
2010-11-18 17:02:51 +00:00
|
|
|
return 0;
|
2010-06-15 10:34:14 +00:00
|
|
|
}
|
|
|
|
|
2004-04-17 09:57:15 +00:00
|
|
|
} // End of namespace Common
|