C++ Utilities
4.9.0
Common C++ classes and routines used by my applications 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... | |
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... | |
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... | |
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 28 of file binaryreader.cpp.
BinaryReader::BinaryReader | ( | const BinaryReader & | other | ) |
Copies the specified BinaryReader.
Definition at line 38 of file binaryreader.cpp.
BinaryReader::~BinaryReader | ( | ) |
Destroys the BinaryReader.
Definition at line 47 of file binaryreader.cpp.
|
inline |
Returns an indication whether a stream is assigned the reader can read from.
Definition at line 164 of file binaryreader.h.
|
static |
Reads length bytes from the buffer and computes the CRC-32 for that block of data.
Definition at line 282 of file binaryreader.cpp.
|
inline |
The reader will not take ownership over the assigned stream.
Definition at line 140 of file binaryreader.h.
|
inline |
Returns an indication whether the end-of-stream bit of the assigned stream is set.
Definition at line 156 of file binaryreader.h.
|
inline |
Returns an indication whether the fail bit of the assigned stream is set.
Definition at line 148 of file binaryreader.h.
|
inline |
The reader will take ownership over the assigned stream.
Definition at line 126 of file binaryreader.h.
|
inline |
Returns whether the reader takes ownership over the assigned stream.
Definition at line 114 of file binaryreader.h.
|
delete |
|
inline |
Reads the specified number of characters from the stream in the character array.
Definition at line 172 of file binaryreader.h.
|
inline |
Reads the specified number of bytes from the stream in the character array.
Definition at line 180 of file binaryreader.h.
|
inline |
Reads the specified number of bytes from the stream in the specified buffer.
Definition at line 188 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 504 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 494 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 485 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 265 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 530 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 556 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 522 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 548 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 323 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 467 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 332 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 476 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 197 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 341 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 215 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 359 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 239 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 383 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 257 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 401 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 281 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 425 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 305 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 449 of file binaryreader.h.
string BinaryReader::readLengthPrefixedString | ( | ) |
Reads a length prefixed string from the current stream.
Definition at line 101 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 176 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 218 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 196 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 242 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 86 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 124 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 514 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 540 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 139 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 157 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 206 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 350 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 229 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 373 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 248 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 392 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 271 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 415 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 295 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 439 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 314 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 458 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 65 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 102 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 92 of file binaryreader.h.
|
static |
CRC-32 table.
Definition at line 79 of file binaryreader.h.