389
389
virtual String readLine();
394
* SubReadStream provides access to a ReadStream restricted to the range
395
* [currentPosition, currentPosition+end).
397
* Manipulating the parent stream directly /will/ mess up a substream.
398
* Likewise, manipulating two substreams of a parent stream will cause them to
399
* step on each others toes.
401
class SubReadStream : virtual public ReadStream {
403
ReadStream *_parentStream;
404
DisposeAfterUse::Flag _disposeParentStream;
409
SubReadStream(ReadStream *parentStream, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
410
: _parentStream(parentStream),
411
_disposeParentStream(disposeParentStream),
415
assert(parentStream);
418
if (_disposeParentStream)
419
delete _parentStream;
422
virtual bool eos() const { return _eos; }
423
virtual bool err() const { return _parentStream->err(); }
424
virtual void clearErr() { _eos = false; _parentStream->clearErr(); }
425
virtual uint32 read(void *dataPtr, uint32 dataSize);
429
* SeekableSubReadStream provides access to a SeekableReadStream restricted to
430
* the range [begin, end).
431
* The same caveats apply to SeekableSubReadStream as do to SeekableReadStream.
433
* Manipulating the parent stream directly /will/ mess up a substream.
436
class SeekableSubReadStream : public SubReadStream, public SeekableReadStream {
438
SeekableReadStream *_parentStream;
441
SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
443
virtual int32 pos() const { return _pos - _begin; }
444
virtual int32 size() const { return _end - _begin; }
446
virtual bool seek(int32 offset, int whence = SEEK_SET);
450
* This is a wrapper around SeekableSubReadStream, but it adds non-endian
451
* read methods whose endianness is set on the stream creation.
453
* Manipulating the parent stream directly /will/ mess up a substream.
456
class SeekableSubReadStreamEndian : public SeekableSubReadStream {
458
const bool _bigEndian;
461
SeekableSubReadStreamEndian(SeekableReadStream *parentStream, uint32 begin, uint32 end, bool bigEndian = false, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
462
: SeekableSubReadStream(parentStream, begin, end, disposeParentStream), _bigEndian(bigEndian) {
465
uint16 readUint16() {
468
return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
471
uint32 readUint32() {
474
return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
477
FORCEINLINE int16 readSint16() {
478
return (int16)readUint16();
481
FORCEINLINE int32 readSint32() {
482
return (int32)readUint32();
487
* Wrapper class which adds buffering to any given ReadStream.
488
* Users can specify how big the buffer should be, and whether the
489
* wrapped stream should be disposed when the wrapper is disposed.
491
class BufferedReadStream : virtual public ReadStream {
493
ReadStream *_parentStream;
494
DisposeAfterUse::Flag _disposeParentStream;
497
bool _eos; // end of stream
502
BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
503
virtual ~BufferedReadStream();
505
virtual bool eos() const { return _eos; }
506
virtual bool err() const { return _parentStream->err(); }
507
virtual void clearErr() { _eos = false; _parentStream->clearErr(); }
509
virtual uint32 read(void *dataPtr, uint32 dataSize);
513
* Wrapper class which adds buffering to any given SeekableReadStream.
514
* @see BufferedReadStream
516
class BufferedSeekableReadStream : public BufferedReadStream, public SeekableReadStream {
518
SeekableReadStream *_parentStream;
520
BufferedSeekableReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
522
virtual int32 pos() const { return _parentStream->pos() - (_bufSize - _pos); }
523
virtual int32 size() const { return _parentStream->size(); }
525
virtual bool seek(int32 offset, int whence = SEEK_SET);
529
* Wrapper class which adds buffering to any WriteStream.
531
class BufferedWriteStream : public WriteStream {
533
WriteStream *_parentStream;
534
DisposeAfterUse::Flag _disposeParentStream;
538
bool flushBuffer(); // write out the data in the buffer
541
BufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
542
virtual ~BufferedWriteStream();
544
virtual uint32 write(const void *dataPtr, uint32 dataSize);
545
virtual bool flush();
549
* Simple memory based 'stream', which implements the ReadStream interface for
550
* a plain memory block.
552
class MemoryReadStream : public SeekableReadStream {
554
const byte * const _ptrOrig;
559
DisposeAfterUse::Flag _disposeMemory;
565
* This constructor takes a pointer to a memory buffer and a length, and
566
* wraps it. If disposeMemory is true, the MemoryReadStream takes ownership
567
* of the buffer and hence free's it when destructed.
569
MemoryReadStream(const byte *dataPtr, uint32 dataSize, DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) :
575
_disposeMemory(disposeMemory),
578
~MemoryReadStream() {
580
free(const_cast<byte *>(_ptrOrig));
583
void setEnc(byte value) { _encbyte = value; }
585
uint32 read(void *dataPtr, uint32 dataSize);
587
bool eos() const { return _eos; }
588
void clearErr() { _eos = false; }
590
int32 pos() const { return _pos; }
591
int32 size() const { return _size; }
593
bool seek(int32 offs, int whence = SEEK_SET);
598
* This is a wrapper around MemoryReadStream, but it adds non-endian
599
* read methods whose endianness is set on the stream creation.
601
class MemoryReadStreamEndian : public Common::MemoryReadStream {
603
const bool _bigEndian;
606
MemoryReadStreamEndian(const byte *buf, uint32 len, bool bigEndian = false) : MemoryReadStream(buf, len), _bigEndian(bigEndian) {}
608
uint16 readUint16() {
611
return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
614
uint32 readUint32() {
617
return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
620
FORCEINLINE int16 readSint16() {
621
return (int16)readUint16();
624
FORCEINLINE int32 readSint32() {
625
return (int32)readUint32();
630
* Simple memory based 'stream', which implements the WriteStream interface for
631
* a plain memory block.
633
class MemoryWriteStream : public WriteStream {
636
const uint32 _bufSize;
639
MemoryWriteStream(byte *buf, uint32 len) : _ptr(buf), _bufSize(len), _pos(0) {}
641
uint32 write(const void *dataPtr, uint32 dataSize) {
642
// Write at most as many bytes as are still available...
643
if (dataSize > _bufSize - _pos)
644
dataSize = _bufSize - _pos;
645
memcpy(_ptr, dataPtr, dataSize);
651
uint32 pos() const { return _pos; }
652
uint32 size() const { return _bufSize; }
656
* A sort of hybrid between MemoryWriteStream and Array classes. A stream
657
* that grows as it's written to.
659
class MemoryWriteStreamDynamic : public Common::WriteStream {
666
DisposeAfterUse::Flag _disposeMemory;
668
void ensureCapacity(uint32 new_len) {
669
if (new_len <= _capacity)
672
byte *old_data = _data;
674
_capacity = new_len + 32;
675
_data = (byte *)malloc(_capacity);
680
memcpy(_data, old_data, _size);
687
MemoryWriteStreamDynamic(DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) : _capacity(0), _size(0), _ptr(0), _data(0), _pos(0), _disposeMemory(disposeMemory) {}
689
~MemoryWriteStreamDynamic() {
694
uint32 write(const void *dataPtr, uint32 dataSize) {
695
ensureCapacity(_pos + dataSize);
696
memcpy(_ptr, dataPtr, dataSize);
704
uint32 pos() const { return _pos; }
705
uint32 size() const { return _size; }
707
byte *getData() { return _data; }
709
bool seek(int32 offset, int whence = SEEK_SET);
393
* This is a ReadStream mixin subclass which adds non-endian read
394
* methods whose endianness is set during the stream creation.
396
class ReadStreamEndian : virtual public ReadStream {
398
const bool _bigEndian;
401
ReadStreamEndian(bool bigEndian) : _bigEndian(bigEndian) {}
403
bool isBE() const { return _bigEndian; }
405
uint16 readUint16() {
408
return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
411
uint32 readUint32() {
414
return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
417
FORCEINLINE int16 readSint16() {
418
return (int16)readUint16();
421
FORCEINLINE int32 readSint32() {
422
return (int32)readUint32();
427
* This is a SeekableReadStream subclass which adds non-endian read
428
* methods whose endianness is set during the stream creation.
430
class SeekableReadStreamEndian : public SeekableReadStream, public ReadStreamEndian {
432
SeekableReadStreamEndian(bool bigEndian) : ReadStreamEndian(bigEndian) {}
712
436
} // End of namespace Common