Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OutputStream.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "OutputStream.h"
  21. #include "../files/FileInputStream.h"
  22. #include "../memory/ByteOrder.h"
  23. #include "../memory/MemoryBlock.h"
  24. #include "../text/NewLine.h"
  25. namespace water {
  26. //==============================================================================
  27. OutputStream::OutputStream()
  28. : newLineString (NewLine::getDefault())
  29. {
  30. }
  31. OutputStream::~OutputStream()
  32. {
  33. }
  34. //==============================================================================
  35. bool OutputStream::writeBool (const bool b)
  36. {
  37. return writeByte (b ? (char) 1
  38. : (char) 0);
  39. }
  40. bool OutputStream::writeByte (char byte)
  41. {
  42. return write (&byte, 1);
  43. }
  44. bool OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat)
  45. {
  46. for (size_t i = 0; i < numTimesToRepeat; ++i)
  47. if (! writeByte ((char) byte))
  48. return false;
  49. return true;
  50. }
  51. bool OutputStream::writeShort (short value)
  52. {
  53. const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value);
  54. return write (&v, 2);
  55. }
  56. bool OutputStream::writeShortBigEndian (short value)
  57. {
  58. const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value);
  59. return write (&v, 2);
  60. }
  61. bool OutputStream::writeInt (int value)
  62. {
  63. const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value);
  64. return write (&v, 4);
  65. }
  66. bool OutputStream::writeIntBigEndian (int value)
  67. {
  68. const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value);
  69. return write (&v, 4);
  70. }
  71. bool OutputStream::writeCompressedInt (int value)
  72. {
  73. unsigned int un = (value < 0) ? (unsigned int) -value
  74. : (unsigned int) value;
  75. uint8 data[5];
  76. int num = 0;
  77. while (un > 0)
  78. {
  79. data[++num] = (uint8) un;
  80. un >>= 8;
  81. }
  82. data[0] = (uint8) num;
  83. if (value < 0)
  84. data[0] |= 0x80;
  85. return write (data, (size_t) num + 1);
  86. }
  87. bool OutputStream::writeInt64 (int64 value)
  88. {
  89. const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value);
  90. return write (&v, 8);
  91. }
  92. bool OutputStream::writeInt64BigEndian (int64 value)
  93. {
  94. const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value);
  95. return write (&v, 8);
  96. }
  97. bool OutputStream::writeFloat (float value)
  98. {
  99. union { int asInt; float asFloat; } n;
  100. n.asFloat = value;
  101. return writeInt (n.asInt);
  102. }
  103. bool OutputStream::writeFloatBigEndian (float value)
  104. {
  105. union { int asInt; float asFloat; } n;
  106. n.asFloat = value;
  107. return writeIntBigEndian (n.asInt);
  108. }
  109. bool OutputStream::writeDouble (double value)
  110. {
  111. union { int64 asInt; double asDouble; } n;
  112. n.asDouble = value;
  113. return writeInt64 (n.asInt);
  114. }
  115. bool OutputStream::writeDoubleBigEndian (double value)
  116. {
  117. union { int64 asInt; double asDouble; } n;
  118. n.asDouble = value;
  119. return writeInt64BigEndian (n.asInt);
  120. }
  121. bool OutputStream::writeString (const String& text)
  122. {
  123. return write (text.toRawUTF8(), text.getNumBytesAsUTF8() + 1);
  124. }
  125. bool OutputStream::writeText (const String& text, const bool asUTF16,
  126. const bool writeUTF16ByteOrderMark)
  127. {
  128. if (asUTF16)
  129. {
  130. if (writeUTF16ByteOrderMark)
  131. write ("\x0ff\x0fe", 2);
  132. CharPointer_UTF8 src (text.getCharPointer());
  133. bool lastCharWasReturn = false;
  134. for (;;)
  135. {
  136. const water_uchar c = src.getAndAdvance();
  137. if (c == 0)
  138. break;
  139. if (c == '\n' && ! lastCharWasReturn)
  140. writeShort ((short) '\r');
  141. lastCharWasReturn = (c == L'\r');
  142. if (! writeShort ((short) c))
  143. return false;
  144. }
  145. }
  146. else
  147. {
  148. const char* src = text.toUTF8();
  149. const char* t = src;
  150. for (;;)
  151. {
  152. if (*t == '\n')
  153. {
  154. if (t > src)
  155. if (! write (src, (size_t) (t - src)))
  156. return false;
  157. if (! write ("\r\n", 2))
  158. return false;
  159. src = t + 1;
  160. }
  161. else if (*t == '\r')
  162. {
  163. if (t[1] == '\n')
  164. ++t;
  165. }
  166. else if (*t == 0)
  167. {
  168. if (t > src)
  169. if (! write (src, (size_t) (t - src)))
  170. return false;
  171. break;
  172. }
  173. ++t;
  174. }
  175. }
  176. return true;
  177. }
  178. int64 OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite)
  179. {
  180. if (numBytesToWrite < 0)
  181. numBytesToWrite = std::numeric_limits<int64>::max();
  182. int64 numWritten = 0;
  183. while (numBytesToWrite > 0)
  184. {
  185. char buffer [8192];
  186. const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));
  187. if (num <= 0)
  188. break;
  189. write (buffer, (size_t) num);
  190. numBytesToWrite -= num;
  191. numWritten += num;
  192. }
  193. return numWritten;
  194. }
  195. //==============================================================================
  196. void OutputStream::setNewLineString (const String& newLineString_)
  197. {
  198. newLineString = newLineString_;
  199. }
  200. //==============================================================================
  201. template <typename IntegerType>
  202. static void writeIntToStream (OutputStream& stream, IntegerType number)
  203. {
  204. char buffer [NumberToStringConverters::charsNeededForInt];
  205. char* end = buffer + numElementsInArray (buffer);
  206. const char* start = NumberToStringConverters::numberToString (end, number);
  207. stream.write (start, (size_t) (end - start - 1));
  208. }
  209. OutputStream& operator<< (OutputStream& stream, const int number)
  210. {
  211. writeIntToStream (stream, number);
  212. return stream;
  213. }
  214. OutputStream& operator<< (OutputStream& stream, const int64 number)
  215. {
  216. writeIntToStream (stream, number);
  217. return stream;
  218. }
  219. OutputStream& operator<< (OutputStream& stream, const double number)
  220. {
  221. return stream << String (number);
  222. }
  223. OutputStream& operator<< (OutputStream& stream, const char character)
  224. {
  225. stream.writeByte (character);
  226. return stream;
  227. }
  228. OutputStream& operator<< (OutputStream& stream, const char* const text)
  229. {
  230. stream.write (text, strlen (text));
  231. return stream;
  232. }
  233. OutputStream& operator<< (OutputStream& stream, const MemoryBlock& data)
  234. {
  235. if (data.getSize() > 0)
  236. stream.write (data.getData(), data.getSize());
  237. return stream;
  238. }
  239. OutputStream& operator<< (OutputStream& stream, const File& fileToRead)
  240. {
  241. FileInputStream in (fileToRead);
  242. if (in.openedOk())
  243. return stream << in;
  244. return stream;
  245. }
  246. OutputStream& operator<< (OutputStream& stream, InputStream& streamToRead)
  247. {
  248. stream.writeFromInputStream (streamToRead, -1);
  249. return stream;
  250. }
  251. OutputStream& operator<< (OutputStream& stream, const NewLine&)
  252. {
  253. return stream << stream.getNewLineString();
  254. }
  255. }