summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2014-10-09 16:26:54 +0100
committerCarl Hetherington <cth@carlh.net>2014-10-09 16:26:54 +0100
commit0d2ffd3f00caa22b5ab55ef233aa132a08263688 (patch)
treef47c159327a3d1fc9daf554ba08673fc8d22ab78
parent06524b61729fa552ae255158203a99a321b71647 (diff)
Various fixes and missing bits.
-rw-r--r--src/cxml.cc31
-rw-r--r--src/cxml.h30
-rw-r--r--test/tests.cc20
3 files changed, 60 insertions, 21 deletions
diff --git a/src/cxml.cc b/src/cxml.cc
index f6305d0..810cf92 100644
--- a/src/cxml.cc
+++ b/src/cxml.cc
@@ -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
diff --git a/src/cxml.h b/src/cxml.h
index 0542c4e..e358b9c 100644
--- a/src/cxml.h
+++ b/src/cxml.h
@@ -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;
diff --git a/test/tests.cc b/test/tests.cc
index 32ea830..3395ff8 100644
--- a/test/tests.cc
+++ b/test/tests.cc
@@ -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");