ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

stream.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_STREAM_H
00024 #define COMMON_STREAM_H
00025 
00026 #include "common/endian.h"
00027 #include "common/scummsys.h"
00028 #include "common/str.h"
00029 
00030 namespace Common {
00031 
00032 class SeekableReadStream;
00033 
00037 class Stream {
00038 public:
00039     virtual ~Stream() {}
00040 
00049     virtual bool err() const { return false; }
00050 
00058     virtual void clearErr() {}
00059 };
00060 
00064 class WriteStream : virtual public Stream {
00065 public:
00077     virtual uint32 write(const void *dataPtr, uint32 dataSize) = 0;
00078 
00089     virtual bool flush() { return true; }
00090 
00102     virtual void finalize() {
00103         flush();
00104     }
00105 
00112     virtual int32 pos() const = 0;
00113 
00114 
00115     // The remaining methods all have default implementations; subclasses
00116     // need not (and should not) overload them.
00117 
00118     void writeByte(byte value) {
00119         write(&value, 1);
00120     }
00121 
00122     void writeSByte(int8 value) {
00123         write(&value, 1);
00124     }
00125 
00126     void writeUint16LE(uint16 value) {
00127         value = TO_LE_16(value);
00128         write(&value, 2);
00129     }
00130 
00131     void writeUint32LE(uint32 value) {
00132         value = TO_LE_32(value);
00133         write(&value, 4);
00134     }
00135 
00136     void writeUint64LE(uint64 value) {
00137         value = TO_LE_64(value);
00138         write(&value, 8);
00139     }
00140 
00141     void writeUint16BE(uint16 value) {
00142         value = TO_BE_16(value);
00143         write(&value, 2);
00144     }
00145 
00146     void writeUint32BE(uint32 value) {
00147         value = TO_BE_32(value);
00148         write(&value, 4);
00149     }
00150 
00151     void writeUint64BE(uint64 value) {
00152         value = TO_BE_64(value);
00153         write(&value, 8);
00154     }
00155 
00156     FORCEINLINE void writeSint16LE(int16 value) {
00157         writeUint16LE((uint16)value);
00158     }
00159 
00160     FORCEINLINE void writeSint32LE(int32 value) {
00161         writeUint32LE((uint32)value);
00162     }
00163 
00164     FORCEINLINE void writeSint64LE(int64 value) {
00165         writeUint64LE((uint64)value);
00166     }
00167 
00168     FORCEINLINE void writeSint16BE(int16 value) {
00169         writeUint16BE((uint16)value);
00170     }
00171 
00172     FORCEINLINE void writeSint32BE(int32 value) {
00173         writeUint32BE((uint32)value);
00174     }
00175 
00176     FORCEINLINE void writeSint64BE(int64 value) {
00177         writeUint64BE((uint64)value);
00178     }
00179 
00180 
00185     FORCEINLINE void writeFloatLE(float value) {
00186         uint32 n;
00187 
00188         memcpy(&n, &value, 4);
00189 
00190         writeUint32LE(n);
00191     }
00192 
00193 
00198     FORCEINLINE void writeFloatBE(float value) {
00199         uint32 n;
00200 
00201         memcpy(&n, &value, 4);
00202 
00203         writeUint32BE(n);
00204     }
00205 
00210     FORCEINLINE void writeDoubleLE(double value) {
00211         uint64 n;
00212 
00213         memcpy(&n, &value, 8);
00214 
00215         writeUint64LE(n);
00216     }
00217 
00218 
00223     FORCEINLINE void writeDoubleBE(double value) {
00224         uint64 n;
00225 
00226         memcpy(&n, &value, 8);
00227 
00228         writeUint64BE(n);
00229     }
00230 
00235     void writeString(const String &str);
00236 };
00237 
00241 class SeekableWriteStream : public WriteStream {
00242 public:
00258     virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
00259 
00266     virtual int32 size() const = 0;
00267 };
00268 
00272 class ReadStream : virtual public Stream {
00273 public:
00284     virtual bool eos() const = 0;
00285 
00298     virtual uint32 read(void *dataPtr, uint32 dataSize) = 0;
00299 
00300 
00301     // The remaining methods all have default implementations; subclasses
00302     // in general should not overload them.
00303 
00310     byte readByte() {
00311         byte b = 0; // FIXME: remove initialisation
00312         read(&b, 1);
00313         return b;
00314     }
00315 
00322     FORCEINLINE int8 readSByte() {
00323         return (int8)readByte();
00324     }
00325 
00333     uint16 readUint16LE() {
00334         uint16 val;
00335         read(&val, 2);
00336         return FROM_LE_16(val);
00337     }
00338 
00346     uint32 readUint32LE() {
00347         uint32 val;
00348         read(&val, 4);
00349         return FROM_LE_32(val);
00350     }
00351 
00359     uint64 readUint64LE() {
00360         uint64 val;
00361         read(&val, 8);
00362         return FROM_LE_64(val);
00363     }
00364 
00372     uint16 readUint16BE() {
00373         uint16 val;
00374         read(&val, 2);
00375         return FROM_BE_16(val);
00376     }
00377 
00385     uint32 readUint32BE() {
00386         uint32 val;
00387         read(&val, 4);
00388         return FROM_BE_32(val);
00389     }
00390 
00398     uint64 readUint64BE() {
00399         uint64 val;
00400         read(&val, 8);
00401         return FROM_BE_64(val);
00402     }
00403 
00411     FORCEINLINE int16 readSint16LE() {
00412         return (int16)readUint16LE();
00413     }
00414 
00422     FORCEINLINE int32 readSint32LE() {
00423         return (int32)readUint32LE();
00424     }
00425 
00433     FORCEINLINE int64 readSint64LE() {
00434         return (int64)readUint64LE();
00435     }
00436 
00444     FORCEINLINE int16 readSint16BE() {
00445         return (int16)readUint16BE();
00446     }
00447 
00455     FORCEINLINE int32 readSint32BE() {
00456         return (int32)readUint32BE();
00457     }
00458 
00466     FORCEINLINE int64 readSint64BE() {
00467         return (int64)readUint64BE();
00468     }
00469 
00477     FORCEINLINE float readFloatLE() {
00478         uint32 n = readUint32LE();
00479         float f;
00480 
00481         memcpy(&f, &n, 4);
00482 
00483         return f;
00484     }
00485 
00493     FORCEINLINE float readFloatBE() {
00494         uint32 n = readUint32BE();
00495         float f;
00496 
00497         memcpy(&f, &n, 4);
00498 
00499         return f;
00500     }
00501 
00502 
00510     FORCEINLINE double readDoubleLE() {
00511         uint64 n = readUint64LE();
00512         double d;
00513 
00514         memcpy(&d, &n, 8);
00515 
00516         return d;
00517     }
00518 
00526     FORCEINLINE double readDoubleBE() {
00527         uint64 n = readUint64BE();
00528         double d;
00529 
00530         memcpy(&d, &n, 8);
00531 
00532         return d;
00533     }
00534 
00543     SeekableReadStream *readStream(uint32 dataSize);
00544 
00551     Common::String readPascalString(bool transformCR = true);
00552 
00553 };
00554 
00555 
00561 class SeekableReadStream : virtual public ReadStream {
00562 public:
00563 
00570     virtual int32 pos() const = 0;
00571 
00578     virtual int32 size() const = 0;
00579 
00595     virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
00596 
00601     virtual bool skip(uint32 offset) { return seek(offset, SEEK_CUR); }
00602 
00626     virtual char *readLine(char *s, size_t bufSize);
00627 
00628 
00639     virtual String readLine();
00640 
00648     void hexdump(int len, int bytesPerLine = 16, int startOffset = 0);
00649 };
00650 
00655 class ReadStreamEndian : virtual public ReadStream {
00656 private:
00657     const bool _bigEndian;
00658 
00659 public:
00660     ReadStreamEndian(bool bigEndian) : _bigEndian(bigEndian) {}
00661 
00662     bool isBE() const { return _bigEndian; }
00663 
00664     uint16 readUint16() {
00665         uint16 val;
00666         read(&val, 2);
00667         return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
00668     }
00669 
00670     uint32 readUint32() {
00671         uint32 val;
00672         read(&val, 4);
00673         return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
00674     }
00675 
00676     uint64 readUint64() {
00677         uint64 val;
00678         read(&val, 8);
00679         return (_bigEndian) ? TO_BE_64(val) : TO_LE_64(val);
00680     }
00681 
00682     FORCEINLINE int16 readSint16() {
00683         return (int16)readUint16();
00684     }
00685 
00686     FORCEINLINE int32 readSint32() {
00687         return (int32)readUint32();
00688     }
00689 
00690     FORCEINLINE int64 readSint64() {
00691         return (int64)readUint64();
00692     }
00693 };
00694 
00699 class SeekableReadStreamEndian : public SeekableReadStream, public ReadStreamEndian {
00700 public:
00701     SeekableReadStreamEndian(bool bigEndian) : ReadStreamEndian(bigEndian) {}
00702 };
00703 
00704 
00705 } // End of namespace Common
00706 
00707 #endif


Generated on Sat Sep 14 2019 05:01:06 for ResidualVM by doxygen 1.7.1
curved edge   curved edge