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.

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