Audio plugin host https://kx.studio/carla
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.

231 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "InputStream.h"
  21. #include "../memory/ByteOrder.h"
  22. #include "../streams/MemoryOutputStream.h"
  23. namespace water {
  24. int64 InputStream::getNumBytesRemaining()
  25. {
  26. int64 len = getTotalLength();
  27. if (len >= 0)
  28. len -= getPosition();
  29. return len;
  30. }
  31. char InputStream::readByte()
  32. {
  33. char temp = 0;
  34. read (&temp, 1);
  35. return temp;
  36. }
  37. bool InputStream::readBool()
  38. {
  39. return readByte() != 0;
  40. }
  41. short InputStream::readShort()
  42. {
  43. char temp[2];
  44. if (read (temp, 2) == 2)
  45. return (short) ByteOrder::littleEndianShort (temp);
  46. return 0;
  47. }
  48. short InputStream::readShortBigEndian()
  49. {
  50. char temp[2];
  51. if (read (temp, 2) == 2)
  52. return (short) ByteOrder::bigEndianShort (temp);
  53. return 0;
  54. }
  55. int InputStream::readInt()
  56. {
  57. char temp[4];
  58. if (read (temp, 4) == 4)
  59. return (int) ByteOrder::littleEndianInt (temp);
  60. return 0;
  61. }
  62. int InputStream::readIntBigEndian()
  63. {
  64. char temp[4];
  65. if (read (temp, 4) == 4)
  66. return (int) ByteOrder::bigEndianInt (temp);
  67. return 0;
  68. }
  69. int InputStream::readCompressedInt()
  70. {
  71. const uint8 sizeByte = (uint8) readByte();
  72. if (sizeByte == 0)
  73. return 0;
  74. const int numBytes = (sizeByte & 0x7f);
  75. if (numBytes > 4)
  76. {
  77. jassertfalse; // trying to read corrupt data - this method must only be used
  78. // to read data that was written by OutputStream::writeCompressedInt()
  79. return 0;
  80. }
  81. char bytes[4] = { 0, 0, 0, 0 };
  82. if (read (bytes, numBytes) != numBytes)
  83. return 0;
  84. const int num = (int) ByteOrder::littleEndianInt (bytes);
  85. return (sizeByte >> 7) ? -num : num;
  86. }
  87. int64 InputStream::readInt64()
  88. {
  89. union { uint8 asBytes[8]; uint64 asInt64; } n;
  90. if (read (n.asBytes, 8) == 8)
  91. return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
  92. return 0;
  93. }
  94. int64 InputStream::readInt64BigEndian()
  95. {
  96. union { uint8 asBytes[8]; uint64 asInt64; } n;
  97. if (read (n.asBytes, 8) == 8)
  98. return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
  99. return 0;
  100. }
  101. float InputStream::readFloat()
  102. {
  103. // the union below relies on these types being the same size...
  104. #ifdef CARLA_PROPER_CPP11_SUPPORT
  105. static_jassert (sizeof (int32) == sizeof (float));
  106. #endif
  107. union { int32 asInt; float asFloat; } n;
  108. n.asInt = (int32) readInt();
  109. return n.asFloat;
  110. }
  111. float InputStream::readFloatBigEndian()
  112. {
  113. union { int32 asInt; float asFloat; } n;
  114. n.asInt = (int32) readIntBigEndian();
  115. return n.asFloat;
  116. }
  117. double InputStream::readDouble()
  118. {
  119. union { int64 asInt; double asDouble; } n;
  120. n.asInt = readInt64();
  121. return n.asDouble;
  122. }
  123. double InputStream::readDoubleBigEndian()
  124. {
  125. union { int64 asInt; double asDouble; } n;
  126. n.asInt = readInt64BigEndian();
  127. return n.asDouble;
  128. }
  129. String InputStream::readString()
  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 (++i >= buffer.getSize())
  137. {
  138. buffer.setSize (buffer.getSize() + 512);
  139. data = static_cast<char*> (buffer.getData());
  140. }
  141. }
  142. return String::fromUTF8 (data, (int) i);
  143. }
  144. String InputStream::readNextLine()
  145. {
  146. MemoryBlock buffer (256);
  147. char* data = static_cast<char*> (buffer.getData());
  148. size_t i = 0;
  149. while ((data[i] = readByte()) != 0)
  150. {
  151. if (data[i] == '\n')
  152. break;
  153. if (data[i] == '\r')
  154. {
  155. const int64 lastPos = getPosition();
  156. if (readByte() != '\n')
  157. setPosition (lastPos);
  158. break;
  159. }
  160. if (++i >= buffer.getSize())
  161. {
  162. buffer.setSize (buffer.getSize() + 512);
  163. data = static_cast<char*> (buffer.getData());
  164. }
  165. }
  166. return String::fromUTF8 (data, (int) i);
  167. }
  168. size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
  169. {
  170. MemoryOutputStream mo (block, true);
  171. return (size_t) mo.writeFromInputStream (*this, numBytes);
  172. }
  173. String InputStream::readEntireStreamAsString()
  174. {
  175. MemoryOutputStream mo;
  176. mo << *this;
  177. return mo.toString();
  178. }
  179. }