diff options
Diffstat (limited to 'common/stream.h')
-rw-r--r-- | common/stream.h | 298 |
1 files changed, 11 insertions, 287 deletions
diff --git a/common/stream.h b/common/stream.h index c6605cb42d..65b4971a72 100644 --- a/common/stream.h +++ b/common/stream.h @@ -32,7 +32,7 @@ namespace Common { class String; -class MemoryReadStream; +class SeekableReadStream; /** * Virtual base class for both ReadStream and WriteStream. @@ -301,7 +301,7 @@ public: * the end of the stream was reached. Which can be determined by * calling err() and eos(). */ - MemoryReadStream *readStream(uint32 dataSize); + SeekableReadStream *readStream(uint32 dataSize); }; @@ -389,221 +389,18 @@ public: virtual String readLine(); }; - -/** - * SubReadStream provides access to a ReadStream restricted to the range - * [currentPosition, currentPosition+end). - * - * Manipulating the parent stream directly /will/ mess up a substream. - * Likewise, manipulating two substreams of a parent stream will cause them to - * step on each others toes. - */ -class SubReadStream : virtual public ReadStream { -protected: - ReadStream *_parentStream; - DisposeAfterUse::Flag _disposeParentStream; - uint32 _pos; - uint32 _end; - bool _eos; -public: - SubReadStream(ReadStream *parentStream, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO) - : _parentStream(parentStream), - _disposeParentStream(disposeParentStream), - _pos(0), - _end(end), - _eos(false) { - assert(parentStream); - } - ~SubReadStream() { - if (_disposeParentStream) - delete _parentStream; - } - - 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); -}; - -/* - * SeekableSubReadStream provides access to a SeekableReadStream restricted to - * the range [begin, end). - * The same caveats apply to SeekableSubReadStream as do to SeekableReadStream. - * - * Manipulating the parent stream directly /will/ mess up a substream. - * @see SubReadStream - */ -class SeekableSubReadStream : public SubReadStream, public SeekableReadStream { -protected: - SeekableReadStream *_parentStream; - uint32 _begin; -public: - SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO); - - virtual int32 pos() const { return _pos - _begin; } - virtual int32 size() const { return _end - _begin; } - - virtual bool seek(int32 offset, int whence = SEEK_SET); -}; - /** - * This is a wrapper around SeekableSubReadStream, but it adds non-endian - * read methods whose endianness is set on the stream creation. - * - * Manipulating the parent stream directly /will/ mess up a substream. - * @see SubReadStream + * This is a ReadStream mixin subclass which adds non-endian read + * methods whose endianness is set during the stream creation. */ -class SeekableSubReadStreamEndian : public SeekableSubReadStream { +class ReadStreamEndian : virtual public ReadStream { private: const bool _bigEndian; public: - SeekableSubReadStreamEndian(SeekableReadStream *parentStream, uint32 begin, uint32 end, bool bigEndian = false, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO) - : SeekableSubReadStream(parentStream, begin, end, disposeParentStream), _bigEndian(bigEndian) { - } + ReadStreamEndian(bool bigEndian) : _bigEndian(bigEndian) {} - uint16 readUint16() { - uint16 val; - read(&val, 2); - return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val); - } - - uint32 readUint32() { - uint32 val; - read(&val, 4); - return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val); - } - - FORCEINLINE int16 readSint16() { - return (int16)readUint16(); - } - - FORCEINLINE int32 readSint32() { - return (int32)readUint32(); - } -}; - -/** - * 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: - ReadStream *_parentStream; - DisposeAfterUse::Flag _disposeParentStream; - byte *_buf; - uint32 _pos; - bool _eos; // end of stream - uint32 _bufSize; - uint32 _realBufSize; - -public: - BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO); - 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); -}; - -/** - * 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); -}; - -/** - * Wrapper class which adds buffering to any WriteStream. - */ -class BufferedWriteStream : public WriteStream { -protected: - WriteStream *_parentStream; - DisposeAfterUse::Flag _disposeParentStream; - byte *_buf; - uint32 _pos; - uint32 _bufSize; - bool flushBuffer(); // write out the data in the buffer - -public: - BufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO); - virtual ~BufferedWriteStream(); - - virtual uint32 write(const void *dataPtr, uint32 dataSize); - virtual bool flush(); -}; - -/** - * Simple memory based 'stream', which implements the ReadStream interface for - * a plain memory block. - */ -class MemoryReadStream : public SeekableReadStream { -private: - const byte * const _ptrOrig; - const byte *_ptr; - const uint32 _size; - uint32 _pos; - byte _encbyte; - DisposeAfterUse::Flag _disposeMemory; - bool _eos; - -public: - - /** - * This constructor takes a pointer to a memory buffer and a length, and - * wraps it. If disposeMemory is true, the MemoryReadStream takes ownership - * of the buffer and hence free's it when destructed. - */ - MemoryReadStream(const byte *dataPtr, uint32 dataSize, DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) : - _ptrOrig(dataPtr), - _ptr(dataPtr), - _size(dataSize), - _pos(0), - _encbyte(0), - _disposeMemory(disposeMemory), - _eos(false) {} - - ~MemoryReadStream() { - if (_disposeMemory) - free(const_cast<byte *>(_ptrOrig)); - } - - void setEnc(byte value) { _encbyte = value; } - - uint32 read(void *dataPtr, uint32 dataSize); - - bool eos() const { return _eos; } - void clearErr() { _eos = false; } - - int32 pos() const { return _pos; } - int32 size() const { return _size; } - - bool seek(int32 offs, int whence = SEEK_SET); -}; - - -/** - * This is a wrapper around MemoryReadStream, but it adds non-endian - * read methods whose endianness is set on the stream creation. - */ -class MemoryReadStreamEndian : public Common::MemoryReadStream { -private: - const bool _bigEndian; - -public: - MemoryReadStreamEndian(const byte *buf, uint32 len, bool bigEndian = false) : MemoryReadStream(buf, len), _bigEndian(bigEndian) {} + bool isBE() const { return _bigEndian; } uint16 readUint16() { uint16 val; @@ -627,87 +424,14 @@ public: }; /** - * Simple memory based 'stream', which implements the WriteStream interface for - * a plain memory block. + * This is a SeekableReadStream subclass which adds non-endian read + * methods whose endianness is set during the stream creation. */ -class MemoryWriteStream : public WriteStream { -private: - byte *_ptr; - const uint32 _bufSize; - uint32 _pos; +class SeekableReadStreamEndian : public SeekableReadStream, public ReadStreamEndian { public: - MemoryWriteStream(byte *buf, uint32 len) : _ptr(buf), _bufSize(len), _pos(0) {} - - uint32 write(const void *dataPtr, uint32 dataSize) { - // Write at most as many bytes as are still available... - if (dataSize > _bufSize - _pos) - dataSize = _bufSize - _pos; - memcpy(_ptr, dataPtr, dataSize); - _ptr += dataSize; - _pos += dataSize; - return dataSize; - } - - uint32 pos() const { return _pos; } - uint32 size() const { return _bufSize; } + SeekableReadStreamEndian(bool bigEndian) : ReadStreamEndian(bigEndian) {} }; -/** - * A sort of hybrid between MemoryWriteStream and Array classes. A stream - * that grows as it's written to. - */ -class MemoryWriteStreamDynamic : public Common::WriteStream { -private: - uint32 _capacity; - uint32 _size; - byte *_ptr; - byte *_data; - uint32 _pos; - DisposeAfterUse::Flag _disposeMemory; - - void ensureCapacity(uint32 new_len) { - if (new_len <= _capacity) - return; - - byte *old_data = _data; - - _capacity = new_len + 32; - _data = (byte *)malloc(_capacity); - _ptr = _data + _pos; - - if (old_data) { - // Copy old data - memcpy(_data, old_data, _size); - free(old_data); - } - - _size = new_len; - } -public: - MemoryWriteStreamDynamic(DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) : _capacity(0), _size(0), _ptr(0), _data(0), _pos(0), _disposeMemory(disposeMemory) {} - - ~MemoryWriteStreamDynamic() { - if (_disposeMemory) - free(_data); - } - - uint32 write(const void *dataPtr, uint32 dataSize) { - ensureCapacity(_pos + dataSize); - memcpy(_ptr, dataPtr, dataSize); - _ptr += dataSize; - _pos += dataSize; - if (_pos > _size) - _size = _pos; - return dataSize; - } - - uint32 pos() const { return _pos; } - uint32 size() const { return _size; } - - byte *getData() { return _data; } - - bool seek(int32 offset, int whence = SEEK_SET); -}; } // End of namespace Common |