diff options
author | Paul Gilbert | 2019-05-04 15:17:40 +1000 |
---|---|---|
committer | Filippos Karapetis | 2019-05-12 11:44:15 +0300 |
commit | f4dacdf34dbbed1869da26ed970bc2f5cf97685e (patch) | |
tree | bb132ad131947a9aa4f99dc9f7fa73d3c32f3860 | |
parent | f4d836b8e92655ae00f9b605d2c013887607bda2 (diff) | |
download | scummvm-rg350-f4dacdf34dbbed1869da26ed970bc2f5cf97685e.tar.gz scummvm-rg350-f4dacdf34dbbed1869da26ed970bc2f5cf97685e.tar.bz2 scummvm-rg350-f4dacdf34dbbed1869da26ed970bc2f5cf97685e.zip |
COMMON: Created SeekableWriteStream class
-rw-r--r-- | common/memstream.h | 42 | ||||
-rw-r--r-- | common/stream.h | 31 |
2 files changed, 54 insertions, 19 deletions
diff --git a/common/memstream.h b/common/memstream.h index 8a8326e4a6..d7c59ffb99 100644 --- a/common/memstream.h +++ b/common/memstream.h @@ -88,7 +88,7 @@ public: * Simple memory based 'stream', which implements the WriteStream interface for * a plain memory block. */ -class MemoryWriteStream : public WriteStream { +class MemoryWriteStream : public SeekableWriteStream { private: const uint32 _bufSize; protected: @@ -111,11 +111,13 @@ public: return dataSize; } - int32 pos() const { return _pos; } - uint32 size() const { return _bufSize; } + virtual int32 pos() const override { return _pos; } + virtual int32 size() const override { return _bufSize; } + + virtual bool err() const override { return _err; } + virtual void clearErr() override { _err = false; } - virtual bool err() const { return _err; } - virtual void clearErr() { _err = false; } + virtual bool seek(int32 offset, int whence = SEEK_SET) override { return false; } }; /** @@ -126,7 +128,8 @@ private: byte *_ptrOrig; public: SeekableMemoryWriteStream(byte *buf, uint32 len) : MemoryWriteStream(buf, len), _ptrOrig(buf) {} - uint32 seek(uint32 offset, int whence = SEEK_SET) { + + virtual bool seek(int32 offset, int whence = SEEK_SET) override { switch (whence) { case SEEK_END: // SEEK_END works just like SEEK_SET, only 'reversed', @@ -143,11 +146,12 @@ public: break; } // Post-Condition - if (_pos > size()) { + if ((int32)_pos > size()) { _pos = size(); _ptr = _ptrOrig + _pos; } - return _pos; + + return true; } }; @@ -156,7 +160,7 @@ public: * A sort of hybrid between MemoryWriteStream and Array classes. A stream * that grows as it's written to. */ -class MemoryWriteStreamDynamic : public WriteStream { +class MemoryWriteStreamDynamic : public SeekableWriteStream { protected: uint32 _capacity; uint32 _size; @@ -201,18 +205,18 @@ public: return dataSize; } - int32 pos() const { return _pos; } - uint32 size() const { return _size; } + virtual int32 pos() const override { return _pos; } + virtual int32 size() const override { return _size; } byte *getData() { return _data; } - bool seek(int32 offset, int whence = SEEK_SET); + virtual bool seek(int32 offset, int whence = SEEK_SET) override; }; /** * MemoryStream based on RingBuffer. Grows if has insufficient buffer size. */ -class MemoryReadWriteStream : public SeekableReadStream, public WriteStream { +class MemoryReadWriteStream : public SeekableReadStream, public SeekableWriteStream { private: uint32 _capacity; uint32 _size; @@ -271,7 +275,7 @@ public: return dataSize; } - virtual uint32 read(void *dataPtr, uint32 dataSize) { + virtual uint32 read(void *dataPtr, uint32 dataSize) override { if (_length < dataSize) { dataSize = _length; _eos = true; @@ -289,11 +293,11 @@ public: return dataSize; } - int32 pos() const { return _pos - _length; } // 'read' position in the stream - int32 size() const { return _size; } // that's also 'write' position in the stream, as it's append-only - bool seek(int32, int) { return false; } - bool eos() const { return _eos; } - void clearErr() { _eos = false; } + virtual int32 pos() const override { return _pos - _length; } + virtual int32 size() const override { return _size; } + virtual bool seek(int32, int) override { return false; } + virtual bool eos() const override { return _eos; } + virtual void clearErr() override { _eos = false; } byte *getData() { return _data; } }; diff --git a/common/stream.h b/common/stream.h index fed81c9192..dfb7d6c9b2 100644 --- a/common/stream.h +++ b/common/stream.h @@ -211,6 +211,37 @@ public: }; /** + * Derived abstract base class for write streams streams that are seekable + */ +class SeekableWriteStream : public WriteStream { +public: + /** + * Sets the stream position indicator for the stream. The new position, + * measured in bytes, is obtained by adding offset bytes to the position + * specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or + * SEEK_END, the offset is relative to the start of the file, the current + * position indicator, or end-of-file, respectively. A successful call + * to the seek() method clears the end-of-file indicator for the stream. + * + * @note The semantics of any implementation of this method are + * supposed to match those of ISO C fseek(). + * + * @param offset the relative offset in bytes + * @param whence the seek reference: SEEK_SET, SEEK_CUR, or SEEK_END + * @return true on success, false in case of a failure + */ + virtual bool seek(int32 offset, int whence = SEEK_SET) = 0; + + /** + * Obtains the current size of the stream, measured in bytes. + * If this value is unknown or can not be computed, -1 is returned. + * + * @return the size of the stream, or -1 if an error occurred + */ + virtual int32 size() const = 0; +}; + +/** * Generic interface for a readable data stream. */ class ReadStream : virtual public Stream { |