From 6acfb2c125b91d0f333df935257f4e924df9b89e Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 7 Nov 2007 16:43:04 +0000 Subject: [PATCH] --- src/juce_core/containers/juce_PropertySet.cpp | 48 +++++++++++++++++++ src/juce_core/containers/juce_PropertySet.h | 20 ++++++++ src/juce_core/io/juce_InputStream.cpp | 14 ++++++ src/juce_core/io/juce_InputStream.h | 20 ++++++++ src/juce_core/io/juce_OutputStream.cpp | 14 ++++++ src/juce_core/io/juce_OutputStream.h | 16 +++++++ src/juce_core/io/network/juce_Socket.cpp | 2 +- 7 files changed, 133 insertions(+), 1 deletion(-) diff --git a/src/juce_core/containers/juce_PropertySet.cpp b/src/juce_core/containers/juce_PropertySet.cpp index 7094f01fde..eb54ae4336 100644 --- a/src/juce_core/containers/juce_PropertySet.cpp +++ b/src/juce_core/containers/juce_PropertySet.cpp @@ -68,6 +68,17 @@ PropertySet::~PropertySet() { } +void PropertySet::clear() +{ + const ScopedLock sl (lock); + + if (properties.size() > 0) + { + properties.clear(); + propertyChanged(); + } +} + const String PropertySet::getValue (const String& keyName, const String& defaultValue) const throw() { @@ -200,6 +211,43 @@ void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) thro fallbackProperties = fallbackProperties_; } +XmlElement* PropertySet::createXml (const String& nodeName) const throw() +{ + const ScopedLock sl (lock); + XmlElement* const xml = new XmlElement (nodeName); + + for (int i = 0; i < properties.getAllKeys().size(); ++i) + { + XmlElement* const e = new XmlElement (T("VALUE")); + + e->setAttribute (T("name"), properties.getAllKeys()[i]); + e->setAttribute (T("val"), properties.getAllValues()[i]); + + xml->addChildElement (e); + } + + return xml; +} + +void PropertySet::restoreFromXml (const XmlElement& xml) throw() +{ + const ScopedLock sl (lock); + clear(); + + forEachXmlChildElementWithTagName (xml, e, T("VALUE")) + { + if (e->hasAttribute (T("name")) + && e->hasAttribute (T("val"))) + { + properties.set (e->getStringAttribute (T("name")), + e->getStringAttribute (T("val"))); + } + } + + if (properties.size() > 0) + propertyChanged(); +} + void PropertySet::propertyChanged() { } diff --git a/src/juce_core/containers/juce_PropertySet.h b/src/juce_core/containers/juce_PropertySet.h index 3cbbd8814c..83536c57c1 100644 --- a/src/juce_core/containers/juce_PropertySet.h +++ b/src/juce_core/containers/juce_PropertySet.h @@ -188,6 +188,9 @@ public: /** Returns true if the properies include the given key. */ bool containsKey (const String& keyName) const throw(); + /** Removes all values. */ + void clear(); + //============================================================================== /** Returns the keys/value pair array containing all the properties. */ StringPairArray& getAllProperties() throw() { return properties; } @@ -195,6 +198,23 @@ public: /** Returns the lock used when reading or writing to this set */ const CriticalSection& getLock() const throw() { return lock; } + //============================================================================== + /** Returns an XML element which encapsulates all the items in this property set. + + The string parameter is the tag name that should be used for the node. + + @see restoreFromXml + */ + XmlElement* createXml (const String& nodeName) const throw(); + + /** Reloads a set of properties that were previously stored as XML. + + The node passed in must have been created by the createXml() method. + + @see createXml + */ + void restoreFromXml (const XmlElement& xml) throw(); + //============================================================================== /** Sets up a second PopertySet that will be used to look up any values that aren't set in this one. diff --git a/src/juce_core/io/juce_InputStream.cpp b/src/juce_core/io/juce_InputStream.cpp index bd58a2d926..7a886f616c 100644 --- a/src/juce_core/io/juce_InputStream.cpp +++ b/src/juce_core/io/juce_InputStream.cpp @@ -140,6 +140,13 @@ float InputStream::readFloat() return n.asFloat; } +float InputStream::readFloatBigEndian() +{ + union { int asInt; float asFloat; } n; + n.asInt = readIntBigEndian(); + return n.asFloat; +} + double InputStream::readDouble() { union { int64 asInt; double asDouble; } n; @@ -147,6 +154,13 @@ double InputStream::readDouble() return n.asDouble; } +double InputStream::readDoubleBigEndian() +{ + union { int64 asInt; double asDouble; } n; + n.asInt = readInt64BigEndian(); + return n.asDouble; +} + const String InputStream::readString() { const int tempBufferSize = 256; diff --git a/src/juce_core/io/juce_InputStream.h b/src/juce_core/io/juce_InputStream.h index 76fdff4c72..e1542fd241 100644 --- a/src/juce_core/io/juce_InputStream.h +++ b/src/juce_core/io/juce_InputStream.h @@ -176,6 +176,16 @@ public: */ virtual float readFloat(); + /** Reads four bytes as a 32-bit floating point value. + + The raw 32-bit encoding of the float is read from the stream as a big-endian int. + + If the stream is exhausted partway through reading the bytes, this will return zero. + + @see OutputStream::writeFloatBigEndian, readDoubleBigEndian + */ + virtual float readFloatBigEndian(); + /** Reads eight bytes as a 64-bit floating point value. The raw 64-bit encoding of the double is read from the stream as a little-endian int64. @@ -186,6 +196,16 @@ public: */ virtual double readDouble(); + /** Reads eight bytes as a 64-bit floating point value. + + The raw 64-bit encoding of the double is read from the stream as a big-endian int64. + + If the stream is exhausted partway through reading the bytes, this will return zero. + + @see OutputStream::writeDoubleBigEndian, readFloatBigEndian + */ + virtual double readDoubleBigEndian(); + /** Reads an encoded 32-bit number from the stream using a space-saving compressed format. For small values, this is more space-efficient than using readInt() and OutputStream::writeInt() diff --git a/src/juce_core/io/juce_OutputStream.cpp b/src/juce_core/io/juce_OutputStream.cpp index 919bccd4d1..0abdf41b30 100644 --- a/src/juce_core/io/juce_OutputStream.cpp +++ b/src/juce_core/io/juce_OutputStream.cpp @@ -114,6 +114,13 @@ void OutputStream::writeFloat (float value) writeInt (n.asInt); } +void OutputStream::writeFloatBigEndian (float value) +{ + union { int asInt; float asFloat; } n; + n.asFloat = value; + writeIntBigEndian (n.asInt); +} + void OutputStream::writeDouble (double value) { union { int64 asInt; double asDouble; } n; @@ -121,6 +128,13 @@ void OutputStream::writeDouble (double value) writeInt64 (n.asInt); } +void OutputStream::writeDoubleBigEndian (double value) +{ + union { int64 asInt; double asDouble; } n; + n.asDouble = value; + writeInt64BigEndian (n.asInt); +} + void OutputStream::writeString (const String& text) { const int numBytes = text.copyToUTF8 (0); diff --git a/src/juce_core/io/juce_OutputStream.h b/src/juce_core/io/juce_OutputStream.h index 30d13ba7dd..0e4fc668dd 100644 --- a/src/juce_core/io/juce_OutputStream.h +++ b/src/juce_core/io/juce_OutputStream.h @@ -150,6 +150,14 @@ public: */ virtual void writeFloat (float value); + /** Writes a 32-bit floating point value to the stream. + + The binary 32-bit encoding of the float is written as a big-endian int. + + @see InputStream::readFloatBigEndian + */ + virtual void writeFloatBigEndian (float value); + /** Writes a 64-bit floating point value to the stream. The eight raw bytes of the double value are written out as a little-endian 64-bit int. @@ -158,6 +166,14 @@ public: */ virtual void writeDouble (double value); + /** Writes a 64-bit floating point value to the stream. + + The eight raw bytes of the double value are written out as a big-endian 64-bit int. + + @see InputStream::readDoubleBigEndian + */ + virtual void writeDoubleBigEndian (double value); + /** Writes a condensed encoding of a 32-bit integer. If you're storing a lot of integers which are unlikely to have very large values, diff --git a/src/juce_core/io/network/juce_Socket.cpp b/src/juce_core/io/network/juce_Socket.cpp index 03d45af802..5763a178d7 100644 --- a/src/juce_core/io/network/juce_Socket.cpp +++ b/src/juce_core/io/network/juce_Socket.cpp @@ -243,7 +243,7 @@ static bool connectSocket (int volatile& handle, servTmpAddr.sin_addr = targetAddress; servTmpAddr.sin_port = htons ((uint16) portNumber); - if (handle < 0) + if (handle < 0) handle = (int) socket (AF_INET, isDatagram ? SOCK_DGRAM : SOCK_STREAM, 0); if (handle < 0)