xoreos  0.0.5
writestream.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_WRITESTREAM_H
51 #define COMMON_WRITESTREAM_H
52 
53 #include "src/common/types.h"
54 #include "src/common/endianness.h"
55 #include "src/common/util.h"
56 #include "src/common/error.h"
57 
58 namespace Common {
59 
60 class UString;
61 class ReadStream;
62 
64 class WriteStream {
65 public:
66  WriteStream();
67  virtual ~WriteStream();
68 
76  virtual size_t write(const void *dataPtr, size_t dataSize) = 0;
77 
84  virtual void flush();
85 
86  // --- The following methods should generally not be overloaded ---
87 
88  void writeByte(byte value) {
89  if (write(&value, 1) != 1)
90  throw Exception(kWriteError);
91  }
92 
93  void writeSByte(int8 value) {
94  if (write(&value, 1) != 1)
95  throw Exception(kWriteError);
96  }
97 
98  void writeUint16LE(uint16 value) {
99  value = TO_LE_16(value);
100  if (write(&value, 2) != 2)
101  throw Exception(kWriteError);
102  }
103 
104  void writeUint32LE(uint32 value) {
105  value = TO_LE_32(value);
106  if (write(&value, 4) != 4)
107  throw Exception(kWriteError);
108  }
109 
110  void writeUint64LE(uint64 value) {
111  value = TO_LE_64(value);
112  if (write(&value, 8) != 8)
113  throw Exception(kWriteError);
114  }
115 
116  void writeUint16BE(uint16 value) {
117  value = TO_BE_16(value);
118  if (write(&value, 2) != 2)
119  throw Exception(kWriteError);
120  }
121 
122  void writeUint32BE(uint32 value) {
123  value = TO_BE_32(value);
124  if (write(&value, 4) != 4)
125  throw Exception(kWriteError);
126  }
127 
128  void writeUint64BE(uint64 value) {
129  value = TO_BE_64(value);
130  if (write(&value, 8) != 8)
131  throw Exception(kWriteError);
132  }
133 
135  void writeBytes(byte value, size_t n) {
136  for (size_t i = 0; i < n; ++i) {
137  if (write(&value, 1) != 1)
138  throw Exception(kWriteError);
139  }
140  }
141 
143  FORCEINLINE void writeZeros(size_t n) {
144  writeBytes(0, n);
145  }
146 
148  writeUint16LE((uint16)value);
149  }
150 
152  writeUint32LE((uint32)value);
153  }
154 
156  writeUint64LE((uint64)value);
157  }
158 
160  writeUint16BE((uint16)value);
161  }
162 
164  writeUint32BE((uint32)value);
165  }
166 
168  writeUint64BE((uint64)value);
169  }
170 
171  FORCEINLINE void writeIEEEFloatLE(float value) {
173  }
174 
175  FORCEINLINE void writeIEEEFloatBE(float value) {
177  }
178 
179  FORCEINLINE void writeIEEEDoubleLE(double value) {
181  }
182 
183  FORCEINLINE void writeIEEEDoubleBE(double value) {
185  }
186 
197  size_t writeStream(ReadStream &stream, size_t n);
198 
208  size_t writeStream(ReadStream &stream);
209 
212  void writeString(const UString &str);
213 };
214 
216 public:
218  enum Origin {
223  };
224 
227 
232  virtual size_t pos() const = 0;
233 
239  virtual size_t size() const = 0;
240 
254  virtual size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin) = 0;
255 
265  virtual size_t skip(ptrdiff_t offset) {
266  return seek(offset, kOriginCurrent);
267  }
268 
270  static size_t evalSeek(ptrdiff_t offset, Origin whence, size_t pos, size_t begin, size_t size);
271 };
272 
273 } // End of namespace Common
274 
275 #endif // COMMON_WRITESTREAM_H
Generic interface for a readable data stream.
Definition: readstream.h:64
Definition: 2dafile.h:39
virtual void flush()
Commit any buffered data to the underlying channel or storage medium; unbuffered streams can use the ...
Definition: writestream.cpp:69
A class holding an UTF-8 string.
Definition: ustring.h:48
void writeString(const UString &str)
Write the given string to the stream, encoded as UTF-8.
Definition: writestream.cpp:94
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.
uint64_t uint64
Definition: types.h:206
void writeBytes(byte value, size_t n)
Write n bytes of value to the stream.
Definition: writestream.h:135
Seek from the current position of the stream.
Definition: writestream.h:220
FORCEINLINE void writeIEEEDoubleBE(double value)
Definition: writestream.h:183
Low-level macros and functions to handle different endianness portably.
FORCEINLINE void writeSint16BE(int16 value)
Definition: writestream.h:159
FORCEINLINE void writeSint64BE(int64 value)
Definition: writestream.h:167
FORCEINLINE void writeSint32LE(int32 value)
Definition: writestream.h:151
void writeUint64LE(uint64 value)
Definition: writestream.h:110
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
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
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
FORCEINLINE void writeIEEEDoubleLE(double value)
Definition: writestream.h:179
Basic exceptions to throw.
void writeUint16BE(uint16 value)
Definition: writestream.h:116
void writeUint64BE(uint64 value)
Definition: writestream.h:128
uint16_t uint16
Definition: types.h:202
Utility templates and functions.
virtual size_t skip(ptrdiff_t offset)
Skip the specified number of bytes, adding that offset to the current position in the stream...
Definition: writestream.h:265
FORCEINLINE void writeSint64LE(int64 value)
Definition: writestream.h:155
const Exception kWriteError("Write error")
Exception when writing to a stream failed.
Definition: error.h:64
Low-level type definitions to handle fixed width types portably.
void writeUint16LE(uint16 value)
Definition: writestream.h:98
Seek from the end of the stream.
Definition: writestream.h:221
StackException Exception
Definition: error.h:59
FORCEINLINE void writeSint16LE(int16 value)
Definition: writestream.h:147
virtual size_t write(const void *dataPtr, size_t dataSize)=0
Write data into the stream.
void writeByte(byte value)
Definition: writestream.h:88
Generic interface for a writable data stream.
Definition: writestream.h:64
int64_t int64
Definition: types.h:205
virtual ~WriteStream()
Definition: writestream.cpp:62
void writeSByte(int8 value)
Definition: writestream.h:93
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
FORCEINLINE void writeIEEEFloatLE(float value)
Definition: writestream.h:171
uint32_t uint32
Definition: types.h:204
FORCEINLINE void writeSint32BE(int32 value)
Definition: writestream.h:163
size_t writeStream(ReadStream &stream, size_t n)
Copy n bytes of the given stream into the stream.
Definition: writestream.cpp:72
Origin
The position a seeking offset takes as a base.
Definition: writestream.h:218
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
FORCEINLINE void writeIEEEFloatBE(float value)
Definition: writestream.h:175
void writeUint32BE(uint32 value)
Definition: writestream.h:122
FORCEINLINE void writeZeros(size_t n)
Write n zeros to the stream.
Definition: writestream.h:143
#define FORCEINLINE
Definition: system.h:411
void writeUint32LE(uint32 value)
Definition: writestream.h:104
uint8 byte
Definition: types.h:209
Seek from the begin of the stream.
Definition: writestream.h:219
int32_t int32
Definition: types.h:203