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:
00282     ReadStream() {}
00283 
00294     virtual bool eos() const = 0;
00295 
00308     virtual uint32 read(void *dataPtr, uint32 dataSize) = 0;
00309 
00310 
00311     // The remaining methods all have default implementations; subclasses
00312     // in general should not overload them.
00313 
00320     byte readByte() {
00321         byte b = 0; // FIXME: remove initialisation
00322         read(&b, 1);
00323         return b;
00324     }
00325 
00332     FORCEINLINE int8 readSByte() {
00333         return (int8)readByte();
00334     }
00335 
00343     uint16 readUint16LE() {
00344         uint16 val;
00345         read(&val, 2);
00346         return FROM_LE_16(val);
00347     }
00348 
00356     uint32 readUint32LE() {
00357         uint32 val;
00358         read(&val, 4);
00359         return FROM_LE_32(val);
00360     }
00361 
00369     uint64 readUint64LE() {
00370         uint64 val;
00371         read(&val, 8);
00372         return FROM_LE_64(val);
00373     }
00374 
00382     uint16 readUint16BE() {
00383         uint16 val;
00384         read(&val, 2);
00385         return FROM_BE_16(val);
00386     }
00387 
00395     uint32 readUint32BE() {
00396         uint32 val;
00397         read(&val, 4);
00398         return FROM_BE_32(val);
00399     }
00400 
00408     uint64 readUint64BE() {
00409         uint64 val;
00410         read(&val, 8);
00411         return FROM_BE_64(val);
00412     }
00413 
00421     FORCEINLINE int16 readSint16LE() {
00422         return (int16)readUint16LE();
00423     }
00424 
00432     FORCEINLINE int32 readSint32LE() {
00433         return (int32)readUint32LE();
00434     }
00435 
00443     FORCEINLINE int64 readSint64LE() {
00444         return (int64)readUint64LE();
00445     }
00446 
00454     FORCEINLINE int16 readSint16BE() {
00455         return (int16)readUint16BE();
00456     }
00457 
00465     FORCEINLINE int32 readSint32BE() {
00466         return (int32)readUint32BE();
00467     }
00468 
00476     FORCEINLINE int64 readSint64BE() {
00477         return (int64)readUint64BE();
00478     }
00479 
00487     FORCEINLINE float readFloatLE() {
00488         uint32 n = readUint32LE();
00489         float f;
00490 
00491         memcpy(&f, &n, 4);
00492 
00493         return f;
00494     }
00495 
00503     FORCEINLINE float readFloatBE() {
00504         uint32 n = readUint32BE();
00505         float f;
00506 
00507         memcpy(&f, &n, 4);
00508 
00509         return f;
00510     }
00511 
00512 
00520     FORCEINLINE double readDoubleLE() {
00521         uint64 n = readUint64LE();
00522         double d;
00523 
00524         memcpy(&d, &n, 8);
00525 
00526         return d;
00527     }
00528 
00536     FORCEINLINE double readDoubleBE() {
00537         uint64 n = readUint64BE();
00538         double d;
00539 
00540         memcpy(&d, &n, 8);
00541 
00542         return d;
00543     }
00544 
00553     SeekableReadStream *readStream(uint32 dataSize);
00554 
00561     Common::String readPascalString(bool transformCR = true);
00562 
00563 };
00564 
00565 
00571 class SeekableReadStream : virtual public ReadStream {
00572 public:
00573 
00580     virtual int32 pos() const = 0;
00581 
00588     virtual int32 size() const = 0;
00589 
00605     virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
00606 
00611     virtual bool skip(uint32 offset) { return seek(offset, SEEK_CUR); }
00612 
00637     virtual char *readLine(char *s, size_t bufSize, bool handleCR = true);
00638 
00639 
00652     virtual String readLine(bool handleCR = true);
00653 
00661     void hexdump(int len, int bytesPerLine = 16, int startOffset = 0);
00662 };
00663 
00668 class ReadStreamEndian : virtual public ReadStream {
00669 private:
00670     const bool _bigEndian;
00671 
00672 public:
00673     ReadStreamEndian(bool bigEndian) : _bigEndian(bigEndian) {}
00674 
00675     bool isBE() const { return _bigEndian; }
00676 
00677     uint16 readUint16() {
00678         uint16 val;
00679         read(&val, 2);
00680         return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
00681     }
00682 
00683     uint32 readUint32() {
00684         uint32 val;
00685         read(&val, 4);
00686         return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
00687     }
00688 
00689     uint64 readUint64() {
00690         uint64 val;
00691         read(&val, 8);
00692         return (_bigEndian) ? TO_BE_64(val) : TO_LE_64(val);
00693     }
00694 
00695     FORCEINLINE int16 readSint16() {
00696         return (int16)readUint16();
00697     }
00698 
00699     FORCEINLINE int32 readSint32() {
00700         return (int32)readUint32();
00701     }
00702 
00703     FORCEINLINE int64 readSint64() {
00704         return (int64)readUint64();
00705     }
00706 };
00707 
00712 class SeekableReadStreamEndian : virtual public SeekableReadStream, virtual public ReadStreamEndian {
00713 public:
00714     SeekableReadStreamEndian(bool bigEndian) : ReadStreamEndian(bigEndian) {}
00715 };
00716 
00717 
00718 } // End of namespace Common
00719 
00720 #endif


Generated on Sat Sep 19 2020 05:01:10 for ResidualVM by doxygen 1.7.1
curved edge   curved edge