LMMS
Loading...
Searching...
No Matches
InputStream.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
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>
7
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/
10
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.
14
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.
22
23 ==============================================================================
24*/
25
26#include "InputStream.h"
27#include "../memory/ByteOrder.h"
29
30namespace water {
31
33{
34 int64 len = getTotalLength();
35
36 if (len >= 0)
37 len -= getPosition();
38
39 return len;
40}
41
43{
44 char temp = 0;
45 read (&temp, 1);
46 return temp;
47}
48
50{
51 return readByte() != 0;
52}
53
55{
56 char temp[2];
57
58 if (read (temp, 2) == 2)
59 return (short) ByteOrder::littleEndianShort (temp);
60
61 return 0;
62}
63
65{
66 char temp[2];
67
68 if (read (temp, 2) == 2)
69 return (short) ByteOrder::bigEndianShort (temp);
70
71 return 0;
72}
73
75{
76 char temp[4];
77
78 if (read (temp, 4) == 4)
79 return (int) ByteOrder::littleEndianInt (temp);
80
81 return 0;
82}
83
85{
86 char temp[4];
87
88 if (read (temp, 4) == 4)
89 return (int) ByteOrder::bigEndianInt (temp);
90
91 return 0;
92}
93
95{
96 const uint8 sizeByte = (uint8) readByte();
97 if (sizeByte == 0)
98 return 0;
99
100 const int numBytes = (sizeByte & 0x7f);
101 if (numBytes > 4)
102 {
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;
106 }
107
108 char bytes[4] = { 0, 0, 0, 0 };
109 if (read (bytes, numBytes) != numBytes)
110 return 0;
111
112 const int num = (int) ByteOrder::littleEndianInt (bytes);
113 return (sizeByte >> 7) ? -num : num;
114}
115
117{
118 union { uint8 asBytes[8]; uint64 asInt64; } n;
119
120 if (read (n.asBytes, 8) == 8)
121 return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
122
123 return 0;
124}
125
127{
128 union { uint8 asBytes[8]; uint64 asInt64; } n;
129
130 if (read (n.asBytes, 8) == 8)
131 return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
132
133 return 0;
134}
135
137{
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;
145}
146
148{
149 union { int32 asInt; float asFloat; } n;
150 n.asInt = (int32) readIntBigEndian();
151 return n.asFloat;
152}
153
155{
156 union { int64 asInt; double asDouble; } n;
157 n.asInt = readInt64();
158 return n.asDouble;
159}
160
162{
163 union { int64 asInt; double asDouble; } n;
164 n.asInt = readInt64BigEndian();
165 return n.asDouble;
166}
167
169{
170 MemoryBlock buffer (256);
171 char* data = static_cast<char*> (buffer.getData());
172 size_t i = 0;
173
174 while ((data[i] = readByte()) != 0)
175 {
176 if (++i >= buffer.getSize())
177 {
178 buffer.setSize (buffer.getSize() + 512);
179 data = static_cast<char*> (buffer.getData());
180 }
181 }
182
183 return String::fromUTF8 (data, (int) i);
184}
185
187{
188 MemoryBlock buffer (256);
189 char* data = static_cast<char*> (buffer.getData());
190 size_t i = 0;
191
192 while ((data[i] = readByte()) != 0)
193 {
194 if (data[i] == '\n')
195 break;
196
197 if (data[i] == '\r')
198 {
199 const int64 lastPos = getPosition();
200
201 if (readByte() != '\n')
202 setPosition (lastPos);
203
204 break;
205 }
206
207 if (++i >= buffer.getSize())
208 {
209 buffer.setSize (buffer.getSize() + 512);
210 data = static_cast<char*> (buffer.getData());
211 }
212 }
213
214 return String::fromUTF8 (data, (int) i);
215}
216
217size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
218{
219 MemoryOutputStream mo (block, true);
220 return (size_t) mo.writeFromInputStream (*this, numBytes);
221}
222
224{
226 mo << *this;
227 return mo.toString();
228}
229
230//==============================================================================
232{
233 if (numBytesToSkip > 0)
234 {
235 const int skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
236 HeapBlock<char> temp;
237 temp.malloc((size_t) skipBufferSize); // another FIXME
238
239 while (numBytesToSkip > 0 && ! isExhausted())
240 numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
241 }
242}
243
244}
static uint32 littleEndianInt(const void *bytes) noexcept
Definition ByteOrder.h:236
static uint16 swapIfLittleEndian(uint16 value) noexcept
Definition ByteOrder.h:227
static uint16 bigEndianShort(const void *bytes) noexcept
Definition ByteOrder.h:241
static uint32 bigEndianInt(const void *bytes) noexcept
Definition ByteOrder.h:239
static uint16 littleEndianShort(const void *bytes) noexcept
Definition ByteOrder.h:238
static uint16 swapIfBigEndian(uint16 value) noexcept
Definition ByteOrder.h:218
Definition HeapBlock.h:77
bool malloc(const size_t newNumElements, const size_t elementSize=sizeof(ElementType)) noexcept
Definition HeapBlock.h:171
virtual int64 readInt64BigEndian()
Definition InputStream.cpp:126
virtual short readShortBigEndian()
Definition InputStream.cpp:64
virtual short readShort()
Definition InputStream.cpp:54
virtual int read(void *destBuffer, int maxBytesToRead)=0
virtual int64 getTotalLength()=0
virtual int64 getPosition()=0
virtual void skipNextBytes(int64 numBytesToSkip)
Definition InputStream.cpp:231
virtual int readIntBigEndian()
Definition InputStream.cpp:84
virtual size_t readIntoMemoryBlock(MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1)
Definition InputStream.cpp:217
virtual bool isExhausted()=0
virtual float readFloat()
Definition InputStream.cpp:136
virtual double readDouble()
Definition InputStream.cpp:154
virtual int readInt()
Definition InputStream.cpp:74
virtual String readEntireStreamAsString()
Definition InputStream.cpp:223
virtual String readNextLine()
Definition InputStream.cpp:186
virtual int readCompressedInt()
Definition InputStream.cpp:94
virtual char readByte()
Definition InputStream.cpp:42
virtual String readString()
Definition InputStream.cpp:168
virtual double readDoubleBigEndian()
Definition InputStream.cpp:161
virtual bool readBool()
Definition InputStream.cpp:49
int64 getNumBytesRemaining()
Definition InputStream.cpp:32
virtual bool setPosition(int64 newPosition)=0
virtual float readFloatBigEndian()
Definition InputStream.cpp:147
virtual int64 readInt64()
Definition InputStream.cpp:116
Definition MemoryBlock.h:39
Definition MemoryOutputStream.h:42
Definition String.h:48
static String fromUTF8(const char *utf8buffer, int bufferSizeBytes=-1)
Definition String.cpp:1961
register unsigned i
Definition inflate.c:1575
JSAMPIMAGE data
Definition jpeglib.h:945
#define wassertfalse
#define static_wassert(expression)
Definition AudioSampleBuffer.h:33
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
unsigned long long uint64
Definition water.h:102
long long int64
Definition water.h:100
unsigned char uint8
Definition water.h:90
signed int int32
Definition water.h:96
int n
Definition crypt.c:458
typedef int(UZ_EXP MsgFn)()
mo
Definition zipinfo.c:2287