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     void writeString(const String &str);
00211 };
00212 
00216 class ReadStream : virtual public Stream {
00217 public:
00228     virtual bool eos() const = 0;
00229 
00242     virtual uint32 read(void *dataPtr, uint32 dataSize) = 0;
00243 
00244 
00245     // The remaining methods all have default implementations; subclasses
00246     // in general should not overload them.
00247 
00254     byte readByte() {
00255         byte b = 0; // FIXME: remove initialisation
00256         read(&b, 1);
00257         return b;
00258     }
00259 
00266     FORCEINLINE int8 readSByte() {
00267         return (int8)readByte();
00268     }
00269 
00277     uint16 readUint16LE() {
00278         uint16 val;
00279         read(&val, 2);
00280         return FROM_LE_16(val);
00281     }
00282 
00290     uint32 readUint32LE() {
00291         uint32 val;
00292         read(&val, 4);
00293         return FROM_LE_32(val);
00294     }
00295 
00303     uint64 readUint64LE() {
00304         uint64 val;
00305         read(&val, 8);
00306         return FROM_LE_64(val);
00307     }
00308 
00316     uint16 readUint16BE() {
00317         uint16 val;
00318         read(&val, 2);
00319         return FROM_BE_16(val);
00320     }
00321 
00329     uint32 readUint32BE() {
00330         uint32 val;
00331         read(&val, 4);
00332         return FROM_BE_32(val);
00333     }
00334 
00342     uint64 readUint64BE() {
00343         uint64 val;
00344         read(&val, 8);
00345         return FROM_BE_64(val);
00346     }
00347 
00355     FORCEINLINE int16 readSint16LE() {
00356         return (int16)readUint16LE();
00357     }
00358 
00366     FORCEINLINE int32 readSint32LE() {
00367         return (int32)readUint32LE();
00368     }
00369 
00377     FORCEINLINE int64 readSint64LE() {
00378         return (int64)readUint64LE();
00379     }
00380 
00388     FORCEINLINE int16 readSint16BE() {
00389         return (int16)readUint16BE();
00390     }
00391 
00399     FORCEINLINE int32 readSint32BE() {
00400         return (int32)readUint32BE();
00401     }
00402 
00410     FORCEINLINE int64 readSint64BE() {
00411         return (int64)readUint64BE();
00412     }
00413 
00421     FORCEINLINE float readFloatLE() {
00422         uint32 n = readUint32LE();
00423         float f;
00424 
00425         memcpy(&f, &n, 4);
00426 
00427         return f;
00428     }
00429 
00437     FORCEINLINE float readFloatBE() {
00438         uint32 n = readUint32BE();
00439         float f;
00440 
00441         memcpy(&f, &n, 4);
00442 
00443         return f;
00444     }
00445 
00454     SeekableReadStream *readStream(uint32 dataSize);
00455 
00462     Common::String readPascalString(bool transformCR = true);
00463 
00464 };
00465 
00466 
00472 class SeekableReadStream : virtual public ReadStream {
00473 public:
00474 
00481     virtual int32 pos() const = 0;
00482 
00489     virtual int32 size() const = 0;
00490 
00506     virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
00507 
00512     virtual bool skip(uint32 offset) { return seek(offset, SEEK_CUR); }
00513 
00537     virtual char *readLine(char *s, size_t bufSize);
00538 
00539 
00550     virtual String readLine();
00551 
00559     void hexdump(int len, int bytesPerLine = 16, int startOffset = 0);
00560 };
00561 
00566 class ReadStreamEndian : virtual public ReadStream {
00567 private:
00568     const bool _bigEndian;
00569 
00570 public:
00571     ReadStreamEndian(bool bigEndian) : _bigEndian(bigEndian) {}
00572 
00573     bool isBE() const { return _bigEndian; }
00574 
00575     uint16 readUint16() {
00576         uint16 val;
00577         read(&val, 2);
00578         return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
00579     }
00580 
00581     uint32 readUint32() {
00582         uint32 val;
00583         read(&val, 4);
00584         return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
00585     }
00586 
00587     uint64 readUint64() {
00588         uint64 val;
00589         read(&val, 8);
00590         return (_bigEndian) ? TO_BE_64(val) : TO_LE_64(val);
00591     }
00592 
00593     FORCEINLINE int16 readSint16() {
00594         return (int16)readUint16();
00595     }
00596 
00597     FORCEINLINE int32 readSint32() {
00598         return (int32)readUint32();
00599     }
00600 
00601     FORCEINLINE int64 readSint64() {
00602         return (int64)readUint64();
00603     }
00604 };
00605 
00610 class SeekableReadStreamEndian : public SeekableReadStream, public ReadStreamEndian {
00611 public:
00612     SeekableReadStreamEndian(bool bigEndian) : ReadStreamEndian(bigEndian) {}
00613 };
00614 
00615 
00616 } // End of namespace Common
00617 
00618 #endif


Generated on Sat Mar 16 2019 05:01:55 for ResidualVM by doxygen 1.7.1
curved edge   curved edge