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.

250 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. int64 InputStream::getNumBytesRemaining()
  20. {
  21. auto len = getTotalLength();
  22. if (len >= 0)
  23. len -= getPosition();
  24. return len;
  25. }
  26. ssize_t InputStream::read (void* destBuffer, size_t size)
  27. {
  28. ssize_t totalRead = 0;
  29. while (size > 0)
  30. {
  31. auto numToRead = (int) std::min (size, (size_t) 0x70000000);
  32. auto numRead = read (juce::addBytesToPointer (destBuffer, totalRead), numToRead);
  33. jassert (numRead <= numToRead);
  34. if (numRead < 0) return (ssize_t) numRead;
  35. if (numRead == 0) break;
  36. size -= (size_t) numRead;
  37. totalRead += numRead;
  38. }
  39. return totalRead;
  40. }
  41. char InputStream::readByte()
  42. {
  43. char temp = 0;
  44. read (&temp, 1);
  45. return temp;
  46. }
  47. bool InputStream::readBool()
  48. {
  49. return readByte() != 0;
  50. }
  51. short InputStream::readShort()
  52. {
  53. char temp[2];
  54. if (read (temp, 2) == 2)
  55. return (short) ByteOrder::littleEndianShort (temp);
  56. return 0;
  57. }
  58. short InputStream::readShortBigEndian()
  59. {
  60. char temp[2];
  61. if (read (temp, 2) == 2)
  62. return (short) ByteOrder::bigEndianShort (temp);
  63. return 0;
  64. }
  65. int InputStream::readInt()
  66. {
  67. char temp[4];
  68. if (read (temp, 4) == 4)
  69. return (int) ByteOrder::littleEndianInt (temp);
  70. return 0;
  71. }
  72. int InputStream::readIntBigEndian()
  73. {
  74. char temp[4];
  75. if (read (temp, 4) == 4)
  76. return (int) ByteOrder::bigEndianInt (temp);
  77. return 0;
  78. }
  79. int InputStream::readCompressedInt()
  80. {
  81. auto sizeByte = (uint8) readByte();
  82. if (sizeByte == 0)
  83. return 0;
  84. const int numBytes = (sizeByte & 0x7f);
  85. if (numBytes > 4)
  86. {
  87. jassertfalse; // trying to read corrupt data - this method must only be used
  88. // to read data that was written by OutputStream::writeCompressedInt()
  89. return 0;
  90. }
  91. char bytes[4] = {};
  92. if (read (bytes, numBytes) != numBytes)
  93. return 0;
  94. auto num = (int) ByteOrder::littleEndianInt (bytes);
  95. return (sizeByte >> 7) ? -num : num;
  96. }
  97. int64 InputStream::readInt64()
  98. {
  99. union { uint8 asBytes[8]; uint64 asInt64; } n;
  100. if (read (n.asBytes, 8) == 8)
  101. return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
  102. return 0;
  103. }
  104. int64 InputStream::readInt64BigEndian()
  105. {
  106. union { uint8 asBytes[8]; uint64 asInt64; } n;
  107. if (read (n.asBytes, 8) == 8)
  108. return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
  109. return 0;
  110. }
  111. float InputStream::readFloat()
  112. {
  113. static_assert (sizeof (int32) == sizeof (float), "Union assumes float has the same size as an int32");
  114. union { int32 asInt; float asFloat; } n;
  115. n.asInt = (int32) readInt();
  116. return n.asFloat;
  117. }
  118. float InputStream::readFloatBigEndian()
  119. {
  120. union { int32 asInt; float asFloat; } n;
  121. n.asInt = (int32) readIntBigEndian();
  122. return n.asFloat;
  123. }
  124. double InputStream::readDouble()
  125. {
  126. union { int64 asInt; double asDouble; } n;
  127. n.asInt = readInt64();
  128. return n.asDouble;
  129. }
  130. double InputStream::readDoubleBigEndian()
  131. {
  132. union { int64 asInt; double asDouble; } n;
  133. n.asInt = readInt64BigEndian();
  134. return n.asDouble;
  135. }
  136. String InputStream::readString()
  137. {
  138. MemoryOutputStream buffer;
  139. for (;;)
  140. {
  141. auto c = readByte();
  142. buffer.writeByte (c);
  143. if (c == 0)
  144. return buffer.toUTF8();
  145. }
  146. }
  147. String InputStream::readNextLine()
  148. {
  149. MemoryOutputStream buffer;
  150. for (;;)
  151. {
  152. auto c = readByte();
  153. if (c == 0 || c == '\n')
  154. break;
  155. if (c == '\r')
  156. {
  157. auto lastPos = getPosition();
  158. if (readByte() != '\n')
  159. setPosition (lastPos);
  160. break;
  161. }
  162. buffer.writeByte (c);
  163. }
  164. return buffer.toUTF8();
  165. }
  166. size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
  167. {
  168. MemoryOutputStream mo (block, true);
  169. return (size_t) mo.writeFromInputStream (*this, numBytes);
  170. }
  171. String InputStream::readEntireStreamAsString()
  172. {
  173. MemoryOutputStream mo;
  174. mo << *this;
  175. return mo.toString();
  176. }
  177. //==============================================================================
  178. void InputStream::skipNextBytes (int64 numBytesToSkip)
  179. {
  180. if (numBytesToSkip > 0)
  181. {
  182. auto skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
  183. HeapBlock<char> temp (skipBufferSize);
  184. while (numBytesToSkip > 0 && ! isExhausted())
  185. numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
  186. }
  187. }
  188. } // namespace juce