Add initial bits for Qt6 support
[carla.git] / source / modules / water / streams / InputStream.cpp
blobf2bf2b5e22bc6ece774243415b557e1db33edcb0
1 /*
2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #include "InputStream.h"
27 #include "../memory/ByteOrder.h"
28 #include "../streams/MemoryOutputStream.h"
30 namespace water {
32 int64 InputStream::getNumBytesRemaining()
34 int64 len = getTotalLength();
36 if (len >= 0)
37 len -= getPosition();
39 return len;
42 char InputStream::readByte()
44 char temp = 0;
45 read (&temp, 1);
46 return temp;
49 bool InputStream::readBool()
51 return readByte() != 0;
54 short InputStream::readShort()
56 char temp[2];
58 if (read (temp, 2) == 2)
59 return (short) ByteOrder::littleEndianShort (temp);
61 return 0;
64 short InputStream::readShortBigEndian()
66 char temp[2];
68 if (read (temp, 2) == 2)
69 return (short) ByteOrder::bigEndianShort (temp);
71 return 0;
74 int InputStream::readInt()
76 char temp[4];
78 if (read (temp, 4) == 4)
79 return (int) ByteOrder::littleEndianInt (temp);
81 return 0;
84 int InputStream::readIntBigEndian()
86 char temp[4];
88 if (read (temp, 4) == 4)
89 return (int) ByteOrder::bigEndianInt (temp);
91 return 0;
94 int InputStream::readCompressedInt()
96 const uint8 sizeByte = (uint8) readByte();
97 if (sizeByte == 0)
98 return 0;
100 const int numBytes = (sizeByte & 0x7f);
101 if (numBytes > 4)
103 wassertfalse; // trying to read corrupt data - this method must only be used
104 // to read data that was written by OutputStream::writeCompressedInt()
105 return 0;
108 char bytes[4] = { 0, 0, 0, 0 };
109 if (read (bytes, numBytes) != numBytes)
110 return 0;
112 const int num = (int) ByteOrder::littleEndianInt (bytes);
113 return (sizeByte >> 7) ? -num : num;
116 int64 InputStream::readInt64()
118 union { uint8 asBytes[8]; uint64 asInt64; } n;
120 if (read (n.asBytes, 8) == 8)
121 return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
123 return 0;
126 int64 InputStream::readInt64BigEndian()
128 union { uint8 asBytes[8]; uint64 asInt64; } n;
130 if (read (n.asBytes, 8) == 8)
131 return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
133 return 0;
136 float InputStream::readFloat()
138 // the union below relies on these types being the same size...
139 #ifdef CARLA_PROPER_CPP11_SUPPORT
140 static_wassert (sizeof (int32) == sizeof (float));
141 #endif
142 union { int32 asInt; float asFloat; } n;
143 n.asInt = (int32) readInt();
144 return n.asFloat;
147 float InputStream::readFloatBigEndian()
149 union { int32 asInt; float asFloat; } n;
150 n.asInt = (int32) readIntBigEndian();
151 return n.asFloat;
154 double InputStream::readDouble()
156 union { int64 asInt; double asDouble; } n;
157 n.asInt = readInt64();
158 return n.asDouble;
161 double InputStream::readDoubleBigEndian()
163 union { int64 asInt; double asDouble; } n;
164 n.asInt = readInt64BigEndian();
165 return n.asDouble;
168 String InputStream::readString()
170 MemoryBlock buffer (256);
171 char* data = static_cast<char*> (buffer.getData());
172 size_t i = 0;
174 while ((data[i] = readByte()) != 0)
176 if (++i >= buffer.getSize())
178 buffer.setSize (buffer.getSize() + 512);
179 data = static_cast<char*> (buffer.getData());
183 return String::fromUTF8 (data, (int) i);
186 String InputStream::readNextLine()
188 MemoryBlock buffer (256);
189 char* data = static_cast<char*> (buffer.getData());
190 size_t i = 0;
192 while ((data[i] = readByte()) != 0)
194 if (data[i] == '\n')
195 break;
197 if (data[i] == '\r')
199 const int64 lastPos = getPosition();
201 if (readByte() != '\n')
202 setPosition (lastPos);
204 break;
207 if (++i >= buffer.getSize())
209 buffer.setSize (buffer.getSize() + 512);
210 data = static_cast<char*> (buffer.getData());
214 return String::fromUTF8 (data, (int) i);
217 size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
219 MemoryOutputStream mo (block, true);
220 return (size_t) mo.writeFromInputStream (*this, numBytes);
223 String InputStream::readEntireStreamAsString()
225 MemoryOutputStream mo;
226 mo << *this;
227 return mo.toString();
230 //==============================================================================
231 void InputStream::skipNextBytes (int64 numBytesToSkip)
233 if (numBytesToSkip > 0)
235 const int skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
236 HeapBlock<char> temp;
237 temp.malloc((size_t) skipBufferSize); // another FIXME
239 while (numBytesToSkip > 0 && ! isExhausted())
240 numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));