C++ Utilities
4.6.1
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 12 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 37 of file binaryreader.cpp.
BinaryReader::~BinaryReader | ( | ) |
Destroys the BinaryReader.
Definition at line 45 of file binaryreader.cpp.
|
inline |
Returns an indication whether a stream is assigned the reader can read from.
Definition at line 166 of file binaryreader.h.
|
static |
Reads length bytes from the buffer and computes the CRC-32 for that block of data.
Definition at line 280 of file binaryreader.cpp.
|
inline |
The reader will not take ownership over the assigned stream.
Definition at line 142 of file binaryreader.h.
|
inline |
Returns an indication whether the end-of-stream bit of the assigned stream is set.
Definition at line 158 of file binaryreader.h.
|
inline |
Returns an indication whether the fail bit of the assigned stream is set.
Definition at line 150 of file binaryreader.h.
|
inline |
The reader will take ownership over the assigned stream.
Definition at line 128 of file binaryreader.h.
|
inline |
Returns whether the reader takes ownership over the assigned stream.
Definition at line 116 of file binaryreader.h.
|
delete |
|
inline |
Reads the specified number of characters from the stream in the character array.
Definition at line 174 of file binaryreader.h.
|
inline |
Reads the specified number of bytes from the stream in the character array.
Definition at line 182 of file binaryreader.h.
|
inline |
Reads the specified number of bytes from the stream in the specified buffer.
Definition at line 190 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 506 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 496 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 487 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 263 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 532 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 558 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 524 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 550 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 325 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 469 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 334 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 478 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 199 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 343 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 217 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 361 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 241 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 385 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 259 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 403 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 283 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 427 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 307 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 451 of file binaryreader.h.
string BinaryReader::readLengthPrefixedString | ( | ) |
Reads a length prefixed string from the current stream.
Definition at line 99 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 174 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 216 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 194 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 240 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 84 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 122 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 516 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 542 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 137 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 155 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 208 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 352 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 231 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 375 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 250 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 394 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 273 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 417 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 297 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 441 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 316 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 460 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 63 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 104 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 94 of file binaryreader.h.
|
static |
CRC-32 table.
Definition at line 81 of file binaryreader.h.