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.

229 lines
5.7KB

  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. char InputStream::readByte()
  21. {
  22. char temp = 0;
  23. read (&temp, 1);
  24. return temp;
  25. }
  26. bool InputStream::readBool()
  27. {
  28. return readByte() != 0;
  29. }
  30. short InputStream::readShort()
  31. {
  32. char temp[2];
  33. if (read (temp, 2) == 2)
  34. return (short) ByteOrder::littleEndianShort (temp);
  35. return 0;
  36. }
  37. short InputStream::readShortBigEndian()
  38. {
  39. char temp[2];
  40. if (read (temp, 2) == 2)
  41. return (short) ByteOrder::bigEndianShort (temp);
  42. return 0;
  43. }
  44. int InputStream::readInt()
  45. {
  46. char temp[4];
  47. if (read (temp, 4) == 4)
  48. return (int) ByteOrder::littleEndianInt (temp);
  49. return 0;
  50. }
  51. int InputStream::readIntBigEndian()
  52. {
  53. char temp[4];
  54. if (read (temp, 4) == 4)
  55. return (int) ByteOrder::bigEndianInt (temp);
  56. return 0;
  57. }
  58. int InputStream::readCompressedInt()
  59. {
  60. const uint8 sizeByte = (uint8) readByte();
  61. if (sizeByte == 0)
  62. return 0;
  63. const int numBytes = (sizeByte & 0x7f);
  64. if (numBytes > 4)
  65. {
  66. jassertfalse; // trying to read corrupt data - this method must only be used
  67. // to read data that was written by OutputStream::writeCompressedInt()
  68. return 0;
  69. }
  70. char bytes[4] = { 0, 0, 0, 0 };
  71. if (read (bytes, numBytes) != numBytes)
  72. return 0;
  73. const int num = (int) ByteOrder::littleEndianInt (bytes);
  74. return (sizeByte >> 7) ? -num : num;
  75. }
  76. int64 InputStream::readInt64()
  77. {
  78. union { uint8 asBytes[8]; uint64 asInt64; } n;
  79. if (read (n.asBytes, 8) == 8)
  80. return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
  81. return 0;
  82. }
  83. int64 InputStream::readInt64BigEndian()
  84. {
  85. union { uint8 asBytes[8]; uint64 asInt64; } n;
  86. if (read (n.asBytes, 8) == 8)
  87. return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
  88. return 0;
  89. }
  90. float InputStream::readFloat()
  91. {
  92. // the union below relies on these types being the same size...
  93. static_jassert (sizeof (int32) == sizeof (float));
  94. union { int32 asInt; float asFloat; } n;
  95. n.asInt = (int32) readInt();
  96. return n.asFloat;
  97. }
  98. float InputStream::readFloatBigEndian()
  99. {
  100. union { int32 asInt; float asFloat; } n;
  101. n.asInt = (int32) readIntBigEndian();
  102. return n.asFloat;
  103. }
  104. double InputStream::readDouble()
  105. {
  106. union { int64 asInt; double asDouble; } n;
  107. n.asInt = readInt64();
  108. return n.asDouble;
  109. }
  110. double InputStream::readDoubleBigEndian()
  111. {
  112. union { int64 asInt; double asDouble; } n;
  113. n.asInt = readInt64BigEndian();
  114. return n.asDouble;
  115. }
  116. String InputStream::readString()
  117. {
  118. MemoryBlock buffer (256);
  119. char* data = static_cast<char*> (buffer.getData());
  120. size_t i = 0;
  121. while ((data[i] = readByte()) != 0)
  122. {
  123. if (++i >= buffer.getSize())
  124. {
  125. buffer.setSize (buffer.getSize() + 512);
  126. data = static_cast<char*> (buffer.getData());
  127. }
  128. }
  129. return String::fromUTF8 (data, (int) i);
  130. }
  131. String InputStream::readNextLine()
  132. {
  133. MemoryBlock buffer (256);
  134. char* data = static_cast<char*> (buffer.getData());
  135. size_t i = 0;
  136. while ((data[i] = readByte()) != 0)
  137. {
  138. if (data[i] == '\n')
  139. break;
  140. if (data[i] == '\r')
  141. {
  142. const int64 lastPos = getPosition();
  143. if (readByte() != '\n')
  144. setPosition (lastPos);
  145. break;
  146. }
  147. if (++i >= buffer.getSize())
  148. {
  149. buffer.setSize (buffer.getSize() + 512);
  150. data = static_cast<char*> (buffer.getData());
  151. }
  152. }
  153. return String::fromUTF8 (data, (int) i);
  154. }
  155. int InputStream::readIntoMemoryBlock (MemoryBlock& block, int numBytes)
  156. {
  157. MemoryOutputStream mo (block, true);
  158. return mo.writeFromInputStream (*this, numBytes);
  159. }
  160. String InputStream::readEntireStreamAsString()
  161. {
  162. MemoryOutputStream mo;
  163. mo << *this;
  164. return mo.toString();
  165. }
  166. //==============================================================================
  167. void InputStream::skipNextBytes (int64 numBytesToSkip)
  168. {
  169. if (numBytesToSkip > 0)
  170. {
  171. const int skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
  172. HeapBlock<char> temp ((size_t) skipBufferSize);
  173. while (numBytesToSkip > 0 && ! isExhausted())
  174. numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
  175. }
  176. }
  177. END_JUCE_NAMESPACE