C++ Utilities  4.6.1
Common C++ classes and routines used by my applications 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 <vector>
7 #include <string>
8 #include <istream>
9 
10 namespace IoUtilities
11 {
13 {
14 
15 public:
16  BinaryReader(std::istream *stream);
17  BinaryReader(const BinaryReader &other);
18  BinaryReader & operator=(const BinaryReader & rhs) = delete;
19  ~BinaryReader();
20 
21  const std::istream *stream() const;
22  std::istream *stream();
23  void setStream(std::istream *stream, bool giveOwnership = false);
24  bool hasOwnership() const;
25  void giveOwnership();
26  void detatchOwnership();
27  bool fail() const;
28  bool eof() const;
29  bool canRead() const;
30  std::istream::pos_type readStreamsize();
31  void read(char *buffer, std::streamsize length);
32  void read(byte *buffer, std::streamsize length);
33  void read(std::vector<char> &buffer, std::streamsize length);
34  int16 readInt16BE();
35  uint16 readUInt16BE();
36  int32 readInt24BE();
37  uint32 readUInt24BE();
38  int32 readInt32BE();
39  uint32 readUInt32BE();
40  int64 readInt40BE();
41  uint64 readUInt40BE();
42  int64 readInt56BE();
43  uint64 readUInt56BE();
44  int64 readInt64BE();
45  uint64 readUInt64BE();
46  float32 readFloat32BE();
47  float64 readFloat64BE();
48  int16 readInt16LE();
49  uint16 readUInt16LE();
50  int32 readInt24LE();
51  uint32 readUInt24LE();
52  int32 readInt32LE();
53  uint32 readUInt32LE();
54  int64 readInt40LE();
55  uint64 readUInt40LE();
56  int64 readInt56LE();
57  uint64 readUInt56LE();
58  int64 readInt64LE();
59  uint64 readUInt64LE();
60  float32 readFloat32LE();
61  float64 readFloat64LE();
62  char readChar();
63  byte readByte();
64  bool readBool();
65  std::string readLengthPrefixedString();
66  std::string readString(std::size_t length);
67  std::string readTerminatedString(byte termination = 0);
68  std::string readTerminatedString(size_t maxBytesToRead, byte termination = 0);
69  std::string readMultibyteTerminatedStringBE(uint16 termination = 0);
70  std::string readMultibyteTerminatedStringLE(uint16 termination = 0);
71  std::string readMultibyteTerminatedStringBE(std::size_t maxBytesToRead, uint16 termination = 0);
72  std::string readMultibyteTerminatedStringLE(std::size_t maxBytesToRead, uint16 termination = 0);
73  uint32 readSynchsafeUInt32BE();
74  float32 readFixed8BE();
75  float32 readFixed16BE();
76  uint32 readSynchsafeUInt32LE();
77  float32 readFixed8LE();
78  float32 readFixed16LE();
79  uint32 readCrc32(std::size_t length);
80  static uint32 computeCrc32(const char *buffer, std::size_t length);
81  static const uint32 crc32Table[];
82 
83 private:
84  std::istream *m_stream;
85  bool m_ownership;
86  char m_buffer[8];
87 };
88 
94 inline std::istream *BinaryReader::stream()
95 {
96  return m_stream;
97 }
98 
104 inline const std::istream *BinaryReader::stream() const
105 {
106  return m_stream;
107 }
108 
116 inline bool BinaryReader::hasOwnership() const
117 {
118  return m_ownership;
119 }
120 
129 {
130  if(m_stream) {
131  m_ownership = true;
132  }
133 }
134 
143 {
144  m_ownership = false;
145 }
146 
150 inline bool BinaryReader::fail() const
151 {
152  return m_stream ? m_stream->fail() : false;
153 }
154 
158 inline bool BinaryReader::eof() const
159 {
160  return m_stream && m_stream->eof();
161 }
162 
166 inline bool BinaryReader::canRead() const
167 {
168  return m_stream && m_stream->good();
169 }
170 
174 inline void BinaryReader::read(char *buffer, std::streamsize length)
175 {
176  m_stream->read(buffer, length);
177 }
178 
182 inline void BinaryReader::read(byte *buffer, std::streamsize length)
183 {
184  m_stream->read(reinterpret_cast<char *>(buffer), length);
185 }
186 
190 inline void BinaryReader::read(std::vector<char> &buffer, std::streamsize length)
191 {
192  buffer.resize(length);
193  m_stream->read(buffer.data(), length);
194 }
195 
200 {
201  m_stream->read(m_buffer, sizeof(int16));
202  return ConversionUtilities::BE::toInt16(m_buffer);
203 }
204 
209 {
210  m_stream->read(m_buffer, sizeof(uint16));
211  return ConversionUtilities::BE::toUInt16(m_buffer);
212 }
213 
218 {
219  *m_buffer = 0;
220  m_stream->read(m_buffer + 1, 3);
221  auto val = ConversionUtilities::BE::toInt32(m_buffer);
222  if(val >= 0x800000) {
223  val = -(0x1000000 - val);
224  }
225  return val;
226 }
227 
232 {
233  *m_buffer = 0;
234  m_stream->read(m_buffer + 1, 3);
235  return ConversionUtilities::BE::toUInt32(m_buffer);
236 }
237 
242 {
243  m_stream->read(m_buffer, sizeof(int32));
244  return ConversionUtilities::BE::toInt32(m_buffer);
245 }
246 
251 {
252  m_stream->read(m_buffer, sizeof(uint32));
253  return ConversionUtilities::BE::toUInt32(m_buffer);
254 }
255 
260 {
261  *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
262  m_stream->read(m_buffer + 3, 5);
263  auto val = ConversionUtilities::BE::toInt64(m_buffer);
264  if(val >= 0x8000000000) {
265  val = -(0x10000000000 - val);
266  }
267  return val;
268 }
269 
274 {
275  *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
276  m_stream->read(m_buffer + 3, 5);
277  return ConversionUtilities::BE::toUInt64(m_buffer);
278 }
279 
284 {
285  *m_buffer = 0;
286  m_stream->read(m_buffer + 1, 7);
287  auto val = ConversionUtilities::BE::toInt64(m_buffer);
288  if(val >= 0x80000000000000) {
289  val = -(0x100000000000000 - val);
290  }
291  return val;
292 }
293 
298 {
299  *m_buffer = 0;
300  m_stream->read(m_buffer + 1, 7);
301  return ConversionUtilities::BE::toUInt64(m_buffer);
302 }
303 
308 {
309  m_stream->read(m_buffer, sizeof(int64));
310  return ConversionUtilities::BE::toInt64(m_buffer);
311 }
312 
317 {
318  m_stream->read(m_buffer, sizeof(uint64));
319  return ConversionUtilities::BE::toUInt64(m_buffer);
320 }
321 
326 {
327  m_stream->read(m_buffer, sizeof(float32));
328  return ConversionUtilities::BE::toFloat32(m_buffer);
329 }
330 
335 {
336  m_stream->read(m_buffer, sizeof(float64));
337  return ConversionUtilities::BE::toFloat64(m_buffer);
338 }
339 
344 {
345  m_stream->read(m_buffer, sizeof(int16));
346  return ConversionUtilities::LE::toInt16(m_buffer);
347 }
348 
353 {
354  m_stream->read(m_buffer, sizeof(uint16));
355  return ConversionUtilities::LE::toUInt16(m_buffer);
356 }
357 
362 {
363  *(m_buffer + 3) = 0;
364  m_stream->read(m_buffer, 3);
365  auto val = ConversionUtilities::LE::toInt32(m_buffer);
366  if(val >= 0x800000) {
367  val = -(0x1000000 - val);
368  }
369  return val;
370 }
371 
376 {
377  *(m_buffer + 3) = 0;
378  m_stream->read(m_buffer, 3);
379  return ConversionUtilities::LE::toUInt32(m_buffer);
380 }
381 
386 {
387  m_stream->read(m_buffer, sizeof(int32));
388  return ConversionUtilities::LE::toInt32(m_buffer);
389 }
390 
395 {
396  m_stream->read(m_buffer, sizeof(uint32));
397  return ConversionUtilities::LE::toUInt32(m_buffer);
398 }
399 
404 {
405  *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
406  m_stream->read(m_buffer, 5);
407  auto val = ConversionUtilities::LE::toInt64(m_buffer);
408  if(val >= 0x8000000000) {
409  val = -(0x10000000000 - val);
410  }
411  return val;
412 }
413 
418 {
419  *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
420  m_stream->read(m_buffer, 5);
421  return ConversionUtilities::LE::toUInt64(m_buffer);
422 }
423 
428 {
429  *(m_buffer + 7) = 0;
430  m_stream->read(m_buffer, 7);
431  auto val = ConversionUtilities::LE::toInt64(m_buffer);
432  if(val >= 0x80000000000000) {
433  val = -(0x100000000000000 - val);
434  }
435  return val;
436 }
437 
442 {
443  *(m_buffer + 7) = 0;
444  m_stream->read(m_buffer, 7);
445  return ConversionUtilities::LE::toUInt64(m_buffer);
446 }
447 
452 {
453  m_stream->read(m_buffer, sizeof(int64));
454  return ConversionUtilities::LE::toInt64(m_buffer);
455 }
456 
461 {
462  m_stream->read(m_buffer, sizeof(uint64));
463  return ConversionUtilities::LE::toUInt64(m_buffer);
464 }
465 
470 {
471  m_stream->read(m_buffer, sizeof(float32));
472  return ConversionUtilities::LE::toFloat32(m_buffer);
473 }
474 
479 {
480  m_stream->read(m_buffer, sizeof(float64));
481  return ConversionUtilities::LE::toFloat64(m_buffer);
482 }
483 
488 {
489  m_stream->read(m_buffer, sizeof(char));
490  return m_buffer[0];
491 }
492 
497 {
498  m_stream->read(m_buffer, sizeof(char));
499  return static_cast<byte>(m_buffer[0]);
500 }
501 
507 {
508  return readByte() != 0;
509 }
510 
517 {
518  return ConversionUtilities::toNormalInt(readUInt32BE());
519 }
520 
525 {
526  return ConversionUtilities::toFloat32(readUInt16BE());
527 }
528 
533 {
534  return ConversionUtilities::toFloat32(readUInt32BE());
535 }
536 
543 {
544  return ConversionUtilities::toNormalInt(readUInt32LE());
545 }
546 
551 {
552  return ConversionUtilities::toFloat32(readUInt16LE());
553 }
554 
559 {
560  return ConversionUtilities::toFloat32(readUInt32LE());
561 }
562 
563 }
564 
565 #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:451
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
Definition: binaryreader.h:150
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
Definition: binaryreader.h:506
float32 readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:325
int64 readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:259
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:343
Reads primitive data types from a std::istream.
Definition: binaryreader.h:12
float64 readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:478
int32 readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:385
float32 readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:469
uint32 readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:231
int16 readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:199
void read(char *buffer, std::streamsize length)
Reads the specified number of characters from the stream in the character array.
Definition: binaryreader.h:174
int64 readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:307
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:334
int64 readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:283
uint64 readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:441
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
Definition: binaryreader.h:116
uint32 readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:394
float32 readFixed16BE()
Reads a 16.16 fixed point big endian representation from the current stream and returns it as 32-bit ...
Definition: binaryreader.h:532
uint32 readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:250
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:273
uint64 readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:316
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:352
void giveOwnership()
The reader will take ownership over the assigned stream.
Definition: binaryreader.h:128
uint16 readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:208
void detatchOwnership()
The reader will not take ownership over the assigned stream.
Definition: binaryreader.h:142
int32 readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:361
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:104
int32 readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:241
int32 readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:217
uint32 readSynchsafeUInt32LE()
Reads a 32-bit little endian synchsafe integer from the current stream and advances the current posit...
Definition: binaryreader.h:542
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:166
uint32 readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:375
int64 readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:427
byte readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
Definition: binaryreader.h:496
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:487
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:524
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:460
float32 readFixed16LE()
Reads a 16.16 fixed point little endian representation from the current stream and returns it as 32-b...
Definition: binaryreader.h:558
#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:158
float32 readFixed8LE()
Reads a 8.8 fixed point little endian representation from the current stream and returns it as 32-bit...
Definition: binaryreader.h:550
uint64 readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:297
int64 readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:403
uint32 readSynchsafeUInt32BE()
Reads a 32-bit big endian synchsafe integer from the current stream and advances the current position...
Definition: binaryreader.h:516
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:417