C++ Utilities  4.12.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
binaryreader.h
Go to the documentation of this file.
1 #ifndef IOUTILITIES_BINERYREADER_H
2 #define IOUTILITIES_BINERYREADER_H
3 
4 #include "../conversion/binaryconversion.h"
5 
6 #include <istream>
7 #include <string>
8 #include <vector>
9 
10 namespace IoUtilities {
12 
13 public:
14  BinaryReader(std::istream *stream);
15  BinaryReader(const BinaryReader &other);
16  BinaryReader &operator=(const BinaryReader &rhs) = delete;
17  ~BinaryReader();
18 
19  const std::istream *stream() const;
20  std::istream *stream();
21  void setStream(std::istream *stream, bool giveOwnership = false);
22  bool hasOwnership() const;
23  void giveOwnership();
24  void detatchOwnership();
25  bool fail() const;
26  bool eof() const;
27  bool canRead() const;
28  std::istream::pos_type readStreamsize();
29  void read(char *buffer, std::streamsize length);
30  void read(byte *buffer, std::streamsize length);
31  void read(std::vector<char> &buffer, std::streamsize length);
32  int16 readInt16BE();
33  uint16 readUInt16BE();
34  int32 readInt24BE();
35  uint32 readUInt24BE();
36  int32 readInt32BE();
37  uint32 readUInt32BE();
38  int64 readInt40BE();
39  uint64 readUInt40BE();
40  int64 readInt56BE();
41  uint64 readUInt56BE();
42  int64 readInt64BE();
43  uint64 readUInt64BE();
44  float32 readFloat32BE();
45  float64 readFloat64BE();
46  int16 readInt16LE();
47  uint16 readUInt16LE();
48  int32 readInt24LE();
49  uint32 readUInt24LE();
50  int32 readInt32LE();
51  uint32 readUInt32LE();
52  int64 readInt40LE();
53  uint64 readUInt40LE();
54  int64 readInt56LE();
55  uint64 readUInt56LE();
56  int64 readInt64LE();
57  uint64 readUInt64LE();
58  float32 readFloat32LE();
59  float64 readFloat64LE();
60  char readChar();
61  byte readByte();
62  bool readBool();
63  std::string readLengthPrefixedString();
64  std::string readString(std::size_t length);
65  std::string readTerminatedString(byte termination = 0);
66  std::string readTerminatedString(size_t maxBytesToRead, byte termination = 0);
67  std::string readMultibyteTerminatedStringBE(uint16 termination = 0);
68  std::string readMultibyteTerminatedStringLE(uint16 termination = 0);
69  std::string readMultibyteTerminatedStringBE(std::size_t maxBytesToRead, uint16 termination = 0);
70  std::string readMultibyteTerminatedStringLE(std::size_t maxBytesToRead, uint16 termination = 0);
71  uint32 readSynchsafeUInt32BE();
72  float32 readFixed8BE();
73  float32 readFixed16BE();
74  uint32 readSynchsafeUInt32LE();
75  float32 readFixed8LE();
76  float32 readFixed16LE();
77  uint32 readCrc32(std::size_t length);
78  static uint32 computeCrc32(const char *buffer, std::size_t length);
79  static const uint32 crc32Table[];
80 
81 private:
82  std::istream *m_stream;
83  bool m_ownership;
84  char m_buffer[8];
85 };
86 
92 inline std::istream *BinaryReader::stream()
93 {
94  return m_stream;
95 }
96 
102 inline const std::istream *BinaryReader::stream() const
103 {
104  return m_stream;
105 }
106 
114 inline bool BinaryReader::hasOwnership() const
115 {
116  return m_ownership;
117 }
118 
127 {
128  if (m_stream) {
129  m_ownership = true;
130  }
131 }
132 
141 {
142  m_ownership = false;
143 }
144 
148 inline bool BinaryReader::fail() const
149 {
150  return m_stream ? m_stream->fail() : false;
151 }
152 
156 inline bool BinaryReader::eof() const
157 {
158  return m_stream && m_stream->eof();
159 }
160 
164 inline bool BinaryReader::canRead() const
165 {
166  return m_stream && m_stream->good();
167 }
168 
172 inline void BinaryReader::read(char *buffer, std::streamsize length)
173 {
174  m_stream->read(buffer, length);
175 }
176 
180 inline void BinaryReader::read(byte *buffer, std::streamsize length)
181 {
182  m_stream->read(reinterpret_cast<char *>(buffer), length);
183 }
184 
188 inline void BinaryReader::read(std::vector<char> &buffer, std::streamsize length)
189 {
190  buffer.resize(length);
191  m_stream->read(buffer.data(), length);
192 }
193 
198 {
199  m_stream->read(m_buffer, sizeof(int16));
200  return ConversionUtilities::BE::toInt16(m_buffer);
201 }
202 
207 {
208  m_stream->read(m_buffer, sizeof(uint16));
209  return ConversionUtilities::BE::toUInt16(m_buffer);
210 }
211 
216 {
217  *m_buffer = 0;
218  m_stream->read(m_buffer + 1, 3);
219  auto val = ConversionUtilities::BE::toInt32(m_buffer);
220  if (val >= 0x800000) {
221  val = -(0x1000000 - val);
222  }
223  return val;
224 }
225 
230 {
231  *m_buffer = 0;
232  m_stream->read(m_buffer + 1, 3);
233  return ConversionUtilities::BE::toUInt32(m_buffer);
234 }
235 
240 {
241  m_stream->read(m_buffer, sizeof(int32));
242  return ConversionUtilities::BE::toInt32(m_buffer);
243 }
244 
249 {
250  m_stream->read(m_buffer, sizeof(uint32));
251  return ConversionUtilities::BE::toUInt32(m_buffer);
252 }
253 
258 {
259  *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
260  m_stream->read(m_buffer + 3, 5);
261  auto val = ConversionUtilities::BE::toInt64(m_buffer);
262  if (val >= 0x8000000000) {
263  val = -(0x10000000000 - val);
264  }
265  return val;
266 }
267 
272 {
273  *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
274  m_stream->read(m_buffer + 3, 5);
275  return ConversionUtilities::BE::toUInt64(m_buffer);
276 }
277 
282 {
283  *m_buffer = 0;
284  m_stream->read(m_buffer + 1, 7);
285  auto val = ConversionUtilities::BE::toInt64(m_buffer);
286  if (val >= 0x80000000000000) {
287  val = -(0x100000000000000 - val);
288  }
289  return val;
290 }
291 
296 {
297  *m_buffer = 0;
298  m_stream->read(m_buffer + 1, 7);
299  return ConversionUtilities::BE::toUInt64(m_buffer);
300 }
301 
306 {
307  m_stream->read(m_buffer, sizeof(int64));
308  return ConversionUtilities::BE::toInt64(m_buffer);
309 }
310 
315 {
316  m_stream->read(m_buffer, sizeof(uint64));
317  return ConversionUtilities::BE::toUInt64(m_buffer);
318 }
319 
324 {
325  m_stream->read(m_buffer, sizeof(float32));
326  return ConversionUtilities::BE::toFloat32(m_buffer);
327 }
328 
333 {
334  m_stream->read(m_buffer, sizeof(float64));
335  return ConversionUtilities::BE::toFloat64(m_buffer);
336 }
337 
342 {
343  m_stream->read(m_buffer, sizeof(int16));
344  return ConversionUtilities::LE::toInt16(m_buffer);
345 }
346 
351 {
352  m_stream->read(m_buffer, sizeof(uint16));
353  return ConversionUtilities::LE::toUInt16(m_buffer);
354 }
355 
360 {
361  *(m_buffer + 3) = 0;
362  m_stream->read(m_buffer, 3);
363  auto val = ConversionUtilities::LE::toInt32(m_buffer);
364  if (val >= 0x800000) {
365  val = -(0x1000000 - val);
366  }
367  return val;
368 }
369 
374 {
375  *(m_buffer + 3) = 0;
376  m_stream->read(m_buffer, 3);
377  return ConversionUtilities::LE::toUInt32(m_buffer);
378 }
379 
384 {
385  m_stream->read(m_buffer, sizeof(int32));
386  return ConversionUtilities::LE::toInt32(m_buffer);
387 }
388 
393 {
394  m_stream->read(m_buffer, sizeof(uint32));
395  return ConversionUtilities::LE::toUInt32(m_buffer);
396 }
397 
402 {
403  *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
404  m_stream->read(m_buffer, 5);
405  auto val = ConversionUtilities::LE::toInt64(m_buffer);
406  if (val >= 0x8000000000) {
407  val = -(0x10000000000 - val);
408  }
409  return val;
410 }
411 
416 {
417  *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
418  m_stream->read(m_buffer, 5);
419  return ConversionUtilities::LE::toUInt64(m_buffer);
420 }
421 
426 {
427  *(m_buffer + 7) = 0;
428  m_stream->read(m_buffer, 7);
429  auto val = ConversionUtilities::LE::toInt64(m_buffer);
430  if (val >= 0x80000000000000) {
431  val = -(0x100000000000000 - val);
432  }
433  return val;
434 }
435 
440 {
441  *(m_buffer + 7) = 0;
442  m_stream->read(m_buffer, 7);
443  return ConversionUtilities::LE::toUInt64(m_buffer);
444 }
445 
450 {
451  m_stream->read(m_buffer, sizeof(int64));
452  return ConversionUtilities::LE::toInt64(m_buffer);
453 }
454 
459 {
460  m_stream->read(m_buffer, sizeof(uint64));
461  return ConversionUtilities::LE::toUInt64(m_buffer);
462 }
463 
468 {
469  m_stream->read(m_buffer, sizeof(float32));
470  return ConversionUtilities::LE::toFloat32(m_buffer);
471 }
472 
477 {
478  m_stream->read(m_buffer, sizeof(float64));
479  return ConversionUtilities::LE::toFloat64(m_buffer);
480 }
481 
486 {
487  m_stream->read(m_buffer, sizeof(char));
488  return m_buffer[0];
489 }
490 
495 {
496  m_stream->read(m_buffer, sizeof(char));
497  return static_cast<byte>(m_buffer[0]);
498 }
499 
505 {
506  return readByte() != 0;
507 }
508 
515 {
517 }
518 
523 {
525 }
526 
531 {
533 }
534 
541 {
543 }
544 
549 {
551 }
552 
557 {
559 }
560 } // namespace IoUtilities
561 
562 #endif // IOUTILITIES_BINERYREADER_H
int64 readInt64LE()
Reads a 64-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:449
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
Definition: binaryreader.h:148
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
Definition: binaryreader.h:504
float32 readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:323
int64 readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:257
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
int16 readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:341
Reads primitive data types from a std::istream.
Definition: binaryreader.h:11
float64 readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:476
int32 readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:383
float32 readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:467
uint32 readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:229
int16 readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:197
void read(char *buffer, std::streamsize length)
Reads the specified number of characters from the stream in the character array.
Definition: binaryreader.h:172
int64 readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:305
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
float64 readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:332
int64 readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:281
uint64 readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:439
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
Definition: binaryreader.h:114
uint32 readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:392
float32 readFixed16BE()
Reads a 16.16 fixed point big endian representation from the current stream and returns it as 32-bit ...
Definition: binaryreader.h:530
uint32 readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:248
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10
uint64 readUInt40BE()
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:271
uint64 readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:314
std::uint32_t uint32
unsigned 32-bit integer
Definition: types.h:44
uint16 readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:350
void giveOwnership()
The reader will take ownership over the assigned stream.
Definition: binaryreader.h:126
uint16 readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:206
void detatchOwnership()
The reader will not take ownership over the assigned stream.
Definition: binaryreader.h:140
int32 readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:359
std::int32_t int32
signed 32-bit integer
Definition: types.h:24
const std::istream * stream() const
Returns a pointer to the stream the reader will read from when calling one of the read-methods...
Definition: binaryreader.h:102
int32 readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:239
int32 readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:215
uint32 readSynchsafeUInt32LE()
Reads a 32-bit little endian synchsafe integer from the current stream and advances the current posit...
Definition: binaryreader.h:540
std::uint8_t byte
unsigned byte
Definition: types.h:14
bool canRead() const
Returns an indication whether a stream is assigned the reader can read from.
Definition: binaryreader.h:164
uint32 readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:373
int64 readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:425
byte readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
Definition: binaryreader.h:494
CPP_UTILITIES_EXPORT constexpr float32 toFloat32(uint16 fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation...
char readChar()
Reads a single character from the current stream and advances the current position of the stream by o...
Definition: binaryreader.h:485
float32 readFixed8BE()
Reads a 8.8 fixed point big endian representation from the current stream and returns it as 32-bit fl...
Definition: binaryreader.h:522
std::int16_t int16
signed 16-bit integer
Definition: types.h:19
CPP_UTILITIES_EXPORT constexpr uint32 toNormalInt(uint32 synchsafeInt)
Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.
uint64 readUInt64LE()
Reads a 64-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:458
float32 readFixed16LE()
Reads a 16.16 fixed point little endian representation from the current stream and returns it as 32-b...
Definition: binaryreader.h:556
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
bool eof() const
Returns an indication whether the end-of-stream bit of the assigned stream is set.
Definition: binaryreader.h:156
float32 readFixed8LE()
Reads a 8.8 fixed point little endian representation from the current stream and returns it as 32-bit...
Definition: binaryreader.h:548
uint64 readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:295
int64 readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:401
uint32 readSynchsafeUInt32BE()
Reads a 32-bit big endian synchsafe integer from the current stream and advances the current position...
Definition: binaryreader.h:514
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39
uint64 readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:415