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.

384 lines
10KB

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