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.

350 lines
9.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 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. #if JUCE_DEBUG
  27. struct DanglingStreamChecker
  28. {
  29. DanglingStreamChecker() {}
  30. ~DanglingStreamChecker()
  31. {
  32. /*
  33. It's always a bad idea to leak any object, but if you're leaking output
  34. streams, then there's a good chance that you're failing to flush a file
  35. to disk properly, which could result in corrupted data and other similar
  36. nastiness..
  37. */
  38. jassert (activeStreams.size() == 0);
  39. }
  40. Array<void*, CriticalSection> activeStreams;
  41. };
  42. static DanglingStreamChecker danglingStreamChecker;
  43. #endif
  44. //==============================================================================
  45. OutputStream::OutputStream()
  46. : newLineString (NewLine::getDefault())
  47. {
  48. #if JUCE_DEBUG
  49. danglingStreamChecker.activeStreams.add (this);
  50. #endif
  51. }
  52. OutputStream::~OutputStream()
  53. {
  54. #if JUCE_DEBUG
  55. danglingStreamChecker.activeStreams.removeFirstMatchingValue (this);
  56. #endif
  57. }
  58. //==============================================================================
  59. bool OutputStream::writeBool (const bool b)
  60. {
  61. return writeByte (b ? (char) 1
  62. : (char) 0);
  63. }
  64. bool OutputStream::writeByte (char byte)
  65. {
  66. return write (&byte, 1);
  67. }
  68. bool OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat)
  69. {
  70. for (size_t i = 0; i < numTimesToRepeat; ++i)
  71. if (! writeByte ((char) byte))
  72. return false;
  73. return true;
  74. }
  75. bool OutputStream::writeShort (short value)
  76. {
  77. const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value);
  78. return write (&v, 2);
  79. }
  80. bool OutputStream::writeShortBigEndian (short value)
  81. {
  82. const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value);
  83. return write (&v, 2);
  84. }
  85. bool OutputStream::writeInt (int value)
  86. {
  87. const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value);
  88. return write (&v, 4);
  89. }
  90. bool OutputStream::writeIntBigEndian (int value)
  91. {
  92. const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value);
  93. return write (&v, 4);
  94. }
  95. bool OutputStream::writeCompressedInt (int value)
  96. {
  97. unsigned int un = (value < 0) ? (unsigned int) -value
  98. : (unsigned int) value;
  99. uint8 data[5];
  100. int num = 0;
  101. while (un > 0)
  102. {
  103. data[++num] = (uint8) un;
  104. un >>= 8;
  105. }
  106. data[0] = (uint8) num;
  107. if (value < 0)
  108. data[0] |= 0x80;
  109. return write (data, (size_t) num + 1);
  110. }
  111. bool OutputStream::writeInt64 (int64 value)
  112. {
  113. const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value);
  114. return write (&v, 8);
  115. }
  116. bool OutputStream::writeInt64BigEndian (int64 value)
  117. {
  118. const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value);
  119. return write (&v, 8);
  120. }
  121. bool OutputStream::writeFloat (float value)
  122. {
  123. union { int asInt; float asFloat; } n;
  124. n.asFloat = value;
  125. return writeInt (n.asInt);
  126. }
  127. bool OutputStream::writeFloatBigEndian (float value)
  128. {
  129. union { int asInt; float asFloat; } n;
  130. n.asFloat = value;
  131. return writeIntBigEndian (n.asInt);
  132. }
  133. bool OutputStream::writeDouble (double value)
  134. {
  135. union { int64 asInt; double asDouble; } n;
  136. n.asDouble = value;
  137. return writeInt64 (n.asInt);
  138. }
  139. bool OutputStream::writeDoubleBigEndian (double value)
  140. {
  141. union { int64 asInt; double asDouble; } n;
  142. n.asDouble = value;
  143. return writeInt64BigEndian (n.asInt);
  144. }
  145. bool OutputStream::writeString (const String& text)
  146. {
  147. return write (text.toRawUTF8(), text.getNumBytesAsUTF8() + 1);
  148. }
  149. bool OutputStream::writeText (const String& text, const bool asUTF16,
  150. const bool writeUTF16ByteOrderMark)
  151. {
  152. if (asUTF16)
  153. {
  154. if (writeUTF16ByteOrderMark)
  155. write ("\x0ff\x0fe", 2);
  156. String::CharPointerType src (text.getCharPointer());
  157. bool lastCharWasReturn = false;
  158. for (;;)
  159. {
  160. const juce_wchar c = src.getAndAdvance();
  161. if (c == 0)
  162. break;
  163. if (c == '\n' && ! lastCharWasReturn)
  164. writeShort ((short) '\r');
  165. lastCharWasReturn = (c == L'\r');
  166. if (! writeShort ((short) c))
  167. return false;
  168. }
  169. }
  170. else
  171. {
  172. const char* src = text.toUTF8();
  173. const char* t = src;
  174. for (;;)
  175. {
  176. if (*t == '\n')
  177. {
  178. if (t > src)
  179. if (! write (src, (size_t) (t - src)))
  180. return false;
  181. if (! write ("\r\n", 2))
  182. return false;
  183. src = t + 1;
  184. }
  185. else if (*t == '\r')
  186. {
  187. if (t[1] == '\n')
  188. ++t;
  189. }
  190. else if (*t == 0)
  191. {
  192. if (t > src)
  193. if (! write (src, (size_t) (t - src)))
  194. return false;
  195. break;
  196. }
  197. ++t;
  198. }
  199. }
  200. return true;
  201. }
  202. int64 OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite)
  203. {
  204. if (numBytesToWrite < 0)
  205. numBytesToWrite = std::numeric_limits<int64>::max();
  206. int64 numWritten = 0;
  207. while (numBytesToWrite > 0)
  208. {
  209. char buffer [8192];
  210. const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));
  211. if (num <= 0)
  212. break;
  213. write (buffer, (size_t) num);
  214. numBytesToWrite -= num;
  215. numWritten += num;
  216. }
  217. return numWritten;
  218. }
  219. //==============================================================================
  220. void OutputStream::setNewLineString (const String& newLineString_)
  221. {
  222. newLineString = newLineString_;
  223. }
  224. //==============================================================================
  225. template <typename IntegerType>
  226. static void writeIntToStream (OutputStream& stream, IntegerType number)
  227. {
  228. char buffer [NumberToStringConverters::charsNeededForInt];
  229. char* end = buffer + numElementsInArray (buffer);
  230. const char* start = NumberToStringConverters::numberToString (end, number);
  231. stream.write (start, (size_t) (end - start - 1));
  232. }
  233. OutputStream& operator<< (OutputStream& stream, const int number)
  234. {
  235. writeIntToStream (stream, number);
  236. return stream;
  237. }
  238. OutputStream& operator<< (OutputStream& stream, const int64 number)
  239. {
  240. writeIntToStream (stream, number);
  241. return stream;
  242. }
  243. OutputStream& operator<< (OutputStream& stream, const double number)
  244. {
  245. return stream << String (number);
  246. }
  247. OutputStream& operator<< (OutputStream& stream, const char character)
  248. {
  249. stream.writeByte (character);
  250. return stream;
  251. }
  252. OutputStream& operator<< (OutputStream& stream, const char* const text)
  253. {
  254. stream.write (text, strlen (text));
  255. return stream;
  256. }
  257. OutputStream& operator<< (OutputStream& stream, const MemoryBlock& data)
  258. {
  259. if (data.getSize() > 0)
  260. stream.write (data.getData(), data.getSize());
  261. return stream;
  262. }
  263. OutputStream& operator<< (OutputStream& stream, const File& fileToRead)
  264. {
  265. FileInputStream in (fileToRead);
  266. if (in.openedOk())
  267. return stream << in;
  268. return stream;
  269. }
  270. OutputStream& operator<< (OutputStream& stream, InputStream& streamToRead)
  271. {
  272. stream.writeFromInputStream (streamToRead, -1);
  273. return stream;
  274. }
  275. OutputStream& operator<< (OutputStream& stream, const NewLine&)
  276. {
  277. return stream << stream.getNewLineString();
  278. }
  279. }