BinaryWriter.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // BinaryWriter.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/BinaryWriter.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: BinaryReaderWriter
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/BinaryWriter.h"
  16. #include "Poco/ByteOrder.h"
  17. #include "Poco/TextEncoding.h"
  18. #include "Poco/TextConverter.h"
  19. #include <cstring>
  20. namespace Poco {
  21. BinaryWriter::BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder):
  22. _ostr(ostr),
  23. _pTextConverter(0)
  24. {
  25. #if defined(POCO_ARCH_BIG_ENDIAN)
  26. _flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER);
  27. #else
  28. _flipBytes = (byteOrder == BIG_ENDIAN_BYTE_ORDER);
  29. #endif
  30. }
  31. BinaryWriter::BinaryWriter(std::ostream& ostr, TextEncoding& encoding, StreamByteOrder byteOrder):
  32. _ostr(ostr),
  33. _pTextConverter(new TextConverter(Poco::TextEncoding::global(), encoding))
  34. {
  35. #if defined(POCO_ARCH_BIG_ENDIAN)
  36. _flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER);
  37. #else
  38. _flipBytes = (byteOrder == BIG_ENDIAN_BYTE_ORDER);
  39. #endif
  40. }
  41. BinaryWriter::~BinaryWriter()
  42. {
  43. delete _pTextConverter;
  44. }
  45. BinaryWriter& BinaryWriter::operator << (bool value)
  46. {
  47. _ostr.write((const char*) &value, sizeof(value));
  48. return *this;
  49. }
  50. BinaryWriter& BinaryWriter::operator << (char value)
  51. {
  52. _ostr.write((const char*) &value, sizeof(value));
  53. return *this;
  54. }
  55. BinaryWriter& BinaryWriter::operator << (unsigned char value)
  56. {
  57. _ostr.write((const char*) &value, sizeof(value));
  58. return *this;
  59. }
  60. BinaryWriter& BinaryWriter::operator << (signed char value)
  61. {
  62. _ostr.write((const char*) &value, sizeof(value));
  63. return *this;
  64. }
  65. BinaryWriter& BinaryWriter::operator << (short value)
  66. {
  67. if (_flipBytes)
  68. {
  69. short fValue = ByteOrder::flipBytes(value);
  70. _ostr.write((const char*) &fValue, sizeof(fValue));
  71. }
  72. else
  73. {
  74. _ostr.write((const char*) &value, sizeof(value));
  75. }
  76. return *this;
  77. }
  78. BinaryWriter& BinaryWriter::operator << (unsigned short value)
  79. {
  80. if (_flipBytes)
  81. {
  82. unsigned short fValue = ByteOrder::flipBytes(value);
  83. _ostr.write((const char*) &fValue, sizeof(fValue));
  84. }
  85. else
  86. {
  87. _ostr.write((const char*) &value, sizeof(value));
  88. }
  89. return *this;
  90. }
  91. BinaryWriter& BinaryWriter::operator << (int value)
  92. {
  93. if (_flipBytes)
  94. {
  95. int fValue = ByteOrder::flipBytes(value);
  96. _ostr.write((const char*) &fValue, sizeof(fValue));
  97. }
  98. else
  99. {
  100. _ostr.write((const char*) &value, sizeof(value));
  101. }
  102. return *this;
  103. }
  104. BinaryWriter& BinaryWriter::operator << (unsigned int value)
  105. {
  106. if (_flipBytes)
  107. {
  108. unsigned int fValue = ByteOrder::flipBytes(value);
  109. _ostr.write((const char*) &fValue, sizeof(fValue));
  110. }
  111. else
  112. {
  113. _ostr.write((const char*) &value, sizeof(value));
  114. }
  115. return *this;
  116. }
  117. BinaryWriter& BinaryWriter::operator << (long value)
  118. {
  119. if (_flipBytes)
  120. {
  121. #if defined(POCO_LONG_IS_64_BIT)
  122. long fValue = ByteOrder::flipBytes((Int64) value);
  123. #else
  124. long fValue = ByteOrder::flipBytes((Int32) value);
  125. #endif
  126. _ostr.write((const char*) &fValue, sizeof(fValue));
  127. }
  128. else
  129. {
  130. _ostr.write((const char*) &value, sizeof(value));
  131. }
  132. return *this;
  133. }
  134. BinaryWriter& BinaryWriter::operator << (unsigned long value)
  135. {
  136. if (_flipBytes)
  137. {
  138. #if defined(POCO_LONG_IS_64_BIT)
  139. long fValue = ByteOrder::flipBytes((UInt64) value);
  140. #else
  141. long fValue = ByteOrder::flipBytes((UInt32) value);
  142. #endif
  143. _ostr.write((const char*) &fValue, sizeof(fValue));
  144. }
  145. else
  146. {
  147. _ostr.write((const char*) &value, sizeof(value));
  148. }
  149. return *this;
  150. }
  151. BinaryWriter& BinaryWriter::operator << (float value)
  152. {
  153. if (_flipBytes)
  154. {
  155. const char* ptr = (const char*) &value;
  156. ptr += sizeof(value);
  157. for (unsigned i = 0; i < sizeof(value); ++i)
  158. _ostr.write(--ptr, 1);
  159. }
  160. else
  161. {
  162. _ostr.write((const char*) &value, sizeof(value));
  163. }
  164. return *this;
  165. }
  166. BinaryWriter& BinaryWriter::operator << (double value)
  167. {
  168. if (_flipBytes)
  169. {
  170. const char* ptr = (const char*) &value;
  171. ptr += sizeof(value);
  172. for (unsigned i = 0; i < sizeof(value); ++i)
  173. _ostr.write(--ptr, 1);
  174. }
  175. else
  176. {
  177. _ostr.write((const char*) &value, sizeof(value));
  178. }
  179. return *this;
  180. }
  181. #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
  182. BinaryWriter& BinaryWriter::operator << (Int64 value)
  183. {
  184. if (_flipBytes)
  185. {
  186. Int64 fValue = ByteOrder::flipBytes(value);
  187. _ostr.write((const char*) &fValue, sizeof(fValue));
  188. }
  189. else
  190. {
  191. _ostr.write((const char*) &value, sizeof(value));
  192. }
  193. return *this;
  194. }
  195. BinaryWriter& BinaryWriter::operator << (UInt64 value)
  196. {
  197. if (_flipBytes)
  198. {
  199. UInt64 fValue = ByteOrder::flipBytes(value);
  200. _ostr.write((const char*) &fValue, sizeof(fValue));
  201. }
  202. else
  203. {
  204. _ostr.write((const char*) &value, sizeof(value));
  205. }
  206. return *this;
  207. }
  208. #endif
  209. BinaryWriter& BinaryWriter::operator << (const std::string& value)
  210. {
  211. if (_pTextConverter)
  212. {
  213. std::string converted;
  214. _pTextConverter->convert(value, converted);
  215. UInt32 length = (UInt32) converted.size();
  216. write7BitEncoded(length);
  217. _ostr.write(converted.data(), length);
  218. }
  219. else
  220. {
  221. UInt32 length = (UInt32) value.size();
  222. write7BitEncoded(length);
  223. _ostr.write(value.data(), length);
  224. }
  225. return *this;
  226. }
  227. BinaryWriter& BinaryWriter::operator << (const char* value)
  228. {
  229. poco_check_ptr (value);
  230. if (_pTextConverter)
  231. {
  232. std::string converted;
  233. _pTextConverter->convert(value, static_cast<int>(std::strlen(value)), converted);
  234. UInt32 length = (UInt32) converted.size();
  235. write7BitEncoded(length);
  236. _ostr.write(converted.data(), length);
  237. }
  238. else
  239. {
  240. UInt32 length = static_cast<UInt32>(std::strlen(value));
  241. write7BitEncoded(length);
  242. _ostr.write(value, length);
  243. }
  244. return *this;
  245. }
  246. void BinaryWriter::write7BitEncoded(UInt32 value)
  247. {
  248. do
  249. {
  250. unsigned char c = (unsigned char) (value & 0x7F);
  251. value >>= 7;
  252. if (value) c |= 0x80;
  253. _ostr.write((const char*) &c, 1);
  254. }
  255. while (value);
  256. }
  257. #if defined(POCO_HAVE_INT64)
  258. void BinaryWriter::write7BitEncoded(UInt64 value)
  259. {
  260. do
  261. {
  262. unsigned char c = (unsigned char) (value & 0x7F);
  263. value >>= 7;
  264. if (value) c |= 0x80;
  265. _ostr.write((const char*) &c, 1);
  266. }
  267. while (value);
  268. }
  269. #endif
  270. void BinaryWriter::writeRaw(const std::string& rawData)
  271. {
  272. _ostr.write(rawData.data(), (std::streamsize) rawData.length());
  273. }
  274. void BinaryWriter::writeRaw(const char* buffer, std::streamsize length)
  275. {
  276. _ostr.write(buffer, length);
  277. }
  278. void BinaryWriter::writeBOM()
  279. {
  280. UInt16 value = 0xFEFF;
  281. if (_flipBytes) value = ByteOrder::flipBytes(value);
  282. _ostr.write((const char*) &value, sizeof(value));
  283. }
  284. void BinaryWriter::flush()
  285. {
  286. _ostr.flush();
  287. }
  288. } // namespace Poco