forked from andrewprock/ustl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mistream.h
279 lines (255 loc) · 10.8 KB
/
mistream.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
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#pragma once
#include "memlink.h"
#include "uexception.h"
#include "strmsize.h"
#include "utf8.h"
#include "uios.h"
#if WANT_BSTREAM_EXCEPTIONS
#include "typeinfo.h"
#endif
namespace ustl {
class ostream;
class memlink;
class string;
/// \class istream mistream.h ustl.h
/// \ingroup BinaryStreams
///
/// \brief Helper class to read packed binary streams.
///
/// This class contains a set of functions to read integral types from an
/// unstructured memory block. Unpacking binary file data can be done this
/// way, for instance. aligning the data is your responsibility, and can
/// be accomplished by proper ordering of reads and by calling the align()
/// function. Unaligned access is usually slower by orders of magnitude and,
/// on some architectures, such as PowerPC, can cause your program to crash.
/// Therefore, all read functions have asserts to check alignment.
/// Overreading the end of the stream will also cause a crash (an assert in
/// debug builds). Oh, and don't be intimidated by the size of the inlines
/// here. In the assembly code the compiler will usually chop everything down
/// to five instructions each.
///
/// Alignment rules for your objects:
/// - Assume your writes start off 4-byte aligned.
/// - After completion, \ref istream::align the stream to at least 4.
/// - If data portability between 32bit and 64bit platforms is important
/// (it often is not, in config files and the like), ensure you are always
/// using fixed-size types and are aligning to a fixed grain. Avoid writing
/// 8-byte types, and if you do, manually align before doing so.
/// - Non-default alignment is allowed if you plan to frequently write this
/// object in array form and alignment would be costly. For example, an
/// array of uint16_t-sized objects may leave the stream uint16_t aligned
/// as long as you know about it and will default-align the stream after
/// writing the array (note: \ref vector will already do this for you)
///
/// Example code:
/// \code
/// memblock b;
/// b.read_file ("test.file");
/// ostream is (b);
/// is >> boolVar >> ios::talign<int>();
/// is >> intVar >> floatVar;
/// is.read (binaryData, binaryDataSize);
/// is.align();
/// \endcode
///
class istream : public cmemlink, public ios_base {
public:
constexpr istream (void) : cmemlink(), _pos (0) {}
constexpr istream (const void* p, streamsize n) : cmemlink(p, n), _pos (0) {}
constexpr explicit istream (const cmemlink& source) : cmemlink (source), _pos (0) {}
explicit istream (const ostream& source) noexcept;
constexpr iterator end (void) const { return cmemlink::end(); }
inline void link (const void* p, streamsize n) { cmemlink::link (p, n); }
inline void link (const cmemlink& l) { cmemlink::link (l.cdata(), l.readable_size()); }
inline void link (const void* f, const void* l) { cmemlink::link (f, l); }
constexpr void relink (const void* p, streamsize n) { cmemlink::relink (p, n); _pos = 0; }
constexpr void relink (const cmemlink& l) { relink (l.cdata(), l.readable_size()); }
virtual void unlink (void) noexcept override;
virtual streamsize underflow (streamsize = 1);
constexpr uoff_t pos (void) const { return _pos; }
constexpr const_iterator ipos (void) const { return begin() + pos(); }
constexpr streamsize remaining (void) const { return size() - pos(); }
inline void seek (uoff_t newPos);
inline void iseek (const_iterator newPos);
inline void skip (streamsize nBytes);
constexpr bool aligned (streamsize grain = c_DefaultAlignment) const
{ return pos() % grain == 0; }
inline bool verify_remaining (const char* op, const char* type, streamsize n);
constexpr streamsize align_size (streamsize grain = c_DefaultAlignment) const
{ return Align (pos(), grain) - pos(); }
inline void align (streamsize grain = c_DefaultAlignment);
constexpr void swap (istream& is)
{ cmemlink::swap (is); ::ustl::swap (_pos, is._pos); }
inline void read (void* buffer, streamsize size);
inline void read (memlink& buf) { read (buf.begin(), buf.writable_size()); }
void read_strz (string& str);
streamsize readsome (void* s, streamsize n);
inline void read (istream&) { }
void write (ostream& os) const;
void text_write (ostringstream& os) const;
constexpr streamsize stream_size (void) const { return remaining(); }
template <typename T>
inline void iread (T& v);
inline void ungetc (void) { seek (pos() - 1); }
constexpr off_t tellg (void) const { return pos(); }
inline void seekg (off_t p, seekdir d = beg);
private:
streamoff _pos; ///< The current read position.
};
//----------------------------------------------------------------------
template <typename T, typename Stream>
inline constexpr streamsize required_stream_size (T, const Stream&) { return 1; }
template <typename T>
inline constexpr streamsize required_stream_size (T v, const istream&) { return stream_size_of(v); }
template <typename Stream>
inline constexpr bool stream_at_eof (const Stream& stm) { return stm.eof(); }
template <>
inline constexpr bool stream_at_eof (const istream&) { return false; }
/// \class istream_iterator
/// \ingroup BinaryStreamIterators
///
/// \brief An iterator over an istream to use with uSTL algorithms.
///
template <typename T, typename Stream = istream>
class istream_iterator {
public:
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = const value_type*;
using reference = const value_type&;
using size_type = typename Stream::size_type;
using iterator_category = input_iterator_tag;
public:
constexpr istream_iterator (void) : _pis (nullptr), _v() {}
constexpr explicit istream_iterator (Stream& is) : _pis (&is), _v() { Read(); }
constexpr istream_iterator (const istream_iterator& i) : _pis (i._pis), _v (i._v) {}
/// Reads and returns the next value.
constexpr const T& operator* (void) { return _v; }
inline istream_iterator& operator++ (void) { Read(); return *this; }
inline istream_iterator& operator-- (void) { _pis->seek (_pis->pos() - 2 * stream_size_of(_v)); return operator++(); }
inline istream_iterator operator++ (int) { istream_iterator old (*this); operator++(); return old; }
inline istream_iterator operator-- (int) { istream_iterator old (*this); operator--(); return old; }
inline istream_iterator& operator+= (streamsize n) { while (n--) operator++(); return *this; }
inline istream_iterator& operator-= (streamsize n) { _pis->seek (_pis->pos() - (n + 1) * stream_size_of(_v)); return operator++(); }
inline istream_iterator operator- (streamoff n) const { istream_iterator result (*this); return result -= n; }
constexpr difference_type operator- (const istream_iterator& i) const { return distance (i._pis->pos(), _pis->pos()) / stream_size_of(_v); }
constexpr bool operator== (const istream_iterator& i) const { return (!_pis && !i._pis) || (_pis && i._pis && _pis->pos() == i._pis->pos()); }
constexpr bool operator< (const istream_iterator& i) const { return !i._pis || (_pis && _pis->pos() < i._pis->pos()); }
private:
void Read (void)
{
if (!_pis)
return;
const streamsize rs (required_stream_size (_v, *_pis));
if (_pis->remaining() < rs && _pis->underflow (rs) < rs) {
_pis = nullptr;
return;
}
*_pis >> _v;
if (stream_at_eof (*_pis))
_pis = nullptr;
}
private:
Stream* _pis; ///< The host stream.
T _v; ///< Last read value; cached to be returnable as a const reference.
};
//----------------------------------------------------------------------
/// Checks that \p n bytes are available in the stream, or else throws.
inline bool istream::verify_remaining (const char* op, const char* type, streamsize n)
{
const streamsize rem = remaining();
bool enough = n <= rem;
if (!enough) overrun (op, type, n, pos(), rem);
return enough;
}
/// Sets the current read position to \p newPos
inline void istream::seek (uoff_t newPos)
{
#if WANT_BSTREAM_EXCEPTIONS
if (newPos > size())
return overrun ("seekg", "byte", newPos, pos(), size());
#else
assert (newPos <= size());
#endif
_pos = newPos;
}
/// Sets the current read position to \p newPos
inline void istream::iseek (const_iterator newPos)
{
seek (distance (begin(), newPos));
}
/// Sets the current write position to \p p based on \p d.
inline void istream::seekg (off_t p, seekdir d)
{
switch (d) {
case beg: seek (p); break;
case cur: seek (pos() + p); break;
case ios_base::end: seek (size() - p); break;
}
}
/// Skips \p nBytes without reading them.
inline void istream::skip (streamsize nBytes)
{
seek (pos() + nBytes);
}
/// aligns the read position on \p grain
inline void istream::align (streamsize grain)
{
seek (Align (pos(), grain));
}
/// Reads type T from the stream via a direct pointer cast.
template <typename T>
inline void istream::iread (T& v)
{
assert (aligned (stream_align_of (v)));
#if WANT_BSTREAM_EXCEPTIONS
if (!verify_remaining ("read", typeid(v).name(), sizeof(T)))
return;
#else
assert (remaining() >= sizeof(T));
#endif
v = *reinterpret_cast<const T*>(ipos());
_pos += sizeof(T);
}
/// Reads \p n bytes into \p buffer.
inline void istream::read (void* buffer, size_type n)
{
#if WANT_BSTREAM_EXCEPTIONS
if (!verify_remaining ("read", "binary data", n))
return;
#else
assert (remaining() >= n && "Reading past end of buffer. Make sure you are reading the right format.");
#endif
memcpy (static_cast<value_type*>(buffer), ipos(), n);
_pos += n;
}
//----------------------------------------------------------------------
template <typename T> struct object_reader {
inline void operator()(istream& is, T& v) const { v.read (is); }
};
template <typename T> struct integral_object_reader {
inline void operator()(istream& is, T& v) const { is.iread (v); }
};
template <typename T>
inline istream& operator>> (istream& is, T& v) {
using object_reader_t = typename tm::Select <numeric_limits<T>::is_integral, integral_object_reader<T>, object_reader<T>>::Result;
object_reader_t()(is, v);
return is;
}
template <typename T>
inline istream& operator>> (istream& is, const T& v) { v.read (is); return is; }
//----------------------------------------------------------------------
using istream_iterator_for_utf8 = istream_iterator<utf8subchar_t> ;
using utf8istream_iterator = utf8in_iterator<istream_iterator_for_utf8> ;
/// Returns a UTF-8 adaptor reading from \p is.
inline utf8istream_iterator utf8in (istream& is)
{
istream_iterator_for_utf8 si (is);
return utf8istream_iterator (si);
}
//----------------------------------------------------------------------
} // namespace ustl