The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

395 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. #if JUCE_DEBUG
  20. //==============================================================================
  21. struct DanglingStreamChecker
  22. {
  23. DanglingStreamChecker() = default;
  24. ~DanglingStreamChecker()
  25. {
  26. /*
  27. It's always a bad idea to leak any object, but if you're leaking output
  28. streams, then there's a good chance that you're failing to flush a file
  29. to disk properly, which could result in corrupted data and other similar
  30. nastiness..
  31. */
  32. jassert (activeStreams.size() == 0);
  33. // We need to flag when this helper struct has been destroyed to prevent some
  34. // nasty order-of-static-destruction issues
  35. hasBeenDestroyed = true;
  36. }
  37. Array<void*, CriticalSection> activeStreams;
  38. static bool hasBeenDestroyed;
  39. };
  40. bool DanglingStreamChecker::hasBeenDestroyed = false;
  41. static DanglingStreamChecker danglingStreamChecker;
  42. #endif
  43. //==============================================================================
  44. OutputStream::OutputStream()
  45. : newLineString (NewLine::getDefault())
  46. {
  47. #if JUCE_DEBUG
  48. if (! DanglingStreamChecker::hasBeenDestroyed)
  49. danglingStreamChecker.activeStreams.add (this);
  50. #endif
  51. }
  52. OutputStream::~OutputStream()
  53. {
  54. #if JUCE_DEBUG
  55. if (! DanglingStreamChecker::hasBeenDestroyed)
  56. danglingStreamChecker.activeStreams.removeFirstMatchingValue (this);
  57. #endif
  58. }
  59. //==============================================================================
  60. bool OutputStream::writeBool (bool b)
  61. {
  62. return writeByte (b ? (char) 1
  63. : (char) 0);
  64. }
  65. bool OutputStream::writeByte (char byte)
  66. {
  67. return write (&byte, 1);
  68. }
  69. bool OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat)
  70. {
  71. for (size_t i = 0; i < numTimesToRepeat; ++i)
  72. if (! writeByte ((char) byte))
  73. return false;
  74. return true;
  75. }
  76. bool OutputStream::writeShort (short value)
  77. {
  78. auto v = ByteOrder::swapIfBigEndian ((uint16) value);
  79. return write (&v, 2);
  80. }
  81. bool OutputStream::writeShortBigEndian (short value)
  82. {
  83. auto v = ByteOrder::swapIfLittleEndian ((uint16) value);
  84. return write (&v, 2);
  85. }
  86. bool OutputStream::writeInt (int value)
  87. {
  88. auto v = ByteOrder::swapIfBigEndian ((uint32) value);
  89. return write (&v, 4);
  90. }
  91. bool OutputStream::writeIntBigEndian (int value)
  92. {
  93. auto v = ByteOrder::swapIfLittleEndian ((uint32) value);
  94. return write (&v, 4);
  95. }
  96. bool OutputStream::writeCompressedInt (int value)
  97. {
  98. auto un = (value < 0) ? (unsigned int) -value
  99. : (unsigned int) value;
  100. uint8 data[5];
  101. int num = 0;
  102. while (un > 0)
  103. {
  104. data[++num] = (uint8) un;
  105. un >>= 8;
  106. }
  107. data[0] = (uint8) num;
  108. if (value < 0)
  109. data[0] |= 0x80;
  110. return write (data, (size_t) num + 1);
  111. }
  112. bool OutputStream::writeInt64 (int64 value)
  113. {
  114. auto v = ByteOrder::swapIfBigEndian ((uint64) value);
  115. return write (&v, 8);
  116. }
  117. bool OutputStream::writeInt64BigEndian (int64 value)
  118. {
  119. auto v = ByteOrder::swapIfLittleEndian ((uint64) value);
  120. return write (&v, 8);
  121. }
  122. bool OutputStream::writeFloat (float value)
  123. {
  124. union { int asInt; float asFloat; } n;
  125. n.asFloat = value;
  126. return writeInt (n.asInt);
  127. }
  128. bool OutputStream::writeFloatBigEndian (float value)
  129. {
  130. union { int asInt; float asFloat; } n;
  131. n.asFloat = value;
  132. return writeIntBigEndian (n.asInt);
  133. }
  134. bool OutputStream::writeDouble (double value)
  135. {
  136. union { int64 asInt; double asDouble; } n;
  137. n.asDouble = value;
  138. return writeInt64 (n.asInt);
  139. }
  140. bool OutputStream::writeDoubleBigEndian (double value)
  141. {
  142. union { int64 asInt; double asDouble; } n;
  143. n.asDouble = value;
  144. return writeInt64BigEndian (n.asInt);
  145. }
  146. bool OutputStream::writeString (const String& text)
  147. {
  148. auto numBytes = text.getNumBytesAsUTF8() + 1;
  149. #if (JUCE_STRING_UTF_TYPE == 8)
  150. return write (text.toRawUTF8(), numBytes);
  151. #else
  152. // (This avoids using toUTF8() to prevent the memory bloat that it would leave behind
  153. // if lots of large, persistent strings were to be written to streams).
  154. HeapBlock<char> temp (numBytes);
  155. text.copyToUTF8 (temp, numBytes);
  156. return write (temp, numBytes);
  157. #endif
  158. }
  159. bool OutputStream::writeText (const String& text, bool asUTF16, bool writeUTF16ByteOrderMark, const char* lf)
  160. {
  161. bool replaceLineFeedWithUnix = lf != nullptr && lf[0] == '\n' && lf[1] == 0;
  162. bool replaceLineFeedWithWindows = lf != nullptr && lf[0] == '\r' && lf[1] == '\n' && lf[2] == 0;
  163. // The line-feed passed in must be either nullptr, or "\n" or "\r\n"
  164. jassert (lf == nullptr || replaceLineFeedWithWindows || replaceLineFeedWithUnix);
  165. if (asUTF16)
  166. {
  167. if (writeUTF16ByteOrderMark)
  168. write ("\x0ff\x0fe", 2);
  169. auto src = text.getCharPointer();
  170. bool lastCharWasReturn = false;
  171. for (;;)
  172. {
  173. auto c = src.getAndAdvance();
  174. if (c == 0)
  175. break;
  176. if (replaceLineFeedWithWindows)
  177. {
  178. if (c == '\n' && ! lastCharWasReturn)
  179. writeShort ((short) '\r');
  180. lastCharWasReturn = (c == L'\r');
  181. }
  182. else if (replaceLineFeedWithUnix && c == '\r')
  183. {
  184. continue;
  185. }
  186. if (! writeShort ((short) c))
  187. return false;
  188. }
  189. }
  190. else
  191. {
  192. const char* src = text.toRawUTF8();
  193. if (replaceLineFeedWithWindows)
  194. {
  195. for (auto t = src;;)
  196. {
  197. if (*t == '\n')
  198. {
  199. if (t > src)
  200. if (! write (src, (size_t) (t - src)))
  201. return false;
  202. if (! write ("\r\n", 2))
  203. return false;
  204. src = t + 1;
  205. }
  206. else if (*t == '\r')
  207. {
  208. if (t[1] == '\n')
  209. ++t;
  210. }
  211. else if (*t == 0)
  212. {
  213. if (t > src)
  214. if (! write (src, (size_t) (t - src)))
  215. return false;
  216. break;
  217. }
  218. ++t;
  219. }
  220. }
  221. else if (replaceLineFeedWithUnix)
  222. {
  223. for (;;)
  224. {
  225. auto c = *src++;
  226. if (c == 0)
  227. break;
  228. if (c != '\r')
  229. if (! writeByte (c))
  230. return false;
  231. }
  232. }
  233. else
  234. {
  235. return write (src, text.getNumBytesAsUTF8());
  236. }
  237. }
  238. return true;
  239. }
  240. int64 OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite)
  241. {
  242. if (numBytesToWrite < 0)
  243. numBytesToWrite = std::numeric_limits<int64>::max();
  244. int64 numWritten = 0;
  245. while (numBytesToWrite > 0)
  246. {
  247. char buffer[8192];
  248. auto num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));
  249. if (num <= 0)
  250. break;
  251. write (buffer, (size_t) num);
  252. numBytesToWrite -= num;
  253. numWritten += num;
  254. }
  255. return numWritten;
  256. }
  257. //==============================================================================
  258. void OutputStream::setNewLineString (const String& newLineStringToUse)
  259. {
  260. newLineString = newLineStringToUse;
  261. }
  262. //==============================================================================
  263. template <typename IntegerType>
  264. static void writeIntToStream (OutputStream& stream, IntegerType number)
  265. {
  266. char buffer[NumberToStringConverters::charsNeededForInt];
  267. char* end = buffer + numElementsInArray (buffer);
  268. const char* start = NumberToStringConverters::numberToString (end, number);
  269. stream.write (start, (size_t) (end - start - 1));
  270. }
  271. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const int number)
  272. {
  273. writeIntToStream (stream, number);
  274. return stream;
  275. }
  276. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const int64 number)
  277. {
  278. writeIntToStream (stream, number);
  279. return stream;
  280. }
  281. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const double number)
  282. {
  283. return stream << String (number);
  284. }
  285. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char character)
  286. {
  287. stream.writeByte (character);
  288. return stream;
  289. }
  290. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char* const text)
  291. {
  292. stream.write (text, strlen (text));
  293. return stream;
  294. }
  295. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const MemoryBlock& data)
  296. {
  297. if (! data.isEmpty())
  298. stream.write (data.getData(), data.getSize());
  299. return stream;
  300. }
  301. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const File& fileToRead)
  302. {
  303. FileInputStream in (fileToRead);
  304. if (in.openedOk())
  305. return stream << in;
  306. return stream;
  307. }
  308. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, InputStream& streamToRead)
  309. {
  310. stream.writeFromInputStream (streamToRead, -1);
  311. return stream;
  312. }
  313. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const NewLine&)
  314. {
  315. return stream << stream.getNewLineString();
  316. }
  317. } // namespace juce