1 #ifndef IOUTILITIES_BINERYREADER_H 2 #define IOUTILITIES_BINERYREADER_H 4 #include "../conversion/binaryconversion.h" 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 void read(
char *buffer, std::streamsize length);
30 void read(
byte *buffer, std::streamsize length);
31 void read(std::vector<char> &buffer, std::streamsize length);
44 uint64 readVariableLengthUIntBE();
45 float32 readFloat32BE();
46 float64 readFloat64BE();
59 uint64 readVariableLengthUIntLE();
60 float32 readFloat32LE();
61 float64 readFloat64LE();
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);
84 void read(
char &oneCharacter);
85 void read(
byte &oneByte);
86 void read(
bool &oneBool);
87 void read(std::string &lengthPrefixedString);
88 void read(
int16 &one16BitInt);
89 void read(
uint16 &one16BitUInt);
90 void read(
int32 &one32BitInt);
91 void read(
uint32 &one32BitUInt);
92 void read(
int64 &one64BitInt);
93 void read(
uint64 &one64BitUInt);
94 void read(float32 &one32BitFloat);
95 void read(float64 &one64BitFloat);
98 void bufferVariableLengthInteger();
100 std::istream *m_stream;
168 return m_stream ? m_stream->fail() :
false;
176 return m_stream && m_stream->eof();
184 return m_stream && m_stream->good();
192 m_stream->read(buffer, length);
200 m_stream->read(reinterpret_cast<char *>(buffer), length);
208 buffer.resize(
static_cast<std::vector<char>::size_type
>(length));
209 m_stream->read(buffer.data(), length);
217 m_stream->read(m_buffer,
sizeof(
int16));
218 return ConversionUtilities::BE::toInt16(m_buffer);
226 m_stream->read(m_buffer,
sizeof(
uint16));
227 return ConversionUtilities::BE::toUInt16(m_buffer);
236 m_stream->read(m_buffer + 1, 3);
237 auto val = ConversionUtilities::BE::toInt32(m_buffer);
238 if (val >= 0x800000) {
239 val = -(0x1000000 - val);
250 m_stream->read(m_buffer + 1, 3);
251 return ConversionUtilities::BE::toUInt32(m_buffer);
259 m_stream->read(m_buffer,
sizeof(
int32));
260 return ConversionUtilities::BE::toInt32(m_buffer);
268 m_stream->read(m_buffer,
sizeof(
uint32));
269 return ConversionUtilities::BE::toUInt32(m_buffer);
277 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
278 m_stream->read(m_buffer + 3, 5);
279 auto val = ConversionUtilities::BE::toInt64(m_buffer);
280 if (val >= 0x8000000000) {
281 val = -(0x10000000000 - val);
291 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
292 m_stream->read(m_buffer + 3, 5);
293 return ConversionUtilities::BE::toUInt64(m_buffer);
302 m_stream->read(m_buffer + 1, 7);
303 auto val = ConversionUtilities::BE::toInt64(m_buffer);
304 if (val >= 0x80000000000000) {
305 val = -(0x100000000000000 - val);
316 m_stream->read(m_buffer + 1, 7);
317 return ConversionUtilities::BE::toUInt64(m_buffer);
325 m_stream->read(m_buffer,
sizeof(
int64));
326 return ConversionUtilities::BE::toInt64(m_buffer);
334 m_stream->read(m_buffer,
sizeof(
uint64));
335 return ConversionUtilities::BE::toUInt64(m_buffer);
344 bufferVariableLengthInteger();
345 return ConversionUtilities::BE::toUInt64(m_buffer);
353 m_stream->read(m_buffer,
sizeof(float32));
362 m_stream->read(m_buffer,
sizeof(float64));
363 return ConversionUtilities::BE::toFloat64(m_buffer);
371 m_stream->read(m_buffer,
sizeof(
int16));
372 return ConversionUtilities::LE::toInt16(m_buffer);
380 m_stream->read(m_buffer,
sizeof(
uint16));
381 return ConversionUtilities::LE::toUInt16(m_buffer);
390 m_stream->read(m_buffer, 3);
391 auto val = ConversionUtilities::LE::toInt32(m_buffer);
392 if (val >= 0x800000) {
393 val = -(0x1000000 - val);
404 m_stream->read(m_buffer, 3);
405 return ConversionUtilities::LE::toUInt32(m_buffer);
413 m_stream->read(m_buffer,
sizeof(
int32));
414 return ConversionUtilities::LE::toInt32(m_buffer);
422 m_stream->read(m_buffer,
sizeof(
uint32));
423 return ConversionUtilities::LE::toUInt32(m_buffer);
431 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
432 m_stream->read(m_buffer, 5);
433 auto val = ConversionUtilities::LE::toInt64(m_buffer);
434 if (val >= 0x8000000000) {
435 val = -(0x10000000000 - val);
445 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
446 m_stream->read(m_buffer, 5);
447 return ConversionUtilities::LE::toUInt64(m_buffer);
456 m_stream->read(m_buffer, 7);
457 auto val = ConversionUtilities::LE::toInt64(m_buffer);
458 if (val >= 0x80000000000000) {
459 val = -(0x100000000000000 - val);
470 m_stream->read(m_buffer, 7);
471 return ConversionUtilities::LE::toUInt64(m_buffer);
479 m_stream->read(m_buffer,
sizeof(
int64));
480 return ConversionUtilities::LE::toInt64(m_buffer);
488 m_stream->read(m_buffer,
sizeof(
uint64));
489 return ConversionUtilities::LE::toUInt64(m_buffer);
498 bufferVariableLengthInteger();
499 return ConversionUtilities::LE::toUInt64(m_buffer);
507 m_stream->read(m_buffer,
sizeof(float32));
516 m_stream->read(m_buffer,
sizeof(float64));
517 return ConversionUtilities::LE::toFloat64(m_buffer);
525 m_stream->read(m_buffer,
sizeof(
char));
534 m_stream->read(m_buffer,
sizeof(
char));
535 return static_cast<byte>(m_buffer[0]);
700 #endif // IOUTILITIES_BINERYREADER_H int64 readInt64LE()
Reads a 64-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.
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
float32 readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
int64 readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
std::int64_t int64
signed 64-bit integer
int16 readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
Reads primitive data types from a std::istream.
float64 readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
int32 readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
float32 readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
uint32 readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
int16 readInt16BE()
Reads a 16-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.
int64 readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
std::uint64_t uint64
unsigned 64-bit integer
float64 readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
int64 readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
uint64 readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
uint32 readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
float32 readFixed16BE()
Reads a 16.16 fixed point big endian representation from the current stream and returns it as 32-bit ...
uint32 readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
Contains utility classes helping to read and write streams.
uint64 readVariableLengthUIntLE()
Reads an up to 8 byte long little endian unsigned integer from the current stream and advances the cu...
uint64 readUInt40BE()
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position ...
uint64 readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
std::uint32_t uint32
unsigned 32-bit integer
uint16 readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
void giveOwnership()
The reader will take ownership over the assigned stream.
uint16 readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
void detatchOwnership()
The reader will not take ownership over the assigned stream.
int32 readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
std::int32_t int32
signed 32-bit integer
const std::istream * stream() const
Returns a pointer to the stream the reader will read from when calling one of the read-methods...
int32 readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
int32 readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
uint32 readSynchsafeUInt32LE()
Reads a 32-bit little endian synchsafe integer from the current stream and advances the current posit...
std::uint8_t byte
unsigned byte
bool canRead() const
Returns an indication whether a stream is assigned the reader can read from.
uint64 readVariableLengthUIntBE()
Reads an up to 8 byte long big endian unsigned integer from the current stream and advances the curre...
uint32 readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
int64 readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
byte readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
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...
float32 readFixed8BE()
Reads a 8.8 fixed point big endian representation from the current stream and returns it as 32-bit fl...
std::int16_t int16
signed 16-bit integer
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...
float32 readFixed16LE()
Reads a 16.16 fixed point little endian representation from the current stream and returns it as 32-b...
#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.
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
float32 readFixed8LE()
Reads a 8.8 fixed point little endian representation from the current stream and returns it as 32-bit...
uint64 readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
int64 readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
uint32 readSynchsafeUInt32BE()
Reads a 32-bit big endian synchsafe integer from the current stream and advances the current position...
std::uint16_t uint16
unsigned 16-bit integer
uint64 readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...