Various bits of development.
authorCarl Hetherington <cth@carlh.net>
Sat, 30 Mar 2013 16:08:12 +0000 (16:08 +0000)
committerCarl Hetherington <cth@carlh.net>
Sat, 30 Mar 2013 16:08:12 +0000 (16:08 +0000)
src/cxml.cc
src/cxml.h
wscript

index 2a755bf8751afed7131bc93f830ddb0b5316e6d3..20d06ee89d0b0bc907b9ddb4bde2b764113a0409 100644 (file)
@@ -21,8 +21,15 @@ cxml::Node::Node (xmlpp::Node const * node)
 
 }
 
+string
+cxml::Node::name () const
+{
+       assert (_node);
+       return _node->get_name ();
+}
+
 shared_ptr<cxml::Node>
-cxml::Node::node_child (string name)
+cxml::Node::node_child (string name) const
 {
        list<shared_ptr<cxml::Node> > n = node_children (name);
        if (n.size() > 1) {
@@ -34,8 +41,21 @@ cxml::Node::node_child (string name)
        return n.front ();
 }
 
+shared_ptr<cxml::Node>
+cxml::Node::optional_node_child (string name) const
+{
+       list<shared_ptr<cxml::Node> > n = node_children (name);
+       if (n.size() > 1) {
+               throw cxml::Error ("duplicate XML tag " + name);
+       } else if (n.empty ()) {
+               return shared_ptr<cxml::Node> ();
+       }
+       
+       return n.front ();
+}
+
 list<shared_ptr<cxml::Node> >
-cxml::Node::node_children (string name)
+cxml::Node::node_children (string name) const
 {
        /* XXX: using find / get_path should work here, but I can't follow
           how get_path works.
@@ -55,13 +75,13 @@ cxml::Node::node_children (string name)
 }
 
 string
-cxml::Node::string_child (string c)
+cxml::Node::string_child (string c) const
 {
        return node_child(c)->content ();
 }
 
 optional<string>
-cxml::Node::optional_string_child (string c)
+cxml::Node::optional_string_child (string c) const
 {
        list<shared_ptr<Node> > nodes = node_children (c);
        if (nodes.size() > 1) {
@@ -76,14 +96,14 @@ cxml::Node::optional_string_child (string c)
 }
 
 bool
-cxml::Node::bool_child (string c)
+cxml::Node::bool_child (string c) const
 {
        string const s = string_child (c);
        return (s == "1" || s == "yes");
 }
 
 optional<bool>
-cxml::Node::optional_bool_child (string c)
+cxml::Node::optional_bool_child (string c) const
 {
        optional<string> s = optional_string_child (c);
        if (!s) {
@@ -94,13 +114,13 @@ cxml::Node::optional_bool_child (string c)
 }
 
 void
-cxml::Node::ignore_child (string name)
+cxml::Node::ignore_child (string name) const
 {
        _taken.push_back (name);
 }
 
 string
-cxml::Node::string_attribute (string name)
+cxml::Node::string_attribute (string name) const
 {
        xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
        if (!e) {
@@ -116,7 +136,7 @@ cxml::Node::string_attribute (string name)
 }
 
 optional<string>
-cxml::Node::optional_string_attribute (string name)
+cxml::Node::optional_string_attribute (string name) const
 {
        xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
        if (!e) {
@@ -132,14 +152,14 @@ cxml::Node::optional_string_attribute (string name)
 }
 
 bool
-cxml::Node::bool_attribute (string name)
+cxml::Node::bool_attribute (string name) const
 {
        string const s = string_attribute (name);
        return (s == "1" || s == "yes");
 }
 
 optional<bool>
-cxml::Node::optional_bool_attribute (string name)
+cxml::Node::optional_bool_attribute (string name) const
 {
        optional<string> s = optional_string_attribute (name);
        if (!s) {
@@ -150,7 +170,7 @@ cxml::Node::optional_bool_attribute (string name)
 }
 
 void
-cxml::Node::done ()
+cxml::Node::done () const
 {
        xmlpp::Node::NodeList c = _node->get_children ();
        for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
@@ -161,7 +181,7 @@ cxml::Node::done ()
 }
 
 string
-cxml::Node::content ()
+cxml::Node::content () const
 {
        string content;
        
index bf2a2d3667366930383dfb12f6820374a42a1e71..c9f1065060f5e5edad983fdc8c7bdd50590874ab 100644 (file)
@@ -8,6 +8,7 @@
 #include <boost/optional.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/algorithm/string/erase.hpp>
+#include <glibmm.h>
 
 namespace xmlpp {
        class Node;
@@ -52,6 +53,8 @@ public:
         */
        Node (xmlpp::Node const * node);
 
+       std::string name () const;
+
        /* A set of methods which look up a child of this node by
         * its name, and return its contents as some type or other.
         *
@@ -74,14 +77,14 @@ public:
         * than one of the specified child node.
         */
 
-       std::string string_child (std::string c);
-       boost::optional<std::string> optional_string_child (std::string);
+       std::string string_child (std::string c) const;
+       boost::optional<std::string> optional_string_child (std::string) const;
 
-       bool bool_child (std::string);
-       boost::optional<bool> optional_bool_child (std::string);
+       bool bool_child (std::string) const;
+       boost::optional<bool> optional_bool_child (std::string) const;
 
        template <class T>
-       T numerical_child (std::string c)
+       T numerical_child (std::string c) const
        {
                std::string s = string_child (c);
                boost::erase_all (s, " ");
@@ -89,7 +92,7 @@ public:
        }
 
        template <class T>
-       boost::optional<T> optional_numerical_child (std::string c)
+       boost::optional<T> optional_numerical_child (std::string c) const
        {
                boost::optional<std::string> s = optional_string_child (c);
                if (!s) {
@@ -102,25 +105,25 @@ public:
        }
                
        /** This will mark a child as to be ignored when calling done() */
-       void ignore_child (std::string);
+       void ignore_child (std::string) const;
 
        /** Check whether all children of this Node have been looked up
         *  or passed to ignore_child().  If not, an exception is thrown.
         */
-       void done ();
+       void done () const;
 
        /* These methods look for an attribute of this node, in the
         * same way as the child methods do.
         */
 
-       std::string string_attribute (std::string);
-       boost::optional<std::string> optional_string_attribute (std::string);
+       std::string string_attribute (std::string) const;
+       boost::optional<std::string> optional_string_attribute (std::string) const;
 
-       bool bool_attribute (std::string);
-       boost::optional<bool> optional_bool_attribute (std::string);
+       bool bool_attribute (std::string) const;
+       boost::optional<bool> optional_bool_attribute (std::string) const;
 
        template <class T>
-       T numerical_attribute (std::string c)
+       T numerical_attribute (std::string c) const
        {
                std::string s = string_attribute (c);
                boost::erase_all (s, " ");
@@ -128,7 +131,7 @@ public:
        }
 
        template <class T>
-       boost::optional<T> optional_numerical_attribute (std::string c)
+       boost::optional<T> optional_numerical_attribute (std::string c) const
        {
                boost::optional<std::string> s = optional_string_attribute (c);
                if (!s) {
@@ -141,18 +144,18 @@ public:
        }
 
        /** @return The content of this node */
-       std::string content ();
+       std::string content () const;
 
-       boost::shared_ptr<Node> node_child (std::string);
-       boost::shared_ptr<Node> optional_node_child (std::string);
+       boost::shared_ptr<Node> node_child (std::string) const;
+       boost::shared_ptr<Node> optional_node_child (std::string) const;
 
-       std::list<boost::shared_ptr<Node> > node_children (std::string);
+       std::list<boost::shared_ptr<Node> > node_children (std::string) const;
        
 protected:
        xmlpp::Node const * _node;
        
 private:
-       std::list<Glib::ustring> _taken;
+       mutable std::list<Glib::ustring> _taken;
 };
 
 class File : public Node
diff --git a/wscript b/wscript
index f1e0dd30a0d90c4c248c85ac6fa4b7e0b196bbd8..85ac40e713a87253884e148e7ebbcf0f0a703586 100644 (file)
--- a/wscript
+++ b/wscript
@@ -26,7 +26,7 @@ def build(bld):
     bld(source = 'libcxml.pc.in',
         version = VERSION,
         includedir = '%s/include' % bld.env.PREFIX,
-        libs = "-L${libdir}",
+        libs = "-L${libdir} -lcxml",
         install_path = '${LIBDIR}/pkgconfig')
 
     bld.recurse('src')