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.

318 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. #if JUCE_DEBUG
  21. struct DanglingStreamChecker
  22. {
  23. DanglingStreamChecker() {}
  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. }
  34. Array<void*, CriticalSection> activeStreams;
  35. };
  36. static DanglingStreamChecker danglingStreamChecker;
  37. #endif
  38. //==============================================================================
  39. OutputStream::OutputStream()
  40. : newLineString (NewLine::getDefault())
  41. {
  42. #if JUCE_DEBUG
  43. danglingStreamChecker.activeStreams.add (this);
  44. #endif
  45. }
  46. OutputStream::~OutputStream()
  47. {
  48. #if JUCE_DEBUG
  49. danglingStreamChecker.activeStreams.removeValue (this);
  50. #endif
  51. }
  52. //==============================================================================
  53. void OutputStream::writeBool (const bool b)
  54. {
  55. writeByte (b ? (char) 1
  56. : (char) 0);
  57. }
  58. void OutputStream::writeByte (char byte)
  59. {
  60. write (&byte, 1);
  61. }
  62. void OutputStream::writeRepeatedByte (uint8 byte, int numTimesToRepeat)
  63. {
  64. while (--numTimesToRepeat >= 0)
  65. writeByte ((char) byte);
  66. }
  67. void OutputStream::writeShort (short value)
  68. {
  69. const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value);
  70. write (&v, 2);
  71. }
  72. void OutputStream::writeShortBigEndian (short value)
  73. {
  74. const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value);
  75. write (&v, 2);
  76. }
  77. void OutputStream::writeInt (int value)
  78. {
  79. const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value);
  80. write (&v, 4);
  81. }
  82. void OutputStream::writeIntBigEndian (int value)
  83. {
  84. const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value);
  85. write (&v, 4);
  86. }
  87. void OutputStream::writeCompressedInt (int value)
  88. {
  89. unsigned int un = (value < 0) ? (unsigned int) -value
  90. : (unsigned int) value;
  91. uint8 data[5];
  92. int num = 0;
  93. while (un > 0)
  94. {
  95. data[++num] = (uint8) un;
  96. un >>= 8;
  97. }
  98. data[0] = (uint8) num;
  99. if (value < 0)
  100. data[0] |= 0x80;
  101. write (data, num + 1);
  102. }
  103. void OutputStream::writeInt64 (int64 value)
  104. {
  105. const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value);
  106. write (&v, 8);
  107. }
  108. void OutputStream::writeInt64BigEndian (int64 value)
  109. {
  110. const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value);
  111. write (&v, 8);
  112. }
  113. void OutputStream::writeFloat (float value)
  114. {
  115. union { int asInt; float asFloat; } n;
  116. n.asFloat = value;
  117. writeInt (n.asInt);
  118. }
  119. void OutputStream::writeFloatBigEndian (float value)
  120. {
  121. union { int asInt; float asFloat; } n;
  122. n.asFloat = value;
  123. writeIntBigEndian (n.asInt);
  124. }
  125. void OutputStream::writeDouble (double value)
  126. {
  127. union { int64 asInt; double asDouble; } n;
  128. n.asDouble = value;
  129. writeInt64 (n.asInt);
  130. }
  131. void OutputStream::writeDoubleBigEndian (double value)
  132. {
  133. union { int64 asInt; double asDouble; } n;
  134. n.asDouble = value;
  135. writeInt64BigEndian (n.asInt);
  136. }
  137. void OutputStream::writeString (const String& text)
  138. {
  139. // (This avoids using toUTF8() to prevent the memory bloat that it would leave behind
  140. // if lots of large, persistent strings were to be written to streams).
  141. const int numBytes = text.getNumBytesAsUTF8() + 1;
  142. HeapBlock<char> temp ((size_t) numBytes);
  143. text.copyToUTF8 (temp, numBytes);
  144. write (temp, numBytes);
  145. }
  146. void 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. writeShort ((short) c);
  164. }
  165. }
  166. else
  167. {
  168. const char* src = text.toUTF8();
  169. const char* t = src;
  170. for (;;)
  171. {
  172. if (*t == '\n')
  173. {
  174. if (t > src)
  175. write (src, (int) (t - src));
  176. write ("\r\n", 2);
  177. src = t + 1;
  178. }
  179. else if (*t == '\r')
  180. {
  181. if (t[1] == '\n')
  182. ++t;
  183. }
  184. else if (*t == 0)
  185. {
  186. if (t > src)
  187. write (src, (int) (t - src));
  188. break;
  189. }
  190. ++t;
  191. }
  192. }
  193. }
  194. int OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite)
  195. {
  196. if (numBytesToWrite < 0)
  197. numBytesToWrite = std::numeric_limits<int64>::max();
  198. int numWritten = 0;
  199. while (numBytesToWrite > 0 && ! source.isExhausted())
  200. {
  201. char buffer [8192];
  202. const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));
  203. if (num <= 0)
  204. break;
  205. write (buffer, num);
  206. numBytesToWrite -= num;
  207. numWritten += num;
  208. }
  209. return numWritten;
  210. }
  211. //==============================================================================
  212. void OutputStream::setNewLineString (const String& newLineString_)
  213. {
  214. newLineString = newLineString_;
  215. }
  216. //==============================================================================
  217. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const int number)
  218. {
  219. return stream << String (number);
  220. }
  221. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const double number)
  222. {
  223. return stream << String (number);
  224. }
  225. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char character)
  226. {
  227. stream.writeByte (character);
  228. return stream;
  229. }
  230. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char* const text)
  231. {
  232. stream.write (text, (int) strlen (text));
  233. return stream;
  234. }
  235. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const MemoryBlock& data)
  236. {
  237. stream.write (data.getData(), (int) data.getSize());
  238. return stream;
  239. }
  240. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const File& fileToRead)
  241. {
  242. FileInputStream in (fileToRead);
  243. return stream << in;
  244. }
  245. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, InputStream& streamToRead)
  246. {
  247. stream.writeFromInputStream (streamToRead, -1);
  248. return stream;
  249. }
  250. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const NewLine&)
  251. {
  252. return stream << stream.getNewLineString();
  253. }
  254. END_JUCE_NAMESPACE