aboutsummaryrefslogtreecommitdiff
path: root/common/stream.h
diff options
context:
space:
mode:
authorMax Horn2007-02-20 21:41:01 +0000
committerMax Horn2007-02-20 21:41:01 +0000
commit42f11e9e490971f6fea8044ed9c830bca4ccb897 (patch)
tree9d20318fc95d1ab5f19f1beb84daadcaacd02b2d /common/stream.h
parentcc210d75129619afe9c2dad6470bfdebb0d494a3 (diff)
downloadscummvm-rg350-42f11e9e490971f6fea8044ed9c830bca4ccb897.tar.gz
scummvm-rg350-42f11e9e490971f6fea8044ed9c830bca4ccb897.tar.bz2
scummvm-rg350-42f11e9e490971f6fea8044ed9c830bca4ccb897.zip
Added new ReadStream::readStream method which can be used to read a portion of an arbitrary ReadStream into a memory buffer wrapped by a MemoryReadStream
svn-id: r25754
Diffstat (limited to 'common/stream.h')
-rw-r--r--common/stream.h51
1 files changed, 24 insertions, 27 deletions
diff --git a/common/stream.h b/common/stream.h
index 85b4315aed..3332b1beda 100644
--- a/common/stream.h
+++ b/common/stream.h
@@ -29,6 +29,7 @@
namespace Common {
class String;
+class MemoryReadStream;
/**
* Virtual base class for both ReadStream and WriteStream.
@@ -201,6 +202,13 @@ public:
int32 readSint32BE() {
return (int32)readUint32BE();
}
+
+ /**
+ * Read the specified amount of data into a malloc'ed buffer
+ * which then is wrapped into a MemoryReadStream.
+ */
+ MemoryReadStream *readStream(uint32 dataSize);
+
};
@@ -284,51 +292,40 @@ public:
*/
class MemoryReadStream : public SeekableReadStream {
private:
- const byte *_ptr;
const byte * const _ptrOrig;
- const uint32 _bufSize;
+ const byte *_ptr;
+ const uint32 _size;
uint32 _pos;
byte _encbyte;
bool _disposeMemory;
public:
- MemoryReadStream(const byte *buf, uint32 len, bool disposeMemory = false) :
- _ptr(buf),
- _ptrOrig(buf),
- _bufSize(len),
+
+ /**
+ * 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, bool disposeMemory = false) :
+ _ptrOrig(dataPtr),
+ _ptr(dataPtr),
+ _size(dataSize),
_pos(0),
_encbyte(0),
_disposeMemory(disposeMemory) {}
~MemoryReadStream() {
if (_disposeMemory)
- free((void *)_ptrOrig);
+ free(const_cast<byte *>(_ptrOrig));
}
void setEnc(byte value) { _encbyte = value; }
- uint32 read(void *dataPtr, uint32 dataSize) {
- // Read at most as many bytes as are still available...
- if (dataSize > _bufSize - _pos)
- dataSize = _bufSize - _pos;
- memcpy(dataPtr, _ptr, dataSize);
-
- if (_encbyte) {
- byte *p = (byte *)dataPtr;
- byte *end = p + dataSize;
- while (p < end)
- *p++ ^= _encbyte;
- }
-
- _ptr += dataSize;
- _pos += dataSize;
-
- return dataSize;
- }
+ uint32 read(void *dataPtr, uint32 dataSize);
- bool eos() const { return _pos == _bufSize; }
+ bool eos() const { return _pos == _size; }
uint32 pos() const { return _pos; }
- uint32 size() const { return _bufSize; }
+ uint32 size() const { return _size; }
void seek(int32 offs, int whence = SEEK_SET);
};