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
This commit is contained in:
Max Horn 2007-02-20 21:41:01 +00:00
parent cc210d7512
commit 42f11e9e49
2 changed files with 54 additions and 30 deletions

View file

@ -31,14 +31,41 @@ void WriteStream::writeString(const String &str) {
write(str.c_str(), str.size());
}
MemoryReadStream *ReadStream::readStream(uint32 dataSize) {
void *buf = malloc(dataSize);
dataSize = read(buf, dataSize);
assert(dataSize > 0);
return new MemoryReadStream((byte *)buf, dataSize, true);
}
uint32 MemoryReadStream::read(void *dataPtr, uint32 dataSize) {
// Read at most as many bytes as are still available...
if (dataSize > _size - _pos)
dataSize = _size - _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;
}
void MemoryReadStream::seek(int32 offs, int whence) {
// Pre-Condition
assert(_pos <= _bufSize);
assert(_pos <= _size);
switch (whence) {
case SEEK_END:
// SEEK_END works just like SEEK_SET, only 'reversed',
// i.e. from the end.
offs = _bufSize - offs;
offs = _size - offs;
// Fall through
case SEEK_SET:
_ptr = _ptrOrig + offs;
@ -51,7 +78,7 @@ void MemoryReadStream::seek(int32 offs, int whence) {
break;
}
// Post-Condition
assert(_pos <= _bufSize);
assert(_pos <= _size);
}
#define LF 0x0A