Utilities  1
Collection of utility classes and functions used by my C++ applications.
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Friends Macros
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, ByteOrder byteOrder) :
24  m_stream(stream),
25  m_ownership(false),
26  m_byteOrder(byteOrder)
27 {}
28 
35  m_stream(other.m_stream),
36  m_ownership(false),
37  m_byteOrder(other.m_byteOrder)
38 {}
39 
44 {
45  if(m_stream && m_ownership) {
46  delete m_stream;
47  }
48 }
49 
56 {
57  return m_stream;
58 }
59 
65 const ostream *BinaryWriter::stream() const
66 {
67  return m_stream;
68 }
69 
81 void BinaryWriter::setStream(ostream *stream, bool giveOwnership)
82 {
83  if(m_stream && m_ownership)
84  delete m_stream;
85  if(stream) {
86  m_stream = stream;
87  m_ownership = giveOwnership;
88  } else {
89  m_stream = nullptr;
90  m_ownership = false;
91  }
92 }
93 
102 {
103  return m_ownership;
104 }
105 
114 {
115  if(m_stream) {
116  m_ownership = true;
117  }
118 }
119 
128 {
129  m_ownership = false;
130 }
131 
139 {
140  return m_byteOrder;
141 }
142 
150 {
151  m_byteOrder = value;
152 }
153 
158 {
159  if(m_stream) {
160  m_stream->flush();
161  }
162 }
163 
167 bool BinaryWriter::fail() const
168 {
169  if(m_stream) {
170  return m_stream->fail();
171  } else {
172  return false;
173  }
174 }
175 
179 void BinaryWriter::write(const char *buffer, streamsize length)
180 {
181  m_stream->write(buffer, length);
182 }
183 
188 void BinaryWriter::write(const std::vector<char> &buffer, streamsize length)
189 {
190  m_stream->write(buffer.data(), length);
191 }
192 
196 void BinaryWriter::writeChar(char value)
197 {
198  char buff[1] = {value};
199  m_stream->write(buff, 1);
200 }
201 
206 {
207  char buff[1] = {*reinterpret_cast<char *>(&value)};
208  m_stream->write(buff, 1);
209 }
210 
215 {
216  char buff[sizeof(int16)];
217  getBytes(value, buff, 0, m_byteOrder);
218  m_stream->write(buff, sizeof(int16));
219 }
220 
225 {
226  char buff[sizeof(uint16)];
227  getBytes(value, buff, 0, m_byteOrder);
228  m_stream->write(buff, sizeof(uint16));
229 }
230 
236 {
237  // discard most significant byte
238  char buff[4] = {0};
239  getBytes(value, buff, 0, m_byteOrder);
240  switch(m_byteOrder) {
241  case ByteOrder::BigEndian:
242  m_stream->write(buff + 1, 3);
243  break;
244  case ByteOrder::LittleEndian:
245  m_stream->write(buff, 3);
246  break;
247  }
248 }
249 
255 {
256  // discard most significant byte
257  char buff[4] = {0};
258  getBytes(value, buff, 0, m_byteOrder);
259  switch(m_byteOrder) {
260  case ByteOrder::BigEndian:
261  m_stream->write(buff + 1, 3);
262  break;
263  case ByteOrder::LittleEndian:
264  m_stream->write(buff, 3);
265  break;
266  }
267 }
268 
273 {
274  char buff[sizeof(int32)];
275  getBytes(value, buff, 0, m_byteOrder);
276  m_stream->write(buff, sizeof(int32));
277 }
278 
283 {
284  char buff[sizeof(uint32)];
285  getBytes(value, buff, 0, m_byteOrder);
286  m_stream->write(buff, sizeof(uint32));
287 }
288 
293 {
294  char buff[sizeof(int64)];
295  getBytes(value, buff, 0, m_byteOrder);
296  m_stream->write(buff, sizeof(int64));
297 }
298 
303 {
304  char buff[sizeof(uint64)];
305  getBytes(value, buff, 0, m_byteOrder);
306  m_stream->write(buff, sizeof(uint64));
307 }
308 
312 void BinaryWriter::writeFloat32(float32 value)
313 {
314  char buff[sizeof(float32)];
315  getBytes(value, buff, 0, m_byteOrder);
316  m_stream->write(buff, sizeof(float32));
317 }
318 
322 void BinaryWriter::writeFloat64(float64 value)
323 {
324  char buff[sizeof(float64)];
325  getBytes(value, buff, 0, m_byteOrder);
326  m_stream->write(buff, sizeof(float64));
327 }
328 
332 void BinaryWriter::writeString(const string &value)
333 {
334  m_stream->write(value.c_str(), value.length());
335 }
336 
343 {
344  size_t length = value.length();
345  char buff[4] = {0};
346  if(length < 0x80) {
347  buff[0] = 0x80 | length;
348  m_stream->write(buff, 1);
349  } else if(length < 0x4000) {
350  getBytes(static_cast<uint16>(0x4000 | length), buff, 0, ByteOrder::BigEndian);
351  m_stream->write(buff, 2);
352  } else if(length < 0x200000) {
353  getBytes(static_cast<uint32>(0x200000 | length), buff, 0, ByteOrder::BigEndian);
354  m_stream->write(buff + 1, 3);
355  } else if(length < 0x10000000) {
356  getBytes(static_cast<uint32>(0x10000000 | length), buff, 0, ByteOrder::BigEndian);
357  m_stream->write(buff, 4);
358  } else {
359  throw ConversionException("The size of the string exceeds the maximum.");
360  }
361  m_stream->write(value.c_str(), length);
362 }
363 
367 void BinaryWriter::writeBool(bool value)
368 {
369  writeByte(value ? 1 : 0);
370 }
371 
379 void BinaryWriter::writeSynchsafeUInt32(uint32 valueToConvertAndWrite)
380 {
381  writeUInt32(ConversionUtilities::toSynchsafeInt(valueToConvertAndWrite));
382 }
383 
void getBytes(int16 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
Stores the specified 16-bit signed integer value at a specified position in a char array...
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
ByteOrder
Specifies the byte order/endianness.
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
Writes primitive data types to a std::ostream using a specified ConversionUtilities::ByteOrder.
Definition: binarywriter.h:15
The exception that is thrown when an conversion error occurs.
void writeUInt24(uint32 value)
Writes a 24-bit unsigned integer to the current stream and advances the current position of the strea...
void writeString(const std::string &value)
Writes a string to the current stream and advances the current position of the stream by the length o...
STL namespace.
void flush()
Calls the flush() method of the assigned stream.
void writeUInt32(uint32 value)
Writes a 32-bit unsigned integer to the current stream and advances the current position of the strea...
void writeInt24(int32 value)
Writes a 24-bit signed integer to the current stream and advances the current position of the stream ...
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
void writeInt32(int32 value)
Writes a 32-bit signed integer to the current stream and advances the current position of the stream ...
bool hasOwnership() const
Returns whether the writer takes ownership over the assigned stream.
void giveOwnership()
The writer will take ownership over the assigned stream.
void writeFloat32(float32 value)
Writes a 32-bit floating point value to the current stream and advances the current position of the s...
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
void writeFloat64(float64 value)
Writes a 64-bit floating point value to the current stream and advances the current position of the s...
Contains utility classes helping to read and write streams.
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
void writeInt16(int16 value)
Writes a 16-bit signed integer to the current stream and advances the current position of the stream ...
void writeSynchsafeUInt32(uint32 valueToConvertAndWrite)
Writes 32-bit synchsafe integer to the current stream and advances the current position of the stream...
void write(const char *buffer, std::streamsize length)
Contains several functions providing conversions between different data types.
std::uint32_t uint32
unsigned 32-bit integer
Definition: types.h:44
BinaryWriter(std::ostream *stream, ConversionUtilities::ByteOrder byteOrder=ConversionUtilities::ByteOrder::LittleEndian)
Constructs a new BinaryWriter.
uint32 toSynchsafeInt(uint32 normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.
void writeUInt64(uint64 value)
Writes a 64-bit unsigned integer to the current stream and advances the current position of the strea...
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
~BinaryWriter()
Destroys the BinaryWriter.
std::int32_t int32
signed 32-bit integer
Definition: types.h:24
void setByteOrder(ConversionUtilities::ByteOrder value)
Sets the byte order used when converting the provided values to the raw bytes written to the stream...
void writeInt64(int64 value)
Writes a 64-bit signed integer to the current stream and advances the current position of the stream ...
void detatchOwnership()
The writer will not take ownership over the assigned stream.
std::uint8_t byte
unsigned byte
Definition: types.h:14
void writeByte(byte value)
Writes a single byte to the current stream and advances the current position of the stream by one byt...
std::int16_t int16
signed 16-bit integer
Definition: types.h:19
void writeChar(char value)
Writes a single character to the current stream and advances the current position of the stream by on...
ConversionUtilities::ByteOrder byteOrder() const
Returns the byte order used when converting the provided values to the raw bytes written to the strea...
void writeUInt16(uint16 value)
Writes a 16-bit unsigned integer to the current stream and advances the current position of the strea...
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39