[Xmlstorage-commits] SF.net SVN: xmlstorage: [87] trunk/c++
Brought to you by:
martinfuchs
|
From: <mar...@us...> - 2008-02-07 21:56:15
|
Revision: 87
http://xmlstorage.svn.sourceforge.net/xmlstorage/?rev=87&view=rev
Author: martinfuchs
Date: 2008-02-07 13:56:14 -0800 (Thu, 07 Feb 2008)
Log Message:
-----------
utility functions to manage key/value properties stored in XML
Modified Paths:
--------------
trunk/c++/xmlstorage.cpp
trunk/c++/xmlstorage.h
trunk/c++/xs-native.cpp
Modified: trunk/c++/xmlstorage.cpp
===================================================================
--- trunk/c++/xmlstorage.cpp 2008-02-07 21:18:15 UTC (rev 86)
+++ trunk/c++/xmlstorage.cpp 2008-02-07 21:56:14 UTC (rev 87)
@@ -56,7 +56,11 @@
const LPCXSSTR XS_FLOATFMT = XS_FLOATFMT_STR;
#endif
+const XS_String XS_KEY = XS_KEY_STR;
+const XS_String XS_VALUE = XS_VALUE_STR;
+const XS_String XS_PROPERTY = XS_PROPERTY_STR;
+
/// remove escape characters from zero terminated string
static std::string unescape(const char* s, char b, char e)
{
Modified: trunk/c++/xmlstorage.h
===================================================================
--- trunk/c++/xmlstorage.h 2008-02-07 21:18:15 UTC (rev 86)
+++ trunk/c++/xmlstorage.h 2008-02-07 21:56:14 UTC (rev 87)
@@ -395,6 +395,10 @@
#define XS_INTFMT_STR XS_TEXT("%d")
#define XS_FLOATFMT_STR XS_TEXT("%f")
+#define XS_KEY_STR XS_TEXT("key")
+#define XS_VALUE_STR XS_TEXT("value")
+#define XS_PROPERTY_STR XS_TEXT("property")
+
// work around GCC's wide string constant bug
#ifdef __GNUC__
extern const LPCXSSTR XS_EMPTY;
@@ -410,7 +414,11 @@
#define XS_FLOATFMT XS_FLOATFMT_STR
#endif
+extern const XS_String XS_KEY;
+extern const XS_String XS_VALUE;
+extern const XS_String XS_PROPERTY;
+
#ifndef XS_STRING_UTF8
// from UTF-8 to XS internal string encoding
@@ -1257,6 +1265,7 @@
struct iterator
{
typedef XMLNode::Children::iterator BaseIterator;
+ typedef iterator myType;
iterator(BaseIterator begin, BaseIterator end, const XS_String& filter_name)
: _cur(begin),
@@ -1281,7 +1290,7 @@
return *_cur;
}
- iterator& operator++()
+ myType& operator++()
{
++_cur;
search_next();
@@ -1289,9 +1298,9 @@
return *this;
}
- iterator operator++(int)
+ myType operator++(int)
{
- iterator ret = *this;
+ myType ret = *this;
++_cur;
search_next();
@@ -1299,14 +1308,14 @@
return ret;
}
- bool operator==(const BaseIterator& other) const
+ bool operator==(const myType& other) const
{
- return _cur == other;
+ return _cur == other._cur;
}
- bool operator!=(const BaseIterator& other) const
+ bool operator!=(const myType& other) const
{
- return _cur != other;
+ return _cur != other._cur;
}
protected:
@@ -1356,6 +1365,7 @@
struct const_iterator
{
typedef XMLNode::Children::const_iterator BaseIterator;
+ typedef const_iterator myType;
const_iterator(BaseIterator begin, BaseIterator end, const XS_String& filter_name)
: _cur(begin),
@@ -1375,7 +1385,7 @@
return *_cur;
}
- const_iterator& operator++()
+ myType& operator++()
{
++_cur;
search_next();
@@ -1383,9 +1393,9 @@
return *this;
}
- const_iterator operator++(int)
+ myType operator++(int)
{
- const_iterator ret = *this;
+ myType ret = *this;
++_cur;
search_next();
@@ -1393,14 +1403,14 @@
return ret;
}
- bool operator==(const BaseIterator& other) const
+ bool operator==(const myType& other) const
{
- return _cur == other;
+ return _cur == other._cur;
}
- bool operator!=(const BaseIterator& other) const
+ bool operator!=(const myType& other) const
{
- return _cur != other;
+ return _cur != other._cur;
}
protected:
@@ -1686,6 +1696,15 @@
XS_String& str() {return *_cur;}
const XS_String& str() const {return *_cur;}
+ // property (key/value pair) setter functions
+ void set_property(const XS_String& key, int value, const XS_String& name=XS_PROPERTY);
+ void set_property(const XS_String& key, double value, const XS_String& name=XS_PROPERTY);
+ void set_property(const XS_String& key, const XS_String& value, const XS_String& name=XS_PROPERTY);
+ void set_property(const XS_String& key, const struct XMLBool& value, const XS_String& name=XS_PROPERTY);
+
+ void set_property(const XS_String& key, const char* value, const XS_String& name=XS_PROPERTY)
+ {set_property(key, XS_String(value), name);}
+
protected:
XMLNode* _root;
XMLNode* _cur;
@@ -1815,7 +1834,7 @@
XMLBool(LPCXSSTR value, bool def=false)
{
- if (value && *value)
+ if (value && *value)//@@ also handle white space and return def instead of false
_value = !XS_icmp(value, XS_TRUE);
else
_value = def;
@@ -1905,7 +1924,7 @@
XMLInt(LPCXSSTR value, int def=0)
{
- if (value && *value)
+ if (value && *value)//@@ also handle white space and return def instead of 0
_value = XS_toi(value);
else
_value = def;
@@ -1986,7 +2005,7 @@
{
LPTSTR end;
- if (value && *value)
+ if (value && *value)//@@ also handle white space and return def instead of 0
_value = XS_tod(value, &end);
else
_value = def;
@@ -2160,6 +2179,141 @@
}
+inline void XMLPos::set_property(const XS_String& key, int value, const XS_String& name)
+{
+ smart_create(name, XS_KEY, key);
+ XMLIntRef(_cur, XS_VALUE) = value;
+ back();
+}
+
+inline void XMLPos::set_property(const XS_String& key, double value, const XS_String& name)
+{
+ smart_create(name, XS_KEY, key);
+ XMLDoubleRef(_cur, XS_VALUE) = value;
+ back();
+}
+
+inline void XMLPos::set_property(const XS_String& key, const XS_String& value, const XS_String& name)
+{
+ smart_create(name, XS_KEY, key);
+ put(XS_VALUE, value);
+ back();
+}
+
+inline void XMLPos::set_property(const XS_String& key, const XMLBool& value, const XS_String& name)
+{
+ smart_create(name, XS_KEY, key);
+ XMLBoolRef(_cur, XS_VALUE) = value;
+ back();
+}
+
+
+ /// a key/value pair for property data access
+struct XMLProperty {
+ XMLProperty(const XMLNode* node)
+ : _key(node->get(XS_KEY)),
+ _value(node->get(XS_VALUE))
+ {
+ }
+
+ XS_String _key;
+ XS_String _value;
+};
+
+
+ /// utility class to read property settings from a XML tree
+struct XMLPropertyReader
+{
+ XMLPropertyReader(const XMLNode::Children& children)
+ : _filter(children, XS_PROPERTY),
+ _begin(_filter.begin(), _filter.end()),
+ _end(_filter.end(), _filter.end())
+ {
+ }
+
+ XMLPropertyReader(const XMLNode* node)
+ : _filter(node, XS_PROPERTY),
+ _begin(_filter.begin(), _filter.end()),
+ _end(_filter.end(), _filter.end())
+ {
+ }
+
+ /// internal iterator class
+ struct const_iterator
+ {
+ typedef const_XMLChildrenFilter::const_iterator BaseIterator;
+ typedef const_iterator myType;
+
+ const_iterator(BaseIterator begin, BaseIterator end)
+ : _cur(begin),
+ _end(end)
+ {
+ }
+
+ operator BaseIterator()
+ {
+ return _cur;
+ }
+
+ XMLProperty operator*() const
+ {
+ return XMLProperty(*_cur);
+ }
+
+ const XMLNode* get_node() const
+ {
+ return *_cur;
+ }
+
+ myType& operator++()
+ {
+ ++_cur;
+
+ return *this;
+ }
+
+ myType operator++(int)
+ {
+ myType ret = *this;
+
+ ++_cur;
+
+ return ret;
+ }
+
+ bool operator==(const myType& other) const
+ {
+ return _cur == other._cur;
+ }
+
+ bool operator!=(const myType& other) const
+ {
+ return _cur != other._cur;
+ }
+
+ protected:
+ BaseIterator _cur;
+ BaseIterator _end;
+ };
+
+ const_iterator begin()
+ {
+ return _begin;
+ }
+
+ const_iterator end()
+ {
+ return _end;
+ }
+
+protected:
+ const_XMLChildrenFilter _filter;
+
+ const_iterator _begin;
+ const_iterator _end;
+};
+
+
#ifdef _MSC_VER
#pragma warning(disable: 4355)
#endif
Modified: trunk/c++/xs-native.cpp
===================================================================
--- trunk/c++/xs-native.cpp 2008-02-07 21:18:15 UTC (rev 86)
+++ trunk/c++/xs-native.cpp 2008-02-07 21:56:14 UTC (rev 87)
@@ -1,8 +1,8 @@
//
- // XML storage C++ classes version 1.2
+ // XML storage C++ classes version 1.3
//
- // Copyright (c) 2006, 2007 Martin Fuchs <mar...@gm...>
+ // Copyright (c) 2006, 2007, 2008 Martin Fuchs <mar...@gm...>
//
/// \file xs-native.cpp
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|