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.

225 lines
5.6KB

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