ScummVM API documentation
stream.h
1/* ScummVM - Graphic Adventure Engine
2 *
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.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef COMMON_STREAM_H
23#define COMMON_STREAM_H
24
25#include "common/endian.h"
26#include "common/ptr.h"
27#include "common/scummsys.h"
28#include "common/str.h"
29#include "common/data-io.h"
30
31namespace Common {
32
42class ReadStream;
43class SeekableReadStream;
44
48class Stream {
49public:
50 virtual ~Stream() {}
51
61 virtual bool err() const { return false; }
62
71 virtual void clearErr() {}
72};
73
77class WriteStream : virtual public Stream {
78public:
91 virtual uint32 write(const void *dataPtr, uint32 dataSize) = 0;
92
103 virtual bool flush() { return true; }
104
118 virtual void finalize() {
119 flush();
120 }
121
127 virtual int64 pos() const = 0;
128
140 void writeByte(byte value) {
141 write(&value, 1);
142 }
146 void writeSByte(int8 value) {
147 write(&value, 1);
148 }
152 void writeUint16LE(uint16 value) {
153 value = TO_LE_16(value);
154 write(&value, 2);
155 }
159 void writeUint32LE(uint32 value) {
160 value = TO_LE_32(value);
161 write(&value, 4);
162 }
166 void writeUint64LE(uint64 value) {
167 value = TO_LE_64(value);
168 write(&value, 8);
169 }
173 void writeUint16BE(uint16 value) {
174 value = TO_BE_16(value);
175 write(&value, 2);
176 }
180 void writeUint32BE(uint32 value) {
181 value = TO_BE_32(value);
182 write(&value, 4);
183 }
187 void writeUint64BE(uint64 value) {
188 value = TO_BE_64(value);
189 write(&value, 8);
190 }
194 FORCEINLINE void writeSint16LE(int16 value) {
195 writeUint16LE((uint16)value);
196 }
200 FORCEINLINE void writeSint32LE(int32 value) {
201 writeUint32LE((uint32)value);
202 }
206 FORCEINLINE void writeSint64LE(int64 value) {
207 writeUint64LE((uint64)value);
208 }
212 FORCEINLINE void writeSint16BE(int16 value) {
213 writeUint16BE((uint16)value);
214 }
218 FORCEINLINE void writeSint32BE(int32 value) {
219 writeUint32BE((uint32)value);
220 }
224 FORCEINLINE void writeSint64BE(int64 value) {
225 writeUint64BE((uint64)value);
226 }
227
228
233 FORCEINLINE void writeFloatLE(float value) {
234 uint32 n;
235
236 memcpy(&n, &value, 4);
237
238 writeUint32LE(n);
239 }
240
241
246 FORCEINLINE void writeFloatBE(float value) {
247 uint32 n;
248
249 memcpy(&n, &value, 4);
250
251 writeUint32BE(n);
252 }
253
258 FORCEINLINE void writeDoubleLE(double value) {
259 uint64 n;
260
261 memcpy(&n, &value, 8);
262
263 writeUint64LE(n);
264 }
265
266
271 FORCEINLINE void writeDoubleBE(double value) {
272 uint64 n;
273
274 memcpy(&n, &value, 8);
275
276 writeUint64BE(n);
277 }
278
283 template<class TDataFormat, class... T>
284 bool writeMultiple(const TDataFormat &dataFormat, const T &...values) {
285 const TDataFormat dataFormatCopy = dataFormat; // Copy to help compiler alias analysis, parameter is const ref to ensure TDataFormat is a concrete type
286
287 byte buffer[DataMultipleIO<TDataFormat, T...>::kMaxSize];
288 const uint actualSize = DataMultipleIO<TDataFormat, T...>::computeSize(dataFormatCopy);
289
290 DataMultipleIO<TDataFormat, T...>::encode(dataFormatCopy, buffer, values...);
291
292 if (this->write(buffer, actualSize) != actualSize)
293 return false;
294
295 return true;
296 }
297
302 template<class... T>
303 inline bool writeMultipleEndian(bool isLittle, const T &...values) {
304 return this->writeMultiple<EndianStorageFormat, T...>(isLittle ? EndianStorageFormat::Little : EndianStorageFormat::Big, values...);
305 }
306
311 template<class... T>
312 inline bool writeMultipleLE(const T &...values) {
313 return this->writeMultiple<EndianStorageFormat, T...>(EndianStorageFormat::Little, values...);
314 }
315
320 template<class... T>
321 inline bool writeMultipleBE(const T &...values) {
322 return this->writeMultiple<EndianStorageFormat, T...>(EndianStorageFormat::Big, values...);
323 }
324
331 uint32 writeStream(ReadStream *stream, uint32 dataSize);
338 uint32 writeStream(ReadStream *stream);
339
344 void writeString(const String &str);
346};
347
352public:
370 virtual bool seek(int64 offset, int whence = SEEK_SET) = 0;
371
379 virtual int64 size() const = 0;
380};
381
385class ReadStream : virtual public Stream {
386public:
387 ReadStream() {}
388
400 virtual bool eos() const = 0;
401
417 virtual uint32 read(void *dataPtr, uint32 dataSize) = 0;
418
434 byte readByte() {
435 byte b = 0; // FIXME: remove initialisation
436 read(&b, 1);
437 return b;
438 }
439
447 FORCEINLINE int8 readSByte() {
448 return (int8)readByte();
449 }
450
459 uint16 readUint16LE() {
460 uint16 val;
461 read(&val, 2);
462 return FROM_LE_16(val);
463 }
464
473 uint32 readUint32LE() {
474 uint32 val;
475 read(&val, 4);
476 return FROM_LE_32(val);
477 }
478
487 uint64 readUint64LE() {
488 uint64 val;
489 read(&val, 8);
490 return FROM_LE_64(val);
491 }
492
501 uint16 readUint16BE() {
502 uint16 val;
503 read(&val, 2);
504 return FROM_BE_16(val);
505 }
506
515 uint32 readUint32BE() {
516 uint32 val;
517 read(&val, 4);
518 return FROM_BE_32(val);
519 }
520
529 uint64 readUint64BE() {
530 uint64 val;
531 read(&val, 8);
532 return FROM_BE_64(val);
533 }
534
543 FORCEINLINE int16 readSint16LE() {
544 return (int16)readUint16LE();
545 }
546
555 FORCEINLINE int32 readSint32LE() {
556 return (int32)readUint32LE();
557 }
558
567 FORCEINLINE int64 readSint64LE() {
568 return (int64)readUint64LE();
569 }
570
579 FORCEINLINE int16 readSint16BE() {
580 return (int16)readUint16BE();
581 }
582
591 FORCEINLINE int32 readSint32BE() {
592 return (int32)readUint32BE();
593 }
594
603 FORCEINLINE int64 readSint64BE() {
604 return (int64)readUint64BE();
605 }
606
615 FORCEINLINE float readFloatLE() {
616 uint8 val[4];
617 read(val, 4);
618 return READ_LE_FLOAT32(val);
619 }
620
629 FORCEINLINE float readFloatBE() {
630 uint8 val[4];
631 read(val, 4);
632 return READ_BE_FLOAT32(val);
633 }
634
635
644 FORCEINLINE double readDoubleLE() {
645 uint8 val[8];
646 read(val, 8);
647 return READ_LE_FLOAT64(val);
648 }
649
658 FORCEINLINE double readDoubleBE() {
659 uint8 val[8];
660 read(val, 8);
661 return READ_BE_FLOAT64(val);
662 }
663
668 template<class TDataFormat, class... T>
669 bool readMultiple(const TDataFormat &dataFormat, T &...values) {
670 const TDataFormat dataFormatCopy = dataFormat; // Copy to help compiler alias analysis, parameter is const ref to ensure TDataFormat is a concrete type
671
672 byte buffer[DataMultipleIO<TDataFormat, T...>::kMaxSize];
673 const uint actualSize = DataMultipleIO<TDataFormat, T...>::computeSize(dataFormatCopy);
674
675 if (read(buffer, actualSize) != actualSize)
676 return false;
677
678 DataMultipleIO<TDataFormat, T...>::decode(dataFormatCopy, buffer, values...);
679 return true;
680 }
681
686 template<class... T>
687 inline bool readMultipleEndian(bool isLittle, T &...values) {
688 return this->readMultiple<EndianStorageFormat, T...>(isLittle ? EndianStorageFormat::Little : EndianStorageFormat::Big, values...);
689 }
690
695 template<class... T>
696 inline bool readMultipleLE(T &...values) {
697 return this->readMultiple<EndianStorageFormat, T...>(EndianStorageFormat::Little, values...);
698 }
699
704 template<class... T>
705 inline bool readMultipleBE(T &...values) {
706 return this->readMultiple<EndianStorageFormat, T...>(EndianStorageFormat::Big, values...);
707 }
708
719
728 String readString(char terminator = 0, size_t len = String::npos);
729
736 Common::String readPascalString(bool transformCR = true);
738};
739
745class SeekableReadStream : virtual public ReadStream {
746public:
747
753 virtual int64 pos() const = 0;
754
761 virtual int64 size() const = 0;
762
780 virtual bool seek(int64 offset, int whence = SEEK_SET) = 0;
781
793 virtual bool skip(uint32 offset) { return seek(offset, SEEK_CUR); }
794
821 virtual char *readLine(char *s, size_t bufSize, bool handleCR = true);
822
823
837 virtual String readLine(bool handleCR = true);
838
847 void hexdump(int len, int bytesPerLine = 16, int startOffset = 0);
848};
849
854class ReadStreamEndian : virtual public ReadStream {
855private:
856 const bool _bigEndian;
857
858public:
865 ReadStreamEndian(bool bigEndian) : _bigEndian(bigEndian) {}
869 bool isBE() const { return _bigEndian; }
874 uint16 readUint16() {
875 uint16 val;
876 read(&val, 2);
877 return (_bigEndian) ? FROM_BE_16(val) : FROM_LE_16(val);
878 }
883 uint32 readUint32() {
884 uint32 val;
885 read(&val, 4);
886 return (_bigEndian) ? FROM_BE_32(val) : FROM_LE_32(val);
887 }
892 uint64 readUint64() {
893 uint64 val;
894 read(&val, 8);
895 return (_bigEndian) ? FROM_BE_64(val) : FROM_LE_64(val);
896 }
901 FORCEINLINE int16 readSint16() {
902 return (int16)readUint16();
903 }
908 FORCEINLINE int32 readSint32() {
909 return (int32)readUint32();
910 }
915 FORCEINLINE int64 readSint64() {
916 return (int64)readUint64();
917 }
918
923 FORCEINLINE float readFloat() {
924 uint8 val[4];
925 read(val, 4);
926 return (_bigEndian) ? READ_BE_FLOAT32(val) : READ_LE_FLOAT32(val);
927 }
928
933 FORCEINLINE double readDouble() {
934 uint8 val[8];
935 read(val, 8);
936 return (_bigEndian) ? READ_BE_FLOAT64(val) : READ_LE_FLOAT64(val);
937 }
938};
939
944class SeekableReadStreamEndian : virtual public SeekableReadStream, virtual public ReadStreamEndian {
945public:
952 SeekableReadStreamEndian(bool bigEndian) : ReadStreamEndian(bigEndian) {}
953};
954
962protected:
964
965public:
966 SeekableReadStreamEndianWrapper(SeekableReadStream *parentStream, bool bigEndian, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
967 : SeekableReadStreamEndian(bigEndian),
968 ReadStreamEndian(bigEndian),
969 _parentStream(parentStream, disposeParentStream) {
970 assert(parentStream);
971 }
972
973 /* Stream APIs */
974 bool err() const override { return _parentStream->err(); }
975 void clearErr() override { _parentStream->clearErr(); }
976
977 /* ReadStream APIs */
978 bool eos() const override { return _parentStream->eos(); }
979 uint32 read(void *dataPtr, uint32 dataSize) override { return _parentStream->read(dataPtr, dataSize); }
980
981 /* SeekableReadStream APIs */
982 int64 pos() const override { return _parentStream->pos(); }
983 int64 size() const override { return _parentStream->size(); }
984 bool seek(int64 offset, int whence = SEEK_SET) override { return _parentStream->seek(offset, whence); }
985};
986
989} // End of namespace Common
990
991#endif
Definition: ptr.h:656
Definition: stream.h:854
FORCEINLINE int64 readSint64()
Definition: stream.h:915
FORCEINLINE double readDouble()
Definition: stream.h:933
bool isBE() const
Definition: stream.h:869
FORCEINLINE int16 readSint16()
Definition: stream.h:901
ReadStreamEndian(bool bigEndian)
Definition: stream.h:865
FORCEINLINE int32 readSint32()
Definition: stream.h:908
uint16 readUint16()
Definition: stream.h:874
uint64 readUint64()
Definition: stream.h:892
uint32 readUint32()
Definition: stream.h:883
FORCEINLINE float readFloat()
Definition: stream.h:923
Definition: stream.h:385
uint16 readUint16LE()
Definition: stream.h:459
Common::String readPascalString(bool transformCR=true)
FORCEINLINE float readFloatLE()
Definition: stream.h:615
FORCEINLINE int64 readSint64LE()
Definition: stream.h:567
String readString(char terminator=0, size_t len=String::npos)
uint64 readUint64BE()
Definition: stream.h:529
FORCEINLINE int16 readSint16LE()
Definition: stream.h:543
uint32 readUint32BE()
Definition: stream.h:515
bool readMultiple(const TDataFormat &dataFormat, T &...values)
Definition: stream.h:669
uint16 readUint16BE()
Definition: stream.h:501
virtual bool eos() const =0
SeekableReadStream * readStream(uint32 dataSize)
FORCEINLINE float readFloatBE()
Definition: stream.h:629
FORCEINLINE int8 readSByte()
Definition: stream.h:447
virtual uint32 read(void *dataPtr, uint32 dataSize)=0
FORCEINLINE int64 readSint64BE()
Definition: stream.h:603
bool readMultipleBE(T &...values)
Definition: stream.h:705
FORCEINLINE int32 readSint32BE()
Definition: stream.h:591
bool readMultipleLE(T &...values)
Definition: stream.h:696
uint64 readUint64LE()
Definition: stream.h:487
FORCEINLINE int16 readSint16BE()
Definition: stream.h:579
uint32 readUint32LE()
Definition: stream.h:473
FORCEINLINE int32 readSint32LE()
Definition: stream.h:555
bool readMultipleEndian(bool isLittle, T &...values)
Definition: stream.h:687
byte readByte()
Definition: stream.h:434
FORCEINLINE double readDoubleBE()
Definition: stream.h:658
FORCEINLINE double readDoubleLE()
Definition: stream.h:644
Definition: stream.h:944
SeekableReadStreamEndian(bool bigEndian)
Definition: stream.h:952
bool eos() const override
Definition: stream.h:978
int64 size() const override
Definition: stream.h:983
int64 pos() const override
Definition: stream.h:982
void clearErr() override
Definition: stream.h:975
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: stream.h:979
bool err() const override
Definition: stream.h:974
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: stream.h:984
Definition: stream.h:745
virtual char * readLine(char *s, size_t bufSize, bool handleCR=true)
virtual int64 pos() const =0
void hexdump(int len, int bytesPerLine=16, int startOffset=0)
virtual int64 size() const =0
virtual String readLine(bool handleCR=true)
virtual bool skip(uint32 offset)
Definition: stream.h:793
virtual bool seek(int64 offset, int whence=SEEK_SET)=0
Definition: stream.h:351
virtual bool seek(int64 offset, int whence=SEEK_SET)=0
virtual int64 size() const =0
Definition: stream.h:48
virtual void clearErr()
Definition: stream.h:71
virtual bool err() const
Definition: stream.h:61
Definition: str.h:59
Definition: stream.h:77
FORCEINLINE void writeSint64BE(int64 value)
Definition: stream.h:224
bool writeMultipleLE(const T &...values)
Definition: stream.h:312
FORCEINLINE void writeFloatBE(float value)
Definition: stream.h:246
bool writeMultiple(const TDataFormat &dataFormat, const T &...values)
Definition: stream.h:284
virtual bool flush()
Definition: stream.h:103
void writeUint32LE(uint32 value)
Definition: stream.h:159
virtual int64 pos() const =0
uint32 writeStream(ReadStream *stream, uint32 dataSize)
void writeUint16BE(uint16 value)
Definition: stream.h:173
void writeUint64BE(uint64 value)
Definition: stream.h:187
FORCEINLINE void writeSint64LE(int64 value)
Definition: stream.h:206
bool writeMultipleEndian(bool isLittle, const T &...values)
Definition: stream.h:303
void writeUint16LE(uint16 value)
Definition: stream.h:152
FORCEINLINE void writeSint16BE(int16 value)
Definition: stream.h:212
void writeUint64LE(uint64 value)
Definition: stream.h:166
uint32 writeStream(ReadStream *stream)
bool writeMultipleBE(const T &...values)
Definition: stream.h:321
void writeString(const String &str)
FORCEINLINE void writeSint32BE(int32 value)
Definition: stream.h:218
void writeByte(byte value)
Definition: stream.h:140
FORCEINLINE void writeSint16LE(int16 value)
Definition: stream.h:194
void writeSByte(int8 value)
Definition: stream.h:146
FORCEINLINE void writeFloatLE(float value)
Definition: stream.h:233
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0
FORCEINLINE void writeDoubleBE(double value)
Definition: stream.h:271
FORCEINLINE void writeSint32LE(int32 value)
Definition: stream.h:200
FORCEINLINE void writeDoubleLE(double value)
Definition: stream.h:258
virtual void finalize()
Definition: stream.h:118
void writeUint32BE(uint32 value)
Definition: stream.h:180
Definition: algorithm.h:29
Definition: data-io.h:224