1 /* ScummVM - Graphic Adventure Engine
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #ifndef COMMON_STREAM_H
27 #define COMMON_STREAM_H
29 #include "common/scummsys.h"
34 class MemoryReadStream
;
37 * Virtual base class for both ReadStream and WriteStream.
44 * Returns true if an I/O failure occurred.
45 * This flag is never cleared automatically. In order to clear it,
46 * client code has to call clearErr() explicitly.
48 virtual bool err() const { return false; }
51 * Reset the I/O error status as returned by err().
52 * For a ReadStream, also reset the end-of-stream status returned by eos().
54 virtual void clearErr() {}
58 * Generic interface for a writable data stream.
60 class WriteStream
: virtual public Stream
{
63 * Write data into the stream. Subclasses must implement this
64 * method; all other write methods are implemented using it.
66 * @param dataPtr pointer to the data to be written
67 * @param dataSize number of bytes to be written
68 * @return the number of bytes which were actually written.
70 virtual uint32
write(const void *dataPtr
, uint32 dataSize
) = 0;
73 * Commit any buffered data to the underlying channel or
74 * storage medium; unbuffered streams can use the default
77 * @return true on success, false in case of a failure
79 virtual bool flush() { return true; }
82 * Finalize and close this stream. To be called right before this
83 * stream instance is deleted. The goal here is to enable calling
84 * code to detect and handle I/O errors which might occur when
85 * closing (and this flushing, if buffered) the stream.
87 * After this method has been called, no further writes may be
88 * performed on the stream. Calling err() is allowed.
90 * By default, this just flushes the stream.
92 virtual void finalize() {
97 // The remaining methods all have default implementations; subclasses
98 // need not (and should not) overload them.
100 void writeByte(byte value
) {
104 void writeSByte(int8 value
) {
108 void writeUint16LE(uint16 value
) {
109 writeByte((byte
)(value
& 0xff));
110 writeByte((byte
)(value
>> 8));
113 void writeUint32LE(uint32 value
) {
114 writeUint16LE((uint16
)(value
& 0xffff));
115 writeUint16LE((uint16
)(value
>> 16));
118 void writeUint16BE(uint16 value
) {
119 writeByte((byte
)(value
>> 8));
120 writeByte((byte
)(value
& 0xff));
123 void writeUint32BE(uint32 value
) {
124 writeUint16BE((uint16
)(value
>> 16));
125 writeUint16BE((uint16
)(value
& 0xffff));
128 void writeSint16LE(int16 value
) {
129 writeUint16LE((uint16
)value
);
132 void writeSint32LE(int32 value
) {
133 writeUint32LE((uint32
)value
);
136 void writeSint16BE(int16 value
) {
137 writeUint16BE((uint16
)value
);
140 void writeSint32BE(int32 value
) {
141 writeUint32BE((uint32
)value
);
145 * Write the given string to the stream.
146 * This writes str.size() characters, but no terminating zero byte.
148 void writeString(const String
&str
);
153 * Generic interface for a readable data stream.
155 class ReadStream
: virtual public Stream
{
158 * Returns true if a read failed because the stream has been reached.
159 * This flag is cleared by clearErr().
160 * For a SeekableReadStream, it is also cleared by a successful seek.
162 virtual bool eos() const = 0;
165 * Read data from the stream. Subclasses must implement this
166 * method; all other read methods are implemented using it.
168 * @param dataPtr pointer to a buffer into which the data is read
169 * @param dataSize number of bytes to be read
170 * @return the number of bytes which were actually read.
172 virtual uint32
read(void *dataPtr
, uint32 dataSize
) = 0;
175 // The remaining methods all have default implementations; subclasses
176 // in general should not overload them.
180 * Default implementation for backward compatibility
182 inline bool ioFailed() { return (eos() || err()); }
185 * Read an unsigned byte from the stream and return it.
186 * Performs no error checking. The return value is undefined
187 * if a read error occurred (for which client code can check by
188 * calling err() and eos() ).
197 * Read a signed byte from the stream and return it.
198 * Performs no error checking. The return value is undefined
199 * if a read error occurred (for which client code can check by
200 * calling err() and eos() ).
209 * Read an unsigned 16-bit word stored in little endian (LSB first) order
210 * from the stream and return it.
211 * Performs no error checking. The return value is undefined
212 * if a read error occurred (for which client code can check by
213 * calling err() and eos() ).
215 uint16
readUint16LE() {
216 uint16 a
= readByte();
217 uint16 b
= readByte();
222 * Read an unsigned 32-bit word stored in little endian (LSB first) order
223 * from the stream and return it.
224 * Performs no error checking. The return value is undefined
225 * if a read error occurred (for which client code can check by
226 * calling err() and eos() ).
228 uint32
readUint32LE() {
229 uint32 a
= readUint16LE();
230 uint32 b
= readUint16LE();
231 return (b
<< 16) | a
;
235 * Read an unsigned 16-bit word stored in big endian (MSB first) order
236 * from the stream and return it.
237 * Performs no error checking. The return value is undefined
238 * if a read error occurred (for which client code can check by
239 * calling err() and eos() ).
241 uint16
readUint16BE() {
242 uint16 b
= readByte();
243 uint16 a
= readByte();
248 * Read an unsigned 32-bit word stored in big endian (MSB first) order
249 * from the stream and return it.
250 * Performs no error checking. The return value is undefined
251 * if a read error occurred (for which client code can check by
252 * calling err() and eos() ).
254 uint32
readUint32BE() {
255 uint32 b
= readUint16BE();
256 uint32 a
= readUint16BE();
257 return (b
<< 16) | a
;
261 * Read a signed 16-bit word stored in little endian (LSB first) order
262 * from the stream and return it.
263 * Performs no error checking. The return value is undefined
264 * if a read error occurred (for which client code can check by
265 * calling err() and eos() ).
267 int16
readSint16LE() {
268 return (int16
)readUint16LE();
272 * Read a signed 32-bit word stored in little endian (LSB first) order
273 * from the stream and return it.
274 * Performs no error checking. The return value is undefined
275 * if a read error occurred (for which client code can check by
276 * calling err() and eos() ).
278 int32
readSint32LE() {
279 return (int32
)readUint32LE();
283 * Read a signed 16-bit word stored in big endian (MSB first) order
284 * from the stream and return it.
285 * Performs no error checking. The return value is undefined
286 * if a read error occurred (for which client code can check by
287 * calling err() and eos() ).
289 int16
readSint16BE() {
290 return (int16
)readUint16BE();
294 * Read a signed 32-bit word stored in big endian (MSB first) order
295 * from the stream and return it.
296 * Performs no error checking. The return value is undefined
297 * if a read error occurred (for which client code can check by
298 * calling err() and eos() ).
300 int32
readSint32BE() {
301 return (int32
)readUint32BE();
305 * Read the specified amount of data into a malloc'ed buffer
306 * which then is wrapped into a MemoryReadStream.
307 * The returned stream might contain less data than requested,
308 * if reading more failed, because of an I/O error or because
309 * the end of the stream was reached. Which can be determined by
310 * calling err() and eos().
312 MemoryReadStream
*readStream(uint32 dataSize
);
318 * Interface for a seekable & readable data stream.
320 * @todo Get rid of SEEK_SET, SEEK_CUR, or SEEK_END, use our own constants
322 class SeekableReadStream
: virtual public ReadStream
{
326 * Obtains the current value of the stream position indicator of the
329 * @return the current position indicator, or -1 if an error occurred.
331 virtual int32
pos() const = 0;
334 * Obtains the total size of the stream, measured in bytes.
335 * If this value is unknown or can not be computed, -1 is returned.
337 * @return the size of the stream, or -1 if an error occurred
339 virtual int32
size() const = 0;
342 * Sets the stream position indicator for the stream. The new position,
343 * measured in bytes, is obtained by adding offset bytes to the position
344 * specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or
345 * SEEK_END, the offset is relative to the start of the file, the current
346 * position indicator, or end-of-file, respectively. A successful call
347 * to the seek() method clears the end-of-file indicator for the stream.
349 * @param offset the relative offset in bytes
350 * @param whence the seek reference: SEEK_SET, SEEK_CUR, or SEEK_END
351 * @return true on success, false in case of a failure
353 virtual bool seek(int32 offset
, int whence
= SEEK_SET
) = 0;
356 * TODO: Get rid of this??? Or keep it and document it
357 * @return true on success, false in case of a failure
359 virtual bool skip(uint32 offset
) { return seek(offset
, SEEK_CUR
); }
362 * Reads at most one less than the number of characters specified
363 * by bufSize from the and stores them in the string buf. Reading
364 * stops when the end of a line is reached (CR, CR/LF or LF), and
365 * at end-of-file or error. The newline, if any, is retained (CR
366 * and CR/LF are translated to LF = 0xA = '\n'). If any characters
367 * are read and there is no error, a `\0' character is appended
370 * Upon successful completion, return a pointer to the string. If
371 * end-of-file occurs before any characters are read, returns NULL
372 * and the buffer contents remain unchanged. If an error occurs,
373 * returns NULL and the buffer contents are indeterminate.
374 * This method does not distinguish between end-of-file and error;
375 * callers must use err() or eos() to determine which occurred.
377 * @note This methods is closely modeled after the standard fgets()
378 * function from stdio.h.
380 * @param buf the buffer to store into
381 * @param bufSize the size of the buffer
382 * @return a pointer to the read string, or NULL if an error occurred
384 virtual char *readLine_NEW(char *s
, size_t bufSize
);
388 * Reads a full line and returns it as a Common::String. Reading
389 * stops when the end of a line is reached (CR, CR/LF or LF), and
390 * at end-of-file or error.
392 * Upon successful completion, return a string with the content
393 * of the line, *without* the end of a line marker. This method
394 * does not indicate whether an error occured. Callers must use
395 * err() or eos() to determine whether an exception occurred.
397 virtual String
readLine();
401 * SubReadStream provides access to a ReadStream restricted to the range
402 * [currentPosition, currentPosition+end).
403 * Manipulating the parent stream directly /will/ mess up a substream.
404 * Likewise, manipulating two substreams of a parent stream will cause them to
405 * step on each others toes.
407 class SubReadStream
: virtual public ReadStream
{
409 ReadStream
*_parentStream
;
410 bool _disposeParentStream
;
415 SubReadStream(ReadStream
*parentStream
, uint32 end
, bool disposeParentStream
= false)
416 : _parentStream(parentStream
),
417 _disposeParentStream(disposeParentStream
),
421 assert(parentStream
);
424 if (_disposeParentStream
) delete _parentStream
;
427 virtual bool eos() const { return _eos
; }
428 virtual bool err() const { return _parentStream
->err(); }
429 virtual void clearErr() { _eos
= false; _parentStream
->clearErr(); }
430 virtual uint32
read(void *dataPtr
, uint32 dataSize
);
434 * SeekableSubReadStream provides access to a SeekableReadStream restricted to
435 * the range [begin, end).
436 * The same caveats apply to SeekableSubReadStream as do to SeekableReadStream.
438 class SeekableSubReadStream
: public SubReadStream
, public SeekableReadStream
{
440 SeekableReadStream
*_parentStream
;
443 SeekableSubReadStream(SeekableReadStream
*parentStream
, uint32 begin
, uint32 end
, bool disposeParentStream
= false);
445 virtual int32
pos() const { return _pos
- _begin
; }
446 virtual int32
size() const { return _end
- _begin
; }
448 virtual bool seek(int32 offset
, int whence
= SEEK_SET
);
452 * This is a wrapper around SeekableSubReadStream, but it adds non-endian
453 * read methods whose endianness is set on the stream creation.
455 class SeekableSubReadStreamEndian
: public SeekableSubReadStream
{
459 SeekableSubReadStreamEndian(SeekableReadStream
*parentStream
, uint32 begin
, uint32 end
, bool bigEndian
= false, bool disposeParentStream
= false)
460 : SeekableSubReadStream(parentStream
, begin
, end
, disposeParentStream
), _bigEndian(bigEndian
) {
463 inline uint16
readUint16() {
464 return (_bigEndian
) ? readUint16BE() : readUint16LE();
467 inline uint32
readUint32() {
468 return (_bigEndian
) ? readUint32BE() : readUint32LE();
471 inline int16
readSint16() {
472 return (int16
)readUint16();
475 inline int32
readSint32() {
476 return (int32
)readUint32();
481 * Wrapper class which adds buffering to any given ReadStream.
482 * Users can specify how big the buffer should be, and whether the
483 * wrapped stream should be disposed when the wrapper is disposed.
485 class BufferedReadStream
: virtual public ReadStream
{
487 ReadStream
*_parentStream
;
488 bool _disposeParentStream
;
495 BufferedReadStream(ReadStream
*parentStream
, uint32 bufSize
, bool disposeParentStream
= false);
496 ~BufferedReadStream();
498 virtual bool eos() const { return (_pos
== _bufSize
) && _parentStream
->eos(); }
499 virtual bool err() const { return _parentStream
->err(); }
500 virtual void clearErr() { _parentStream
->clearErr(); }
502 virtual uint32
read(void *dataPtr
, uint32 dataSize
);
506 * Wrapper class which adds buffering to any given SeekableReadStream.
507 * @see BufferedReadStream
509 class BufferedSeekableReadStream
: public BufferedReadStream
, public SeekableReadStream
{
511 SeekableReadStream
*_parentStream
;
513 BufferedSeekableReadStream(SeekableReadStream
*parentStream
, uint32 bufSize
, bool disposeParentStream
= false);
515 virtual int32
pos() const { return _parentStream
->pos() - (_bufSize
- _pos
); }
516 virtual int32
size() const { return _parentStream
->size(); }
518 virtual bool seek(int32 offset
, int whence
= SEEK_SET
);
524 * Simple memory based 'stream', which implements the ReadStream interface for
525 * a plain memory block.
527 class MemoryReadStream
: public SeekableReadStream
{
529 const byte
* const _ptrOrig
;
540 * This constructor takes a pointer to a memory buffer and a length, and
541 * wraps it. If disposeMemory is true, the MemoryReadStream takes ownership
542 * of the buffer and hence free's it when destructed.
544 MemoryReadStream(const byte
*dataPtr
, uint32 dataSize
, bool disposeMemory
= false) :
550 _disposeMemory(disposeMemory
),
553 ~MemoryReadStream() {
555 free(const_cast<byte
*>(_ptrOrig
));
558 void setEnc(byte value
) { _encbyte
= value
; }
560 uint32
read(void *dataPtr
, uint32 dataSize
);
562 bool eos() const { return _eos
; }
563 void clearErr() { _eos
= false; }
565 int32
pos() const { return _pos
; }
566 int32
size() const { return _size
; }
568 bool seek(int32 offs
, int whence
= SEEK_SET
);
573 * This is a wrapper around MemoryReadStream, but it adds non-endian
574 * read methods whose endianness is set on the stream creation.
576 class MemoryReadStreamEndian
: public Common::MemoryReadStream
{
580 MemoryReadStreamEndian(const byte
*buf
, uint32 len
, bool bigEndian
= false) : MemoryReadStream(buf
, len
), _bigEndian(bigEndian
) {}
582 inline uint16
readUint16() {
583 return (_bigEndian
) ? readUint16BE() : readUint16LE();
586 inline uint32
readUint32() {
587 return (_bigEndian
) ? readUint32BE() : readUint32LE();
590 inline int16
readSint16() {
591 return (int16
)readUint16();
594 inline int32
readSint32() {
595 return (int32
)readUint32();
600 * Simple memory based 'stream', which implements the WriteStream interface for
601 * a plain memory block.
603 class MemoryWriteStream
: public WriteStream
{
606 const uint32 _bufSize
;
609 MemoryWriteStream(byte
*buf
, uint32 len
) : _ptr(buf
), _bufSize(len
), _pos(0) {}
611 uint32
write(const void *dataPtr
, uint32 dataSize
) {
612 // Write at most as many bytes as are still available...
613 if (dataSize
> _bufSize
- _pos
)
614 dataSize
= _bufSize
- _pos
;
615 memcpy(_ptr
, dataPtr
, dataSize
);
621 uint32
pos() const { return _pos
; }
622 uint32
size() const { return _bufSize
; }
626 * A sort of hybrid between MemoryWriteStream and Array classes. A stream
627 * that grows as it's written to.
629 class MemoryWriteStreamDynamic
: public Common::WriteStream
{
638 void ensureCapacity(uint32 new_len
) {
639 if (new_len
<= _capacity
)
642 byte
*old_data
= _data
;
644 _capacity
= new_len
+ 32;
645 _data
= new byte
[_capacity
];
650 memcpy(_data
, old_data
, _size
);
657 MemoryWriteStreamDynamic(bool disposeMemory
= false) : _capacity(0), _size(0), _ptr(0), _data(0), _pos(0), _disposeMemory(disposeMemory
) {}
659 ~MemoryWriteStreamDynamic() {
664 uint32
write(const void *dataPtr
, uint32 dataSize
) {
665 ensureCapacity(_pos
+ dataSize
);
666 memcpy(_ptr
, dataPtr
, dataSize
);
674 uint32
pos() const { return _pos
; }
675 uint32
size() const { return _size
; }
677 byte
*getData() { return _data
; }
680 } // End of namespace Common