xoreos  0.0.5
readstream.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos 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 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #ifndef COMMON_READSTREAM_H
51 #define COMMON_READSTREAM_H
52 
53 #include "src/common/types.h"
54 #include "src/common/endianness.h"
56 #include "src/common/util.h"
57 #include "src/common/error.h"
58 
59 namespace Common {
60 
61 class MemoryReadStream;
62 
64 class ReadStream {
65 public:
67  static const uint32 kEOF = 0xFFFFFFFF;
68 
69  static const size_t kSizeInvalid = SIZE_MAX;
70  static const size_t kPositionInvalid = SIZE_MAX;
71 
72  ReadStream();
73  virtual ~ReadStream();
74 
78  virtual bool eos() const = 0;
79 
87  virtual size_t read(void *dataPtr, size_t dataSize) = 0;
88 
89  // --- The following methods should generally not be overloaded ---
90 
93  byte b;
94  if (read(&b, 1) != 1)
95  throw Exception(kReadError);
96 
97  return b;
98  }
99 
102  return (int8)readByte();
103  }
104 
109  byte b;
110  try {
111  b = readByte();
112  } catch (...) {
113  return kEOF;
114  }
115 
116  return b;
117  }
118 
123  uint16 val;
124  if (read(&val, 2) != 2)
125  throw Exception(kReadError);
126 
127  return FROM_LE_16(val);
128  }
129 
134  uint32 val;
135  if (read(&val, 4) != 4)
136  throw Exception(kReadError);
137 
138  return FROM_LE_32(val);
139  }
140 
145  uint64 val;
146  if (read(&val, 8) != 8)
147  throw Exception(kReadError);
148 
149  return FROM_LE_64(val);
150  }
151 
156  uint16 val;
157  if (read(&val, 2) != 2)
158  throw Exception(kReadError);
159 
160  return FROM_BE_16(val);
161  }
162 
167  uint32 val;
168  if (read(&val, 4) != 4)
169  throw Exception(kReadError);
170 
171  return FROM_BE_32(val);
172  }
173 
178  uint64 val;
179  if (read(&val, 8) != 8)
180  throw Exception(kReadError);
181 
182  return FROM_BE_64(val);
183  }
184 
189  return (int16)readUint16LE();
190  }
191 
196  return (int32)readUint32LE();
197  }
198 
203  return (int64)readUint64LE();
204  }
205 
210  return (int16)readUint16BE();
211  }
212 
217  return (int32)readUint32BE();
218  }
219 
224  return (int64)readUint64BE();
225  }
226 
231  return convertIEEEFloat(readUint32LE());
232  }
233 
238  return convertIEEEFloat(readUint32BE());
239  }
240 
246  }
247 
253  }
254 
260  MemoryReadStream *readStream(size_t dataSize);
261 };
262 
263 
265 class SeekableReadStream : virtual public ReadStream {
266 public:
268  enum Origin {
273  };
274 
277 
282  virtual size_t pos() const = 0;
283 
289  virtual size_t size() const = 0;
290 
305  virtual size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin) = 0;
306 
317  virtual size_t skip(ptrdiff_t offset) {
318  return seek(offset, kOriginCurrent);
319  }
320 
322  static size_t evalSeek(ptrdiff_t offset, Origin whence, size_t pos, size_t begin, size_t size);
323 };
324 
325 
333 class SubReadStream : virtual public ReadStream {
334 public:
335  SubReadStream(ReadStream *parentStream, size_t end, bool disposeParentStream = false);
336  ~SubReadStream();
337 
338  bool eos() const;
339 
340  size_t read(void *dataPtr, size_t dataSize);
341 
342 protected:
344 
345  size_t _pos;
346  size_t _end;
347 
348  bool _eos;
349 };
350 
351 
360 public:
361  SeekableSubReadStream(SeekableReadStream *parentStream, size_t begin, size_t end,
362  bool disposeParentStream = false);
364 
365  size_t pos() const;
366  size_t size() const;
367 
368  size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin);
369 
370 protected:
372 
373  size_t _begin;
374 };
375 
376 
384 private:
385  const bool _bigEndian;
386 
387 public:
388  SeekableSubReadStreamEndian(SeekableReadStream *parentStream, size_t begin, size_t end,
389  bool bigEndian = false, bool disposeParentStream = false);
391 
393  return _bigEndian ? readUint16BE() : readUint16LE();
394  }
395 
397  return _bigEndian ? readUint32BE() : readUint32LE();
398  }
399 
401  return _bigEndian ? readUint64BE() : readUint64LE();
402  }
403 
405  return _bigEndian ? readSint16BE() : readSint16LE();
406  }
407 
409  return _bigEndian ? readSint32BE() : readSint32LE();
410  }
411 
413  return _bigEndian ? readSint64BE() : readSint64LE();
414  }
415 
416  float readIEEEFloat() {
418  }
419 
420  double readIEEEDouble() {
422  }
423 };
424 
425 } // End of namespace Common
426 
427 #endif // COMMON_READSTREAM_H
FORCEINLINE int64 readSint64LE()
Read a signed 64-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:202
uint16 readUint16LE()
Read an unsigned 16-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:122
uint32 readUint32LE()
Read an unsigned 32-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:133
Generic interface for a readable data stream.
Definition: readstream.h:64
This is a wrapper around SeekableSubReadStream, but it adds non-endian read methods whose endianness ...
Definition: readstream.h:383
Definition: 2dafile.h:39
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
uint64 readUint64BE()
Read an unsigned 64-bit word stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:177
static const size_t kPositionInvalid
Definition: readstream.h:70
DisposablePtr< ReadStream > _parentStream
Definition: readstream.h:343
MemoryReadStream * readStream(size_t dataSize)
Read the specified amount of data into a new[]&#39;ed buffer which then is wrapped into a MemoryReadStrea...
Definition: readstream.cpp:67
FORCEINLINE int32 readSint32BE()
Read a signed 32-bit word stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:216
uint64_t uint64
Definition: types.h:206
FORCEINLINE int8 readSByte()
Read a signed byte from the stream and return it.
Definition: readstream.h:101
virtual bool eos() const =0
Returns true if a read failed because the stream has been reached.
Low-level macros and functions to handle different endianness portably.
size_t pos() const
Obtains the current value of the stream position indicator of the stream.
Definition: readstream.cpp:140
static const uint32 kEOF
Return value for end-of-file.
Definition: readstream.h:67
FORCEINLINE double readIEEEDoubleBE()
Read a 64-bit IEEE double stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:251
int16_t int16
Definition: types.h:201
double convertIEEEDouble(uint64 data)
Convert a uint64 holding the bit pattern of a 64-bit IEEE 754 double precision floating point value i...
Definition: util.cpp:143
FORCEINLINE int32 readSint32LE()
Read a signed 32-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:195
size_t size() const
Obtains the total size of the stream, measured in bytes.
Definition: readstream.cpp:144
Seek from the end of the stream.
Definition: readstream.h:271
Origin
The position a seeking offset takes as a base.
Definition: readstream.h:268
Basic exceptions to throw.
size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Sets the stream position indicator for the stream.
Definition: readstream.cpp:148
uint16_t uint16
Definition: types.h:202
Utility templates and functions.
uint16 readUint16BE()
Read an unsigned 16-bit word stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:155
SubReadStream(ReadStream *parentStream, size_t end, bool disposeParentStream=false)
Definition: readstream.cpp:101
virtual size_t skip(ptrdiff_t offset)
Skip the specified number of bytes, adding that offset to the current position in the stream...
Definition: readstream.h:317
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
Simple memory based &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
Low-level type definitions to handle fixed width types portably.
static const size_t kSizeInvalid
Definition: readstream.h:69
virtual ~ReadStream()
Definition: readstream.cpp:64
StackException Exception
Definition: error.h:59
const Exception kReadError("Read error")
Exception when reading from a stream failed.
Definition: error.h:62
FORCEINLINE int16 readSint16LE()
Read a signed 16-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:188
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
uint64 readUint64LE()
Read an unsigned 64-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:144
A disposable plain pointer, allowing pointer-y access and normal deletion.
int64_t int64
Definition: types.h:205
size_t read(void *dataPtr, size_t dataSize)
Read data from the stream.
Definition: readstream.cpp:114
uint32 readUint32BE()
Read an unsigned 32-bit word stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:166
Seek from the current position of the stream.
Definition: readstream.h:270
FORCEINLINE float readIEEEFloatLE()
Read a 32-bit IEEE float stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:230
FORCEINLINE int16 readSint16BE()
Read a signed 16-bit word stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:209
SeekableSubReadStreamEndian(SeekableReadStream *parentStream, size_t begin, size_t end, bool bigEndian=false, bool disposeParentStream=false)
Definition: readstream.cpp:166
A smart pointer with a deletion flag.
FORCEINLINE int64 readSint64BE()
Read a signed 64-bit word stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:223
int8_t int8
Definition: types.h:199
float convertIEEEFloat(uint32 data)
Convert a uint32 holding the bit pattern of a 32-bit IEEE 754 single precision floating point value i...
Definition: util.cpp:116
uint32_t uint32
Definition: types.h:204
FORCEINLINE double readIEEEDoubleLE()
Read a 64-bit IEEE double stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:244
static size_t evalSeek(ptrdiff_t offset, Origin whence, size_t pos, size_t begin, size_t size)
Evaluate the seek offset relative to whence into a position from the beginning.
Definition: readstream.cpp:83
SeekableSubReadStream(SeekableReadStream *parentStream, size_t begin, size_t end, bool disposeParentStream=false)
Definition: readstream.cpp:127
#define SIZE_MAX
Definition: types.h:172
uint32 readChar()
Reads the next character from stream and returns it as an unsigned char cast to an uint32...
Definition: readstream.h:108
bool eos() const
Returns true if a read failed because the stream has been reached.
Definition: readstream.cpp:110
SeekableSubReadStream provides access to a SeekableReadStream restricted to the range [begin...
Definition: readstream.h:359
#define FORCEINLINE
Definition: system.h:411
Seek from the begin of the stream.
Definition: readstream.h:269
SeekableReadStream * _parentStream
Definition: readstream.h:371
SubReadStream provides access to a ReadStream restricted to the range [currentPosition, currentPosition+end).
Definition: readstream.h:333
Interface for a seekable & readable data stream.
Definition: readstream.h:265
byte readByte()
Read an unsigned byte from the stream and return it.
Definition: readstream.h:92
uint8 byte
Definition: types.h:209
int32_t int32
Definition: types.h:203
FORCEINLINE float readIEEEFloatBE()
Read a 32-bit IEEE float stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:237