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.

326 lines
8.7KB

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