1 #ifndef IOUTILITIES_BINERYREADER_H 2 #define IOUTILITIES_BINERYREADER_H 4 #include "../conversion/binaryconversion.h" 21 const std::istream *stream()
const;
22 std::istream *stream();
23 void setStream(std::istream *stream,
bool giveOwnership =
false);
24 bool hasOwnership()
const;
26 void detatchOwnership();
30 std::istream::pos_type readStreamsize();
31 void read(
char *buffer, std::streamsize length);
32 void read(
byte *buffer, std::streamsize length);
33 void read(std::vector<char> &buffer, std::streamsize length);
46 float32 readFloat32BE();
47 float64 readFloat64BE();
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 std::istream *m_stream;
152 return m_stream ? m_stream->fail() :
false;
160 return m_stream && m_stream->eof();
168 return m_stream && m_stream->good();
176 m_stream->read(buffer, length);
184 m_stream->read(reinterpret_cast<char *>(buffer), length);
192 buffer.resize(length);
193 m_stream->read(buffer.data(), length);
201 m_stream->read(m_buffer,
sizeof(
int16));
202 return ConversionUtilities::BE::toInt16(m_buffer);
210 m_stream->read(m_buffer,
sizeof(
uint16));
211 return ConversionUtilities::BE::toUInt16(m_buffer);
220 m_stream->read(m_buffer + 1, 3);
221 auto val = ConversionUtilities::BE::toInt32(m_buffer);
222 if(val >= 0x800000) {
223 val = -(0x1000000 - val);
234 m_stream->read(m_buffer + 1, 3);
235 return ConversionUtilities::BE::toUInt32(m_buffer);
243 m_stream->read(m_buffer,
sizeof(
int32));
244 return ConversionUtilities::BE::toInt32(m_buffer);
252 m_stream->read(m_buffer,
sizeof(
uint32));
253 return ConversionUtilities::BE::toUInt32(m_buffer);
261 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
262 m_stream->read(m_buffer + 3, 5);
263 auto val = ConversionUtilities::BE::toInt64(m_buffer);
264 if(val >= 0x8000000000) {
265 val = -(0x10000000000 - val);
275 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
276 m_stream->read(m_buffer + 3, 5);
277 return ConversionUtilities::BE::toUInt64(m_buffer);
286 m_stream->read(m_buffer + 1, 7);
287 auto val = ConversionUtilities::BE::toInt64(m_buffer);
288 if(val >= 0x80000000000000) {
289 val = -(0x100000000000000 - val);
300 m_stream->read(m_buffer + 1, 7);
301 return ConversionUtilities::BE::toUInt64(m_buffer);
309 m_stream->read(m_buffer,
sizeof(
int64));
310 return ConversionUtilities::BE::toInt64(m_buffer);
318 m_stream->read(m_buffer,
sizeof(
uint64));
319 return ConversionUtilities::BE::toUInt64(m_buffer);
327 m_stream->read(m_buffer,
sizeof(float32));
336 m_stream->read(m_buffer,
sizeof(float64));
337 return ConversionUtilities::BE::toFloat64(m_buffer);
345 m_stream->read(m_buffer,
sizeof(
int16));
346 return ConversionUtilities::LE::toInt16(m_buffer);
354 m_stream->read(m_buffer,
sizeof(
uint16));
355 return ConversionUtilities::LE::toUInt16(m_buffer);
364 m_stream->read(m_buffer, 3);
365 auto val = ConversionUtilities::LE::toInt32(m_buffer);
366 if(val >= 0x800000) {
367 val = -(0x1000000 - val);
378 m_stream->read(m_buffer, 3);
379 return ConversionUtilities::LE::toUInt32(m_buffer);
387 m_stream->read(m_buffer,
sizeof(
int32));
388 return ConversionUtilities::LE::toInt32(m_buffer);
396 m_stream->read(m_buffer,
sizeof(
uint32));
397 return ConversionUtilities::LE::toUInt32(m_buffer);
405 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
406 m_stream->read(m_buffer, 5);
407 auto val = ConversionUtilities::LE::toInt64(m_buffer);
408 if(val >= 0x8000000000) {
409 val = -(0x10000000000 - val);
419 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
420 m_stream->read(m_buffer, 5);
421 return ConversionUtilities::LE::toUInt64(m_buffer);
430 m_stream->read(m_buffer, 7);
431 auto val = ConversionUtilities::LE::toInt64(m_buffer);
432 if(val >= 0x80000000000000) {
433 val = -(0x100000000000000 - val);
444 m_stream->read(m_buffer, 7);
445 return ConversionUtilities::LE::toUInt64(m_buffer);
453 m_stream->read(m_buffer,
sizeof(
int64));
454 return ConversionUtilities::LE::toInt64(m_buffer);
462 m_stream->read(m_buffer,
sizeof(
uint64));
463 return ConversionUtilities::LE::toUInt64(m_buffer);
471 m_stream->read(m_buffer,
sizeof(float32));
480 m_stream->read(m_buffer,
sizeof(float64));
481 return ConversionUtilities::LE::toFloat64(m_buffer);
489 m_stream->read(m_buffer,
sizeof(
char));
498 m_stream->read(m_buffer,
sizeof(
char));
499 return static_cast<byte>(m_buffer[0]);
508 return readByte() != 0;
565 #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 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.
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.
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...