From a4aca30079e7e2be99211d11490b4911215a8336 Mon Sep 17 00:00:00 2001 From: Julian Storer Date: Mon, 5 Jul 2010 13:37:38 +0100 Subject: [PATCH] Refactored the MemoryOutputStream constructors and added some new features. Tidied up some stream methods to avoid relying on the stream length, which can be unreliable in http streams. Tweaked some mac atomic functions. Fixed some win32 dllexport declarations. --- .../Source/Utility/jucer_FileHelpers.cpp | 2 +- .../Source/Utility/jucer_FileHelpers.h | 2 +- src/application/juce_Application.cpp | 2 + .../juce_ApplicationCommandTarget.cpp | 5 ++ .../juce_ApplicationCommandTarget.h | 4 +- src/containers/juce_Variant.cpp | 7 +- src/core/juce_Atomic.h | 20 ++--- src/core/juce_StandardHeader.h | 2 +- src/gui/graphics/drawables/juce_Drawable.cpp | 7 +- src/gui/graphics/geometry/juce_Path.cpp | 2 +- .../image_file_formats/juce_JPEGLoader.cpp | 10 +-- src/io/network/juce_URL.cpp | 4 +- src/io/streams/juce_InputStream.cpp | 58 ++----------- src/io/streams/juce_MemoryOutputStream.cpp | 82 +++++++++++++------ src/io/streams/juce_MemoryOutputStream.h | 55 +++++++++---- src/native/linux/juce_linux_NativeIncludes.h | 2 +- src/text/juce_String.cpp | 64 +++++++-------- src/text/juce_String.h | 66 +++++++-------- src/text/juce_XmlDocument.cpp | 18 +--- src/text/juce_XmlElement.cpp | 2 +- 20 files changed, 213 insertions(+), 201 deletions(-) diff --git a/extras/Jucer (experimental)/Source/Utility/jucer_FileHelpers.cpp b/extras/Jucer (experimental)/Source/Utility/jucer_FileHelpers.cpp index 5aebac4fc3..cf2d5252cf 100644 --- a/extras/Jucer (experimental)/Source/Utility/jucer_FileHelpers.cpp +++ b/extras/Jucer (experimental)/Source/Utility/jucer_FileHelpers.cpp @@ -43,7 +43,7 @@ namespace FileHelpers return stream != 0 ? calculateStreamHashCode (*stream) : 0; } - bool overwriteFileWithNewDataIfDifferent (const File& file, const char* data, int numBytes) + bool overwriteFileWithNewDataIfDifferent (const File& file, const void* data, int numBytes) { if (file.getSize() == numBytes) { diff --git a/extras/Jucer (experimental)/Source/Utility/jucer_FileHelpers.h b/extras/Jucer (experimental)/Source/Utility/jucer_FileHelpers.h index dd19d66aa3..6baa12f3c1 100644 --- a/extras/Jucer (experimental)/Source/Utility/jucer_FileHelpers.h +++ b/extras/Jucer (experimental)/Source/Utility/jucer_FileHelpers.h @@ -33,7 +33,7 @@ namespace FileHelpers int64 calculateStreamHashCode (InputStream& stream); int64 calculateFileHashCode (const File& file); - bool overwriteFileWithNewDataIfDifferent (const File& file, const char* data, int numBytes); + bool overwriteFileWithNewDataIfDifferent (const File& file, const void* data, int numBytes); bool overwriteFileWithNewDataIfDifferent (const File& file, const MemoryOutputStream& newData); bool overwriteFileWithNewDataIfDifferent (const File& file, const String& newData); diff --git a/src/application/juce_Application.cpp b/src/application/juce_Application.cpp index a2c5fca273..c0106aab94 100644 --- a/src/application/juce_Application.cpp +++ b/src/application/juce_Application.cpp @@ -257,6 +257,8 @@ int JUCEApplication::shutdownAppAndClearUp() JUCE_TRY { + app->releaseMessageListener(); + shutdownJuce_GUI(); returnValue = app->getApplicationReturnValue(); diff --git a/src/application/juce_ApplicationCommandTarget.cpp b/src/application/juce_ApplicationCommandTarget.cpp index ca046910f4..962ff9fd0f 100644 --- a/src/application/juce_ApplicationCommandTarget.cpp +++ b/src/application/juce_ApplicationCommandTarget.cpp @@ -37,6 +37,11 @@ ApplicationCommandTarget::ApplicationCommandTarget() } ApplicationCommandTarget::~ApplicationCommandTarget() +{ + releaseMessageListener(); +} + +void ApplicationCommandTarget::releaseMessageListener() { messageInvoker = 0; } diff --git a/src/application/juce_ApplicationCommandTarget.h b/src/application/juce_ApplicationCommandTarget.h index cadeb2dde0..2f3b5c4516 100644 --- a/src/application/juce_ApplicationCommandTarget.h +++ b/src/application/juce_ApplicationCommandTarget.h @@ -232,10 +232,12 @@ public: */ ApplicationCommandTarget* findFirstTargetParentComponent(); - //============================================================================== juce_UseDebuggingNewOperator + /** @internal */ + void releaseMessageListener(); + private: // (for async invocation of commands) class CommandTargetMessageInvoker : public MessageListener diff --git a/src/containers/juce_Variant.cpp b/src/containers/juce_Variant.cpp index f809228a70..4b3a9f4036 100644 --- a/src/containers/juce_Variant.cpp +++ b/src/containers/juce_Variant.cpp @@ -29,6 +29,7 @@ BEGIN_JUCE_NAMESPACE #include "juce_Variant.h" #include "juce_DynamicObject.h" +#include "../io/streams/juce_MemoryOutputStream.h" //============================================================================== @@ -358,9 +359,9 @@ const var var::readFromStream (InputStream& input) case 4: return var (input.readDouble()); case 5: { - MemoryBlock mb; - input.readIntoMemoryBlock (mb, numBytes - 1); - return var (String::fromUTF8 (static_cast (mb.getData()), (int) mb.getSize())); + MemoryOutputStream mo; + mo.writeFromInputStream (input, numBytes - 1); + return var (mo.toUTF8()); } default: input.skipNextBytes (numBytes - 1); break; diff --git a/src/core/juce_Atomic.h b/src/core/juce_Atomic.h index e847415349..5f8f383cd9 100644 --- a/src/core/juce_Atomic.h +++ b/src/core/juce_Atomic.h @@ -144,7 +144,7 @@ public: This is exposed publically in case you need to manipulate it directly for performance reasons. */ - Type value; + volatile Type value; }; @@ -158,9 +158,9 @@ public: #if JUCE_PPC || JUCE_IPHONE // None of these atomics are available for PPC or for iPhoneOS 3.1 or earlier!! - template static Type OSAtomicAdd64 (Type b, volatile Type* a) throw() { jassertfalse; return *a += b; } - template static Type OSAtomicIncrement64 (volatile Type* a) throw() { jassertfalse; return ++*a; } - template static Type OSAtomicDecrement64 (volatile Type* a) throw() { jassertfalse; return --*a; } + template static Type OSAtomicAdd64Barrier (Type b, volatile Type* a) throw() { jassertfalse; return *a += b; } + template static Type OSAtomicIncrement64Barrier (volatile Type* a) throw() { jassertfalse; return ++*a; } + template static Type OSAtomicDecrement64Barrier (volatile Type* a) throw() { jassertfalse; return --*a; } template static bool OSAtomicCompareAndSwap64Barrier (Type old, Type newValue, volatile Type* value) throw() { jassertfalse; if (old == *value) { *value = newValue; return true; } return false; } #define JUCE_64BIT_ATOMICS_UNAVAILABLE 1 @@ -250,8 +250,8 @@ template inline Type Atomic::operator+= (const Type amountToAdd) throw() { #if JUCE_ATOMICS_MAC - return sizeof (Type) == 4 ? (Type) OSAtomicAdd32 ((int32_t) amountToAdd, (int32_t*) &value) - : (Type) OSAtomicAdd64 ((int64_t) amountToAdd, (int64_t*) &value); + return sizeof (Type) == 4 ? (Type) OSAtomicAdd32Barrier ((int32_t) amountToAdd, (int32_t*) &value) + : (Type) OSAtomicAdd64Barrier ((int64_t) amountToAdd, (int64_t*) &value); #elif JUCE_ATOMICS_WINDOWS return sizeof (Type) == 4 ? (Type) (juce_InterlockedExchangeAdd ((volatile long*) &value, (long) amountToAdd) + (long) amountToAdd) : (Type) (juce_InterlockedExchangeAdd64 ((volatile __int64*) &value, (__int64) amountToAdd) + (__int64) amountToAdd); @@ -270,8 +270,8 @@ template inline Type Atomic::operator++() throw() { #if JUCE_ATOMICS_MAC - return sizeof (Type) == 4 ? (Type) OSAtomicIncrement32 ((int32_t*) &value) - : (Type) OSAtomicIncrement64 ((int64_t*) &value); + return sizeof (Type) == 4 ? (Type) OSAtomicIncrement32Barrier ((int32_t*) &value) + : (Type) OSAtomicIncrement64Barrier ((int64_t*) &value); #elif JUCE_ATOMICS_WINDOWS return sizeof (Type) == 4 ? (Type) juce_InterlockedIncrement ((volatile long*) &value) : (Type) juce_InterlockedIncrement64 ((volatile __int64*) &value); @@ -284,8 +284,8 @@ template inline Type Atomic::operator--() throw() { #if JUCE_ATOMICS_MAC - return sizeof (Type) == 4 ? (Type) OSAtomicDecrement32 ((int32_t*) &value) - : (Type) OSAtomicDecrement64 ((int64_t*) &value); + return sizeof (Type) == 4 ? (Type) OSAtomicDecrement32Barrier ((int32_t*) &value) + : (Type) OSAtomicDecrement64Barrier ((int64_t*) &value); #elif JUCE_ATOMICS_WINDOWS return sizeof (Type) == 4 ? (Type) juce_InterlockedDecrement ((volatile long*) &value) : (Type) juce_InterlockedDecrement64 ((volatile __int64*) &value); diff --git a/src/core/juce_StandardHeader.h b/src/core/juce_StandardHeader.h index a35501b76c..934b1679ce 100644 --- a/src/core/juce_StandardHeader.h +++ b/src/core/juce_StandardHeader.h @@ -33,7 +33,7 @@ */ #define JUCE_MAJOR_VERSION 1 #define JUCE_MINOR_VERSION 52 -#define JUCE_BUILDNUMBER 34 +#define JUCE_BUILDNUMBER 35 /** Current Juce version number. diff --git a/src/gui/graphics/drawables/juce_Drawable.cpp b/src/gui/graphics/drawables/juce_Drawable.cpp index fc6b683918..a5dbc224cf 100644 --- a/src/gui/graphics/drawables/juce_Drawable.cpp +++ b/src/gui/graphics/drawables/juce_Drawable.cpp @@ -35,6 +35,7 @@ BEGIN_JUCE_NAMESPACE #include "../imaging/juce_ImageFileFormat.h" #include "../../../text/juce_XmlDocument.h" #include "../../../io/files/juce_FileInputStream.h" +#include "../../../io/streams/juce_MemoryOutputStream.h" //============================================================================== @@ -120,10 +121,10 @@ Drawable* Drawable::createFromImageData (const void* data, const size_t numBytes Drawable* Drawable::createFromImageDataStream (InputStream& dataSource) { - MemoryBlock mb; - dataSource.readIntoMemoryBlock (mb); + MemoryOutputStream mo; + mo.writeFromInputStream (dataSource, -1); - return createFromImageData (mb.getData(), mb.getSize()); + return createFromImageData (mo.getData(), mo.getDataSize()); } Drawable* Drawable::createFromImageFile (const File& file) diff --git a/src/gui/graphics/geometry/juce_Path.cpp b/src/gui/graphics/geometry/juce_Path.cpp index cc6558e256..f35cdd5a14 100644 --- a/src/gui/graphics/geometry/juce_Path.cpp +++ b/src/gui/graphics/geometry/juce_Path.cpp @@ -1385,7 +1385,7 @@ void Path::writePathToStream (OutputStream& dest) const const String Path::toString() const { - MemoryOutputStream s (2048, 2048); + MemoryOutputStream s (2048); if (! useNonZeroWinding) s << 'a'; diff --git a/src/gui/graphics/imaging/image_file_formats/juce_JPEGLoader.cpp b/src/gui/graphics/imaging/image_file_formats/juce_JPEGLoader.cpp index 1bc0ca28af..4f66cd0a55 100644 --- a/src/gui/graphics/imaging/image_file_formats/juce_JPEGLoader.cpp +++ b/src/gui/graphics/imaging/image_file_formats/juce_JPEGLoader.cpp @@ -128,7 +128,7 @@ BEGIN_JUCE_NAMESPACE #include "../juce_ImageFileFormat.h" #include "../../../../io/streams/juce_InputStream.h" -#include "../../../../io/streams/juce_OutputStream.h" +#include "../../../../io/streams/juce_MemoryOutputStream.h" #include "../../colour/juce_PixelFormats.h" //============================================================================== @@ -254,12 +254,12 @@ const Image JPEGImageFormat::decodeImage (InputStream& in) using namespace jpeglibNamespace; using namespace JPEGHelpers; - MemoryBlock mb; - in.readIntoMemoryBlock (mb); + MemoryOutputStream mb; + mb.writeFromInputStream (in, -1); Image image; - if (mb.getSize() > 16) + if (mb.getDataSize() > 16) { struct jpeg_decompress_struct jpegDecompStruct; @@ -279,7 +279,7 @@ const Image JPEGImageFormat::decodeImage (InputStream& in) jpegDecompStruct.src->term_source = dummyCallback1; jpegDecompStruct.src->next_input_byte = static_cast (mb.getData()); - jpegDecompStruct.src->bytes_in_buffer = mb.getSize(); + jpegDecompStruct.src->bytes_in_buffer = mb.getDataSize(); try { diff --git a/src/io/network/juce_URL.cpp b/src/io/network/juce_URL.cpp index e53c611e9c..a384e9d820 100644 --- a/src/io/network/juce_URL.cpp +++ b/src/io/network/juce_URL.cpp @@ -361,7 +361,7 @@ private: void createHeadersAndPostData (const URL& url) { - MemoryOutputStream data (256, 256, &postData); + MemoryOutputStream data (postData, false); if (url.getFilesToUpload().size() > 0) { @@ -444,7 +444,7 @@ bool URL::readEntireBinaryStream (MemoryBlock& destData, if (in != 0) { - in->readIntoMemoryBlock (destData, -1); + in->readIntoMemoryBlock (destData); return true; } diff --git a/src/io/streams/juce_InputStream.cpp b/src/io/streams/juce_InputStream.cpp index 40d27e4076..d9838d6a82 100644 --- a/src/io/streams/juce_InputStream.cpp +++ b/src/io/streams/juce_InputStream.cpp @@ -28,6 +28,7 @@ BEGIN_JUCE_NAMESPACE #include "juce_InputStream.h" +#include "juce_MemoryOutputStream.h" //============================================================================== @@ -206,62 +207,15 @@ const String InputStream::readNextLine() int InputStream::readIntoMemoryBlock (MemoryBlock& block, int numBytes) { - const int64 totalLength = getTotalLength(); - - if (totalLength >= 0) - { - const int totalBytesRemaining = (int) jmin ((int64) 0x7fffffff, - totalLength - getPosition()); - - if (numBytes < 0) - numBytes = totalBytesRemaining; - else if (numBytes > 0) - numBytes = jmin (numBytes, totalBytesRemaining); - else - return 0; - } - - const size_t originalBlockSize = block.getSize(); - int totalBytesRead = 0; - - if (numBytes > 0) - { - // know how many bytes we want, so we can resize the block first.. - block.setSize (originalBlockSize + numBytes, false); - totalBytesRead = read (static_cast (block.getData()) + originalBlockSize, numBytes); - } - else - { - // read until end of stram.. - const int chunkSize = 32768; - - for (;;) - { - block.ensureSize (originalBlockSize + totalBytesRead + chunkSize, false); - - const int bytesJustIn = read (static_cast (block.getData()) - + originalBlockSize - + totalBytesRead, - chunkSize); - - if (bytesJustIn == 0) - break; - - totalBytesRead += bytesJustIn; - } - } - - // trim off any excess left at the end - block.setSize (originalBlockSize + totalBytesRead, false); - return totalBytesRead; + MemoryOutputStream mo (block, true); + return mo.writeFromInputStream (*this, numBytes); } const String InputStream::readEntireStreamAsString() { - MemoryBlock mb; - const int size = readIntoMemoryBlock (mb); - - return String::createStringFromData (static_cast (mb.getData()), size); + MemoryOutputStream mo; + mo.writeFromInputStream (*this, -1); + return mo.toString(); } //============================================================================== diff --git a/src/io/streams/juce_MemoryOutputStream.cpp b/src/io/streams/juce_MemoryOutputStream.cpp index a16ae4e45f..900f346a38 100644 --- a/src/io/streams/juce_MemoryOutputStream.cpp +++ b/src/io/streams/juce_MemoryOutputStream.cpp @@ -31,18 +31,22 @@ BEGIN_JUCE_NAMESPACE //============================================================================== -MemoryOutputStream::MemoryOutputStream (const size_t initialSize, - const size_t blockSizeToIncreaseBy, - MemoryBlock* const memoryBlockToWriteTo) +MemoryOutputStream::MemoryOutputStream (const size_t initialSize) + : data (internalBlock), + position (0), + size (0) +{ + internalBlock.setSize (initialSize, false); +} + +MemoryOutputStream::MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo, + const bool appendToExistingBlockContent) : data (memoryBlockToWriteTo), position (0), - size (0), - blockSize (jmax ((size_t) 16, blockSizeToIncreaseBy)) + size (0) { - if (data == 0) - dataToDelete = data = new MemoryBlock (initialSize); - else - data->setSize (initialSize, false); + if (appendToExistingBlockContent) + position = size = memoryBlockToWriteTo.getSize(); } MemoryOutputStream::~MemoryOutputStream() @@ -52,8 +56,13 @@ MemoryOutputStream::~MemoryOutputStream() void MemoryOutputStream::flush() { - if (dataToDelete == 0) - data->setSize (size, false); + if (&data != &internalBlock) + data.setSize (size, false); +} + +void MemoryOutputStream::preallocate (const size_t bytesToPreallocate) +{ + data.ensureSize (bytesToPreallocate + 1); } void MemoryOutputStream::reset() throw() @@ -66,18 +75,12 @@ bool MemoryOutputStream::write (const void* const buffer, int howMany) { if (howMany > 0) { - size_t storageNeeded = position + howMany; + const size_t storageNeeded = position + howMany; - if (storageNeeded >= data->getSize()) - { - // if we need more space, increase the block by at least 10%.. - storageNeeded += jmax (blockSize, storageNeeded / 10); - storageNeeded = storageNeeded - (storageNeeded % blockSize) + blockSize; + if (storageNeeded >= data.getSize()) + data.ensureSize ((storageNeeded + jmin (storageNeeded / 2, (size_t) (1024 * 1024)) + 32) & ~31); - data->ensureSize (storageNeeded); - } - - data->copyFrom (buffer, (int) position, howMany); + memcpy (static_cast (data.getData()) + position, buffer, howMany); position += howMany; size = jmax (size, position); } @@ -85,12 +88,12 @@ bool MemoryOutputStream::write (const void* const buffer, int howMany) return true; } -const char* MemoryOutputStream::getData() const throw() +const void* MemoryOutputStream::getData() const throw() { - char* const d = static_cast (data->getData()); + void* const d = data.getData(); - if (data->getSize() > size) - d [size] = 0; + if (data.getSize() > size) + static_cast (d) [size] = 0; return d; } @@ -110,9 +113,36 @@ bool MemoryOutputStream::setPosition (int64 newPosition) } } +int MemoryOutputStream::writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite) +{ + // before writing from an input, see if we can preallocate to make it more efficient.. + int64 availableData = source.getTotalLength() - source.getPosition(); + + if (availableData > 0) + { + if (maxNumBytesToWrite > 0 && maxNumBytesToWrite < availableData) + availableData = maxNumBytesToWrite; + + preallocate (data.getSize() + (size_t) maxNumBytesToWrite); + } + + return OutputStream::writeFromInputStream (source, maxNumBytesToWrite); +} + const String MemoryOutputStream::toUTF8() const { - return String (getData(), getDataSize()); + return String (static_cast (getData()), getDataSize()); +} + +const String MemoryOutputStream::toString() const +{ + return String::createStringFromData (getData(), getDataSize()); +} + +OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead) +{ + stream.write (streamToRead.getData(), streamToRead.getDataSize()); + return stream; } END_JUCE_NAMESPACE diff --git a/src/io/streams/juce_MemoryOutputStream.h b/src/io/streams/juce_MemoryOutputStream.h index 3311d69fab..c358eb8cae 100644 --- a/src/io/streams/juce_MemoryOutputStream.h +++ b/src/io/streams/juce_MemoryOutputStream.h @@ -41,21 +41,28 @@ class JUCE_API MemoryOutputStream : public OutputStream { public: //============================================================================== - /** Creates a memory stream ready for writing into. + /** Creates an empty memory stream ready for writing into. - @param initialSize the intial amount of space to allocate for writing into - @param granularity the increments by which the internal storage will be increased - @param memoryBlockToWriteTo if this is non-zero, then this block will be used as the - place that the data gets stored. If it's zero, the stream - will allocate its own storage internally, which you can - access using getData() and getDataSize() + @param initialSize the intial amount of capacity to allocate for writing into */ - MemoryOutputStream (size_t initialSize = 256, - size_t granularity = 256, - MemoryBlock* memoryBlockToWriteTo = 0); + MemoryOutputStream (size_t initialSize = 256); + + /** Creates a memory stream for writing into into a pre-existing MemoryBlock object. + + Note that the destination block will always be larger than the amount of data + that has been written to the stream, because the MemoryOutputStream keeps some + spare capactity at its end. To trim the block's size down to fit the actual + data, call flush(), or delete the MemoryOutputStream. + + @param memoryBlockToWriteTo the block into which new data will be written. + @param appendToExistingBlockContent if this is true, the contents of the block will be + kept, and new data will be appended to it. If false, + the block will be cleared before use + */ + MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo, + bool appendToExistingBlockContent); /** Destructor. - This will free any data that was written to it. */ ~MemoryOutputStream(); @@ -65,7 +72,7 @@ public: @see getDataSize */ - const char* getData() const throw(); + const void* getData() const throw(); /** Returns the number of bytes of data that have been written to the stream. @@ -76,26 +83,46 @@ public: /** Resets the stream, clearing any data that has been written to it so far. */ void reset() throw(); + /** Increases the internal storage capacity to be able to contain at least the specified + amount of data without needing to be resized. + */ + void preallocate (size_t bytesToPreallocate); + /** Returns a String created from the (UTF8) data that has been written to the stream. */ const String toUTF8() const; + /** Attempts to detect the encoding of the data and convert it to a string. + @see String::createStringFromData + */ + const String toString() const; + //============================================================================== + /** If the stream is writing to a user-supplied MemoryBlock, this will trim any excess + capacity off the block, so that its length matches the amount of actual data that + has been written so far. + */ void flush(); + bool write (const void* buffer, int howMany); int64 getPosition() { return position; } bool setPosition (int64 newPosition); + int writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite); //============================================================================== juce_UseDebuggingNewOperator private: - MemoryBlock* data; - ScopedPointer dataToDelete; + MemoryBlock& data; + MemoryBlock internalBlock; size_t position, size, blockSize; MemoryOutputStream (const MemoryOutputStream&); MemoryOutputStream& operator= (const MemoryOutputStream&); }; +/** Copies all the data that has been written to a MemoryOutputStream into another stream. */ +OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead); + + #endif // __JUCE_MEMORYOUTPUTSTREAM_JUCEHEADER__ diff --git a/src/native/linux/juce_linux_NativeIncludes.h b/src/native/linux/juce_linux_NativeIncludes.h index 4692c1664e..c2aea4e64c 100644 --- a/src/native/linux/juce_linux_NativeIncludes.h +++ b/src/native/linux/juce_linux_NativeIncludes.h @@ -55,7 +55,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/text/juce_String.cpp b/src/text/juce_String.cpp index e504ed871d..c3ad771cbe 100644 --- a/src/text/juce_String.cpp +++ b/src/text/juce_String.cpp @@ -494,52 +494,52 @@ int64 String::hashCode64() const throw() } //============================================================================== -bool JUCE_CALLTYPE operator== (const String& string1, const String& string2) throw() +bool JUCE_PUBLIC_FUNCTION operator== (const String& string1, const String& string2) throw() { return string1.compare (string2) == 0; } -bool JUCE_CALLTYPE operator== (const String& string1, const char* string2) throw() +bool JUCE_PUBLIC_FUNCTION operator== (const String& string1, const char* string2) throw() { return string1.compare (string2) == 0; } -bool JUCE_CALLTYPE operator== (const String& string1, const juce_wchar* string2) throw() +bool JUCE_PUBLIC_FUNCTION operator== (const String& string1, const juce_wchar* string2) throw() { return string1.compare (string2) == 0; } -bool JUCE_CALLTYPE operator!= (const String& string1, const String& string2) throw() +bool JUCE_PUBLIC_FUNCTION operator!= (const String& string1, const String& string2) throw() { return string1.compare (string2) != 0; } -bool JUCE_CALLTYPE operator!= (const String& string1, const char* string2) throw() +bool JUCE_PUBLIC_FUNCTION operator!= (const String& string1, const char* string2) throw() { return string1.compare (string2) != 0; } -bool JUCE_CALLTYPE operator!= (const String& string1, const juce_wchar* string2) throw() +bool JUCE_PUBLIC_FUNCTION operator!= (const String& string1, const juce_wchar* string2) throw() { return string1.compare (string2) != 0; } -bool JUCE_CALLTYPE operator> (const String& string1, const String& string2) throw() +bool JUCE_PUBLIC_FUNCTION operator> (const String& string1, const String& string2) throw() { return string1.compare (string2) > 0; } -bool JUCE_CALLTYPE operator< (const String& string1, const String& string2) throw() +bool JUCE_PUBLIC_FUNCTION operator< (const String& string1, const String& string2) throw() { return string1.compare (string2) < 0; } -bool JUCE_CALLTYPE operator>= (const String& string1, const String& string2) throw() +bool JUCE_PUBLIC_FUNCTION operator>= (const String& string1, const String& string2) throw() { return string1.compare (string2) >= 0; } -bool JUCE_CALLTYPE operator<= (const String& string1, const String& string2) throw() +bool JUCE_PUBLIC_FUNCTION operator<= (const String& string1, const String& string2) throw() { return string1.compare (string2) <= 0; } @@ -658,114 +658,114 @@ void String::append (const juce_wchar* const other, const int howMany) } //============================================================================== -const String JUCE_CALLTYPE operator+ (const char* const string1, const String& string2) +const String JUCE_PUBLIC_FUNCTION operator+ (const char* const string1, const String& string2) { String s (string1); return s += string2; } -const String JUCE_CALLTYPE operator+ (const juce_wchar* const string1, const String& string2) +const String JUCE_PUBLIC_FUNCTION operator+ (const juce_wchar* const string1, const String& string2) { String s (string1); return s += string2; } -const String JUCE_CALLTYPE operator+ (const char string1, const String& string2) +const String JUCE_PUBLIC_FUNCTION operator+ (const char string1, const String& string2) { return String::charToString (string1) + string2; } -const String JUCE_CALLTYPE operator+ (const juce_wchar string1, const String& string2) +const String JUCE_PUBLIC_FUNCTION operator+ (const juce_wchar string1, const String& string2) { return String::charToString (string1) + string2; } -const String JUCE_CALLTYPE operator+ (String string1, const String& string2) +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, const String& string2) { return string1 += string2; } -const String JUCE_CALLTYPE operator+ (String string1, const char* const string2) +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, const char* const string2) { return string1 += string2; } -const String JUCE_CALLTYPE operator+ (String string1, const juce_wchar* const string2) +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, const juce_wchar* const string2) { return string1 += string2; } -const String JUCE_CALLTYPE operator+ (String string1, const char string2) +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, const char string2) { return string1 += string2; } -const String JUCE_CALLTYPE operator+ (String string1, const juce_wchar string2) +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, const juce_wchar string2) { return string1 += string2; } -String& JUCE_CALLTYPE operator<< (String& string1, const char characterToAppend) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const char characterToAppend) { return string1 += characterToAppend; } -String& JUCE_CALLTYPE operator<< (String& string1, const juce_wchar characterToAppend) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const juce_wchar characterToAppend) { return string1 += characterToAppend; } -String& JUCE_CALLTYPE operator<< (String& string1, const char* const string2) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const char* const string2) { return string1 += string2; } -String& JUCE_CALLTYPE operator<< (String& string1, const juce_wchar* const string2) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const juce_wchar* const string2) { return string1 += string2; } -String& JUCE_CALLTYPE operator<< (String& string1, const String& string2) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const String& string2) { return string1 += string2; } -String& JUCE_CALLTYPE operator<< (String& string1, const short number) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const short number) { return string1 += (int) number; } -String& JUCE_CALLTYPE operator<< (String& string1, const int number) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const int number) { return string1 += number; } -String& JUCE_CALLTYPE operator<< (String& string1, const unsigned int number) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const unsigned int number) { return string1 += number; } -String& JUCE_CALLTYPE operator<< (String& string1, const long number) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const long number) { return string1 += (int) number; } -String& JUCE_CALLTYPE operator<< (String& string1, const unsigned long number) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const unsigned long number) { return string1 += (unsigned int) number; } -String& JUCE_CALLTYPE operator<< (String& string1, const float number) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const float number) { return string1 += String (number); } -String& JUCE_CALLTYPE operator<< (String& string1, const double number) +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const double number) { return string1 += String (number); } -OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& text) +OutputStream& JUCE_PUBLIC_FUNCTION operator<< (OutputStream& stream, const String& text) { // (This avoids using toUTF8() to prevent the memory bloat that it would leave behind // if lots of large, persistent strings were to be written to streams). diff --git a/src/text/juce_String.h b/src/text/juce_String.h index 401123d5f7..16d1eb5d04 100644 --- a/src/text/juce_String.h +++ b/src/text/juce_String.h @@ -1048,86 +1048,86 @@ private: //============================================================================== /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (const char* string1, const String& string2); +const String JUCE_PUBLIC_FUNCTION operator+ (const char* string1, const String& string2); /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (const juce_wchar* string1, const String& string2); +const String JUCE_PUBLIC_FUNCTION operator+ (const juce_wchar* string1, const String& string2); /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (char string1, const String& string2); +const String JUCE_PUBLIC_FUNCTION operator+ (char string1, const String& string2); /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (juce_wchar string1, const String& string2); +const String JUCE_PUBLIC_FUNCTION operator+ (juce_wchar string1, const String& string2); /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (String string1, const String& string2); +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, const String& string2); /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (String string1, const char* string2); +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, const char* string2); /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (String string1, const juce_wchar* string2); +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, const juce_wchar* string2); /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (String string1, char characterToAppend); +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, char characterToAppend); /** Concatenates two strings. */ -const String JUCE_CALLTYPE operator+ (String string1, juce_wchar characterToAppend); +const String JUCE_PUBLIC_FUNCTION operator+ (String string1, juce_wchar characterToAppend); //============================================================================== /** Appends a character at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, char characterToAppend); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, char characterToAppend); /** Appends a character at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, juce_wchar characterToAppend); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, juce_wchar characterToAppend); /** Appends a string to the end of the first one. */ -String& JUCE_CALLTYPE operator<< (String& string1, const char* string2); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const char* string2); /** Appends a string to the end of the first one. */ -String& JUCE_CALLTYPE operator<< (String& string1, const juce_wchar* string2); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const juce_wchar* string2); /** Appends a string to the end of the first one. */ -String& JUCE_CALLTYPE operator<< (String& string1, const String& string2); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, const String& string2); /** Appends a decimal number at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, short number); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, short number); /** Appends a decimal number at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, int number); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, int number); /** Appends a decimal number at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, unsigned int number); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, unsigned int number); /** Appends a decimal number at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, long number); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, long number); /** Appends a decimal number at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, unsigned long number); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, unsigned long number); /** Appends a decimal number at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, float number); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, float number); /** Appends a decimal number at the end of a string. */ -String& JUCE_CALLTYPE operator<< (String& string1, double number); +String& JUCE_PUBLIC_FUNCTION operator<< (String& string1, double number); //============================================================================== /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator== (const String& string1, const String& string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator== (const String& string1, const String& string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator== (const String& string1, const char* string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator== (const String& string1, const char* string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator== (const String& string1, const juce_wchar* string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator== (const String& string1, const juce_wchar* string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator!= (const String& string1, const String& string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator!= (const String& string1, const String& string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator!= (const String& string1, const char* string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator!= (const String& string1, const char* string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator!= (const String& string1, const juce_wchar* string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator!= (const String& string1, const juce_wchar* string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator> (const String& string1, const String& string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator> (const String& string1, const String& string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator< (const String& string1, const String& string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator< (const String& string1, const String& string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator>= (const String& string1, const String& string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator>= (const String& string1, const String& string2) throw(); /** Case-sensitive comparison of two strings. */ -bool JUCE_CALLTYPE operator<= (const String& string1, const String& string2) throw(); +bool JUCE_PUBLIC_FUNCTION operator<= (const String& string1, const String& string2) throw(); //============================================================================== /** This streaming override allows you to pass a juce String directly into std output streams. This is very handy for writing strings to std::cout, std::cerr, etc. */ template -std::basic_ostream & JUCE_CALLTYPE operator<< (std::basic_ostream & stream, const String& stringToWrite) +std::basic_ostream & JUCE_PUBLIC_FUNCTION operator<< (std::basic_ostream & stream, const String& stringToWrite) { return stream << stringToWrite.toUTF8(); } /** Writes a string to an OutputStream as UTF8. */ -OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const String& text); +OutputStream& JUCE_PUBLIC_FUNCTION operator<< (OutputStream& stream, const String& text); #endif // __JUCE_STRING_JUCEHEADER__ diff --git a/src/text/juce_XmlDocument.cpp b/src/text/juce_XmlDocument.cpp index 1f2920a917..935608a0eb 100644 --- a/src/text/juce_XmlDocument.cpp +++ b/src/text/juce_XmlDocument.cpp @@ -29,6 +29,7 @@ BEGIN_JUCE_NAMESPACE #include "juce_XmlDocument.h" #include "../io/streams/juce_FileInputSource.h" +#include "../io/streams/juce_MemoryOutputStream.h" //============================================================================== @@ -80,20 +81,9 @@ XmlElement* XmlDocument::getDocumentElement (const bool onlyReadOuterDocumentEle if (in != 0) { - MemoryBlock data; - - in->readIntoMemoryBlock (data, onlyReadOuterDocumentElement ? 8192 : -1); - - if (data.getSize() >= 2 - && ((data[0] == (char)-2 && data[1] == (char)-1) - || (data[0] == (char)-1 && data[1] == (char)-2))) - { - textToParse = String::createStringFromData (static_cast (data.getData()), (int) data.getSize()); - } - else - { - textToParse = String::fromUTF8 (static_cast (data.getData()), (int) data.getSize()); - } + MemoryOutputStream data; + data.writeFromInputStream (*in, onlyReadOuterDocumentElement ? 8192 : -1); + textToParse = data.toString(); if (! onlyReadOuterDocumentElement) originalText = textToParse; diff --git a/src/text/juce_XmlElement.cpp b/src/text/juce_XmlElement.cpp index cc15a419a7..52d430ccbd 100644 --- a/src/text/juce_XmlElement.cpp +++ b/src/text/juce_XmlElement.cpp @@ -386,7 +386,7 @@ const String XmlElement::createDocument (const String& dtdToUse, const String& encodingType, const int lineWrapLength) const { - MemoryOutputStream mem (2048, 4096); + MemoryOutputStream mem (2048); writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength); return mem.toUTF8();