C++ Utilities
4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
|
Reads primitive data types from a std::istream. More...
#include <binaryreader.h>
Public Member Functions | |
BinaryReader (std::istream *stream) | |
Constructs a new BinaryReader. More... | |
BinaryReader (const BinaryReader &other) | |
Copies the specified BinaryReader. More... | |
BinaryReader & | operator= (const BinaryReader &rhs)=delete |
~BinaryReader () | |
Destroys the BinaryReader. More... | |
const std::istream * | stream () const |
Returns a pointer to the stream the reader will read from when calling one of the read-methods. More... | |
std::istream * | stream () |
Returns a pointer to the stream the reader will read from when calling one of the read-methods. More... | |
void | setStream (std::istream *stream, bool giveOwnership=false) |
Assigns the stream the reader will read from when calling one of the read-methods. More... | |
bool | hasOwnership () const |
Returns whether the reader takes ownership over the assigned stream. More... | |
void | giveOwnership () |
The reader will take ownership over the assigned stream. More... | |
void | detatchOwnership () |
The reader will not take ownership over the assigned stream. More... | |
bool | fail () const |
Returns an indication whether the fail bit of the assigned stream is set. More... | |
bool | eof () const |
Returns an indication whether the end-of-stream bit of the assigned stream is set. More... | |
bool | canRead () const |
Returns an indication whether a stream is assigned the reader can read from. More... | |
std::istream::pos_type | readStreamsize () |
Returns the size of the assigned stream. More... | |
void | read (char *buffer, std::streamsize length) |
Reads the specified number of characters from the stream in the character array. More... | |
void | read (byte *buffer, std::streamsize length) |
Reads the specified number of bytes from the stream in the character array. More... | |
void | read (std::vector< char > &buffer, std::streamsize length) |
Reads the specified number of bytes from the stream in the specified buffer. More... | |
int16 | readInt16BE () |
Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes. More... | |
uint16 | readUInt16BE () |
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position of the stream by two bytes. More... | |
int32 | readInt24BE () |
Reads a 24-bit big endian signed integer from the current stream and advances the current position of the stream by three bytes. More... | |
uint32 | readUInt24BE () |
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position of the stream by three bytes. More... | |
int32 | readInt32BE () |
Reads a 32-bit big endian signed integer from the current stream and advances the current position of the stream by four bytes. More... | |
uint32 | readUInt32BE () |
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position of the stream by four bytes. More... | |
int64 | readInt40BE () |
Reads a 40-bit big endian signed integer from the current stream and advances the current position of the stream by five bytes. More... | |
uint64 | readUInt40BE () |
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position of the stream by five bytes. More... | |
int64 | readInt56BE () |
Reads a 56-bit big endian signed integer from the current stream and advances the current position of the stream by seven bytes. More... | |
uint64 | readUInt56BE () |
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position of the stream by seven bytes. More... | |
int64 | readInt64BE () |
Reads a 64-bit big endian signed integer from the current stream and advances the current position of the stream by eight bytes. More... | |
uint64 | readUInt64BE () |
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position of the stream by eight bytes. More... | |
uint64 | readVariableLengthUIntBE () |
Reads an up to 8 byte long big endian unsigned integer from the current stream and advances the current position of the stream by one to eight byte. More... | |
float32 | readFloat32BE () |
Reads a 32-bit big endian floating point value from the current stream and advances the current position of the stream by four bytes. More... | |
float64 | readFloat64BE () |
Reads a 64-bit big endian floating point value from the current stream and advances the current position of the stream by eight bytes. More... | |
int16 | readInt16LE () |
Reads a 16-bit little endian signed integer from the current stream and advances the current position of the stream by two bytes. More... | |
uint16 | readUInt16LE () |
Reads a 16-bit little endian unsigned integer from the current stream and advances the current position of the stream by two bytes. More... | |
int32 | readInt24LE () |
Reads a 24-bit little endian signed integer from the current stream and advances the current position of the stream by three bytes. More... | |
uint32 | readUInt24LE () |
Reads a 24-bit little endian unsigned integer from the current stream and advances the current position of the stream by three bytes. More... | |
int32 | readInt32LE () |
Reads a 32-bit little endian signed integer from the current stream and advances the current position of the stream by four bytes. More... | |
uint32 | readUInt32LE () |
Reads a 32-bit little endian unsigned integer from the current stream and advances the current position of the stream by four bytes. More... | |
int64 | readInt40LE () |
Reads a 40-bit little endian signed integer from the current stream and advances the current position of the stream by five bytes. More... | |
uint64 | readUInt40LE () |
Reads a 40-bit little endian unsigned integer from the current stream and advances the current position of the stream by five bytes. More... | |
int64 | readInt56LE () |
Reads a 56-bit little endian signed integer from the current stream and advances the current position of the stream by seven bytes. More... | |
uint64 | readUInt56LE () |
Reads a 56-bit little endian unsigned integer from the current stream and advances the current position of the stream by seven bytes. More... | |
int64 | readInt64LE () |
Reads a 64-bit little endian signed integer from the current stream and advances the current position of the stream by eight bytes. More... | |
uint64 | readUInt64LE () |
Reads a 64-bit little endian unsigned integer from the current stream and advances the current position of the stream by eight bytes. More... | |
uint64 | readVariableLengthUIntLE () |
Reads an up to 8 byte long little endian unsigned integer from the current stream and advances the current position of the stream by one to eight byte. More... | |
float32 | readFloat32LE () |
Reads a 32-bit little endian floating point value from the current stream and advances the current position of the stream by four bytes. More... | |
float64 | readFloat64LE () |
Reads a 64-bit little endian floating point value from the current stream and advances the current position of the stream by eight bytes. More... | |
char | readChar () |
Reads a single character from the current stream and advances the current position of the stream by one byte. More... | |
byte | readByte () |
Reads a single byte/unsigned character from the current stream and advances the current position of the stream by one byte. More... | |
bool | readBool () |
Reads a boolean value from the current stream and advances the current position of the stream by one byte. More... | |
std::string | readLengthPrefixedString () |
Reads a length prefixed string from the current stream. More... | |
std::string | readString (std::size_t length) |
Reads a string from the current stream of the given length from the stream and advances the current position of the stream by length byte. More... | |
std::string | readTerminatedString (byte termination=0) |
Reads a terminated string from the current stream. More... | |
std::string | readTerminatedString (size_t maxBytesToRead, byte termination=0) |
Reads a terminated string from the current stream. More... | |
std::string | readMultibyteTerminatedStringBE (uint16 termination=0) |
Reads a multibyte-terminated string from the current stream. More... | |
std::string | readMultibyteTerminatedStringLE (uint16 termination=0) |
Reads a multibyte-terminated string from the current stream. More... | |
std::string | readMultibyteTerminatedStringBE (std::size_t maxBytesToRead, uint16 termination=0) |
Reads a terminated string from the current stream. More... | |
std::string | readMultibyteTerminatedStringLE (std::size_t maxBytesToRead, uint16 termination=0) |
Reads a terminated string from the current stream. More... | |
uint32 | readSynchsafeUInt32BE () |
Reads a 32-bit big endian synchsafe integer from the current stream and advances the current position of the stream by four bytes. More... | |
float32 | readFixed8BE () |
Reads a 8.8 fixed point big endian representation from the current stream and returns it as 32-bit floating point value. More... | |
float32 | readFixed16BE () |
Reads a 16.16 fixed point big endian representation from the current stream and returns it as 32-bit floating point value. More... | |
uint32 | readSynchsafeUInt32LE () |
Reads a 32-bit little endian synchsafe integer from the current stream and advances the current position of the stream by four bytes. More... | |
float32 | readFixed8LE () |
Reads a 8.8 fixed point little endian representation from the current stream and returns it as 32-bit floating point value. More... | |
float32 | readFixed16LE () |
Reads a 16.16 fixed point little endian representation from the current stream and returns it as 32-bit floating point value. More... | |
uint32 | readCrc32 (std::size_t length) |
Reads length bytes from the stream and computes the CRC-32 for that block of data. More... | |
void | read (char &oneCharacter) |
Reads a single character from the current stream and advances the current position of the stream by one byte. More... | |
void | read (byte &oneByte) |
Reads a single byte/unsigned character from the current stream and advances the current position of the stream by one byte. More... | |
void | read (bool &oneBool) |
Reads a boolean value from the current stream and advances the current position of the stream by one byte. More... | |
void | read (std::string &lengthPrefixedString) |
Reads a length prefixed string from the current stream. More... | |
void | read (int16 &one16BitInt) |
Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes. More... | |
void | read (uint16 &one16BitUInt) |
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position of the stream by two bytes. More... | |
void | read (int32 &one32BitInt) |
Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes. More... | |
void | read (uint32 &one32BitUInt) |
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position of the stream by four bytes. More... | |
void | read (int64 &one64BitInt) |
Reads a 64-bit big endian signed integer from the current stream and advances the current position of the stream by eight bytes. More... | |
void | read (uint64 &one64BitUInt) |
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position of the stream by eight bytes. More... | |
void | read (float32 &one32BitFloat) |
Reads a 32-bit big endian floating point value from the current stream and advances the current position of the stream by four bytes. More... | |
void | read (float64 &one64BitFloat) |
Reads a 64-bit big endian floating point value from the current stream and advances the current position of the stream by eight bytes. More... | |
Static Public Member Functions | |
static uint32 | computeCrc32 (const char *buffer, std::size_t length) |
Reads length bytes from the buffer and computes the CRC-32 for that block of data. More... | |
Static Public Attributes | |
static const uint32 | crc32Table [] |
CRC-32 table. More... | |
Reads primitive data types from a std::istream.
Definition at line 11 of file binaryreader.h.
BinaryReader::BinaryReader | ( | std::istream * | stream | ) |
Constructs a new BinaryReader.
stream | Specifies the stream to read from. |
Definition at line 29 of file binaryreader.cpp.
BinaryReader::BinaryReader | ( | const BinaryReader & | other | ) |
Copies the specified BinaryReader.
Definition at line 39 of file binaryreader.cpp.
BinaryReader::~BinaryReader | ( | ) |
Destroys the BinaryReader.
Definition at line 48 of file binaryreader.cpp.
|
inline |
Returns an indication whether a stream is assigned the reader can read from.
Definition at line 182 of file binaryreader.h.
|
static |
Reads length bytes from the buffer and computes the CRC-32 for that block of data.
Definition at line 312 of file binaryreader.cpp.
|
inline |
The reader will not take ownership over the assigned stream.
Definition at line 158 of file binaryreader.h.
|
inline |
Returns an indication whether the end-of-stream bit of the assigned stream is set.
Definition at line 174 of file binaryreader.h.
|
inline |
Returns an indication whether the fail bit of the assigned stream is set.
Definition at line 166 of file binaryreader.h.
|
inline |
The reader will take ownership over the assigned stream.
Definition at line 144 of file binaryreader.h.
|
inline |
Returns whether the reader takes ownership over the assigned stream.
Definition at line 132 of file binaryreader.h.
|
delete |
|
inline |
Reads the specified number of characters from the stream in the character array.
Definition at line 190 of file binaryreader.h.
|
inline |
Reads the specified number of bytes from the stream in the character array.
Definition at line 198 of file binaryreader.h.
|
inline |
Reads the specified number of bytes from the stream in the specified buffer.
Definition at line 206 of file binaryreader.h.
|
inline |
Reads a single character from the current stream and advances the current position of the stream by one byte.
Definition at line 602 of file binaryreader.h.
|
inline |
Reads a single byte/unsigned character from the current stream and advances the current position of the stream by one byte.
Definition at line 610 of file binaryreader.h.
|
inline |
Reads a boolean value from the current stream and advances the current position of the stream by one byte.
Definition at line 619 of file binaryreader.h.
|
inline |
Reads a length prefixed string from the current stream.
Definition at line 630 of file binaryreader.h.
|
inline |
Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes.
Definition at line 638 of file binaryreader.h.
|
inline |
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position of the stream by two bytes.
Definition at line 646 of file binaryreader.h.
|
inline |
Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes.
Definition at line 654 of file binaryreader.h.
|
inline |
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position of the stream by four bytes.
Definition at line 662 of file binaryreader.h.
|
inline |
Reads a 64-bit big endian signed integer from the current stream and advances the current position of the stream by eight bytes.
Definition at line 670 of file binaryreader.h.
|
inline |
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position of the stream by eight bytes.
Definition at line 678 of file binaryreader.h.
|
inline |
Reads a 32-bit big endian floating point value from the current stream and advances the current position of the stream by four bytes.
Definition at line 686 of file binaryreader.h.
|
inline |
Reads a 64-bit big endian floating point value from the current stream and advances the current position of the stream by eight bytes.
Definition at line 694 of file binaryreader.h.
|
inline |
Reads a boolean value from the current stream and advances the current position of the stream by one byte.
Definition at line 542 of file binaryreader.h.
|
inline |
Reads a single byte/unsigned character from the current stream and advances the current position of the stream by one byte.
Definition at line 532 of file binaryreader.h.
|
inline |
Reads a single character from the current stream and advances the current position of the stream by one byte.
Definition at line 523 of file binaryreader.h.
uint32 BinaryReader::readCrc32 | ( | std::size_t | length | ) |
Reads length bytes from the stream and computes the CRC-32 for that block of data.
Definition at line 295 of file binaryreader.cpp.
|
inline |
Reads a 16.16 fixed point big endian representation from the current stream and returns it as 32-bit floating point value.
Definition at line 568 of file binaryreader.h.
|
inline |
Reads a 16.16 fixed point little endian representation from the current stream and returns it as 32-bit floating point value.
Definition at line 594 of file binaryreader.h.
|
inline |
Reads a 8.8 fixed point big endian representation from the current stream and returns it as 32-bit floating point value.
Definition at line 560 of file binaryreader.h.
|
inline |
Reads a 8.8 fixed point little endian representation from the current stream and returns it as 32-bit floating point value.
Definition at line 586 of file binaryreader.h.
|
inline |
Reads a 32-bit big endian floating point value from the current stream and advances the current position of the stream by four bytes.
Definition at line 351 of file binaryreader.h.
|
inline |
Reads a 32-bit little endian floating point value from the current stream and advances the current position of the stream by four bytes.
Definition at line 505 of file binaryreader.h.
|
inline |
Reads a 64-bit big endian floating point value from the current stream and advances the current position of the stream by eight bytes.
Definition at line 360 of file binaryreader.h.
|
inline |
Reads a 64-bit little endian floating point value from the current stream and advances the current position of the stream by eight bytes.
Definition at line 514 of file binaryreader.h.
|
inline |
Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes.
Definition at line 215 of file binaryreader.h.
|
inline |
Reads a 16-bit little endian signed integer from the current stream and advances the current position of the stream by two bytes.
Definition at line 369 of file binaryreader.h.
|
inline |
Reads a 24-bit big endian signed integer from the current stream and advances the current position of the stream by three bytes.
Definition at line 233 of file binaryreader.h.
|
inline |
Reads a 24-bit little endian signed integer from the current stream and advances the current position of the stream by three bytes.
Definition at line 387 of file binaryreader.h.
|
inline |
Reads a 32-bit big endian signed integer from the current stream and advances the current position of the stream by four bytes.
Definition at line 257 of file binaryreader.h.
|
inline |
Reads a 32-bit little endian signed integer from the current stream and advances the current position of the stream by four bytes.
Definition at line 411 of file binaryreader.h.
|
inline |
Reads a 40-bit big endian signed integer from the current stream and advances the current position of the stream by five bytes.
Definition at line 275 of file binaryreader.h.
|
inline |
Reads a 40-bit little endian signed integer from the current stream and advances the current position of the stream by five bytes.
Definition at line 429 of file binaryreader.h.
|
inline |
Reads a 56-bit big endian signed integer from the current stream and advances the current position of the stream by seven bytes.
Definition at line 299 of file binaryreader.h.
|
inline |
Reads a 56-bit little endian signed integer from the current stream and advances the current position of the stream by seven bytes.
Definition at line 453 of file binaryreader.h.
|
inline |
Reads a 64-bit big endian signed integer from the current stream and advances the current position of the stream by eight bytes.
Definition at line 323 of file binaryreader.h.
|
inline |
Reads a 64-bit little endian signed integer from the current stream and advances the current position of the stream by eight bytes.
Definition at line 477 of file binaryreader.h.
string BinaryReader::readLengthPrefixedString | ( | ) |
Reads a length prefixed string from the current stream.
Definition at line 120 of file binaryreader.cpp.
string BinaryReader::readMultibyteTerminatedStringBE | ( | uint16 | termination = 0 | ) |
Reads a multibyte-terminated string from the current stream.
Advances the current position of the stream by the string length plus two bytes.
termination | Specifies the two byte sized big endian value to be recognized as termination. |
Definition at line 189 of file binaryreader.cpp.
string BinaryReader::readMultibyteTerminatedStringBE | ( | std::size_t | maxBytesToRead, |
uint16 | termination = 0 |
||
) |
Reads a terminated string from the current stream.
Advances the current position of the stream by the string length plus two bytes but maximal by maxBytesToRead.
maxBytesToRead | The maximal number of bytes to read. |
termination | The two byte sized big endian value to be recognized as termination. |
Definition at line 245 of file binaryreader.cpp.
string BinaryReader::readMultibyteTerminatedStringLE | ( | uint16 | termination = 0 | ) |
Reads a multibyte-terminated string from the current stream.
Advances the current position of the stream by the string length plus two bytes.
termination | Specifies the two byte sized little endian value to be recognized as termination. |
Definition at line 216 of file binaryreader.cpp.
string BinaryReader::readMultibyteTerminatedStringLE | ( | std::size_t | maxBytesToRead, |
uint16 | termination = 0 |
||
) |
Reads a terminated string from the current stream.
Advances the current position of the stream by the string length plus two bytes but maximal by maxBytesToRead.
maxBytesToRead | The maximal number of bytes to read. |
termination | The two byte sized little endian value to be recognized as termination. |
Definition at line 272 of file binaryreader.cpp.
istream::pos_type BinaryReader::readStreamsize | ( | ) |
Returns the size of the assigned stream.
The size is determined by seeking to the end of the stream and returning this offset.
Definition at line 87 of file binaryreader.cpp.
string BinaryReader::readString | ( | std::size_t | length | ) |
Reads a string from the current stream of the given length from the stream and advances the current position of the stream by length byte.
Definition at line 128 of file binaryreader.cpp.
|
inline |
Reads a 32-bit big endian synchsafe integer from the current stream and advances the current position of the stream by four bytes.
Definition at line 552 of file binaryreader.h.
|
inline |
Reads a 32-bit little endian synchsafe integer from the current stream and advances the current position of the stream by four bytes.
Definition at line 578 of file binaryreader.h.
string BinaryReader::readTerminatedString | ( | byte | termination = 0 | ) |
Reads a terminated string from the current stream.
Advances the current position of the stream by the string length plus one byte.
termination | The byte to be recognized as termination value. |
Definition at line 146 of file binaryreader.cpp.
string BinaryReader::readTerminatedString | ( | size_t | maxBytesToRead, |
byte | termination = 0 |
||
) |
Reads a terminated string from the current stream.
Advances the current position of the stream by the string length plus one byte but maximal by maxBytesToRead.
maxBytesToRead | The maximal number of bytes to read. |
termination | The value to be recognized as termination. |
Definition at line 167 of file binaryreader.cpp.
|
inline |
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position of the stream by two bytes.
Definition at line 224 of file binaryreader.h.
|
inline |
Reads a 16-bit little endian unsigned integer from the current stream and advances the current position of the stream by two bytes.
Definition at line 378 of file binaryreader.h.
|
inline |
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position of the stream by three bytes.
Definition at line 247 of file binaryreader.h.
|
inline |
Reads a 24-bit little endian unsigned integer from the current stream and advances the current position of the stream by three bytes.
Definition at line 401 of file binaryreader.h.
|
inline |
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position of the stream by four bytes.
Definition at line 266 of file binaryreader.h.
|
inline |
Reads a 32-bit little endian unsigned integer from the current stream and advances the current position of the stream by four bytes.
Definition at line 420 of file binaryreader.h.
|
inline |
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position of the stream by five bytes.
Definition at line 289 of file binaryreader.h.
|
inline |
Reads a 40-bit little endian unsigned integer from the current stream and advances the current position of the stream by five bytes.
Definition at line 443 of file binaryreader.h.
|
inline |
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position of the stream by seven bytes.
Definition at line 313 of file binaryreader.h.
|
inline |
Reads a 56-bit little endian unsigned integer from the current stream and advances the current position of the stream by seven bytes.
Definition at line 467 of file binaryreader.h.
|
inline |
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position of the stream by eight bytes.
Definition at line 332 of file binaryreader.h.
|
inline |
Reads a 64-bit little endian unsigned integer from the current stream and advances the current position of the stream by eight bytes.
Definition at line 486 of file binaryreader.h.
|
inline |
Reads an up to 8 byte long big endian unsigned integer from the current stream and advances the current position of the stream by one to eight byte.
Throws | ConversionException if the size of the integer exceeds the maximum. |
Definition at line 342 of file binaryreader.h.
|
inline |
Reads an up to 8 byte long little endian unsigned integer from the current stream and advances the current position of the stream by one to eight byte.
Throws | ConversionException if the size of the integer exceeds the maximum. |
Definition at line 496 of file binaryreader.h.
void BinaryReader::setStream | ( | std::istream * | stream, |
bool | giveOwnership = false |
||
) |
Assigns the stream the reader will read from when calling one of the read-methods.
You can assign a null pointer when ensuring that none of the read-methods is called until a stream is assigned.
stream | Specifies the stream to be assigned. |
giveOwnership | Indicated whether the reader should take ownership (default is false). |
Definition at line 66 of file binaryreader.cpp.
|
inline |
Returns a pointer to the stream the reader will read from when calling one of the read-methods.
Definition at line 120 of file binaryreader.h.
|
inline |
Returns a pointer to the stream the reader will read from when calling one of the read-methods.
Definition at line 110 of file binaryreader.h.
|
static |
CRC-32 table.
Definition at line 81 of file binaryreader.h.