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 ReadStream;
00033 class SeekableReadStream;
00034 
00038 class Stream {
00039 public:
00040     virtual ~Stream() {}
00041 
00050     virtual bool err() const { return false; }
00051 
00059     virtual void clearErr() {}
00060 };
00061 
00065 class WriteStream : virtual public Stream {
00066 public:
00078     virtual uint32 write(const void *dataPtr, uint32 dataSize) = 0;
00079 
00090     virtual bool flush() { return true; }
00091 
00103     virtual void finalize() {
00104         flush();
00105     }
00106 
00113     virtual int32 pos() const = 0;
00114 
00115 
00116     // The remaining methods all have default implementations; subclasses
00117     // need not (and should not) overload them.
00118 
00119     void writeByte(byte value) {
00120         write(&value, 1);
00121     }
00122 
00123     void writeSByte(int8 value) {
00124         write(&value, 1);
00125     }
00126 
00127     void writeUint16LE(uint16 value) {
00128         value = TO_LE_16(value);
00129         write(&value, 2);
00130     }
00131 
00132     void writeUint32LE(uint32 value) {
00133         value = TO_LE_32(value);
00134         write(&value, 4);
00135     }
00136 
00137     void writeUint64LE(uint64 value) {
00138         value = TO_LE_64(value);
00139         write(&value, 8);
00140     }
00141 
00142     void writeUint16BE(uint16 value) {
00143         value = TO_BE_16(value);
00144         write(&value, 2);
00145     }
00146 
00147     void writeUint32BE(uint32 value) {
00148         value = TO_BE_32(value);
00149         write(&value, 4);
00150     }
00151 
00152     void writeUint64BE(uint64 value) {
00153         value = TO_BE_64(value);
00154         write(&value, 8);
00155     }
00156 
00157     FORCEINLINE void writeSint16LE(int16 value) {
00158         writeUint16LE((uint16)value);
00159     }
00160 
00161     FORCEINLINE void writeSint32LE(int32 value) {
00162         writeUint32LE((uint32)value);
00163     }
00164 
00165     FORCEINLINE void writeSint64LE(int64 value) {
00166         writeUint64LE((uint64)value);
00167     }
00168 
00169     FORCEINLINE void writeSint16BE(int16 value) {
00170         writeUint16BE((uint16)value);
00171     }
00172 
00173     FORCEINLINE void writeSint32BE(int32 value) {
00174         writeUint32BE((uint32)value);
00175     }
00176 
00177     FORCEINLINE void writeSint64BE(int64 value) {
00178         writeUint64BE((uint64)value);
00179     }
00180 
00181 
00186     FORCEINLINE void writeFloatLE(float value) {
00187         uint32 n;
00188 
00189         memcpy(&n, &value, 4);
00190 
00191         writeUint32LE(n);
00192     }
00193 
00194 
00199     FORCEINLINE void writeFloatBE(float value) {
00200         uint32 n;
00201 
00202         memcpy(&n, &value, 4);
00203 
00204         writeUint32BE(n);
00205     }
00206 
00211     FORCEINLINE void writeDoubleLE(double value) {
00212         uint64 n;
00213 
00214         memcpy(&n, &value, 8);
00215 
00216         writeUint64LE(n);
00217     }
00218 
00219 
00224     FORCEINLINE void writeDoubleBE(double value) {
00225         uint64 n;
00226 
00227         memcpy(&n, &value, 8);
00228 
00229         writeUint64BE(n);
00230     }
00231 
00235     uint32 writeStream(ReadStream *stream, uint32 dataSize);
00236 
00237     uint32 writeStream(SeekableReadStream *stream);
00238 
00243     void writeString(const String &str);
00244 };
00245 
00249 class SeekableWriteStream : public WriteStream {
00250 public:
00266     virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
00267 
00274     virtual int32 size() const = 0;
00275 };
00276 
00280 class ReadStream : virtual public Stream {
00281 public:
00292     virtual bool eos() const = 0;
00293 
00306     virtual uint32 read(void *dataPtr, uint32 dataSize) = 0;
00307 
00308 
00309     // The remaining methods all have default implementations; subclasses
00310     // in general should not overload them.
00311 
00318     byte readByte() {
00319         byte b = 0; // FIXME: remove initialisation
00320         read(&b, 1);
00321         return b;
00322     }
00323 
00330     FORCEINLINE int8 readSByte() {
00331         return (int8)readByte();
00332     }
00333 
00341     uint16 readUint16LE() {
00342         uint16 val;
00343         read(&val, 2);
00344         return FROM_LE_16(val);
00345     }
00346 
00354     uint32 readUint32LE() {
00355         uint32 val;
00356         read(&val, 4);
00357         return FROM_LE_32(val);
00358     }
00359 
00367     uint64 readUint64LE() {
00368         uint64 val;
00369         read(&val, 8);
00370         return FROM_LE_64(val);
00371     }
00372 
00380     uint16 readUint16BE() {
00381         uint16 val;
00382         read(&val, 2);
00383         return FROM_BE_16(val);
00384     }
00385 
00393     uint32 readUint32BE() {
00394         uint32 val;
00395         read(&val, 4);
00396         return FROM_BE_32(val);
00397     }
00398 
00406     uint64 readUint64BE() {
00407         uint64 val;
00408         read(&val, 8);
00409         return FROM_BE_64(val);
00410     }
00411 
00419     FORCEINLINE int16 readSint16LE() {
00420         return (int16)readUint16LE();
00421     }
00422 
00430     FORCEINLINE int32 readSint32LE() {
00431         return (int32)readUint32LE();
00432     }
00433 
00441     FORCEINLINE int64 readSint64LE() {
00442         return (int64)readUint64LE();
00443     }
00444 
00452     FORCEINLINE int16 readSint16BE() {
00453         return (int16)readUint16BE();
00454     }
00455 
00463     FORCEINLINE int32 readSint32BE() {
00464         return (int32)readUint32BE();
00465     }
00466 
00474     FORCEINLINE int64 readSint64BE() {
00475         return (int64)readUint64BE();
00476     }
00477 
00485     FORCEINLINE float readFloatLE() {
00486         uint32 n = readUint32LE();
00487         float f;
00488 
00489         memcpy(&f, &n, 4);
00490 
00491         return f;
00492     }
00493 
00501     FORCEINLINE float readFloatBE() {
00502         uint32 n = readUint32BE();
00503         float f;
00504 
00505         memcpy(&f, &n, 4);
00506 
00507         return f;
00508     }
00509 
00510 
00518     FORCEINLINE double readDoubleLE() {
00519         uint64 n = readUint64LE();
00520         double d;
00521 
00522         memcpy(&d, &n, 8);
00523 
00524         return d;
00525     }
00526 
00534     FORCEINLINE double readDoubleBE() {
00535         uint64 n = readUint64BE();
00536         double d;
00537 
00538         memcpy(&d, &n, 8);
00539 
00540         return d;
00541     }
00542 
00551     SeekableReadStream *readStream(uint32 dataSize);
00552 
00559     Common::String readPascalString(bool transformCR = true);
00560 
00561 };
00562 
00563 
00569 class SeekableReadStream : virtual public ReadStream {
00570 public:
00571 
00578     virtual int32 pos() const = 0;
00579 
00586     virtual int32 size() const = 0;
00587 
00603     virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
00604 
00609     virtual bool skip(uint32 offset) { return seek(offset, SEEK_CUR); }
00610 
00635     virtual char *readLine(char *s, size_t bufSize, bool handleCR = true);
00636 
00637 
00650     virtual String readLine(bool handleCR = true);
00651 
00659     void hexdump(int len, int bytesPerLine = 16, int startOffset = 0);
00660 };
00661 
00666 class ReadStreamEndian : virtual public ReadStream {
00667 private:
00668     const bool _bigEndian;
00669 
00670 public:
00671     ReadStreamEndian(bool bigEndian) : _bigEndian(bigEndian) {}
00672 
00673     bool isBE() const { return _bigEndian; }
00674 
00675     uint16 readUint16() {
00676         uint16 val;
00677         read(&val, 2);
00678         return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
00679     }
00680 
00681     uint32 readUint32() {
00682         uint32 val;
00683         read(&val, 4);
00684         return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
00685     }
00686 
00687     uint64 readUint64() {
00688         uint64 val;
00689         read(&val, 8);
00690         return (_bigEndian) ? TO_BE_64(val) : TO_LE_64(val);
00691     }
00692 
00693     FORCEINLINE int16 readSint16() {
00694         return (int16)readUint16();
00695     }
00696 
00697     FORCEINLINE int32 readSint32() {
00698         return (int32)readUint32();
00699     }
00700 
00701     FORCEINLINE int64 readSint64() {
00702         return (int64)readUint64();
00703     }
00704 };
00705 
00710 class SeekableReadStreamEndian : public SeekableReadStream, public ReadStreamEndian {
00711 public:
00712     SeekableReadStreamEndian(bool bigEndian) : ReadStreamEndian(bigEndian) {}
00713 };
00714 
00715 
00716 } // End of namespace Common
00717 
00718 #endif


Generated on Sat Jul 4 2020 05:01:14 for ResidualVM by doxygen 1.7.1
curved edge   curved edge