/
reader.h
175 lines (150 loc) · 5.16 KB
/
reader.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
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* 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 2 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBDENG2_READER_HH
#define LIBDENG2_READER_HH
#include "../libdeng2.h"
#include "../IByteArray"
#include "../ByteOrder"
namespace de {
class Block;
class String;
class IReadable;
class FixedByteArray;
class IIStream;
/**
* Provides a protocol for reading data from a byte array object (anything with
* a IByteArray interface). Byte order defaults to little-endian but can be
* changed to big-endian.
*
* @ingroup data
*/
class DENG2_PUBLIC Reader
{
public:
/// Seeking is not possible, e.g., when reading from a stream. @ingroup errors
DENG2_ERROR(SeekError);
public:
/**
* Constructs a new reader.
*
* @param source Byte array containing the data to be read.
* @param byteOrder Byte order to use.
* @param offset Offset in @a source where to start reading.
*/
Reader(const IByteArray& source, const ByteOrder& byteOrder = littleEndianByteOrder,
IByteArray::Offset offset = 0);
/**
* Constructs a new reader that reads from a stream.
*
* @param stream Stream where input is read.
* @param byteOrder Byte order to use.
*/
Reader(IIStream& stream, const ByteOrder& byteOrder = littleEndianByteOrder);
/**
* Constructs a new reader that reads from a const stream.
*
* @param stream Const stream where input is read.
* @param byteOrder Byte order to use.
*/
Reader(const IIStream& stream, const ByteOrder& byteOrder = littleEndianByteOrder);
//@{ Read a number from the source buffer, in network byte order.
Reader& operator >> (char& byte);
Reader& operator >> (dchar& byte);
Reader& operator >> (duchar& byte);
Reader& operator >> (dint16& word);
Reader& operator >> (duint16& word);
Reader& operator >> (dint32& dword);
Reader& operator >> (duint32& dword);
Reader& operator >> (dint64& qword);
Reader& operator >> (duint64& qword);
Reader& operator >> (dfloat& value);
Reader& operator >> (ddouble& value);
//@}
/// Reads a string from the source buffer.
Reader& operator >> (String& text);
/// Reads a sequence bytes from the source buffer.
Reader& operator >> (IByteArray& byteArray);
/**
* Reads a fixed-size sequence of bytes from the source buffer.
* The size of the sequence is determined by the size of
* @a fixedByteArray.
*
* @param fixedByteArray Destination buffer.
*
* @return Reference to the Reader.
*/
Reader& operator >> (FixedByteArray& fixedByteArray);
/// Reads a Block from the source buffer.
Reader& operator >> (Block& block);
/// Reads a serializable object from the source buffer.
Reader& operator >> (IReadable& readable);
/**
* Reads bytes from the source buffer until a specified delimiter
* value is encountered. The delimiter is included as part of
* the read data.
*
* @param byteArray Destination buffer.
* @param delimiter Delimiter value.
*/
Reader& readUntil(IByteArray& byteArray, IByteArray::Byte delimiter = 0);
/**
* Returns the source byte array of the reader.
*/
const IByteArray* source() const;
/**
* Returns the offset used by the reader.
*/
IByteArray::Offset offset() const;
/**
* Move to a specific position in the source data.
*
* @param offset Offset to move to.
*/
void setOffset(IByteArray::Offset offset);
/**
* Moves the reader offset forward by a number of bytes. This is a random
* access seek: it is only possible if the source supports random access
* (e.g., it is impossible to seek in streams).
*
* @param count Number of bytes to move forward. Negative values move
* the reader offset backward.
*/
void seek(dint count);
/**
* Marks the current position for rewinding later. After setting the mark,
* you are expected to call rewind() to return to the marked position. This
* method can be used even when reading from streams.
*/
void mark();
/**
* Rewinds the read offset to the mark set previously (using mark()).
* Rewinding can be done with all readers, regardless of where the data
* comes from.
*/
void rewind();
/**
* Returns the byte order of the writer.
*/
const ByteOrder& byteOrder() const;
private:
struct Instance;
Instance* d;
};
} // namespace de
#endif /* LIBDENG2_READER_HH */