1#ifndef IOUTILITIES_BINERYREADER_H
2#define IOUTILITIES_BINERYREADER_H
14 BinaryReader(std::istream *stream,
bool giveOwnership =
false);
19 const std::istream *stream()
const;
20 std::istream *stream();
21 void setStream(std::istream *stream,
bool giveOwnership =
false);
22 bool hasOwnership()
const;
24 void detatchOwnership();
28 std::istream::pos_type readStreamsize();
29 std::istream::pos_type readRemainingBytes();
30 void read(
char *buffer, std::streamsize length);
31 void read(std::uint8_t *buffer, std::streamsize length);
32 void read(std::vector<char> &buffer, std::streamsize length);
33 std::int16_t readInt16BE();
34 std::uint16_t readUInt16BE();
35 std::int32_t readInt24BE();
36 std::uint32_t readUInt24BE();
37 std::int32_t readInt32BE();
38 std::uint32_t readUInt32BE();
39 std::int64_t readInt40BE();
40 std::uint64_t readUInt40BE();
41 std::int64_t readInt56BE();
42 std::uint64_t readUInt56BE();
43 std::int64_t readInt64BE();
44 std::uint64_t readUInt64BE();
45 std::uint64_t readVariableLengthUIntBE();
46 float readFloat32BE();
47 double readFloat64BE();
48 std::int16_t readInt16LE();
49 std::uint16_t readUInt16LE();
50 std::int32_t readInt24LE();
51 std::uint32_t readUInt24LE();
52 std::int32_t readInt32LE();
53 std::uint32_t readUInt32LE();
54 std::int64_t readInt40LE();
55 std::uint64_t readUInt40LE();
56 std::int64_t readInt56LE();
57 std::uint64_t readUInt56LE();
58 std::int64_t readInt64LE();
59 std::uint64_t readUInt64LE();
60 std::uint64_t readVariableLengthUIntLE();
61 float readFloat32LE();
62 double readFloat64LE();
64 std::uint8_t readByte();
66 std::string readLengthPrefixedString();
67 std::string readString(std::size_t length);
68 std::string readTerminatedString(std::uint8_t termination = 0);
69 std::string readTerminatedString(std::size_t maxBytesToRead, std::uint8_t termination = 0);
70 std::uint32_t readSynchsafeUInt32BE();
72 float readFixed16BE();
73 std::uint32_t readSynchsafeUInt32LE();
75 float readFixed16LE();
76 std::uint32_t readCrc32(std::size_t length);
77 static std::uint32_t computeCrc32(
const char *buffer, std::size_t length);
78 static const std::uint32_t crc32Table[];
81 void read(
char &oneCharacter);
82 void read(std::uint8_t &oneByte);
83 void read(
bool &oneBool);
84 void read(std::string &lengthPrefixedString);
85 void read(std::int16_t &one16BitInt);
86 void read(std::uint16_t &one16BitUInt);
87 void read(std::int32_t &one32BitInt);
88 void read(std::uint32_t &one32BitUInt);
89 void read(std::int64_t &one64BitInt);
90 void read(std::uint64_t &one64BitUInt);
91 void read(
float &one32BitFloat);
92 void read(
double &one64BitFloat);
95 void bufferVariableLengthInteger();
97 std::istream *m_stream;
109 , m_ownership(giveOwnership)
118 : m_stream(other.m_stream)
196 return m_stream ? m_stream->fail() :
false;
204 return m_stream && m_stream->eof();
212 return m_stream && m_stream->good();
220 m_stream->read(buffer, length);
228 m_stream->read(
reinterpret_cast<char *
>(buffer), length);
236 buffer.resize(
static_cast<std::vector<char>::size_type
>(length));
237 m_stream->read(buffer.data(), length);
245 m_stream->read(m_buffer,
sizeof(std::int16_t));
254 m_stream->read(m_buffer,
sizeof(std::uint16_t));
264 m_stream->read(m_buffer + 1, 3);
266 if (val >= 0x800000) {
267 val = -(0x1000000 - val);
278 m_stream->read(m_buffer + 1, 3);
287 m_stream->read(m_buffer,
sizeof(std::int32_t));
296 m_stream->read(m_buffer,
sizeof(std::uint32_t));
305 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
306 m_stream->read(m_buffer + 3, 5);
308 if (val >= 0x8000000000) {
309 val = -(0x10000000000 - val);
319 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
320 m_stream->read(m_buffer + 3, 5);
330 m_stream->read(m_buffer + 1, 7);
332 if (val >= 0x80000000000000) {
333 val = -(0x100000000000000 - val);
344 m_stream->read(m_buffer + 1, 7);
353 m_stream->read(m_buffer,
sizeof(std::int64_t));
362 m_stream->read(m_buffer,
sizeof(std::uint64_t));
372 bufferVariableLengthInteger();
381 m_stream->read(m_buffer,
sizeof(
float));
390 m_stream->read(m_buffer,
sizeof(
double));
399 m_stream->read(m_buffer,
sizeof(std::int16_t));
408 m_stream->read(m_buffer,
sizeof(std::uint16_t));
418 m_stream->read(m_buffer, 3);
420 if (val >= 0x800000) {
421 val = -(0x1000000 - val);
432 m_stream->read(m_buffer, 3);
441 m_stream->read(m_buffer,
sizeof(std::int32_t));
450 m_stream->read(m_buffer,
sizeof(std::uint32_t));
459 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
460 m_stream->read(m_buffer, 5);
462 if (val >= 0x8000000000) {
463 val = -(0x10000000000 - val);
473 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
474 m_stream->read(m_buffer, 5);
484 m_stream->read(m_buffer, 7);
486 if (val >= 0x80000000000000) {
487 val = -(0x100000000000000 - val);
498 m_stream->read(m_buffer, 7);
507 m_stream->read(m_buffer,
sizeof(std::int64_t));
516 m_stream->read(m_buffer,
sizeof(std::uint64_t));
526 bufferVariableLengthInteger();
535 m_stream->read(m_buffer,
sizeof(
float));
544 m_stream->read(m_buffer,
sizeof(
double));
553 m_stream->read(m_buffer,
sizeof(
char));
562 m_stream->read(m_buffer,
sizeof(
char));
563 return static_cast<std::uint8_t
>(m_buffer[0]);
Reads primitive data types from a std::istream.
float readFixed8LE()
Reads a 8.8 fixed point little endian representation from the current stream and returns it as 32-bit...
void detatchOwnership()
The reader will not take ownership over the assigned stream.
std::int64_t readInt64LE()
Reads a 64-bit little endian signed integer from the current stream and advances the current position...
float readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
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 p...
std::uint64_t readUInt64LE()
Reads a 64-bit little endian unsigned integer from the current stream and advances the current positi...
float readFixed16BE()
Reads a 16.16 fixed point big endian representation from the current stream and returns it as 32-bit ...
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
std::int64_t readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
std::int32_t readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
std::int32_t readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
std::uint32_t readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
std::uint16_t readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
std::uint64_t readVariableLengthUIntBE()
Reads an up to 8 byte long big endian unsigned integer from the current stream and advances the curre...
std::uint64_t readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
std::uint32_t readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
std::int32_t readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
const std::istream * stream() const
Returns a pointer to the stream the reader will read from when calling one of the read-methods.
std::int16_t readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
std::uint32_t readSynchsafeUInt32LE()
Reads a 32-bit little endian synchsafe integer from the current stream and advances the current posit...
std::uint32_t readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
std::int64_t readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
~BinaryReader()
Destroys the BinaryReader.
std::uint64_t readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
std::uint64_t readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...
char readChar()
Reads a single character from the current stream and advances the current position of the stream by o...
std::uint64_t readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
BinaryReader(std::istream *stream, bool giveOwnership=false)
Constructs a new BinaryReader.
float readFixed16LE()
Reads a 16.16 fixed point little endian representation from the current stream and returns it as 32-b...
std::uint16_t readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
std::int32_t readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
void read(char *buffer, std::streamsize length)
Reads the specified number of characters from the stream in the character array.
std::int64_t readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
void giveOwnership()
The reader will take ownership over the assigned stream.
double readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
std::uint32_t readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
std::int64_t readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
std::int16_t readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
double readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
std::int64_t readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
std::uint64_t readUInt40BE()
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position ...
std::uint32_t readSynchsafeUInt32BE()
Reads a 32-bit big endian synchsafe integer from the current stream and advances the current position...
bool eof() const
Returns an indication whether the end-of-stream bit of the assigned stream is set.
bool canRead() const
Returns an indication whether a stream is assigned the reader can read from.
float readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
BinaryReader & operator=(const BinaryReader &rhs)=delete
std::uint64_t readVariableLengthUIntLE()
Reads an up to 8 byte long little endian unsigned integer from the current stream and advances the cu...
float readFixed8BE()
Reads a 8.8 fixed point big endian representation from the current stream and returns it as 32-bit fl...
std::uint8_t readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CPP_UTILITIES_EXPORT float toFloat32(const char *value)
Returns a 32-bit floating point number converted from four bytes at a specified position in a char ar...
CPP_UTILITIES_EXPORT T toInt(const char *value)
Returns the specified (unsigned) integer converted from the specified char array.
CPP_UTILITIES_EXPORT double toFloat64(const char *value)
Returns a 64-bit floating point number converted from eight bytes at a specified position in a char a...
CPP_UTILITIES_EXPORT double toFloat64(const char *value)
Returns a 64-bit floating point number converted from eight bytes at a specified position in a char a...
CPP_UTILITIES_EXPORT T toInt(const char *value)
Returns the specified (unsigned) integer converted from the specified char array.
CPP_UTILITIES_EXPORT float toFloat32(const char *value)
Returns a 32-bit floating point number converted from four bytes at a specified position in a char ar...
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT constexpr float toFloat32(std::uint16_t fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toNormalInt(std::uint32_t synchsafeInt)
Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.