mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Simplified and tidied up a few things in input/output streams and MemoryBlock
This commit is contained in:
parent
ebdbc29176
commit
a7c56fd89b
4 changed files with 73 additions and 81 deletions
|
|
@ -25,7 +25,7 @@ namespace juce
|
|||
|
||||
int64 InputStream::getNumBytesRemaining()
|
||||
{
|
||||
int64 len = getTotalLength();
|
||||
auto len = getTotalLength();
|
||||
|
||||
if (len >= 0)
|
||||
len -= getPosition();
|
||||
|
|
@ -87,23 +87,26 @@ int InputStream::readIntBigEndian()
|
|||
|
||||
int InputStream::readCompressedInt()
|
||||
{
|
||||
const uint8 sizeByte = (uint8) readByte();
|
||||
auto sizeByte = (uint8) readByte();
|
||||
|
||||
if (sizeByte == 0)
|
||||
return 0;
|
||||
|
||||
const int numBytes = (sizeByte & 0x7f);
|
||||
|
||||
if (numBytes > 4)
|
||||
{
|
||||
jassertfalse; // trying to read corrupt data - this method must only be used
|
||||
jassertfalse; // trying to read corrupt data - this method must only be used
|
||||
// to read data that was written by OutputStream::writeCompressedInt()
|
||||
return 0;
|
||||
}
|
||||
|
||||
char bytes[4] = { 0, 0, 0, 0 };
|
||||
char bytes[4] = {};
|
||||
|
||||
if (read (bytes, numBytes) != numBytes)
|
||||
return 0;
|
||||
|
||||
const int num = (int) ByteOrder::littleEndianInt (bytes);
|
||||
auto num = (int) ByteOrder::littleEndianInt (bytes);
|
||||
return (sizeByte >> 7) ? -num : num;
|
||||
}
|
||||
|
||||
|
|
@ -158,51 +161,42 @@ double InputStream::readDoubleBigEndian()
|
|||
|
||||
String InputStream::readString()
|
||||
{
|
||||
MemoryBlock buffer (256);
|
||||
char* data = static_cast<char*> (buffer.getData());
|
||||
size_t i = 0;
|
||||
MemoryOutputStream buffer;
|
||||
|
||||
while ((data[i] = readByte()) != 0)
|
||||
for (;;)
|
||||
{
|
||||
if (++i >= buffer.getSize())
|
||||
{
|
||||
buffer.setSize (buffer.getSize() + 512);
|
||||
data = static_cast<char*> (buffer.getData());
|
||||
}
|
||||
}
|
||||
auto c = readByte();
|
||||
buffer.writeByte (c);
|
||||
|
||||
return String::fromUTF8 (data, (int) i);
|
||||
if (c == 0)
|
||||
return buffer.toUTF8();
|
||||
}
|
||||
}
|
||||
|
||||
String InputStream::readNextLine()
|
||||
{
|
||||
MemoryBlock buffer (256);
|
||||
auto* data = static_cast<char*> (buffer.getData());
|
||||
size_t i = 0;
|
||||
MemoryOutputStream buffer;
|
||||
|
||||
while ((data[i] = readByte()) != 0)
|
||||
for (;;)
|
||||
{
|
||||
if (data[i] == '\n')
|
||||
auto c = readByte();
|
||||
buffer.writeByte (c);
|
||||
|
||||
if (c == 0 || c == '\n')
|
||||
break;
|
||||
|
||||
if (data[i] == '\r')
|
||||
if (c == '\r')
|
||||
{
|
||||
const int64 lastPos = getPosition();
|
||||
auto lastPos = getPosition();
|
||||
|
||||
if (readByte() != '\n')
|
||||
setPosition (lastPos);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (++i >= buffer.getSize())
|
||||
{
|
||||
buffer.setSize (buffer.getSize() + 512);
|
||||
data = static_cast<char*> (buffer.getData());
|
||||
}
|
||||
}
|
||||
|
||||
return String::fromUTF8 (data, (int) i);
|
||||
return buffer.toUTF8();
|
||||
}
|
||||
|
||||
size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ OutputStream::~OutputStream()
|
|||
}
|
||||
|
||||
//==============================================================================
|
||||
bool OutputStream::writeBool (const bool b)
|
||||
bool OutputStream::writeBool (bool b)
|
||||
{
|
||||
return writeByte (b ? (char) 1
|
||||
: (char) 0);
|
||||
|
|
@ -85,32 +85,32 @@ bool OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat)
|
|||
|
||||
bool OutputStream::writeShort (short value)
|
||||
{
|
||||
const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value);
|
||||
auto v = ByteOrder::swapIfBigEndian ((uint16) value);
|
||||
return write (&v, 2);
|
||||
}
|
||||
|
||||
bool OutputStream::writeShortBigEndian (short value)
|
||||
{
|
||||
const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value);
|
||||
auto v = ByteOrder::swapIfLittleEndian ((uint16) value);
|
||||
return write (&v, 2);
|
||||
}
|
||||
|
||||
bool OutputStream::writeInt (int value)
|
||||
{
|
||||
const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value);
|
||||
auto v = ByteOrder::swapIfBigEndian ((uint32) value);
|
||||
return write (&v, 4);
|
||||
}
|
||||
|
||||
bool OutputStream::writeIntBigEndian (int value)
|
||||
{
|
||||
const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value);
|
||||
auto v = ByteOrder::swapIfLittleEndian ((uint32) value);
|
||||
return write (&v, 4);
|
||||
}
|
||||
|
||||
bool OutputStream::writeCompressedInt (int value)
|
||||
{
|
||||
unsigned int un = (value < 0) ? (unsigned int) -value
|
||||
: (unsigned int) value;
|
||||
auto un = (value < 0) ? (unsigned int) -value
|
||||
: (unsigned int) value;
|
||||
|
||||
uint8 data[5];
|
||||
int num = 0;
|
||||
|
|
@ -131,13 +131,13 @@ bool OutputStream::writeCompressedInt (int value)
|
|||
|
||||
bool OutputStream::writeInt64 (int64 value)
|
||||
{
|
||||
const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value);
|
||||
auto v = ByteOrder::swapIfBigEndian ((uint64) value);
|
||||
return write (&v, 8);
|
||||
}
|
||||
|
||||
bool OutputStream::writeInt64BigEndian (int64 value)
|
||||
{
|
||||
const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value);
|
||||
auto v = ByteOrder::swapIfLittleEndian ((uint64) value);
|
||||
return write (&v, 8);
|
||||
}
|
||||
|
||||
|
|
@ -183,20 +183,19 @@ bool OutputStream::writeString (const String& text)
|
|||
#endif
|
||||
}
|
||||
|
||||
bool OutputStream::writeText (const String& text, const bool asUTF16,
|
||||
const bool writeUTF16ByteOrderMark)
|
||||
bool OutputStream::writeText (const String& text, bool asUTF16, bool writeUTF16ByteOrderMark)
|
||||
{
|
||||
if (asUTF16)
|
||||
{
|
||||
if (writeUTF16ByteOrderMark)
|
||||
write ("\x0ff\x0fe", 2);
|
||||
|
||||
String::CharPointerType src (text.getCharPointer());
|
||||
auto src = text.getCharPointer();
|
||||
bool lastCharWasReturn = false;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
const juce_wchar c = src.getAndAdvance();
|
||||
auto c = src.getAndAdvance();
|
||||
|
||||
if (c == 0)
|
||||
break;
|
||||
|
|
@ -213,7 +212,7 @@ bool OutputStream::writeText (const String& text, const bool asUTF16,
|
|||
else
|
||||
{
|
||||
const char* src = text.toUTF8();
|
||||
const char* t = src;
|
||||
auto* t = src;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
|
|
@ -258,8 +257,8 @@ int64 OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToW
|
|||
|
||||
while (numBytesToWrite > 0)
|
||||
{
|
||||
char buffer [8192];
|
||||
const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));
|
||||
char buffer[8192];
|
||||
auto num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));
|
||||
|
||||
if (num <= 0)
|
||||
break;
|
||||
|
|
@ -283,7 +282,7 @@ void OutputStream::setNewLineString (const String& newLineString_)
|
|||
template <typename IntegerType>
|
||||
static void writeIntToStream (OutputStream& stream, IntegerType number)
|
||||
{
|
||||
char buffer [NumberToStringConverters::charsNeededForInt];
|
||||
char buffer[NumberToStringConverters::charsNeededForInt];
|
||||
char* end = buffer + numElementsInArray (buffer);
|
||||
const char* start = NumberToStringConverters::numberToString (end, number);
|
||||
stream.write (start, (size_t) (end - start - 1));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue