ScummVM API documentation
stream.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_STREAM_H
24 #define COMMON_STREAM_H
25 
26 #include "common/endian.h"
27 #include "common/scummsys.h"
28 #include "common/str.h"
29 
30 namespace Common {
31 
41 class ReadStream;
42 class SeekableReadStream;
43 
47 class Stream {
48 public:
49  virtual ~Stream() {}
50 
60  virtual bool err() const { return false; }
61 
70  virtual void clearErr() {}
71 };
72 
76 class WriteStream : virtual public Stream {
77 public:
90  virtual uint32 write(const void *dataPtr, uint32 dataSize) = 0;
91 
102  virtual bool flush() { return true; }
103 
117  virtual void finalize() {
118  flush();
119  }
120 
126  virtual int32 pos() const = 0;
127 
139  void writeByte(byte value) {
140  write(&value, 1);
141  }
145  void writeSByte(int8 value) {
146  write(&value, 1);
147  }
151  void writeUint16LE(uint16 value) {
152  value = TO_LE_16(value);
153  write(&value, 2);
154  }
158  void writeUint32LE(uint32 value) {
159  value = TO_LE_32(value);
160  write(&value, 4);
161  }
165  void writeUint64LE(uint64 value) {
166  value = TO_LE_64(value);
167  write(&value, 8);
168  }
172  void writeUint16BE(uint16 value) {
173  value = TO_BE_16(value);
174  write(&value, 2);
175  }
179  void writeUint32BE(uint32 value) {
180  value = TO_BE_32(value);
181  write(&value, 4);
182  }
186  void writeUint64BE(uint64 value) {
187  value = TO_BE_64(value);
188  write(&value, 8);
189  }
193  FORCEINLINE void writeSint16LE(int16 value) {
194  writeUint16LE((uint16)value);
195  }
199  FORCEINLINE void writeSint32LE(int32 value) {
200  writeUint32LE((uint32)value);
201  }
205  FORCEINLINE void writeSint64LE(int64 value) {
206  writeUint64LE((uint64)value);
207  }
211  FORCEINLINE void writeSint16BE(int16 value) {
212  writeUint16BE((uint16)value);
213  }
217  FORCEINLINE void writeSint32BE(int32 value) {
218  writeUint32BE((uint32)value);
219  }
223  FORCEINLINE void writeSint64BE(int64 value) {
224  writeUint64BE((uint64)value);
225  }
226 
227 
232  FORCEINLINE void writeFloatLE(float value) {
233  uint32 n;
234 
235  memcpy(&n, &value, 4);
236 
237  writeUint32LE(n);
238  }
239 
240 
245  FORCEINLINE void writeFloatBE(float value) {
246  uint32 n;
247 
248  memcpy(&n, &value, 4);
249 
250  writeUint32BE(n);
251  }
252 
257  FORCEINLINE void writeDoubleLE(double value) {
258  uint64 n;
259 
260  memcpy(&n, &value, 8);
261 
262  writeUint64LE(n);
263  }
264 
265 
270  FORCEINLINE void writeDoubleBE(double value) {
271  uint64 n;
272 
273  memcpy(&n, &value, 8);
274 
275  writeUint64BE(n);
276  }
277 
284  uint32 writeStream(ReadStream *stream, uint32 dataSize);
291  uint32 writeStream(SeekableReadStream *stream);
292 
297  void writeString(const String &str);
299 };
300 
305 public:
323  virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
324 
332  virtual int32 size() const = 0;
333 };
334 
338 class ReadStream : virtual public Stream {
339 public:
340  ReadStream() {}
341 
353  virtual bool eos() const = 0;
354 
370  virtual uint32 read(void *dataPtr, uint32 dataSize) = 0;
371 
387  byte readByte() {
388  byte b = 0; // FIXME: remove initialisation
389  read(&b, 1);
390  return b;
391  }
392 
400  FORCEINLINE int8 readSByte() {
401  return (int8)readByte();
402  }
403 
412  uint16 readUint16LE() {
413  uint16 val;
414  read(&val, 2);
415  return FROM_LE_16(val);
416  }
417 
426  uint32 readUint32LE() {
427  uint32 val;
428  read(&val, 4);
429  return FROM_LE_32(val);
430  }
431 
440  uint64 readUint64LE() {
441  uint64 val;
442  read(&val, 8);
443  return FROM_LE_64(val);
444  }
445 
454  uint16 readUint16BE() {
455  uint16 val;
456  read(&val, 2);
457  return FROM_BE_16(val);
458  }
459 
468  uint32 readUint32BE() {
469  uint32 val;
470  read(&val, 4);
471  return FROM_BE_32(val);
472  }
473 
482  uint64 readUint64BE() {
483  uint64 val;
484  read(&val, 8);
485  return FROM_BE_64(val);
486  }
487 
496  FORCEINLINE int16 readSint16LE() {
497  return (int16)readUint16LE();
498  }
499 
508  FORCEINLINE int32 readSint32LE() {
509  return (int32)readUint32LE();
510  }
511 
520  FORCEINLINE int64 readSint64LE() {
521  return (int64)readUint64LE();
522  }
523 
532  FORCEINLINE int16 readSint16BE() {
533  return (int16)readUint16BE();
534  }
535 
544  FORCEINLINE int32 readSint32BE() {
545  return (int32)readUint32BE();
546  }
547 
556  FORCEINLINE int64 readSint64BE() {
557  return (int64)readUint64BE();
558  }
559 
568  FORCEINLINE float readFloatLE() {
569  uint32 n = readUint32LE();
570  float f;
571 
572  memcpy(&f, &n, 4);
573 
574  return f;
575  }
576 
585  FORCEINLINE float readFloatBE() {
586  uint32 n = readUint32BE();
587  float f;
588 
589  memcpy(&f, &n, 4);
590 
591  return f;
592  }
593 
594 
603  FORCEINLINE double readDoubleLE() {
604  uint64 n = readUint64LE();
605  double d;
606 
607  memcpy(&d, &n, 8);
608 
609  return d;
610  }
611 
620  FORCEINLINE double readDoubleBE() {
621  uint64 n = readUint64BE();
622  double d;
623 
624  memcpy(&d, &n, 8);
625 
626  return d;
627  }
628 
638  SeekableReadStream *readStream(uint32 dataSize);
639 
646  Common::String readPascalString(bool transformCR = true);
648 };
649 
655 class SeekableReadStream : virtual public ReadStream {
656 public:
657 
663  virtual int32 pos() const = 0;
664 
671  virtual int32 size() const = 0;
672 
690  virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
691 
703  virtual bool skip(uint32 offset) { return seek(offset, SEEK_CUR); }
704 
731  virtual char *readLine(char *s, size_t bufSize, bool handleCR = true);
732 
733 
747  virtual String readLine(bool handleCR = true);
748 
757  void hexdump(int len, int bytesPerLine = 16, int startOffset = 0);
758 };
759 
764 class ReadStreamEndian : virtual public ReadStream {
765 private:
766  const bool _bigEndian;
767 
768 public:
775  ReadStreamEndian(bool bigEndian) : _bigEndian(bigEndian) {}
779  bool isBE() const { return _bigEndian; }
784  uint16 readUint16() {
785  uint16 val;
786  read(&val, 2);
787  return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
788  }
793  uint32 readUint32() {
794  uint32 val;
795  read(&val, 4);
796  return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
797  }
802  uint64 readUint64() {
803  uint64 val;
804  read(&val, 8);
805  return (_bigEndian) ? TO_BE_64(val) : TO_LE_64(val);
806  }
811  FORCEINLINE int16 readSint16() {
812  return (int16)readUint16();
813  }
818  FORCEINLINE int32 readSint32() {
819  return (int32)readUint32();
820  }
825  FORCEINLINE int64 readSint64() {
826  return (int64)readUint64();
827  }
828 };
829 
834 class SeekableReadStreamEndian : virtual public SeekableReadStream, virtual public ReadStreamEndian {
835 public:
842  SeekableReadStreamEndian(bool bigEndian) : ReadStreamEndian(bigEndian) {}
843 };
844 
847 } // End of namespace Common
848 
849 #endif
Definition: stream.h:764
uint16 readUint16LE()
Definition: stream.h:412
virtual bool err() const
Definition: stream.h:60
void writeUint32LE(uint32 value)
Definition: stream.h:158
Definition: stream.h:76
FORCEINLINE int32 readSint32LE()
Definition: stream.h:508
FORCEINLINE float readFloatBE()
Definition: stream.h:585
uint32 readUint32LE()
Definition: stream.h:426
virtual void clearErr()
Definition: stream.h:70
FORCEINLINE void writeSint64LE(int64 value)
Definition: stream.h:205
void writeUint16BE(uint16 value)
Definition: stream.h:172
void writeUint32BE(uint32 value)
Definition: stream.h:179
FORCEINLINE void writeSint16BE(int16 value)
Definition: stream.h:211
uint64 readUint64LE()
Definition: stream.h:440
bool isBE() const
Definition: stream.h:779
Definition: stream.h:655
FORCEINLINE double readDoubleBE()
Definition: stream.h:620
ReadStreamEndian(bool bigEndian)
Definition: stream.h:775
virtual bool flush()
Definition: stream.h:102
FORCEINLINE void writeSint64BE(int64 value)
Definition: stream.h:223
FORCEINLINE int16 readSint16()
Definition: stream.h:811
byte readByte()
Definition: stream.h:387
uint64 readUint64BE()
Definition: stream.h:482
uint32 readUint32()
Definition: stream.h:793
FORCEINLINE int16 readSint16BE()
Definition: stream.h:532
FORCEINLINE void writeSint32BE(int32 value)
Definition: stream.h:217
FORCEINLINE int64 readSint64LE()
Definition: stream.h:520
Definition: system.h:43
void writeUint16LE(uint16 value)
Definition: stream.h:151
FORCEINLINE void writeFloatLE(float value)
Definition: stream.h:232
uint16 readUint16()
Definition: stream.h:784
FORCEINLINE void writeDoubleBE(double value)
Definition: stream.h:270
FORCEINLINE double readDoubleLE()
Definition: stream.h:603
FORCEINLINE int64 readSint64BE()
Definition: stream.h:556
FORCEINLINE float readFloatLE()
Definition: stream.h:568
FORCEINLINE void writeFloatBE(float value)
Definition: stream.h:245
FORCEINLINE int16 readSint16LE()
Definition: stream.h:496
FORCEINLINE void writeDoubleLE(double value)
Definition: stream.h:257
FORCEINLINE int32 readSint32BE()
Definition: stream.h:544
uint32 readUint32BE()
Definition: stream.h:468
Definition: stream.h:47
FORCEINLINE void writeSint16LE(int16 value)
Definition: stream.h:193
Definition: stream.h:304
FORCEINLINE int8 readSByte()
Definition: stream.h:400
uint16 readUint16BE()
Definition: stream.h:454
Definition: stream.h:834
FORCEINLINE int64 readSint64()
Definition: stream.h:825
Definition: stream.h:338
void writeSByte(int8 value)
Definition: stream.h:145
SeekableReadStreamEndian(bool bigEndian)
Definition: stream.h:842
virtual void finalize()
Definition: stream.h:117
FORCEINLINE void writeSint32LE(int32 value)
Definition: stream.h:199
FORCEINLINE int32 readSint32()
Definition: stream.h:818
uint64 readUint64()
Definition: stream.h:802
void writeUint64BE(uint64 value)
Definition: stream.h:186
void writeUint64LE(uint64 value)
Definition: stream.h:165
virtual bool skip(uint32 offset)
Definition: stream.h:703
void writeByte(byte value)
Definition: stream.h:139