22 #ifndef COMMON_STREAM_H 23 #define COMMON_STREAM_H 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" 43 class SeekableReadStream;
61 virtual bool err()
const {
return false; }
91 virtual uint32 write(
const void *dataPtr, uint32 dataSize) = 0;
103 virtual bool flush() {
return true; }
127 virtual int64 pos()
const = 0;
153 value = TO_LE_16(value);
160 value = TO_LE_32(value);
167 value = TO_LE_64(value);
174 value = TO_BE_16(value);
181 value = TO_BE_32(value);
188 value = TO_BE_64(value);
195 writeUint16LE((uint16)value);
201 writeUint32LE((uint32)value);
207 writeUint64LE((uint64)value);
213 writeUint16BE((uint16)value);
219 writeUint32BE((uint32)value);
225 writeUint64BE((uint64)value);
236 memcpy(&n, &value, 4);
249 memcpy(&n, &value, 4);
261 memcpy(&n, &value, 8);
274 memcpy(&n, &value, 8);
283 template<
class TDataFormat,
class... T>
285 const TDataFormat dataFormatCopy = dataFormat;
292 if (this->write(buffer, actualSize) != actualSize)
304 return this->writeMultiple<EndianStorageFormat, T...>(isLittle ? EndianStorageFormat::Little : EndianStorageFormat::Big, values...);
313 return this->writeMultiple<EndianStorageFormat, T...>(EndianStorageFormat::Little, values...);
322 return this->writeMultiple<EndianStorageFormat, T...>(EndianStorageFormat::Big, values...);
331 uint32 writeStream(
ReadStream *stream, uint32 dataSize);
344 void writeString(
const String &str);
370 virtual bool seek(int64 offset,
int whence = SEEK_SET) = 0;
379 virtual int64 size()
const = 0;
400 virtual bool eos()
const = 0;
417 virtual uint32 read(
void *dataPtr, uint32 dataSize) = 0;
448 return (int8)readByte();
462 return FROM_LE_16(val);
476 return FROM_LE_32(val);
490 return FROM_LE_64(val);
504 return FROM_BE_16(val);
518 return FROM_BE_32(val);
532 return FROM_BE_64(val);
544 return (int16)readUint16LE();
556 return (int32)readUint32LE();
568 return (int64)readUint64LE();
580 return (int16)readUint16BE();
592 return (int32)readUint32BE();
604 return (int64)readUint64BE();
618 return READ_LE_FLOAT32(val);
632 return READ_BE_FLOAT32(val);
647 return READ_LE_FLOAT64(val);
661 return READ_BE_FLOAT64(val);
668 template<
class TDataFormat,
class... T>
670 const TDataFormat dataFormatCopy = dataFormat;
675 if (read(buffer, actualSize) != actualSize)
688 return this->readMultiple<EndianStorageFormat, T...>(isLittle ? EndianStorageFormat::Little : EndianStorageFormat::Big, values...);
697 return this->readMultiple<EndianStorageFormat, T...>(EndianStorageFormat::Little, values...);
706 return this->readMultiple<EndianStorageFormat, T...>(EndianStorageFormat::Big, values...);
728 String readString(
char terminator = 0,
size_t len = String::npos);
753 virtual int64 pos()
const = 0;
761 virtual int64 size()
const = 0;
780 virtual bool seek(int64 offset,
int whence = SEEK_SET) = 0;
793 virtual bool skip(uint32 offset) {
return seek(offset, SEEK_CUR); }
821 virtual char *readLine(
char *s,
size_t bufSize,
bool handleCR =
true);
837 virtual String readLine(
bool handleCR =
true);
847 void hexdump(
int len,
int bytesPerLine = 16,
int startOffset = 0);
856 const bool _bigEndian;
869 bool isBE()
const {
return _bigEndian; }
877 return (_bigEndian) ? FROM_BE_16(val) : FROM_LE_16(val);
886 return (_bigEndian) ? FROM_BE_32(val) : FROM_LE_32(val);
895 return (_bigEndian) ? FROM_BE_64(val) : FROM_LE_64(val);
902 return (int16)readUint16();
909 return (int32)readUint32();
916 return (int64)readUint64();
926 return (_bigEndian) ? READ_BE_FLOAT32(val) : READ_LE_FLOAT32(val);
936 return (_bigEndian) ? READ_BE_FLOAT64(val) : READ_LE_FLOAT64(val);
967 : _parentStream(parentStream, disposeParentStream),
970 assert(parentStream);
974 bool err()
const override {
return _parentStream->err(); }
975 void clearErr()
override { _parentStream->clearErr(); }
978 bool eos()
const override {
return _parentStream->eos(); }
979 uint32
read(
void *dataPtr, uint32 dataSize)
override {
return _parentStream->read(dataPtr, dataSize); }
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); }
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: stream.h:984
uint16 readUint16LE()
Definition: stream.h:459
virtual bool err() const
Definition: stream.h:61
FORCEINLINE double readDouble()
Definition: stream.h:933
bool readMultipleLE(T &...values)
Definition: stream.h:696
Definition: data-io.h:167
int64 size() const override
Definition: stream.h:983
void writeUint32LE(uint32 value)
Definition: stream.h:159
FORCEINLINE int32 readSint32LE()
Definition: stream.h:555
FORCEINLINE float readFloatBE()
Definition: stream.h:629
uint32 readUint32LE()
Definition: stream.h:473
virtual void clearErr()
Definition: stream.h:71
FORCEINLINE void writeSint64LE(int64 value)
Definition: stream.h:206
void writeUint16BE(uint16 value)
Definition: stream.h:173
void writeUint32BE(uint32 value)
Definition: stream.h:180
FORCEINLINE void writeSint16BE(int16 value)
Definition: stream.h:212
uint64 readUint64LE()
Definition: stream.h:487
bool isBE() const
Definition: stream.h:869
FORCEINLINE double readDoubleBE()
Definition: stream.h:658
ReadStreamEndian(bool bigEndian)
Definition: stream.h:865
virtual bool flush()
Definition: stream.h:103
bool writeMultiple(const TDataFormat &dataFormat, const T &...values)
Definition: stream.h:284
void hexdump(const byte *data, int len, int bytesPerLine=16, int startOffset=0)
int64 pos() const override
Definition: stream.h:982
FORCEINLINE void writeSint64BE(int64 value)
Definition: stream.h:224
FORCEINLINE int16 readSint16()
Definition: stream.h:901
byte readByte()
Definition: stream.h:434
uint64 readUint64BE()
Definition: stream.h:529
bool eos() const override
Definition: stream.h:978
bool err() const override
Definition: stream.h:974
uint32 readUint32()
Definition: stream.h:883
FORCEINLINE int16 readSint16BE()
Definition: stream.h:579
bool writeMultipleLE(const T &...values)
Definition: stream.h:312
FORCEINLINE void writeSint32BE(int32 value)
Definition: stream.h:218
FORCEINLINE int64 readSint64LE()
Definition: stream.h:567
Definition: algorithm.h:29
void writeUint16LE(uint16 value)
Definition: stream.h:152
FORCEINLINE float readFloat()
Definition: stream.h:923
FORCEINLINE void writeFloatLE(float value)
Definition: stream.h:233
uint16 readUint16()
Definition: stream.h:874
FORCEINLINE void writeDoubleBE(double value)
Definition: stream.h:271
bool readMultipleBE(T &...values)
Definition: stream.h:705
FORCEINLINE double readDoubleLE()
Definition: stream.h:644
FORCEINLINE int64 readSint64BE()
Definition: stream.h:603
FORCEINLINE float readFloatLE()
Definition: stream.h:615
FORCEINLINE void writeFloatBE(float value)
Definition: stream.h:246
bool writeMultipleEndian(bool isLittle, const T &...values)
Definition: stream.h:303
FORCEINLINE int16 readSint16LE()
Definition: stream.h:543
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: stream.h:979
FORCEINLINE void writeDoubleLE(double value)
Definition: stream.h:258
FORCEINLINE int32 readSint32BE()
Definition: stream.h:591
uint32 readUint32BE()
Definition: stream.h:515
FORCEINLINE void writeSint16LE(int16 value)
Definition: stream.h:194
FORCEINLINE int8 readSByte()
Definition: stream.h:447
uint16 readUint16BE()
Definition: stream.h:501
FORCEINLINE int64 readSint64()
Definition: stream.h:915
void writeSByte(int8 value)
Definition: stream.h:146
SeekableReadStreamEndian(bool bigEndian)
Definition: stream.h:952
virtual void finalize()
Definition: stream.h:118
bool writeMultipleBE(const T &...values)
Definition: stream.h:321
FORCEINLINE void writeSint32LE(int32 value)
Definition: stream.h:200
bool readMultiple(const TDataFormat &dataFormat, T &...values)
Definition: stream.h:669
FORCEINLINE int32 readSint32()
Definition: stream.h:908
uint64 readUint64()
Definition: stream.h:892
void writeUint64BE(uint64 value)
Definition: stream.h:187
bool readMultipleEndian(bool isLittle, T &...values)
Definition: stream.h:687
void clearErr() override
Definition: stream.h:975
void writeUint64LE(uint64 value)
Definition: stream.h:166
virtual bool skip(uint32 offset)
Definition: stream.h:793
void writeByte(byte value)
Definition: stream.h:140