-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream.h
313 lines (279 loc) · 9.46 KB
/
stream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* smushplay - A simple LucasArts SMUSH video player
*
* smushplay is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
// Based on ScummVM Stream classes (GPLv2+)
#ifndef STREAM_H
#define STREAM_H
#include "util.h"
/**
* Virtual base class for both ReadStream and WriteStream.
*/
class Stream {
public:
virtual ~Stream() {}
/**
* Returns true if an I/O failure occurred.
* This flag is never cleared automatically. In order to clear it,
* client code has to call clearErr() explicitly.
*
* @note The semantics of any implementation of this method are
* supposed to match those of ISO C ferror().
*/
virtual bool err() const { return false; }
/**
* Reset the I/O error status as returned by err().
* For a ReadStream, also reset the end-of-stream status returned by eos().
*
* @note The semantics of any implementation of this method are
* supposed to match those of ISO C clearerr().
*/
virtual void clearErr() {}
};
/**
* Generic interface for a readable data stream.
*/
class ReadStream : virtual public Stream {
public:
/**
* Returns true if a read failed because the stream end has been reached.
* This flag is cleared by clearErr().
* For a SeekableReadStream, it is also cleared by a successful seek.
*
* @note The semantics of any implementation of this method are
* supposed to match those of ISO C feof(). In particular, in a stream
* with N bytes, reading exactly N bytes from the start should *not*
* set eos; only reading *beyond* the available data should set it.
*/
virtual bool eos() const = 0;
/**
* Read data from the stream. Subclasses must implement this
* method; all other read methods are implemented using it.
*
* @note The semantics of any implementation of this method are
* supposed to match those of ISO C fread(), in particular where
* it concerns setting error and end of file/stream flags.
*
* @param dataPtr pointer to a buffer into which the data is read
* @param dataSize number of bytes to be read
* @return the number of bytes which were actually read.
*/
virtual uint32 read(void *dataPtr, uint32 dataSize) = 0;
// The remaining methods all have default implementations; subclasses
// in general should not overload them.
/**
* Read an unsigned byte from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
byte readByte() {
byte b = 0; // FIXME: remove initialisation
read(&b, 1);
return b;
}
/**
* Read a signed byte from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
inline int8 readSByte() {
return (int8)readByte();
}
/**
* Read an unsigned 16-bit word stored in little endian (LSB first) order
* from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
uint16 readUint16LE() {
uint16 val;
read(&val, 2);
return FROM_LE_16(val);
}
/**
* Read an unsigned 32-bit word stored in little endian (LSB first) order
* from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
uint32 readUint32LE() {
uint32 val;
read(&val, 4);
return FROM_LE_32(val);
}
/**
* Read an unsigned 16-bit word stored in big endian (MSB first) order
* from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
uint16 readUint16BE() {
uint16 val;
read(&val, 2);
return FROM_BE_16(val);
}
/**
* Read an unsigned 32-bit word stored in big endian (MSB first) order
* from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
uint32 readUint32BE() {
uint32 val;
read(&val, 4);
return FROM_BE_32(val);
}
/**
* Read a signed 16-bit word stored in little endian (LSB first) order
* from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
inline int16 readSint16LE() {
return (int16)readUint16LE();
}
/**
* Read a signed 32-bit word stored in little endian (LSB first) order
* from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
inline int32 readSint32LE() {
return (int32)readUint32LE();
}
/**
* Read a signed 16-bit word stored in big endian (MSB first) order
* from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
inline int16 readSint16BE() {
return (int16)readUint16BE();
}
/**
* Read a signed 32-bit word stored in big endian (MSB first) order
* from the stream and return it.
* Performs no error checking. The return value is undefined
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
inline int32 readSint32BE() {
return (int32)readUint32BE();
}
};
/**
* Interface for a seekable & readable data stream.
*
* @todo Get rid of SEEK_SET, SEEK_CUR, or SEEK_END, use our own constants
*/
class SeekableReadStream : virtual public ReadStream {
public:
/**
* Obtains the current value of the stream position indicator of the
* stream.
*
* @return the current position indicator, or -1 if an error occurred.
*/
virtual int32 pos() const = 0;
/**
* Obtains the total size of the stream, measured in bytes.
* If this value is unknown or can not be computed, -1 is returned.
*
* @return the size of the stream, or -1 if an error occurred
*/
virtual int32 size() const = 0;
/**
* Sets the stream position indicator for the stream. The new position,
* measured in bytes, is obtained by adding offset bytes to the position
* specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or
* SEEK_END, the offset is relative to the start of the file, the current
* position indicator, or end-of-file, respectively. A successful call
* to the seek() method clears the end-of-file indicator for the stream.
*
* @note The semantics of any implementation of this method are
* supposed to match those of ISO C fseek().
*
* @param offset the relative offset in bytes
* @param whence the seek reference: SEEK_SET, SEEK_CUR, or SEEK_END
* @return true on success, false in case of a failure
*/
virtual bool seek(int32 offset, int whence = SEEK_SET) = 0;
};
/**
* Simple memory based 'stream', which implements the SeekableReadStream interface for
* a plain memory block.
*/
class MemoryReadStream : public SeekableReadStream {
public:
/**
* This constructor takes a pointer to a memory buffer and a length, and
* wraps it. If disposeMemory is true, the MemoryReadStream takes ownership
* of the buffer and hence deletes it when destructed.
*/
MemoryReadStream(const byte *dataPtr, uint32 dataSize, bool disposeMemory = false) :
_ptrOrig(dataPtr),
_ptr(dataPtr),
_size(dataSize),
_pos(0),
_disposeMemory(disposeMemory),
_eos(false) {}
~MemoryReadStream() {
if (_disposeMemory)
delete[] const_cast<byte *>(_ptrOrig);
}
uint32 read(void *dataPtr, uint32 dataSize);
bool eos() const { return _eos; }
void clearErr() { _eos = false; }
int32 pos() const { return _pos; }
int32 size() const { return _size; }
bool seek(int32 offs, int whence = SEEK_SET);
private:
const byte * const _ptrOrig;
const byte *_ptr;
const uint32 _size;
uint32 _pos;
bool _disposeMemory;
bool _eos;
};
/** Open a file with a given path. */
SeekableReadStream *createReadStream(const char *pathName);
/**
* Take an arbitrary SeekableReadStream and wrap it in a custom stream which
* provides transparent on-the-fly decompression. Assumes the data it
* retrieves from the wrapped stream to be either uncompressed or in gzip
* format. In the former case, the original stream is returned unmodified
* (and in particular, not wrapped).
*
* It is safe to call this with a NULL parameter (in this case, NULL is
* returned).
*
* @param toBeWrapped the stream to be wrapped (if it is in gzip-format)
*/
SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped);
#endif