| 1 | #ifndef IOUTILITIES_BINARYWRITER_H |
| 2 | #define IOUTILITIES_BINARYWRITER_H |
| 3 | |
| 4 | #include "../conversion/binaryconversion.h" |
| 5 | |
| 6 | #include <cstdint> |
| 7 | #include <cstring> |
| 8 | #include <ostream> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | namespace CppUtilities { |
| 13 | |
| 14 | class CPP_UTILITIES_EXPORT BinaryWriter { |
| 15 | public: |
| 16 | BinaryWriter(std::ostream *stream, bool giveOwnership = false); |
| 17 | BinaryWriter(const BinaryWriter &other); |
| 18 | BinaryWriter &operator=(const BinaryWriter &rhs) = delete; |
| 19 | ~BinaryWriter(); |
| 20 | |
| 21 | const std::ostream *stream() const; |
| 22 | std::ostream *stream(); |
| 23 | void setStream(std::ostream *stream, bool giveOwnership = false); |
| 24 | bool hasOwnership() const; |
| 25 | void giveOwnership(); |
| 26 | void detatchOwnership(); |
| 27 | void flush(); |
| 28 | bool fail() const; |
| 29 | void write(const char *buffer, std::streamsize length); |
| 30 | void write(const std::vector<char> &buffer, std::streamsize length); |
| 31 | void writeChar(char value); |
| 32 | void writeByte(std::uint8_t value); |
| 33 | void writeInt16BE(std::int16_t value); |
| 34 | void writeUInt16BE(std::uint16_t value); |
| 35 | void writeInt24BE(std::int32_t value); |
| 36 | void writeUInt24BE(std::uint32_t value); |
| 37 | void writeInt32BE(std::int32_t value); |
| 38 | void writeUInt32BE(std::uint32_t value); |
| 39 | void writeInt40BE(std::int64_t value); |
| 40 | void writeUInt40BE(std::uint64_t value); |
| 41 | void writeInt56BE(std::int64_t value); |
| 42 | void writeUInt56BE(std::uint64_t value); |
| 43 | void writeInt64BE(std::int64_t value); |
| 44 | void writeUInt64BE(std::uint64_t value); |
| 45 | void writeVariableLengthUIntBE(std::uint64_t value); |
| 46 | void writeFloat32BE(float value); |
| 47 | void writeFloat64BE(double value); |
| 48 | void writeInt16LE(std::int16_t value); |
| 49 | void writeUInt16LE(std::uint16_t value); |
| 50 | void writeInt24LE(std::int32_t value); |
| 51 | void writeUInt24LE(std::uint32_t value); |
| 52 | void writeInt32LE(std::int32_t value); |
| 53 | void writeUInt32LE(std::uint32_t value); |
| 54 | void writeInt40LE(std::int64_t value); |
| 55 | void writeUInt40LE(std::uint64_t value); |
| 56 | void writeInt56LE(std::int64_t value); |
| 57 | void writeUInt56LE(std::uint64_t value); |
| 58 | void writeInt64LE(std::int64_t value); |
| 59 | void writeUInt64LE(std::uint64_t value); |
| 60 | void writeVariableLengthUIntLE(std::uint64_t value); |
| 61 | void writeFloat32LE(float value); |
| 62 | void writeFloat64LE(double value); |
| 63 | void writeString(const std::string &value); |
| 64 | void writeTerminatedString(const std::string &value); |
| 65 | void writeLengthPrefixedString(const std::string &value); |
| 66 | void writeLengthPrefixedCString(const char *value, std::size_t size); |
| 67 | void writeBool(bool value); |
| 68 | void writeSynchsafeUInt32BE(std::uint32_t valueToConvertAndWrite); |
| 69 | void writeFixed8BE(float valueToConvertAndWrite); |
| 70 | void writeFixed16BE(float valueToConvertAndWrite); |
| 71 | void writeSynchsafeUInt32LE(std::uint32_t valueToConvertAndWrite); |
| 72 | void writeFixed8LE(float valueToConvertAndWrite); |
| 73 | void writeFixed16LE(float valueToConvertAndWrite); |
| 74 | |
| 75 | // declare further overloads for write() to ease use of BinaryWriter in templates |
| 76 | void write(char oneChar); |
| 77 | void write(std::uint8_t oneByte); |
| 78 | void write(bool oneBool); |
| 79 | void write(const std::string &lengthPrefixedString); |
| 80 | void write(std::string_view lengthPrefixedString); |
| 81 | void write(const char *lengthPrefixedString); |
| 82 | void write(std::int16_t one16BitInt); |
| 83 | void write(std::uint16_t one16BitUint); |
| 84 | void write(std::int32_t one32BitInt); |
| 85 | void write(std::uint32_t one32BitUint); |
| 86 | void write(std::int64_t one64BitInt); |
| 87 | void write(std::uint64_t one64BitUint); |
| 88 | void write(float one32BitFloat); |
| 89 | void write(double one64BitFloat); |
| 90 | |
| 91 | private: |
| 92 | void writeVariableLengthInteger(std::uint64_t size, void (*getBytes)(std::uint64_t, char *)); |
| 93 | |
| 94 | std::ostream *m_stream; |
| 95 | bool m_ownership; |
| 96 | char m_buffer[8]; |
| 97 | }; |
| 98 | |
| 99 | /*! |
| 100 | * \brief Constructs a new BinaryWriter. |
| 101 | * \param stream Specifies the stream to write to. |
| 102 | * \param giveOwnership Specifies whether the writer should take ownership. |
| 103 | */ |
| 104 | inline BinaryWriter::BinaryWriter(std::ostream *stream, bool giveOwnership) |
| 105 | : m_stream(stream) |
| 106 | , m_ownership(giveOwnership) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | /*! |
| 111 | * \brief Copies the specified BinaryWriter. |
| 112 | * \remarks The copy will not take ownership over the stream. |
| 113 | */ |
| 114 | inline BinaryWriter::BinaryWriter(const BinaryWriter &other) |
| 115 | : m_stream(other.m_stream) |
| 116 | , m_ownership(false) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | /*! |
| 121 | * \brief Destroys the BinaryWriter. |
| 122 | */ |
| 123 | inline BinaryWriter::~BinaryWriter() |
| 124 | { |
| 125 | if (m_ownership) { |
| 126 | delete m_stream; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /*! |
| 131 | * \brief Returns a pointer to the stream the writer will write to when calling one of the write-methods. |
| 132 | * |
| 133 | * \sa setStream() |
| 134 | */ |
| 135 | inline std::ostream *BinaryWriter::stream() |
| 136 | { |
| 137 | return m_stream; |
| 138 | } |
| 139 | |
| 140 | /*! |
| 141 | * \brief Returns a pointer to the stream the writer will write to when calling one of the write-methods. |
| 142 | * |
| 143 | * \sa setStream() |
| 144 | */ |
| 145 | inline const std::ostream *BinaryWriter::stream() const |
| 146 | { |
| 147 | return m_stream; |
| 148 | } |
| 149 | |
| 150 | /*! |
| 151 | * \brief Returns whether the writer takes ownership over the assigned stream. |
| 152 | * |
| 153 | * \sa setStream() |
| 154 | * \sa giveOwnership() |
| 155 | * \sa detatchOwnership() |
| 156 | */ |
| 157 | inline bool BinaryWriter::hasOwnership() const |
| 158 | { |
| 159 | return m_ownership; |
| 160 | } |
| 161 | |
| 162 | /*! |
| 163 | * \brief The writer will take ownership over the assigned stream. |
| 164 | * |
| 165 | * \sa setStream() |
| 166 | * \sa detatchOwnership() |
| 167 | * \sa hasOwnership() |
| 168 | */ |
| 169 | inline void BinaryWriter::giveOwnership() |
| 170 | { |
| 171 | if (m_stream) { |
| 172 | m_ownership = true; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /*! |
| 177 | * \brief The writer will not take ownership over the assigned stream. |
| 178 | * |
| 179 | * \sa setStream() |
| 180 | * \sa giveOwnership() |
| 181 | * \sa hasOwnership() |
| 182 | */ |
| 183 | inline void BinaryWriter::detatchOwnership() |
| 184 | { |
| 185 | m_ownership = false; |
| 186 | } |
| 187 | |
| 188 | /*! |
| 189 | * \brief Calls the flush() method of the assigned stream. |
| 190 | */ |
| 191 | inline void BinaryWriter::flush() |
| 192 | { |
| 193 | m_stream->flush(); |
| 194 | } |
| 195 | |
| 196 | /*! |
| 197 | * \brief Returns an indication whether the fail bit of the assigned stream is set. |
| 198 | */ |
| 199 | inline bool BinaryWriter::fail() const |
| 200 | { |
| 201 | return m_stream ? m_stream->fail() : false; |
| 202 | } |
| 203 | |
| 204 | /*! |
| 205 | * \brief Writes a character array to the current stream and advances the current position of the stream by the \a length of the array. |
| 206 | */ |
| 207 | inline void BinaryWriter::write(const char *buffer, std::streamsize length) |
| 208 | { |
| 209 | m_stream->write(buffer, length); |
| 210 | } |
| 211 | |
| 212 | /*! |
| 213 | * \brief Writes the specified number of bytes from the \a buffer to the current stream and advances the current position of the stream by |
| 214 | * the specified \a length which must be less or equal to the \a buffer size. |
| 215 | */ |
| 216 | inline void BinaryWriter::write(const std::vector<char> &buffer, std::streamsize length) |
| 217 | { |
| 218 | m_stream->write(buffer.data(), length); |
| 219 | } |
| 220 | |
| 221 | /*! |
| 222 | * \brief Writes a single character to the current stream and advances the current position of the stream by one byte. |
| 223 | */ |
| 224 | inline void BinaryWriter::writeChar(char value) |
| 225 | { |
| 226 | m_buffer[0] = value; |
| 227 | m_stream->write(m_buffer, 1); |
| 228 | } |
| 229 | |
| 230 | /*! |
| 231 | * \brief Writes a single byte to the current stream and advances the current position of the stream by one byte. |
| 232 | */ |
| 233 | inline void BinaryWriter::writeByte(std::uint8_t value) |
| 234 | { |
| 235 | m_buffer[0] = *reinterpret_cast<char *>(&value); |
| 236 | m_stream->write(m_buffer, 1); |
| 237 | } |
| 238 | |
| 239 | /*! |
| 240 | * \brief Writes a boolean value to the current stream and advances the current position of the stream by one byte. |
| 241 | */ |
| 242 | inline void BinaryWriter::writeBool(bool value) |
| 243 | { |
| 244 | writeByte(value ? 1 : 0); |
| 245 | } |
| 246 | |
| 247 | /*! |
| 248 | * \brief Writes a 16-bit big endian signed integer to the current stream and advances the current position of the stream by two bytes. |
| 249 | */ |
| 250 | inline void BinaryWriter::writeInt16BE(std::int16_t value) |
| 251 | { |
| 252 | BE::getBytes(value, m_buffer); |
| 253 | m_stream->write(m_buffer, sizeof(std::int16_t)); |
| 254 | } |
| 255 | |
| 256 | /*! |
| 257 | * \brief Writes a 16-bit big endian unsigned integer to the current stream and advances the current position of the stream by two bytes. |
| 258 | */ |
| 259 | inline void BinaryWriter::writeUInt16BE(std::uint16_t value) |
| 260 | { |
| 261 | BE::getBytes(value, m_buffer); |
| 262 | m_stream->write(m_buffer, sizeof(std::uint16_t)); |
| 263 | } |
| 264 | |
| 265 | /*! |
| 266 | * \brief Writes a 24-bit big endian signed integer to the current stream and advances the current position of the stream by three bytes. |
| 267 | * \remarks The most significant byte of the specified 32-bit signed integer \a value will be discarded. |
| 268 | */ |
| 269 | inline void BinaryWriter::writeInt24BE(std::int32_t value) |
| 270 | { |
| 271 | BE::getBytes(value, m_buffer); |
| 272 | m_stream->write(m_buffer + 1, 3); |
| 273 | } |
| 274 | |
| 275 | /*! |
| 276 | * \brief Writes a 24-bit big endian unsigned integer to the current stream and advances the current position of the stream by three bytes. |
| 277 | * \remarks The most significant byte of the specified 32-bit unsigned integer \a value will be discarded. |
| 278 | */ |
| 279 | inline void BinaryWriter::writeUInt24BE(std::uint32_t value) |
| 280 | { |
| 281 | // discard most significant byte |
| 282 | BE::getBytes(value, m_buffer); |
| 283 | m_stream->write(m_buffer + 1, 3); |
| 284 | } |
| 285 | |
| 286 | /*! |
| 287 | * \brief Writes a 32-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes. |
| 288 | */ |
| 289 | inline void BinaryWriter::writeInt32BE(std::int32_t value) |
| 290 | { |
| 291 | BE::getBytes(value, m_buffer); |
| 292 | m_stream->write(m_buffer, sizeof(std::int32_t)); |
| 293 | } |
| 294 | |
| 295 | /*! |
| 296 | * \brief Writes a 32-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes. |
| 297 | */ |
| 298 | inline void BinaryWriter::writeUInt32BE(std::uint32_t value) |
| 299 | { |
| 300 | BE::getBytes(value, m_buffer); |
| 301 | m_stream->write(m_buffer, sizeof(std::uint32_t)); |
| 302 | } |
| 303 | |
| 304 | /*! |
| 305 | * \brief Writes a 40-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes. |
| 306 | * \remarks The most significant bytes of the specified 64-bit unsigned integer \a value will be discarded. |
| 307 | */ |
| 308 | inline void BinaryWriter::writeInt40BE(std::int64_t value) |
| 309 | { |
| 310 | BE::getBytes(value, m_buffer); |
| 311 | m_stream->write(m_buffer + 3, 5); |
| 312 | } |
| 313 | |
| 314 | /*! |
| 315 | * \brief Writes a 40-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes. |
| 316 | * \remarks The most significant bytes of the specified 64-bit unsigned integer \a value will be discarded. |
| 317 | */ |
| 318 | inline void BinaryWriter::writeUInt40BE(std::uint64_t value) |
| 319 | { |
| 320 | BE::getBytes(value, m_buffer); |
| 321 | m_stream->write(m_buffer + 3, 5); |
| 322 | } |
| 323 | |
| 324 | /*! |
| 325 | * \brief Writes a 56-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes. |
| 326 | * \remarks The most significant byte of the specified 64-bit unsigned integer \a value will be discarded. |
| 327 | */ |
| 328 | inline void BinaryWriter::writeInt56BE(std::int64_t value) |
| 329 | { |
| 330 | BE::getBytes(value, m_buffer); |
| 331 | m_stream->write(m_buffer + 1, 7); |
| 332 | } |
| 333 | |
| 334 | /*! |
| 335 | * \brief Writes a 56-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes. |
| 336 | * \remarks The most significant byte of the specified 64-bit unsigned integer \a value will be discarded. |
| 337 | */ |
| 338 | inline void BinaryWriter::writeUInt56BE(std::uint64_t value) |
| 339 | { |
| 340 | BE::getBytes(value, m_buffer); |
| 341 | m_stream->write(m_buffer + 1, 7); |
| 342 | } |
| 343 | |
| 344 | /*! |
| 345 | * \brief Writes a 64-bit big endian signed integer to the current stream and advances the current position of the stream by eight bytes. |
| 346 | */ |
| 347 | inline void BinaryWriter::writeInt64BE(std::int64_t value) |
| 348 | { |
| 349 | BE::getBytes(value, m_buffer); |
| 350 | m_stream->write(m_buffer, sizeof(std::int64_t)); |
| 351 | } |
| 352 | |
| 353 | /*! |
| 354 | * \brief Writes a 64-bit big endian unsigned integer to the current stream and advances the current position of the stream by eight bytes. |
| 355 | */ |
| 356 | inline void BinaryWriter::writeUInt64BE(std::uint64_t value) |
| 357 | { |
| 358 | BE::getBytes(value, m_buffer); |
| 359 | m_stream->write(m_buffer, sizeof(std::uint64_t)); |
| 360 | } |
| 361 | |
| 362 | /*! |
| 363 | * \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. |
| 364 | * \throws Throws ConversionException if \a value exceeds the maximum. |
| 365 | */ |
| 366 | inline void BinaryWriter::writeVariableLengthUIntBE(std::uint64_t value) |
| 367 | { |
| 368 | writeVariableLengthInteger(value, static_cast<void (*)(std::uint64_t, char *)>(&BE::getBytes)); |
| 369 | } |
| 370 | |
| 371 | /*! |
| 372 | * \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. |
| 373 | */ |
| 374 | inline void BinaryWriter::writeFloat32BE(float value) |
| 375 | { |
| 376 | BE::getBytes(value, m_buffer); |
| 377 | m_stream->write(m_buffer, sizeof(float)); |
| 378 | } |
| 379 | |
| 380 | /*! |
| 381 | * \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. |
| 382 | */ |
| 383 | inline void BinaryWriter::writeFloat64BE(double value) |
| 384 | { |
| 385 | BE::getBytes(value, m_buffer); |
| 386 | m_stream->write(m_buffer, sizeof(double)); |
| 387 | } |
| 388 | |
| 389 | /*! |
| 390 | * \brief Writes a 16-bit little endian signed integer to the current stream and advances the current position of the stream by two bytes. |
| 391 | */ |
| 392 | inline void BinaryWriter::writeInt16LE(std::int16_t value) |
| 393 | { |
| 394 | LE::getBytes(value, m_buffer); |
| 395 | m_stream->write(m_buffer, sizeof(std::int16_t)); |
| 396 | } |
| 397 | |
| 398 | /*! |
| 399 | * \brief Writes a 16-bit little endian unsigned integer to the current stream and advances the current position of the stream by two bytes. |
| 400 | */ |
| 401 | inline void BinaryWriter::writeUInt16LE(std::uint16_t value) |
| 402 | { |
| 403 | LE::getBytes(value, m_buffer); |
| 404 | m_stream->write(m_buffer, sizeof(std::uint16_t)); |
| 405 | } |
| 406 | |
| 407 | /*! |
| 408 | * \brief Writes a 24-bit little endian signed integer to the current stream and advances the current position of the stream by three bytes. |
| 409 | * \remarks The most significant byte of the specified 32-bit signed integer \a value will be discarded. |
| 410 | */ |
| 411 | inline void BinaryWriter::writeInt24LE(std::int32_t value) |
| 412 | { |
| 413 | // discard most significant byte |
| 414 | LE::getBytes(value, m_buffer); |
| 415 | m_stream->write(m_buffer, 3); |
| 416 | } |
| 417 | |
| 418 | /*! |
| 419 | * \brief Writes a 24-bit little endian unsigned integer to the current stream and advances the current position of the stream by three bytes. |
| 420 | * \remarks The most significant byte of the specified 32-bit unsigned integer \a value will be discarded. |
| 421 | */ |
| 422 | inline void BinaryWriter::writeUInt24LE(std::uint32_t value) |
| 423 | { |
| 424 | // discard most significant byte |
| 425 | LE::getBytes(value, m_buffer); |
| 426 | m_stream->write(m_buffer, 3); |
| 427 | } |
| 428 | |
| 429 | /*! |
| 430 | * \brief Writes a 32-bit little endian signed integer to the current stream and advances the current position of the stream by four bytes. |
| 431 | */ |
| 432 | inline void BinaryWriter::writeInt32LE(std::int32_t value) |
| 433 | { |
| 434 | LE::getBytes(value, m_buffer); |
| 435 | m_stream->write(m_buffer, sizeof(std::int32_t)); |
| 436 | } |
| 437 | |
| 438 | /*! |
| 439 | * \brief Writes a 32-bit little endian unsigned integer to the current stream and advances the current position of the stream by four bytes. |
| 440 | */ |
| 441 | inline void BinaryWriter::writeUInt32LE(std::uint32_t value) |
| 442 | { |
| 443 | LE::getBytes(value, m_buffer); |
| 444 | m_stream->write(m_buffer, sizeof(std::uint32_t)); |
| 445 | } |
| 446 | |
| 447 | /*! |
| 448 | * \brief Writes a 40-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes. |
| 449 | * \remarks The most significant bytes of the specified 64-bit unsigned integer \a value will be discarded. |
| 450 | */ |
| 451 | inline void BinaryWriter::writeInt40LE(std::int64_t value) |
| 452 | { |
| 453 | LE::getBytes(value, m_buffer); |
| 454 | m_stream->write(m_buffer, 5); |
| 455 | } |
| 456 | |
| 457 | /*! |
| 458 | * \brief Writes a 40-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes. |
| 459 | * \remarks The most significant bytes of the specified 64-bit unsigned integer \a value will be discarded. |
| 460 | */ |
| 461 | inline void BinaryWriter::writeUInt40LE(std::uint64_t value) |
| 462 | { |
| 463 | LE::getBytes(value, m_buffer); |
| 464 | m_stream->write(m_buffer, 5); |
| 465 | } |
| 466 | |
| 467 | /*! |
| 468 | * \brief Writes a 56-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes. |
| 469 | * \remarks The most significant byte of the specified 64-bit unsigned integer \a value will be discarded. |
| 470 | */ |
| 471 | inline void BinaryWriter::writeInt56LE(std::int64_t value) |
| 472 | { |
| 473 | LE::getBytes(value, m_buffer); |
| 474 | m_stream->write(m_buffer, 7); |
| 475 | } |
| 476 | |
| 477 | /*! |
| 478 | * \brief Writes a 56-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes. |
| 479 | * \remarks The most significant byte of the specified 64-bit unsigned integer \a value will be discarded. |
| 480 | */ |
| 481 | inline void BinaryWriter::writeUInt56LE(std::uint64_t value) |
| 482 | { |
| 483 | LE::getBytes(value, m_buffer); |
| 484 | m_stream->write(m_buffer, 7); |
| 485 | } |
| 486 | |
| 487 | /*! |
| 488 | * \brief Writes a 64-bit little endian signed integer to the current stream and advances the current position of the stream by eight bytes. |
| 489 | */ |
| 490 | inline void BinaryWriter::writeInt64LE(std::int64_t value) |
| 491 | { |
| 492 | LE::getBytes(value, m_buffer); |
| 493 | m_stream->write(m_buffer, sizeof(std::int64_t)); |
| 494 | } |
| 495 | |
| 496 | /*! |
| 497 | * \brief Writes a 64-bit little endian unsigned integer to the current stream and advances the current position of the stream by eight bytes. |
| 498 | */ |
| 499 | inline void BinaryWriter::writeUInt64LE(std::uint64_t value) |
| 500 | { |
| 501 | LE::getBytes(value, m_buffer); |
| 502 | m_stream->write(m_buffer, sizeof(std::uint64_t)); |
| 503 | } |
| 504 | |
| 505 | /*! |
| 506 | * \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. |
| 507 | * \throws Throws ConversionException if \a value exceeds the maximum. |
| 508 | */ |
| 509 | inline void BinaryWriter::writeVariableLengthUIntLE(std::uint64_t value) |
| 510 | { |
| 511 | writeVariableLengthInteger(value, static_cast<void (*)(std::uint64_t, char *)>(&LE::getBytes)); |
| 512 | } |
| 513 | |
| 514 | /*! |
| 515 | * \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. |
| 516 | */ |
| 517 | inline void BinaryWriter::writeFloat32LE(float value) |
| 518 | { |
| 519 | LE::getBytes(value, m_buffer); |
| 520 | m_stream->write(m_buffer, sizeof(float)); |
| 521 | } |
| 522 | |
| 523 | /*! |
| 524 | * \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. |
| 525 | */ |
| 526 | inline void BinaryWriter::writeFloat64LE(double value) |
| 527 | { |
| 528 | LE::getBytes(value, m_buffer); |
| 529 | m_stream->write(m_buffer, sizeof(double)); |
| 530 | } |
| 531 | |
| 532 | /*! |
| 533 | * \brief Writes a string to the current stream and advances the current position of the stream by the length of the string. |
| 534 | */ |
| 535 | inline void BinaryWriter::writeString(const std::string &value) |
| 536 | { |
| 537 | m_stream->write(value.data(), static_cast<std::streamsize>(value.size())); |
| 538 | } |
| 539 | |
| 540 | /*! |
| 541 | * \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. |
| 542 | */ |
| 543 | inline void BinaryWriter::writeTerminatedString(const std::string &value) |
| 544 | { |
| 545 | m_stream->write(value.data(), static_cast<std::streamsize>(value.size() + 1)); |
| 546 | } |
| 547 | |
| 548 | /*! |
| 549 | * \brief Writes the length of a string and the string itself to the current stream. |
| 550 | * |
| 551 | * Advances the current position of the stream by the length of the string plus the size of the length prefix. |
| 552 | * |
| 553 | * \throws Throws ConversionException if the string size exceeds the maximum. |
| 554 | */ |
| 555 | inline void BinaryWriter::writeLengthPrefixedString(const std::string &value) |
| 556 | { |
| 557 | writeVariableLengthUIntBE(value.size()); |
| 558 | m_stream->write(value.data(), static_cast<std::streamsize>(value.size())); |
| 559 | } |
| 560 | |
| 561 | /*! |
| 562 | * \brief Writes the length of a string and the string itself to the current stream. |
| 563 | * |
| 564 | * Advances the current position of the stream by the length of the string plus the size of the length prefix. |
| 565 | * |
| 566 | * \throws Throws ConversionException if the string size exceeds the maximum. |
| 567 | */ |
| 568 | inline void BinaryWriter::writeLengthPrefixedCString(const char *value, std::size_t size) |
| 569 | { |
| 570 | writeVariableLengthUIntBE(size); |
| 571 | m_stream->write(value, static_cast<std::streamsize>(size)); |
| 572 | } |
| 573 | |
| 574 | /*! |
| 575 | * \brief Writes a 32-bit big endian synchsafe integer to the current stream and advances the current position of the stream by four bytes. |
| 576 | * \remarks Synchsafe integers appear in ID3 tags that are attached to an MP3 file. |
| 577 | * \sa <a href="http://id3.org/id3v2.4.0-structure">ID3 tag version 2.4.0 - Main Structure</a> |
| 578 | */ |
| 579 | inline void BinaryWriter::writeSynchsafeUInt32BE(std::uint32_t valueToConvertAndWrite) |
| 580 | { |
| 581 | writeUInt32BE(toSynchsafeInt(valueToConvertAndWrite)); |
| 582 | } |
| 583 | |
| 584 | /*! |
| 585 | * \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. |
| 586 | */ |
| 587 | inline void BinaryWriter::writeFixed8BE(float valueToConvertAndWrite) |
| 588 | { |
| 589 | writeUInt16BE(toFixed8(valueToConvertAndWrite)); |
| 590 | } |
| 591 | |
| 592 | /*! |
| 593 | * \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. |
| 594 | */ |
| 595 | inline void BinaryWriter::writeFixed16BE(float valueToConvertAndWrite) |
| 596 | { |
| 597 | writeUInt32BE(toFixed16(valueToConvertAndWrite)); |
| 598 | } |
| 599 | |
| 600 | /*! |
| 601 | * \brief Writes a 32-bit little endian synchsafe integer to the current stream and advances the current position of the stream by four bytes. |
| 602 | * \remarks Synchsafe integers appear in ID3 tags that are attached to an MP3 file. |
| 603 | * \sa <a href="http://id3.org/id3v2.4.0-structure">ID3 tag version 2.4.0 - Main Structure</a> |
| 604 | */ |
| 605 | inline void BinaryWriter::writeSynchsafeUInt32LE(std::uint32_t valueToConvertAndWrite) |
| 606 | { |
| 607 | writeUInt32LE(toSynchsafeInt(valueToConvertAndWrite)); |
| 608 | } |
| 609 | |
| 610 | /*! |
| 611 | * \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. |
| 612 | */ |
| 613 | inline void BinaryWriter::writeFixed8LE(float valueToConvertAndWrite) |
| 614 | { |
| 615 | writeUInt16LE(toFixed8(valueToConvertAndWrite)); |
| 616 | } |
| 617 | |
| 618 | /*! |
| 619 | * \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. |
| 620 | */ |
| 621 | inline void BinaryWriter::writeFixed16LE(float valueToConvertAndWrite) |
| 622 | { |
| 623 | writeUInt32LE(toFixed16(valueToConvertAndWrite)); |
| 624 | } |
| 625 | |
| 626 | /*! |
| 627 | * \brief Writes a single character to the current stream and advances the current position of the stream by one byte. |
| 628 | */ |
| 629 | inline void BinaryWriter::write(char oneChar) |
| 630 | { |
| 631 | writeChar(oneChar); |
| 632 | } |
| 633 | |
| 634 | /*! |
| 635 | * \brief Writes a single byte to the current stream and advances the current position of the stream by one byte. |
| 636 | */ |
| 637 | inline void BinaryWriter::write(std::uint8_t oneByte) |
| 638 | { |
| 639 | writeByte(oneByte); |
| 640 | } |
| 641 | |
| 642 | /*! |
| 643 | * \brief Writes a boolean value to the current stream and advances the current position of the stream by one byte. |
| 644 | */ |
| 645 | inline void BinaryWriter::write(bool oneBool) |
| 646 | { |
| 647 | writeBool(oneBool); |
| 648 | } |
| 649 | |
| 650 | /*! |
| 651 | * \brief Writes the length of a string and the string itself to the current stream. |
| 652 | * |
| 653 | * Advances the current position of the stream by the length of the string plus the size of the length prefix. |
| 654 | */ |
| 655 | inline void BinaryWriter::write(const std::string &lengthPrefixedString) |
| 656 | { |
| 657 | writeLengthPrefixedString(lengthPrefixedString); |
| 658 | } |
| 659 | |
| 660 | /*! |
| 661 | * \brief Writes the length of a string and the string itself to the current stream. |
| 662 | * |
| 663 | * Advances the current position of the stream by the length of the string plus the size of the length prefix. |
| 664 | */ |
| 665 | inline void BinaryWriter::write(std::string_view lengthPrefixedString) |
| 666 | { |
| 667 | writeLengthPrefixedCString(lengthPrefixedString.data(), lengthPrefixedString.size()); |
| 668 | } |
| 669 | |
| 670 | /*! |
| 671 | * \brief Writes the length of a string and the string itself to the current stream. |
| 672 | * |
| 673 | * Advances the current position of the stream by the length of the string plus the size of the length prefix. |
| 674 | */ |
| 675 | inline void BinaryWriter::write(const char *lengthPrefixedString) |
| 676 | { |
| 677 | writeLengthPrefixedCString(lengthPrefixedString, std::strlen(lengthPrefixedString)); |
| 678 | } |
| 679 | |
| 680 | /*! |
| 681 | * \brief Writes a 16-bit big endian signed integer to the current stream and advances the current position of the stream by two bytes. |
| 682 | */ |
| 683 | inline void BinaryWriter::write(std::int16_t one16BitInt) |
| 684 | { |
| 685 | writeInt16BE(one16BitInt); |
| 686 | } |
| 687 | |
| 688 | /*! |
| 689 | * \brief Writes a 16-bit big endian unsigned integer to the current stream and advances the current position of the stream by two bytes. |
| 690 | */ |
| 691 | inline void BinaryWriter::write(std::uint16_t one16BitUint) |
| 692 | { |
| 693 | writeUInt16BE(one16BitUint); |
| 694 | } |
| 695 | |
| 696 | /*! |
| 697 | * \brief Writes a 32-bit big endian signed integer to the current stream and advances the current position of the stream by four bytes. |
| 698 | */ |
| 699 | inline void BinaryWriter::write(std::int32_t one32BitInt) |
| 700 | { |
| 701 | writeInt32BE(one32BitInt); |
| 702 | } |
| 703 | |
| 704 | /*! |
| 705 | * \brief Writes a 32-bit big endian unsigned integer to the current stream and advances the current position of the stream by four bytes. |
| 706 | */ |
| 707 | inline void BinaryWriter::write(std::uint32_t one32BitUint) |
| 708 | { |
| 709 | writeUInt32BE(one32BitUint); |
| 710 | } |
| 711 | |
| 712 | /*! |
| 713 | * \brief Writes a 64-bit big endian signed integer to the current stream and advances the current position of the stream by eight bytes. |
| 714 | */ |
| 715 | inline void BinaryWriter::write(std::int64_t one64BitInt) |
| 716 | { |
| 717 | writeInt64BE(one64BitInt); |
| 718 | } |
| 719 | |
| 720 | /*! |
| 721 | * \brief Writes a 64-bit big endian unsigned integer to the current stream and advances the current position of the stream by eight bytes. |
| 722 | */ |
| 723 | inline void BinaryWriter::write(std::uint64_t one64BitUint) |
| 724 | { |
| 725 | writeUInt64BE(one64BitUint); |
| 726 | } |
| 727 | |
| 728 | /*! |
| 729 | * \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. |
| 730 | */ |
| 731 | inline void BinaryWriter::write(float one32BitFloat) |
| 732 | { |
| 733 | writeFloat32BE(one32BitFloat); |
| 734 | } |
| 735 | |
| 736 | /*! |
| 737 | * \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. |
| 738 | */ |
| 739 | inline void BinaryWriter::write(double one64BitFloat) |
| 740 | { |
| 741 | writeFloat64BE(one64BitFloat); |
| 742 | } |
| 743 | } // namespace CppUtilities |
| 744 | |
| 745 | #endif // IO_UTILITIES_BINARYWRITER_H |