C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
binarywriter.cpp
Go to the documentation of this file.
1 #include "./binarywriter.h"
2 
3 #include "../conversion/conversionexception.h"
4 
5 #include <cstring>
6 #include <memory>
7 
8 using namespace std;
9 using namespace IoUtilities;
10 using namespace ConversionUtilities;
11 
23 BinaryWriter::BinaryWriter(ostream *stream)
24  : m_stream(stream)
25  , m_ownership(false)
26 {
27 }
28 
34  : m_stream(other.m_stream)
35  , m_ownership(false)
36 {
37 }
38 
43 {
44  if (m_ownership) {
45  delete m_stream;
46  }
47 }
48 
60 void BinaryWriter::setStream(ostream *stream, bool giveOwnership)
61 {
62  if (m_ownership) {
63  delete m_stream;
64  }
65  if (stream) {
66  m_stream = stream;
67  m_ownership = giveOwnership;
68  } else {
69  m_stream = nullptr;
70  m_ownership = false;
71  }
72 }
73 
77 void BinaryWriter::writeVariableLengthInteger(uint64 value, void (*getBytes)(uint64, char *))
78 {
79  uint64 boundCheck = 0x80;
80  byte prefixLength = 1;
81  for (; boundCheck != 0x8000000000000000; boundCheck <<= 7, ++prefixLength) {
82  if (value < boundCheck) {
83  getBytes(value | boundCheck, m_buffer);
84  break;
85  }
86  }
87  if (prefixLength == 9) {
88  throw ConversionException("The variable-length integer to be written exceeds the maximum.");
89  }
90  m_stream->write(m_buffer + 8 - prefixLength, prefixLength);
91 }
92 
102 {
103  writeVariableLengthUIntBE(value.size());
104  m_stream->write(value.data(), static_cast<streamsize>(value.size()));
105 }
106 
115 void BinaryWriter::writeLengthPrefixedCString(const char *value, size_t size)
116 {
118  m_stream->write(value, static_cast<streamsize>(size));
119 }
BinaryWriter(std::ostream *stream)
Constructs a new BinaryWriter.
Writes primitive data types to a std::ostream.
Definition: binarywriter.h:14
The ConversionException class is thrown by the various conversion functions of this library when a co...
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
void giveOwnership()
The writer will take ownership over the assigned stream.
Definition: binarywriter.h:137
void writeVariableLengthUIntBE(uint64 value)
Writes an up to 8 byte long big endian unsigned integer to the current stream and advances the curren...
Definition: binarywriter.h:334
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
Contains several functions providing conversions between different data types.
~BinaryWriter()
Destroys the BinaryWriter.
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
Definition: binarywriter.h:113
void writeLengthPrefixedCString(const char *value, std::size_t size)
Writes the length of a string and the string itself to the current stream.
CPP_UTILITIES_EXPORT void getBytes(int16 value, char *outputbuffer)
Stores the specified 16-bit signed integer value at a specified position in a char array.