Add initial bits for Qt6 support
[carla.git] / source / modules / water / streams / MemoryInputStream.cpp
blob5c65016d619387537584c073bc84140fd1a8dca3
1 /*
2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 - ROLI Ltd.
6 Copyright (C) 2021 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 "MemoryInputStream.h"
28 namespace water {
30 MemoryInputStream::MemoryInputStream (const void* const sourceData,
31 const size_t sourceDataSize,
32 const bool keepInternalCopy)
33 : data (sourceData),
34 dataSize (sourceDataSize),
35 position (0)
37 if (keepInternalCopy)
38 createInternalCopy();
41 MemoryInputStream::MemoryInputStream (const MemoryBlock& sourceData,
42 const bool keepInternalCopy)
43 : data (sourceData.getData()),
44 dataSize (sourceData.getSize()),
45 position (0)
47 if (keepInternalCopy)
48 createInternalCopy();
51 void MemoryInputStream::createInternalCopy()
53 internalCopy.malloc (dataSize);
54 memcpy (internalCopy, data, dataSize);
55 data = internalCopy;
58 MemoryInputStream::~MemoryInputStream()
62 int64 MemoryInputStream::getTotalLength()
64 return (int64) dataSize;
67 int MemoryInputStream::read (void* const buffer, const int howMany)
69 wassert (buffer != nullptr && howMany >= 0);
71 if (howMany <= 0 || position >= dataSize)
72 return 0;
74 const size_t num = jmin ((size_t) howMany, dataSize - position);
76 if (num > 0)
78 memcpy (buffer, addBytesToPointer (data, position), num);
79 position += num;
82 return (int) num;
85 bool MemoryInputStream::isExhausted()
87 return position >= dataSize;
90 bool MemoryInputStream::setPosition (const int64 pos)
92 position = (size_t) jlimit ((int64) 0, (int64) dataSize, pos);
93 return true;
96 int64 MemoryInputStream::getPosition()
98 return (int64) position;
102 //==============================================================================
103 #if WATER_UNIT_TESTS
105 class MemoryStreamTests : public UnitTest
107 public:
108 MemoryStreamTests() : UnitTest ("MemoryInputStream & MemoryOutputStream") {}
110 void runTest() override
112 beginTest ("Basics");
113 Random r = getRandom();
115 int randomInt = r.nextInt();
116 int64 randomInt64 = r.nextInt64();
117 double randomDouble = r.nextDouble();
118 String randomString (createRandomWideCharString (r));
120 MemoryOutputStream mo;
121 mo.writeInt (randomInt);
122 mo.writeIntBigEndian (randomInt);
123 mo.writeCompressedInt (randomInt);
124 mo.writeString (randomString);
125 mo.writeInt64 (randomInt64);
126 mo.writeInt64BigEndian (randomInt64);
127 mo.writeDouble (randomDouble);
128 mo.writeDoubleBigEndian (randomDouble);
130 MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
131 expect (mi.readInt() == randomInt);
132 expect (mi.readIntBigEndian() == randomInt);
133 expect (mi.readCompressedInt() == randomInt);
134 expectEquals (mi.readString(), randomString);
135 expect (mi.readInt64() == randomInt64);
136 expect (mi.readInt64BigEndian() == randomInt64);
137 expect (mi.readDouble() == randomDouble);
138 expect (mi.readDoubleBigEndian() == randomDouble);
141 static String createRandomWideCharString (Random& r)
143 water_wchar buffer [50] = { 0 };
145 for (int i = 0; i < numElementsInArray (buffer) - 1; ++i)
147 if (r.nextBool())
151 buffer[i] = (water_wchar) (1 + r.nextInt (0x10ffff - 1));
153 while (! CharPointer_UTF16::canRepresent (buffer[i]));
155 else
156 buffer[i] = (water_wchar) (1 + r.nextInt (0xff));
159 return CharPointer_UTF32 (buffer);
163 static MemoryStreamTests memoryInputStreamUnitTests;
165 #endif