xoreos  0.0.5
memreadstream.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_MEMREADSTREAM_H
51 #define COMMON_MEMREADSTREAM_H
52 
53 #include <cstring>
54 
55 #include <boost/noncopyable.hpp>
56 
57 #include "src/common/types.h"
59 #include "src/common/readstream.h"
60 
61 namespace Common {
62 
66 class MemoryReadStream : boost::noncopyable, public SeekableReadStream {
67 public:
71  MemoryReadStream(const byte *dataPtr, size_t dataSize, bool disposeMemory = false) :
72  _ptrOrig(dataPtr, disposeMemory), _ptr(dataPtr), _size(dataSize), _pos(0), _eos(false) {
73 
74  }
75 
78  MemoryReadStream(const char *str, bool useTerminator = false) :
79  _ptrOrig(reinterpret_cast<const byte *>(str), false), _ptr(reinterpret_cast<const byte *>(str)),
80  _size(strlen(str) + (useTerminator ? 1 : 0)), _pos(0), _eos(false) {
81 
82  }
83 
86  template<size_t N>
87  MemoryReadStream(const byte (&array)[N]) :
88  _ptrOrig(array, false), _ptr(array), _size(N), _pos(0), _eos(false) {
89 
90  }
91 
93 
94  size_t read(void *dataPtr, size_t dataSize);
95 
96  bool eos() const;
97 
98  size_t pos() const;
99  size_t size() const;
100 
101  size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin);
102 
103  const byte *getData() const;
104 
105 private:
107  const byte *_ptr;
108 
109  const size_t _size;
110 
111  size_t _pos;
112 
113  bool _eos;
114 };
115 
116 
121 private:
122  const bool _bigEndian;
123 
124 public:
125  MemoryReadStreamEndian(const byte *dataPtr, size_t dataSize, bool bigEndian = false,
126  bool disposeMemory = false);
128 
130  return _bigEndian ? readUint16BE() : readUint16LE();
131  }
132 
134  return _bigEndian ? readUint32BE() : readUint32LE();
135  }
136 
138  return _bigEndian ? readUint64BE() : readUint64LE();
139  }
140 
142  return _bigEndian ? readSint16BE() : readSint16LE();
143  }
144 
146  return _bigEndian ? readSint32BE() : readSint32LE();
147  }
148 
150  return _bigEndian ? readSint64BE() : readSint64LE();
151  }
152 
153  float readIEEEFloat() {
155  }
156 
157  double readIEEEDouble() {
159  }
160 };
161 
162 } // End of namespace Common
163 
164 #endif // COMMON_MEMREADSTREAM_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
MemoryReadStreamEndian(const byte *dataPtr, size_t dataSize, bool bigEndian=false, bool disposeMemory=false)
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
Definition: 2dafile.h:39
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
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 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
DisposableArray< const byte > _ptrOrig
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 seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Sets the stream position indicator for the stream.
Origin
The position a seeking offset takes as a base.
Definition: readstream.h:268
const byte * getData() const
uint16_t uint16
Definition: types.h:202
MemoryReadStream(const byte *dataPtr, size_t dataSize, bool disposeMemory=false)
This constructor takes a pointer to a memory buffer and a length, and wraps it.
Definition: memreadstream.h:71
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
This is a wrapper around MemoryReadStream, but it adds non-endian read methods whose endianness is se...
bool eos() const
Returns true if a read failed because the stream has been reached.
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.
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
Basic reading stream interfaces.
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
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
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
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
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
size_t size() const
Obtains the total size of the stream, measured in bytes.
Seek from the begin of the stream.
Definition: readstream.h:269
Interface for a seekable & readable data stream.
Definition: readstream.h:265
size_t pos() const
Obtains the current value of the stream position indicator of the stream.
MemoryReadStream(const byte(&array)[N])
Template constructor to create a MemoryReadStream around a static array buffer.
Definition: memreadstream.h:87
uint8 byte
Definition: types.h:209
size_t read(void *dataPtr, size_t dataSize)
Read data from the stream.
MemoryReadStream(const char *str, bool useTerminator=false)
Create a MemoryReadStream around a static string buffer, optionally including the terminating \0...
Definition: memreadstream.h:78
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