diff --git a/src/pugixml.hpp b/src/pugixml.hpp index 09bd692..ed5a08a 100644 --- a/src/pugixml.hpp +++ b/src/pugixml.hpp @@ -335,9 +335,18 @@ namespace pugi const char_t* name() const; const char_t* value() const; + #ifndef PUGIXML_NO_STL + string_t sname() const { return name(); } + string_t svalue() const { return value(); } + #endif + // Get attribute value, or the default value if attribute is empty const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const; + #ifndef PUGIXML_NO_STL + string_t as_sstring(const string_t &def = string_t()) const { return as_string(def.c_str()); } + #endif + // Get attribute value as a number, or the default value if conversion did not succeed or attribute is empty int as_int(int def = 0) const; unsigned int as_uint(unsigned int def = 0) const; @@ -370,6 +379,10 @@ namespace pugi bool set_value(unsigned long long rhs); #endif + #ifndef PUGIXML_NO_STL + bool set_value(const string_t &rhs) { return set_value(rhs.c_str()); } + #endif + // Set attribute value (equivalent to set_value without error checking) xml_attribute& operator=(const char_t* rhs); xml_attribute& operator=(int rhs); @@ -385,6 +398,10 @@ namespace pugi xml_attribute& operator=(unsigned long long rhs); #endif + #ifndef PUGIXML_NO_STL + xml_attribute& operator=(const string_t &rhs) { return (*this = rhs.c_str()); } + #endif + // Get next/previous attribute in the attribute list of the parent node xml_attribute next_attribute() const; xml_attribute previous_attribute() const; @@ -448,6 +465,11 @@ namespace pugi // Note: For text node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes. const char_t* value() const; + #ifndef PUGIXML_NO_STL + const string_t sname() const { return name(); } + const string_t svalue() const { return value(); } + #endif + // Get attribute list xml_attribute first_attribute() const; xml_attribute last_attribute() const; @@ -701,6 +723,10 @@ namespace pugi unsigned long long as_ullong(unsigned long long def = 0) const; #endif + #ifndef PUGIXML_NO_STL + string_t as_sstring(const string_t &def = string_t()) const { return as_string(def.c_str()); } + #endif + // Get text as bool (returns true if first character is in '1tTyY' set), or the default value if object is empty bool as_bool(bool def = false) const; @@ -736,6 +762,10 @@ namespace pugi xml_text& operator=(unsigned long long rhs); #endif + #ifndef PUGIXML_NO_STL + xml_text& operator=(const string_t &rhs) { return (*this = rhs.c_str()); } + #endif + // Get the data node (node_pcdata or node_cdata) for this object xml_node data() const; }; @@ -1069,6 +1099,10 @@ namespace pugi // Get variable name const char_t* name() const; + #ifndef PUGIXML_NO_STL + string_t sname() const { return name(); } + #endif + // Get variable type xpath_value_type type() const; @@ -1078,6 +1112,10 @@ namespace pugi const char_t* get_string() const; const xpath_node_set& get_node_set() const; + #ifndef PUGIXML_NO_STL + string_t get_sstring() const { return get_string(); } + #endif + // Set variable value; no type conversion is performed, false is returned on type mismatch error bool set(bool value); bool set(double value); diff --git a/tests/test.cpp b/tests/test.cpp index 6347984..bd4bf5b 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -52,6 +52,13 @@ bool test_string_equal(const pugi::char_t* lhs, const pugi::char_t* rhs) #endif } +#ifndef PUGIXML_NO_STL +bool test_string_equal(const pugi::string_t& lhs, const pugi::string_t& rhs) +{ + return (lhs == rhs); +} +#endif + bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const pugi::char_t* indent, unsigned int flags) { xml_writer_string writer; diff --git a/tests/test.hpp b/tests/test.hpp index e700826..5e7a15d 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -32,6 +32,10 @@ struct test_runner bool test_string_equal(const pugi::char_t* lhs, const pugi::char_t* rhs); +#ifndef PUGIXML_NO_STL +bool test_string_equal(const pugi::string_t& lhs, const pugi::string_t& rhs); +#endif + template inline bool test_node_name_value(const Node& node, const pugi::char_t* name, const pugi::char_t* value) { return test_string_equal(node.name(), name) && test_string_equal(node.value(), value); diff --git a/tests/test_dom_text.cpp b/tests/test_dom_text.cpp index 7582460..d9e4b61 100644 --- a/tests/test_dom_text.cpp +++ b/tests/test_dom_text.cpp @@ -59,6 +59,26 @@ TEST_XML_FLAGS(dom_text_as_string, "foofoo", parse_default | parse_pi) +{ + xml_node node = doc.child(STR("node")); + + CHECK_STRING(node.child(STR("a")).text().as_sstring(), string_t(STR("foo"))); + CHECK_STRING(node.child(STR("a")).first_child().text().as_sstring(), string_t(STR("foo"))); + + CHECK_STRING(node.child(STR("b")).text().as_sstring(), string_t(STR("bar"))); + CHECK_STRING(node.child(STR("b")).last_child().text().as_sstring(), string_t(STR("bar"))); + + CHECK_STRING(node.child(STR("c")).text().as_sstring(), string_t(STR(""))); + CHECK_STRING(node.child(STR("c")).first_child().text().as_sstring(), string_t(STR(""))); + + CHECK_STRING(node.child(STR("d")).text().as_sstring(), string_t(STR(""))); + + CHECK_STRING(xml_node().text().as_sstring(), string_t(STR(""))); +} +#endif + TEST_XML(dom_text_as_int, "1-1-214748364821474836470") { xml_node node = doc.child(STR("node")); @@ -442,4 +462,8 @@ TEST(dom_text_defaults) CHECK(text.as_llong(42) == 42); CHECK(text.as_ullong(42) == 42); #endif + +#ifndef PUGIXML_NO_STL + CHECK(text.as_sstring() == string_t()); +#endif }