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.

230 lines
5.5KB

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