2016-08-29 15:35:48 +02:00
|
|
|
#ifndef IOUTILITIES_BINARYWRITER_H
|
|
|
|
#define IOUTILITIES_BINARYWRITER_H
|
|
|
|
|
|
|
|
#include "../conversion/binaryconversion.h"
|
|
|
|
|
2019-03-13 19:00:37 +01:00
|
|
|
#include <cstdint>
|
2018-05-31 23:26:38 +02:00
|
|
|
#include <cstring>
|
2016-08-29 15:35:48 +02:00
|
|
|
#include <ostream>
|
2017-05-01 03:13:11 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-08-29 15:35:48 +02:00
|
|
|
|
2019-06-10 21:56:46 +02:00
|
|
|
namespace CppUtilities {
|
2016-08-29 15:35:48 +02:00
|
|
|
|
2017-05-01 03:13:11 +02:00
|
|
|
class CPP_UTILITIES_EXPORT BinaryWriter {
|
2016-08-29 15:35:48 +02:00
|
|
|
public:
|
2019-06-04 19:27:49 +02:00
|
|
|
BinaryWriter(std::ostream *stream, bool giveOwnership = false);
|
2016-08-29 15:35:48 +02:00
|
|
|
BinaryWriter(const BinaryWriter &other);
|
2017-05-01 03:13:11 +02:00
|
|
|
BinaryWriter &operator=(const BinaryWriter &rhs) = delete;
|
2016-08-29 15:35:48 +02:00
|
|
|
~BinaryWriter();
|
|
|
|
|
|
|
|
const std::ostream *stream() const;
|
|
|
|
std::ostream *stream();
|
|
|
|
void setStream(std::ostream *stream, bool giveOwnership = false);
|
|
|
|
bool hasOwnership() const;
|
|
|
|
void giveOwnership();
|
|
|
|
void detatchOwnership();
|
|
|
|
void flush();
|
|
|
|
bool fail() const;
|
|
|
|
void write(const char *buffer, std::streamsize length);
|
|
|
|
void write(const std::vector<char> &buffer, std::streamsize length);
|
|
|
|
void writeChar(char value);
|
2019-03-13 19:00:37 +01:00
|
|
|
void writeByte(std::uint8_t value);
|
|
|
|
void writeInt16BE(std::int16_t value);
|
|
|
|
void writeUInt16BE(std::uint16_t value);
|
|
|
|
void writeInt24BE(std::int32_t value);
|
|
|
|
void writeUInt24BE(std::uint32_t value);
|
|
|
|
void writeInt32BE(std::int32_t value);
|
|
|
|
void writeUInt32BE(std::uint32_t value);
|
|
|
|
void writeInt40BE(std::int64_t value);
|
|
|
|
void writeUInt40BE(std::uint64_t value);
|
|
|
|
void writeInt56BE(std::int64_t value);
|
|
|
|
void writeUInt56BE(std::uint64_t value);
|
|
|
|
void writeInt64BE(std::int64_t value);
|
|
|
|
void writeUInt64BE(std::uint64_t value);
|
|
|
|
void writeVariableLengthUIntBE(std::uint64_t value);
|
|
|
|
void writeFloat32BE(float value);
|
|
|
|
void writeFloat64BE(double value);
|
|
|
|
void writeInt16LE(std::int16_t value);
|
|
|
|
void writeUInt16LE(std::uint16_t value);
|
|
|
|
void writeInt24LE(std::int32_t value);
|
|
|
|
void writeUInt24LE(std::uint32_t value);
|
|
|
|
void writeInt32LE(std::int32_t value);
|
|
|
|
void writeUInt32LE(std::uint32_t value);
|
|
|
|
void writeInt40LE(std::int64_t value);
|
|
|
|
void writeUInt40LE(std::uint64_t value);
|
|
|
|
void writeInt56LE(std::int64_t value);
|
|
|
|
void writeUInt56LE(std::uint64_t value);
|
|
|
|
void writeInt64LE(std::int64_t value);
|
|
|
|
void writeUInt64LE(std::uint64_t value);
|
|
|
|
void writeVariableLengthUIntLE(std::uint64_t value);
|
|
|
|
void writeFloat32LE(float value);
|
|
|
|
void writeFloat64LE(double value);
|
2016-08-29 15:35:48 +02:00
|
|
|
void writeString(const std::string &value);
|
|
|
|
void writeTerminatedString(const std::string &value);
|
|
|
|
void writeLengthPrefixedString(const std::string &value);
|
2018-05-31 23:26:38 +02:00
|
|
|
void writeLengthPrefixedCString(const char *value, std::size_t size);
|
2016-08-29 15:35:48 +02:00
|
|
|
void writeBool(bool value);
|
2019-03-13 19:00:37 +01:00
|
|
|
void writeSynchsafeUInt32BE(std::uint32_t valueToConvertAndWrite);
|
|
|
|
void writeFixed8BE(float valueToConvertAndWrite);
|
|
|
|
void writeFixed16BE(float valueToConvertAndWrite);
|
|
|
|
void writeSynchsafeUInt32LE(std::uint32_t valueToConvertAndWrite);
|
|
|
|
void writeFixed8LE(float valueToConvertAndWrite);
|
|
|
|
void writeFixed16LE(float valueToConvertAndWrite);
|
2016-08-29 15:35:48 +02:00
|
|
|
|
2018-05-31 23:26:38 +02:00
|
|
|
// declare further overloads for write() to ease use of BinaryWriter in templates
|
|
|
|
void write(char oneChar);
|
2019-03-13 19:00:37 +01:00
|
|
|
void write(std::uint8_t oneByte);
|
2018-05-31 23:26:38 +02:00
|
|
|
void write(bool oneBool);
|
|
|
|
void write(const std::string &lengthPrefixedString);
|
2020-06-26 22:06:44 +02:00
|
|
|
void write(std::string_view lengthPrefixedString);
|
2018-05-31 23:26:38 +02:00
|
|
|
void write(const char *lengthPrefixedString);
|
2019-03-13 19:00:37 +01:00
|
|
|
void write(std::int16_t one16BitInt);
|
|
|
|
void write(std::uint16_t one16BitUint);
|
|
|
|
void write(std::int32_t one32BitInt);
|
|
|
|
void write(std::uint32_t one32BitUint);
|
|
|
|
void write(std::int64_t one64BitInt);
|
|
|
|
void write(std::uint64_t one64BitUint);
|
|
|
|
void write(float one32BitFloat);
|
|
|
|
void write(double one64BitFloat);
|
2018-05-31 23:26:38 +02:00
|
|
|
|
2016-08-29 15:35:48 +02:00
|
|
|
private:
|
2019-03-13 19:00:37 +01:00
|
|
|
void writeVariableLengthInteger(std::uint64_t size, void (*getBytes)(std::uint64_t, char *));
|
2018-05-31 23:26:38 +02:00
|
|
|
|
2016-08-29 15:35:48 +02:00
|
|
|
std::ostream *m_stream;
|
|
|
|
bool m_ownership;
|
|
|
|
char m_buffer[8];
|
|
|
|
};
|
|
|
|
|
2019-06-04 19:27:49 +02:00
|
|
|
/*!
|
|
|
|
* \brief Constructs a new BinaryWriter.
|
|
|
|
* \param stream Specifies the stream to write to.
|
|
|
|
* \param giveOwnership Specifies whether the writer should take ownership.
|
|
|
|
*/
|
|
|
|
inline BinaryWriter::BinaryWriter(std::ostream *stream, bool giveOwnership)
|
|
|
|
: m_stream(stream)
|
|
|
|
, m_ownership(giveOwnership)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Copies the specified BinaryWriter.
|
|
|
|
* \remarks The copy will not take ownership over the stream.
|
|
|
|
*/
|
|
|
|
inline BinaryWriter::BinaryWriter(const BinaryWriter &other)
|
|
|
|
: m_stream(other.m_stream)
|
|
|
|
, m_ownership(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Destroys the BinaryWriter.
|
|
|
|
*/
|
|
|
|
inline BinaryWriter::~BinaryWriter()
|
|
|
|
{
|
|
|
|
if (m_ownership) {
|
|
|
|
delete m_stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 15:35:48 +02:00
|
|
|
/*!
|
|
|
|
* \brief Returns a pointer to the stream the writer will write to when calling one of the write-methods.
|
|
|
|
*
|
|
|
|
* \sa setStream()
|
|
|
|
*/
|
|
|
|
inline std::ostream *BinaryWriter::stream()
|
|
|
|
{
|
|
|
|
return m_stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns a pointer to the stream the writer will write to when calling one of the write-methods.
|
|
|
|
*
|
|
|
|
* \sa setStream()
|
|
|
|
*/
|
|
|
|
inline const std::ostream *BinaryWriter::stream() const
|
|
|
|
{
|
|
|
|
return m_stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns whether the writer takes ownership over the assigned stream.
|
|
|
|
*
|
|
|
|
* \sa setStream()
|
|
|
|
* \sa giveOwnership()
|
|
|
|
* \sa detatchOwnership()
|
|
|
|
*/
|
|
|
|
inline bool BinaryWriter::hasOwnership() const
|
|
|
|
{
|
|
|
|
return m_ownership;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The writer will take ownership over the assigned stream.
|
|
|
|
*
|
|
|
|
* \sa setStream()
|
|
|
|
* \sa detatchOwnership()
|
|
|
|
* \sa hasOwnership()
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::giveOwnership()
|
|
|
|
{
|
2017-05-01 03:13:11 +02:00
|
|
|
if (m_stream) {
|
2016-08-29 15:35:48 +02:00
|
|
|
m_ownership = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The writer will not take ownership over the assigned stream.
|
|
|
|
*
|
|
|
|
* \sa setStream()
|
|
|
|
* \sa giveOwnership()
|
|
|
|
* \sa hasOwnership()
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::detatchOwnership()
|
|
|
|
{
|
|
|
|
m_ownership = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Calls the flush() method of the assigned stream.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::flush()
|
|
|
|
{
|
|
|
|
m_stream->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns an indication whether the fail bit of the assigned stream is set.
|
|
|
|
*/
|
|
|
|
inline bool BinaryWriter::fail() const
|
|
|
|
{
|
|
|
|
return m_stream ? m_stream->fail() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a character array to the current stream and advances the current position of the stream by the \a length of the array.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::write(const char *buffer, std::streamsize length)
|
|
|
|
{
|
|
|
|
m_stream->write(buffer, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the specified number of bytes from the \a buffer to the current stream and advances the current position of the stream by
|
|
|
|
* the specified \a length which must be less or equal to the \a buffer size.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::write(const std::vector<char> &buffer, std::streamsize length)
|
|
|
|
{
|
|
|
|
m_stream->write(buffer.data(), length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a single character to the current stream and advances the current position of the stream by one byte.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::writeChar(char value)
|
|
|
|
{
|
|
|
|
m_buffer[0] = value;
|
|
|
|
m_stream->write(m_buffer, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a single byte to the current stream and advances the current position of the stream by one byte.
|
|
|
|
*/
|
2019-06-05 23:48:33 +02:00
|
|
|
inline void BinaryWriter::writeByte(std::uint8_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
|
|
|
m_buffer[0] = *reinterpret_cast<char *>(&value);
|
|
|
|
m_stream->write(m_buffer, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a boolean value to the current stream and advances the current position of the stream by one byte.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::writeBool(bool value)
|
|
|
|
{
|
|
|
|
writeByte(value ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 16-bit big endian signed integer to the current stream and advances the current position of the stream by two bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt16BE(std::int16_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::int16_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 16-bit big endian unsigned integer to the current stream and advances the current position of the stream by two bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt16BE(std::uint16_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::uint16_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 24-bit big endian signed integer to the current stream and advances the current position of the stream by three bytes.
|
|
|
|
* \remarks The most significant byte of the specified 32-bit signed integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt24BE(std::int32_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer + 1, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 24-bit big endian unsigned integer to the current stream and advances the current position of the stream by three bytes.
|
|
|
|
* \remarks The most significant byte of the specified 32-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt24BE(std::uint32_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
|
|
|
// discard most significant byte
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer + 1, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt32BE(std::int32_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::int32_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt32BE(std::uint32_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::uint32_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 40-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks The most significant bytes of the specified 64-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt40BE(std::int64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer + 3, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 40-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks The most significant bytes of the specified 64-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt40BE(std::uint64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer + 3, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 56-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks The most significant byte of the specified 64-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt56BE(std::int64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer + 1, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 56-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks The most significant byte of the specified 64-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt56BE(std::uint64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer + 1, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit big endian signed integer to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt64BE(std::int64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::int64_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit big endian unsigned integer to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt64BE(std::uint64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::uint64_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:26:38 +02:00
|
|
|
/*!
|
|
|
|
* \brief Writes an up to 8 byte long big endian unsigned integer to the current stream and advances the current position of the stream by one to eight bytes.
|
|
|
|
* \throws Throws ConversionException if \a value exceeds the maximum.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeVariableLengthUIntBE(std::uint64_t value)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
writeVariableLengthInteger(value, static_cast<void (*)(std::uint64_t, char *)>(&BE::getBytes));
|
2018-05-31 23:26:38 +02:00
|
|
|
}
|
|
|
|
|
2016-08-29 15:35:48 +02:00
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit big endian floating point \a value to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeFloat32BE(float value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(float));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit big endian floating point \a value to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeFloat64BE(double value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
BE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(double));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 16-bit little endian signed integer to the current stream and advances the current position of the stream by two bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt16LE(std::int16_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::int16_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 16-bit little endian unsigned integer to the current stream and advances the current position of the stream by two bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt16LE(std::uint16_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::uint16_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 24-bit little endian signed integer to the current stream and advances the current position of the stream by three bytes.
|
|
|
|
* \remarks The most significant byte of the specified 32-bit signed integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt24LE(std::int32_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
|
|
|
// discard most significant byte
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 24-bit little endian unsigned integer to the current stream and advances the current position of the stream by three bytes.
|
|
|
|
* \remarks The most significant byte of the specified 32-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt24LE(std::uint32_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
|
|
|
// discard most significant byte
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit little endian signed integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt32LE(std::int32_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::int32_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit little endian unsigned integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt32LE(std::uint32_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::uint32_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 40-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks The most significant bytes of the specified 64-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt40LE(std::int64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 40-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks The most significant bytes of the specified 64-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt40LE(std::uint64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 56-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks The most significant byte of the specified 64-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt56LE(std::int64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 56-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks The most significant byte of the specified 64-bit unsigned integer \a value will be discarded.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt56LE(std::uint64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2016-08-29 15:35:48 +02:00
|
|
|
m_stream->write(m_buffer, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit little endian signed integer to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeInt64LE(std::int64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::int64_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit little endian unsigned integer to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeUInt64LE(std::uint64_t value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(std::uint64_t));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:26:38 +02:00
|
|
|
/*!
|
|
|
|
* \brief Writes an up to 8 byte long little endian unsigned integer to the current stream and advances the current position of the stream by one to eight bytes.
|
|
|
|
* \throws Throws ConversionException if \a value exceeds the maximum.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeVariableLengthUIntLE(std::uint64_t value)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
writeVariableLengthInteger(value, static_cast<void (*)(std::uint64_t, char *)>(&LE::getBytes));
|
2018-05-31 23:26:38 +02:00
|
|
|
}
|
|
|
|
|
2016-08-29 15:35:48 +02:00
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit little endian floating point \a value to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeFloat32LE(float value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(float));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit little endian floating point \a value to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeFloat64LE(double value)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
LE::getBytes(value, m_buffer);
|
2019-03-13 19:00:37 +01:00
|
|
|
m_stream->write(m_buffer, sizeof(double));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a string to the current stream and advances the current position of the stream by the length of the string.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::writeString(const std::string &value)
|
|
|
|
{
|
|
|
|
m_stream->write(value.c_str(), value.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a terminated string to the current stream and advances the current position of the stream by the length of the string plus 1.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::writeTerminatedString(const std::string &value)
|
|
|
|
{
|
|
|
|
m_stream->write(value.c_str(), value.length() + 1);
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:05:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Writes the length of a string and the string itself to the current stream.
|
|
|
|
*
|
|
|
|
* Advances the current position of the stream by the length of the string plus the size of the length prefix.
|
|
|
|
*
|
|
|
|
* \throws Throws ConversionException if the string size exceeds the maximum.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::writeLengthPrefixedString(const std::string &value)
|
|
|
|
{
|
|
|
|
writeVariableLengthUIntBE(value.size());
|
|
|
|
m_stream->write(value.data(), static_cast<std::streamsize>(value.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the length of a string and the string itself to the current stream.
|
|
|
|
*
|
|
|
|
* Advances the current position of the stream by the length of the string plus the size of the length prefix.
|
|
|
|
*
|
|
|
|
* \throws Throws ConversionException if the string size exceeds the maximum.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::writeLengthPrefixedCString(const char *value, std::size_t size)
|
|
|
|
{
|
|
|
|
writeVariableLengthUIntBE(size);
|
|
|
|
m_stream->write(value, static_cast<std::streamsize>(size));
|
|
|
|
}
|
|
|
|
|
2016-08-29 15:35:48 +02:00
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit big endian synchsafe integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks Synchsafe integers appear in ID3 tags that are attached to an MP3 file.
|
|
|
|
* \sa <a href="http://id3.org/id3v2.4.0-structure">ID3 tag version 2.4.0 - Main Structure</a>
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeSynchsafeUInt32BE(std::uint32_t valueToConvertAndWrite)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
writeUInt32BE(toSynchsafeInt(valueToConvertAndWrite));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the 8.8 fixed point big endian representation for the specified 32-bit floating point \a value to the current stream and advances the current position of the stream by two bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeFixed8BE(float valueToConvertAndWrite)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
writeUInt16BE(toFixed8(valueToConvertAndWrite));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the 16.16 fixed point big endian representation for the specified 32-bit floating point \a value to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeFixed16BE(float valueToConvertAndWrite)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
writeUInt32BE(toFixed16(valueToConvertAndWrite));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit little endian synchsafe integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
* \remarks Synchsafe integers appear in ID3 tags that are attached to an MP3 file.
|
|
|
|
* \sa <a href="http://id3.org/id3v2.4.0-structure">ID3 tag version 2.4.0 - Main Structure</a>
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeSynchsafeUInt32LE(std::uint32_t valueToConvertAndWrite)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
writeUInt32LE(toSynchsafeInt(valueToConvertAndWrite));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the 8.8 fixed point little endian representation for the specified 32-bit floating point \a value to the current stream and advances the current position of the stream by two bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeFixed8LE(float valueToConvertAndWrite)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
writeUInt16LE(toFixed8(valueToConvertAndWrite));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the 16.16 fixed point little endian representation for the specified 32-bit floating point \a value to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::writeFixed16LE(float valueToConvertAndWrite)
|
2016-08-29 15:35:48 +02:00
|
|
|
{
|
2019-06-10 21:56:46 +02:00
|
|
|
writeUInt32LE(toFixed16(valueToConvertAndWrite));
|
2016-08-29 15:35:48 +02:00
|
|
|
}
|
2018-05-31 23:26:38 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a single character to the current stream and advances the current position of the stream by one byte.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::write(char oneChar)
|
|
|
|
{
|
|
|
|
writeChar(oneChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a single byte to the current stream and advances the current position of the stream by one byte.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(std::uint8_t oneByte)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeByte(oneByte);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a boolean value to the current stream and advances the current position of the stream by one byte.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::write(bool oneBool)
|
|
|
|
{
|
|
|
|
writeBool(oneBool);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the length of a string and the string itself to the current stream.
|
|
|
|
*
|
|
|
|
* Advances the current position of the stream by the length of the string plus the size of the length prefix.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::write(const std::string &lengthPrefixedString)
|
2020-06-26 22:06:44 +02:00
|
|
|
{
|
|
|
|
writeLengthPrefixedString(lengthPrefixedString);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the length of a string and the string itself to the current stream.
|
|
|
|
*
|
|
|
|
* Advances the current position of the stream by the length of the string plus the size of the length prefix.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::write(std::string_view lengthPrefixedString)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeLengthPrefixedCString(lengthPrefixedString.data(), lengthPrefixedString.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes the length of a string and the string itself to the current stream.
|
|
|
|
*
|
|
|
|
* Advances the current position of the stream by the length of the string plus the size of the length prefix.
|
|
|
|
*/
|
|
|
|
inline void BinaryWriter::write(const char *lengthPrefixedString)
|
|
|
|
{
|
|
|
|
writeLengthPrefixedCString(lengthPrefixedString, std::strlen(lengthPrefixedString));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 16-bit big endian signed integer to the current stream and advances the current position of the stream by two bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(std::int16_t one16BitInt)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeInt16BE(one16BitInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 16-bit big endian unsigned integer to the current stream and advances the current position of the stream by two bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(std::uint16_t one16BitUint)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeUInt16BE(one16BitUint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(std::int32_t one32BitInt)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeInt32BE(one32BitInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(std::uint32_t one32BitUint)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeUInt32BE(one32BitUint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit big endian signed integer to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(std::int64_t one64BitInt)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeInt64BE(one64BitInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit big endian unsigned integer to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(std::uint64_t one64BitUint)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeUInt64BE(one64BitUint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 32-bit big endian floating point \a value to the current stream and advances the current position of the stream by four bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(float one32BitFloat)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeFloat32BE(one32BitFloat);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Writes a 64-bit big endian floating point \a value to the current stream and advances the current position of the stream by eight bytes.
|
|
|
|
*/
|
2019-03-13 19:00:37 +01:00
|
|
|
inline void BinaryWriter::write(double one64BitFloat)
|
2018-05-31 23:26:38 +02:00
|
|
|
{
|
|
|
|
writeFloat64BE(one64BitFloat);
|
|
|
|
}
|
2019-06-10 21:56:46 +02:00
|
|
|
} // namespace CppUtilities
|
2016-08-29 15:35:48 +02:00
|
|
|
|
|
|
|
#endif // IO_UTILITIES_BINARYWRITER_H
|