summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/pugixml.cpp167
-rw-r--r--src/pugixml.hpp56
2 files changed, 218 insertions, 5 deletions
diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index d98daa8..f332254 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -3312,6 +3312,13 @@ PUGI__NS_BEGIN
}
}
+ inline bool is_text_node(xml_node_struct* node)
+ {
+ xml_node_type type = static_cast<xml_node_type>((node->header & impl::xml_memory_page_type_mask) + 1);
+
+ return type == node_pcdata || type == node_cdata;
+ }
+
// get value with conversion functions
PUGI__FN int get_value_int(const char_t* value)
{
@@ -4088,12 +4095,8 @@ namespace pugi
if (!_root) return PUGIXML_TEXT("");
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
- {
- xml_node_type type_ = static_cast<xml_node_type>((i->header & impl::xml_memory_page_type_mask) + 1);
-
- if (i->value && (type_ == node_pcdata || type_ == node_cdata))
+ if (i->value && impl::is_text_node(i))
return i->value;
- }
return PUGIXML_TEXT("");
}
@@ -4710,6 +4713,160 @@ namespace pugi
}
#endif
+ PUGI__FN xml_text::xml_text(xml_node_struct* root): _root(root)
+ {
+ }
+
+ PUGI__FN xml_node_struct* xml_text::_data() const
+ {
+ if (!_root || impl::is_text_node(_root)) return _root;
+
+ for (xml_node_struct* node = _root->first_child; node; node = node->next_sibling)
+ if (impl::is_text_node(node))
+ return node;
+
+ return 0;
+ }
+
+ PUGI__FN xml_node_struct* xml_text::_data_new()
+ {
+ xml_node_struct* data = _data();
+ if (data) return data;
+
+ return xml_node(_root).append_child(node_pcdata).internal_object();
+ }
+
+ PUGI__FN xml_text::xml_text(): _root(0)
+ {
+ }
+
+ PUGI__FN static void unspecified_bool_xml_text(xml_text***)
+ {
+ }
+
+ PUGI__FN xml_text::operator xml_text::unspecified_bool_type() const
+ {
+ return _data() ? unspecified_bool_xml_text : 0;
+ }
+
+ PUGI__FN bool xml_text::operator!() const
+ {
+ return !_data();
+ }
+
+ PUGI__FN bool xml_text::empty() const
+ {
+ return _data() == 0;
+ }
+
+ PUGI__FN const char_t* xml_text::get() const
+ {
+ xml_node_struct* data = _data();
+
+ return (data && data->value) ? data->value : PUGIXML_TEXT("");
+ }
+
+ PUGI__FN int xml_text::as_int() const
+ {
+ const char_t* text = get();
+
+ return text ? impl::get_value_int(text) : 0;
+ }
+
+ PUGI__FN unsigned int xml_text::as_uint() const
+ {
+ const char_t* text = get();
+
+ return text ? impl::get_value_uint(text) : 0;
+ }
+
+ PUGI__FN double xml_text::as_double() const
+ {
+ const char_t* text = get();
+
+ return text ? impl::get_value_double(text) : 0;
+ }
+
+ PUGI__FN float xml_text::as_float() const
+ {
+ const char_t* text = get();
+
+ return text ? impl::get_value_float(text) : 0;
+ }
+
+ PUGI__FN bool xml_text::as_bool() const
+ {
+ const char_t* text = get();
+
+ return text ? impl::get_value_bool(text) : false;
+ }
+
+ PUGI__FN bool xml_text::set(const char_t* rhs)
+ {
+ xml_node_struct* data = _data_new();
+
+ return data ? impl::strcpy_insitu(data->value, data->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ }
+
+ PUGI__FN bool xml_text::set(int rhs)
+ {
+ xml_node_struct* data = _data_new();
+
+ return data ? impl::set_value_convert(data->value, data->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ }
+
+ PUGI__FN bool xml_text::set(unsigned int rhs)
+ {
+ xml_node_struct* data = _data_new();
+
+ return data ? impl::set_value_convert(data->value, data->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ }
+
+ PUGI__FN bool xml_text::set(double rhs)
+ {
+ xml_node_struct* data = _data_new();
+
+ return data ? impl::set_value_convert(data->value, data->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ }
+
+ PUGI__FN bool xml_text::set(bool rhs)
+ {
+ xml_node_struct* data = _data_new();
+
+ return data ? impl::set_value_convert(data->value, data->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ }
+
+ PUGI__FN xml_text& xml_text::operator=(const char_t* rhs)
+ {
+ set(rhs);
+ return *this;
+ }
+
+ PUGI__FN xml_text& xml_text::operator=(int rhs)
+ {
+ set(rhs);
+ return *this;
+ }
+
+ PUGI__FN xml_text& xml_text::operator=(unsigned int rhs)
+ {
+ set(rhs);
+ return *this;
+ }
+
+ PUGI__FN xml_text& xml_text::operator=(double rhs)
+ {
+ set(rhs);
+ return *this;
+ }
+
+ PUGI__FN xml_text& xml_text::operator=(bool rhs)
+ {
+ set(rhs);
+ return *this;
+ }
+
+
PUGI__FN xml_node_iterator::xml_node_iterator()
{
}
diff --git a/src/pugixml.hpp b/src/pugixml.hpp
index 74c520c..23480f6 100644
--- a/src/pugixml.hpp
+++ b/src/pugixml.hpp
@@ -552,6 +552,62 @@ namespace pugi
bool PUGIXML_FUNCTION operator||(const xml_node& lhs, bool rhs);
#endif
+ // A helper for working with text inside PCDATA nodes
+ class PUGIXML_CLASS xml_text
+ {
+ friend class xml_node;
+
+ xml_node_struct* _root;
+
+ typedef void (*unspecified_bool_type)(xml_text***);
+
+ explicit xml_text(xml_node_struct* root);
+
+ xml_node_struct* _data_new();
+ xml_node_struct* _data() const;
+
+ public:
+ // Default constructor. Constructs an empty object.
+ xml_text();
+
+ // Safe bool conversion operator
+ operator unspecified_bool_type() const;
+
+ // Borland C++ workaround
+ bool operator!() const;
+
+ // Check if text object is empty
+ bool empty() const;
+
+ // Get text, or "" if object is empty
+ const char_t* get() const;
+
+ // Get text as a number, or 0 if conversion did not succeed or object is empty
+ int as_int() const;
+ unsigned int as_uint() const;
+ double as_double() const;
+ float as_float() const;
+
+ // Get text as bool (returns true if first character is in '1tTyY' set), or false if object is empty
+ bool as_bool() const;
+
+ // Set text (returns false if object is empty or there is not enough memory)
+ bool set(const char_t* rhs);
+
+ // Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
+ bool set(int rhs);
+ bool set(unsigned int rhs);
+ bool set(double rhs);
+ bool set(bool rhs);
+
+ // Set text (equivalent to set without error checking)
+ xml_text& operator=(const char_t* rhs);
+ xml_text& operator=(int rhs);
+ xml_text& operator=(unsigned int rhs);
+ xml_text& operator=(double rhs);
+ xml_text& operator=(bool rhs);
+ };
+
// Child node iterator (a bidirectional iterator over a collection of xml_node)
class PUGIXML_CLASS xml_node_iterator
{