mirror of
https://github.com/scummvm/scummvm.git
synced 2026-06-20 05:45:29 +00:00
COMMON: Created SeekableWriteStream class
This commit is contained in:
committed by
Filippos Karapetis
parent
f4d836b8e9
commit
f4dacdf34d
+23
-19
@@ -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 { return _err; }
|
||||
virtual void clearErr() { _err = false; }
|
||||
virtual bool err() const override { return _err; }
|
||||
virtual void clearErr() override { _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; }
|
||||
};
|
||||
|
||||
@@ -210,6 +210,37 @@ public:
|
||||
void writeString(const String &str);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user