Various fixes and missing bits.
authorCarl Hetherington <cth@carlh.net>
Thu, 9 Oct 2014 15:26:54 +0000 (16:26 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 9 Oct 2014 15:26:54 +0000 (16:26 +0100)
src/cxml.cc
src/cxml.h
test/tests.cc

index f6305d0392161102876ac9c943b65b0367c4f467..810cf92aaeafde75534955a7b457acec8a4ffa48 100644 (file)
@@ -31,6 +31,7 @@ using std::string;
 using std::make_pair;
 using std::map;
 using std::stringstream;
+using std::ostream;
 using boost::shared_ptr;
 using boost::optional;
 
@@ -262,18 +263,30 @@ cxml::Node::set_bool_content (bool c)
        _content = c ? "1" : "0";
 }
 
+void
+cxml::Node::add_string_attribute (string name, string value)
+{
+       _attributes.push_back (make_pair (name, value));
+}
+
+void
+cxml::Node::add_bool_attribute (string name, bool value)
+{
+       add_string_attribute (name, value ? "1" : "0");
+}
+
 cxml::NodePtr
-cxml::Node::add_string (string name, string content)
+cxml::Node::add_string_child (string name, string content)
 {
-       NodePtr n = add (name);
+       NodePtr n = add_child (name);
        n->set_string_content (content);
        return n;
 }
        
 cxml::NodePtr
-cxml::Node::add_bool (std::string name, bool content)
+cxml::Node::add_bool_child (std::string name, bool content)
 {
-       NodePtr n = add (name);
+       NodePtr n = add_child (name);
        n->set_bool_content (content);
        return n;
 }
@@ -324,11 +337,19 @@ cxml::Document::write_to_file_formatted (boost::filesystem::path path) const
 }
 
 void
+cxml::Document::write_to_stream_formatted (ostream& stream, string coding) const
+{
+       xmlpp::Document doc;
+       write_to_xmlpp_document (doc);
+       doc.write_to_stream_formatted (stream, coding);
+}
+
+string
 cxml::Document::write_to_string (std::string coding) const
 {
        xmlpp::Document doc;
        write_to_xmlpp_document (doc);
-       doc.write_to_string (coding);
+       return doc.write_to_string (coding);
 }
 
 void
index 0542c4e8d2a869e61b85b5b2f568aa62842a4052..e358b9cdba3f54925b5b2afa5207abd6c3d6e90f 100644 (file)
@@ -21,6 +21,7 @@
 #define LIBCXML_CXML_H
 
 #include <string>
+#include <sstream>
 #include <list>
 #include <stdint.h>
 #include <boost/shared_ptr.hpp>
@@ -35,6 +36,7 @@
 
 namespace xmlpp {
        class Node;
+       class Document;
 }
 
 namespace cxml {
@@ -191,15 +193,30 @@ public:
        }
 
 
+       /* Adding attributes */
+
+       void add_string_attribute (std::string name, std::string value);
+       void add_bool_attribute (std::string name, bool value);
+
+       template <class T>
+       void add_number_attribute (std::string name, T value)
+       {
+               std::stringstream u;
+               u.imbue (std::locale::classic ());
+               u << value;
+               add_string_attribute (name, u.str ());
+       }
+
+
        /* Short-cuts to add nodes with content */
 
-       NodePtr add_string (std::string name, std::string content);
-       NodePtr add_bool (std::string name, bool content);
+       NodePtr add_string_child (std::string name, std::string content);
+       NodePtr add_bool_child (std::string name, bool content);
 
        template <class T>
-       NodePtr add_number (std::string name, T content)
+       NodePtr add_number_child (std::string name, T content)
        {
-               NodePtr n = add (name);
+               NodePtr n = add_child (name);
                n->set_number_content (content);
                return n;
        }
@@ -212,7 +229,7 @@ public:
        NodeList node_children (std::string) const;
 
        /** Add a child node with a given name */
-       NodePtr add (std::string name)
+       NodePtr add_child (std::string name)
        {
                NodePtr n (new cxml::Node ());
                n->set_name (name);
@@ -273,7 +290,8 @@ public:
        void check_root_name (std::string root_name);
 
        void write_to_file_formatted (boost::filesystem::path) const;
-       void write_to_string (std::string) const;
+       void write_to_stream_formatted (std::ostream& stream, std::string coding) const;
+       std::string write_to_string (std::string) const;
 
 private:
        void write_to_xmlpp_document (xmlpp::Document &) const;
index 32ea830453a8d9cebddd677b101ae182d69b1015..3395ff8265b9e3fc36da9b62e7406ea8f973c1cf 100644 (file)
@@ -145,16 +145,16 @@ BOOST_AUTO_TEST_CASE (write_test)
        cxml::Document document;
        document.set_name ("A");
 
-       document.add_number<int> ("B", 42);
-       document.add_string ("C", "fred");
-       document.add_number<double> ("D", 42.9);
-       document.add_bool ("E", true);
-       document.add_number<int> ("F", 1);
-       document.add_number<int> ("F", 2);
-       cxml::NodePtr h = document.add ("H");
-       h->add_string ("I", "testing");
-       h->add_string ("I", "more testing");
-       document.add ("J")->add_string ("K", "jim");
+       document.add_number_child<int> ("B", 42);
+       document.add_string_child ("C", "fred");
+       document.add_number_child<double> ("D", 42.9);
+       document.add_bool_child ("E", true);
+       document.add_number_child<int> ("F", 1);
+       document.add_number_child<int> ("F", 2);
+       cxml::NodePtr h = document.add_child ("H");
+       h->add_string_child ("I", "testing");
+       h->add_string_child ("I", "more testing");
+       document.add_child ("J")->add_string_child ("K", "jim");
 
        document.write_to_file_formatted ("build/test/b.xml");