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 float32 readFloat32BE();
45 float64 readFloat64BE();
58 float32 readFloat32LE();
59 float64 readFloat64LE();
63 std::string readLengthPrefixedString();
64 std::string readString(std::size_t length);
65 std::string readTerminatedString(
byte termination = 0);
66 std::string readTerminatedString(
size_t maxBytesToRead,
byte termination = 0);
67 std::string readMultibyteTerminatedStringBE(
uint16 termination = 0);
68 std::string readMultibyteTerminatedStringLE(
uint16 termination = 0);
69 std::string readMultibyteTerminatedStringBE(std::size_t maxBytesToRead,
uint16 termination = 0);
70 std::string readMultibyteTerminatedStringLE(std::size_t maxBytesToRead,
uint16 termination = 0);
71 uint32 readSynchsafeUInt32BE();
72 float32 readFixed8BE();
73 float32 readFixed16BE();
74 uint32 readSynchsafeUInt32LE();
75 float32 readFixed8LE();
76 float32 readFixed16LE();
77 uint32 readCrc32(std::size_t length);
78 static uint32 computeCrc32(
const char *buffer, std::size_t length);
82 std::istream *m_stream;
150 return m_stream ? m_stream->fail() :
false;
158 return m_stream && m_stream->eof();
166 return m_stream && m_stream->good();
174 m_stream->read(buffer, length);
182 m_stream->read(reinterpret_cast<char *>(buffer), length);
190 buffer.resize(
static_cast<std::vector<char>::size_type
>(length));
191 m_stream->read(buffer.data(), length);
199 m_stream->read(m_buffer,
sizeof(
int16));
200 return ConversionUtilities::BE::toInt16(m_buffer);
208 m_stream->read(m_buffer,
sizeof(
uint16));
209 return ConversionUtilities::BE::toUInt16(m_buffer);
218 m_stream->read(m_buffer + 1, 3);
219 auto val = ConversionUtilities::BE::toInt32(m_buffer);
220 if (val >= 0x800000) {
221 val = -(0x1000000 - val);
232 m_stream->read(m_buffer + 1, 3);
233 return ConversionUtilities::BE::toUInt32(m_buffer);
241 m_stream->read(m_buffer,
sizeof(
int32));
242 return ConversionUtilities::BE::toInt32(m_buffer);
250 m_stream->read(m_buffer,
sizeof(
uint32));
251 return ConversionUtilities::BE::toUInt32(m_buffer);
259 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
260 m_stream->read(m_buffer + 3, 5);
261 auto val = ConversionUtilities::BE::toInt64(m_buffer);
262 if (val >= 0x8000000000) {
263 val = -(0x10000000000 - val);
273 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
274 m_stream->read(m_buffer + 3, 5);
275 return ConversionUtilities::BE::toUInt64(m_buffer);
284 m_stream->read(m_buffer + 1, 7);
285 auto val = ConversionUtilities::BE::toInt64(m_buffer);
286 if (val >= 0x80000000000000) {
287 val = -(0x100000000000000 - val);
298 m_stream->read(m_buffer + 1, 7);
299 return ConversionUtilities::BE::toUInt64(m_buffer);
307 m_stream->read(m_buffer,
sizeof(
int64));
308 return ConversionUtilities::BE::toInt64(m_buffer);
316 m_stream->read(m_buffer,
sizeof(
uint64));
317 return ConversionUtilities::BE::toUInt64(m_buffer);
325 m_stream->read(m_buffer,
sizeof(float32));
334 m_stream->read(m_buffer,
sizeof(float64));
335 return ConversionUtilities::BE::toFloat64(m_buffer);
343 m_stream->read(m_buffer,
sizeof(
int16));
344 return ConversionUtilities::LE::toInt16(m_buffer);
352 m_stream->read(m_buffer,
sizeof(
uint16));
353 return ConversionUtilities::LE::toUInt16(m_buffer);
362 m_stream->read(m_buffer, 3);
363 auto val = ConversionUtilities::LE::toInt32(m_buffer);
364 if (val >= 0x800000) {
365 val = -(0x1000000 - val);
376 m_stream->read(m_buffer, 3);
377 return ConversionUtilities::LE::toUInt32(m_buffer);
385 m_stream->read(m_buffer,
sizeof(
int32));
386 return ConversionUtilities::LE::toInt32(m_buffer);
394 m_stream->read(m_buffer,
sizeof(
uint32));
395 return ConversionUtilities::LE::toUInt32(m_buffer);
403 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
404 m_stream->read(m_buffer, 5);
405 auto val = ConversionUtilities::LE::toInt64(m_buffer);
406 if (val >= 0x8000000000) {
407 val = -(0x10000000000 - val);
417 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
418 m_stream->read(m_buffer, 5);
419 return ConversionUtilities::LE::toUInt64(m_buffer);
428 m_stream->read(m_buffer, 7);
429 auto val = ConversionUtilities::LE::toInt64(m_buffer);
430 if (val >= 0x80000000000000) {
431 val = -(0x100000000000000 - val);
442 m_stream->read(m_buffer, 7);
443 return ConversionUtilities::LE::toUInt64(m_buffer);
451 m_stream->read(m_buffer,
sizeof(
int64));
452 return ConversionUtilities::LE::toInt64(m_buffer);
460 m_stream->read(m_buffer,
sizeof(
uint64));
461 return ConversionUtilities::LE::toUInt64(m_buffer);
469 m_stream->read(m_buffer,
sizeof(float32));
478 m_stream->read(m_buffer,
sizeof(float64));
479 return ConversionUtilities::LE::toFloat64(m_buffer);
487 m_stream->read(m_buffer,
sizeof(
char));
496 m_stream->read(m_buffer,
sizeof(
char));
497 return static_cast<byte>(m_buffer[0]);
562 #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...