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.

345 lines
9.2KB

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