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.

346 lines
9.1KB

  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. #if JUCE_DEBUG
  18. struct DanglingStreamChecker
  19. {
  20. DanglingStreamChecker() {}
  21. ~DanglingStreamChecker()
  22. {
  23. /*
  24. It's always a bad idea to leak any object, but if you're leaking output
  25. streams, then there's a good chance that you're failing to flush a file
  26. to disk properly, which could result in corrupted data and other similar
  27. nastiness..
  28. */
  29. jassert (activeStreams.size() == 0);
  30. }
  31. Array<void*, CriticalSection> activeStreams;
  32. };
  33. static DanglingStreamChecker danglingStreamChecker;
  34. #endif
  35. //==============================================================================
  36. OutputStream::OutputStream()
  37. : newLineString (NewLine::getDefault())
  38. {
  39. #if JUCE_DEBUG
  40. danglingStreamChecker.activeStreams.add (this);
  41. #endif
  42. }
  43. OutputStream::~OutputStream()
  44. {
  45. #if JUCE_DEBUG
  46. danglingStreamChecker.activeStreams.removeFirstMatchingValue (this);
  47. #endif
  48. }
  49. //==============================================================================
  50. bool OutputStream::writeBool (const bool b)
  51. {
  52. return writeByte (b ? (char) 1
  53. : (char) 0);
  54. }
  55. bool OutputStream::writeByte (char byte)
  56. {
  57. return write (&byte, 1);
  58. }
  59. bool OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat)
  60. {
  61. for (size_t i = 0; i < numTimesToRepeat; ++i)
  62. if (! writeByte ((char) byte))
  63. return false;
  64. return true;
  65. }
  66. bool OutputStream::writeShort (short value)
  67. {
  68. const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value);
  69. return write (&v, 2);
  70. }
  71. bool OutputStream::writeShortBigEndian (short value)
  72. {
  73. const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value);
  74. return write (&v, 2);
  75. }
  76. bool OutputStream::writeInt (int value)
  77. {
  78. const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value);
  79. return write (&v, 4);
  80. }
  81. bool OutputStream::writeIntBigEndian (int value)
  82. {
  83. const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value);
  84. return write (&v, 4);
  85. }
  86. bool OutputStream::writeCompressedInt (int value)
  87. {
  88. unsigned int un = (value < 0) ? (unsigned int) -value
  89. : (unsigned int) value;
  90. uint8 data[5];
  91. int num = 0;
  92. while (un > 0)
  93. {
  94. data[++num] = (uint8) un;
  95. un >>= 8;
  96. }
  97. data[0] = (uint8) num;
  98. if (value < 0)
  99. data[0] |= 0x80;
  100. return write (data, (size_t) num + 1);
  101. }
  102. bool OutputStream::writeInt64 (int64 value)
  103. {
  104. const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value);
  105. return write (&v, 8);
  106. }
  107. bool OutputStream::writeInt64BigEndian (int64 value)
  108. {
  109. const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value);
  110. return write (&v, 8);
  111. }
  112. bool OutputStream::writeFloat (float value)
  113. {
  114. union { int asInt; float asFloat; } n;
  115. n.asFloat = value;
  116. return writeInt (n.asInt);
  117. }
  118. bool OutputStream::writeFloatBigEndian (float value)
  119. {
  120. union { int asInt; float asFloat; } n;
  121. n.asFloat = value;
  122. return writeIntBigEndian (n.asInt);
  123. }
  124. bool OutputStream::writeDouble (double value)
  125. {
  126. union { int64 asInt; double asDouble; } n;
  127. n.asDouble = value;
  128. return writeInt64 (n.asInt);
  129. }
  130. bool OutputStream::writeDoubleBigEndian (double value)
  131. {
  132. union { int64 asInt; double asDouble; } n;
  133. n.asDouble = value;
  134. return writeInt64BigEndian (n.asInt);
  135. }
  136. bool OutputStream::writeString (const String& text)
  137. {
  138. #if (JUCE_STRING_UTF_TYPE == 8)
  139. return write (text.toRawUTF8(), text.getNumBytesAsUTF8() + 1);
  140. #else
  141. // (This avoids using toUTF8() to prevent the memory bloat that it would leave behind
  142. // if lots of large, persistent strings were to be written to streams).
  143. const size_t numBytes = text.getNumBytesAsUTF8() + 1;
  144. HeapBlock<char> temp (numBytes);
  145. text.copyToUTF8 (temp, numBytes);
  146. return write (temp, numBytes);
  147. #endif
  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. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const int number)
  234. {
  235. writeIntToStream (stream, number);
  236. return stream;
  237. }
  238. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const int64 number)
  239. {
  240. writeIntToStream (stream, number);
  241. return stream;
  242. }
  243. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const double number)
  244. {
  245. return stream << String (number);
  246. }
  247. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char character)
  248. {
  249. stream.writeByte (character);
  250. return stream;
  251. }
  252. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char* const text)
  253. {
  254. stream.write (text, strlen (text));
  255. return stream;
  256. }
  257. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const MemoryBlock& data)
  258. {
  259. if (data.getSize() > 0)
  260. stream.write (data.getData(), data.getSize());
  261. return stream;
  262. }
  263. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const File& fileToRead)
  264. {
  265. FileInputStream in (fileToRead);
  266. if (in.openedOk())
  267. return stream << in;
  268. return stream;
  269. }
  270. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, InputStream& streamToRead)
  271. {
  272. stream.writeFromInputStream (streamToRead, -1);
  273. return stream;
  274. }
  275. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const NewLine&)
  276. {
  277. return stream << stream.getNewLineString();
  278. }