summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarseny.kapoulkine@gmail.com <arseny.kapoulkine@gmail.com@99668b35-9821-0410-8761-19e4c4f06640>2012-04-03 04:44:36 +0000
committerarseny.kapoulkine@gmail.com <arseny.kapoulkine@gmail.com@99668b35-9821-0410-8761-19e4c4f06640>2012-04-03 04:44:36 +0000
commit40777b2ce1fd576fcdc58cb58fc60b81da3bf791 (patch)
tree96b469499113c3e57198b5c62cec6d65dfb37559
parentf4ac43c549fe98157c80c6c3cd9b65fc42e26d08 (diff)
Added xml_attribute::as_string and xml_text::as_string, added default value to all as_* member functions
git-svn-id: http://pugixml.googlecode.com/svn/trunk@893 99668b35-9821-0410-8761-19e4c4f06640
-rw-r--r--src/pugixml.cpp82
-rw-r--r--src/pugixml.hpp34
2 files changed, 72 insertions, 44 deletions
diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index f6e5b5c..0476f50 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -3288,9 +3288,9 @@ PUGI__NS_BEGIN
}
// get value with conversion functions
- PUGI__FN int get_value_int(const char_t* value)
+ PUGI__FN int get_value_int(const char_t* value, int def)
{
- if (!value) return 0;
+ if (!value) return def;
#ifdef PUGIXML_WCHAR_MODE
return static_cast<int>(wcstol(value, 0, 10));
@@ -3299,9 +3299,9 @@ PUGI__NS_BEGIN
#endif
}
- PUGI__FN unsigned int get_value_uint(const char_t* value)
+ PUGI__FN unsigned int get_value_uint(const char_t* value, unsigned int def)
{
- if (!value) return 0;
+ if (!value) return def;
#ifdef PUGIXML_WCHAR_MODE
return static_cast<unsigned int>(wcstoul(value, 0, 10));
@@ -3310,9 +3310,9 @@ PUGI__NS_BEGIN
#endif
}
- PUGI__FN double get_value_double(const char_t* value)
+ PUGI__FN double get_value_double(const char_t* value, double def)
{
- if (!value) return 0;
+ if (!value) return def;
#ifdef PUGIXML_WCHAR_MODE
return wcstod(value, 0);
@@ -3321,9 +3321,9 @@ PUGI__NS_BEGIN
#endif
}
- PUGI__FN float get_value_float(const char_t* value)
+ PUGI__FN float get_value_float(const char_t* value, float def)
{
- if (!value) return 0;
+ if (!value) return def;
#ifdef PUGIXML_WCHAR_MODE
return static_cast<float>(wcstod(value, 0));
@@ -3332,9 +3332,9 @@ PUGI__NS_BEGIN
#endif
}
- PUGI__FN bool get_value_bool(const char_t* value)
+ PUGI__FN bool get_value_bool(const char_t* value, bool def)
{
- if (!value) return false;
+ if (!value) return def;
// only look at first char
char_t first = *value;
@@ -3775,29 +3775,34 @@ namespace pugi
return _attr && _attr->prev_attribute_c->next_attribute ? xml_attribute(_attr->prev_attribute_c) : xml_attribute();
}
- PUGI__FN int xml_attribute::as_int() const
+ PUGI__FN const char_t* xml_attribute::as_string(const char_t* def) const
{
- return impl::get_value_int(_attr ? _attr->value : 0);
+ return (_attr && _attr->value) ? _attr->value : def;
}
- PUGI__FN unsigned int xml_attribute::as_uint() const
+ PUGI__FN int xml_attribute::as_int(int def) const
{
- return impl::get_value_uint(_attr ? _attr->value : 0);
+ return impl::get_value_int(_attr ? _attr->value : 0, def);
}
- PUGI__FN double xml_attribute::as_double() const
+ PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const
{
- return impl::get_value_double(_attr ? _attr->value : 0);
+ return impl::get_value_uint(_attr ? _attr->value : 0, def);
}
- PUGI__FN float xml_attribute::as_float() const
+ PUGI__FN double xml_attribute::as_double(double def) const
{
- return impl::get_value_float(_attr ? _attr->value : 0);
+ return impl::get_value_double(_attr ? _attr->value : 0, def);
}
- PUGI__FN bool xml_attribute::as_bool() const
+ PUGI__FN float xml_attribute::as_float(float def) const
{
- return impl::get_value_bool(_attr ? _attr->value : 0);
+ return impl::get_value_float(_attr ? _attr->value : 0, def);
+ }
+
+ PUGI__FN bool xml_attribute::as_bool(bool def) const
+ {
+ return impl::get_value_bool(_attr ? _attr->value : 0, def);
}
PUGI__FN bool xml_attribute::empty() const
@@ -4768,29 +4773,46 @@ namespace pugi
return (d && d->value) ? d->value : PUGIXML_TEXT("");
}
- PUGI__FN int xml_text::as_int() const
+ PUGI__FN const char_t* xml_text::as_string(const char_t* def) const
{
- return impl::get_value_int(get());
+ xml_node_struct* d = _data();
+
+ return (d && d->value) ? d->value : def;
+ }
+
+ PUGI__FN int xml_text::as_int(int def) const
+ {
+ xml_node_struct* d = _data();
+
+ return impl::get_value_int(d ? d->value : 0, def);
}
- PUGI__FN unsigned int xml_text::as_uint() const
+ PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const
{
- return impl::get_value_uint(get());
+ xml_node_struct* d = _data();
+
+ return impl::get_value_uint(d ? d->value : 0, def);
}
- PUGI__FN double xml_text::as_double() const
+ PUGI__FN double xml_text::as_double(double def) const
{
- return impl::get_value_double(get());
+ xml_node_struct* d = _data();
+
+ return impl::get_value_double(d ? d->value : 0, def);
}
- PUGI__FN float xml_text::as_float() const
+ PUGI__FN float xml_text::as_float(float def) const
{
- return impl::get_value_float(get());
+ xml_node_struct* d = _data();
+
+ return impl::get_value_float(d ? d->value : 0, def);
}
- PUGI__FN bool xml_text::as_bool() const
+ PUGI__FN bool xml_text::as_bool(bool def) const
{
- return impl::get_value_bool(get());
+ xml_node_struct* d = _data();
+
+ return impl::get_value_bool(d ? d->value : 0, def);
}
PUGI__FN bool xml_text::set(const char_t* rhs)
diff --git a/src/pugixml.hpp b/src/pugixml.hpp
index 8a781ea..0ca0e6f 100644
--- a/src/pugixml.hpp
+++ b/src/pugixml.hpp
@@ -308,14 +308,17 @@ namespace pugi
const char_t* name() const;
const char_t* value() const;
- // Get attribute value as a number, or 0 if conversion did not succeed or attribute is empty
- int as_int() const;
- unsigned int as_uint() const;
- double as_double() const;
- float as_float() const;
+ // Get attribute value, or the default value if attribute is empty
+ const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;
- // Get attribute value as bool (returns true if first character is in '1tTyY' set), or false if attribute is empty
- bool as_bool() const;
+ // 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;
+ double as_double(double def = 0) const;
+ float as_float(float def = 0) const;
+
+ // Get attribute value as bool (returns true if first character is in '1tTyY' set), or the default value if attribute is empty
+ bool as_bool(bool def = false) const;
// Set attribute name/value (returns false if attribute is empty or there is not enough memory)
bool set_name(const char_t* rhs);
@@ -613,14 +616,17 @@ namespace pugi
// 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, or the default value if object is empty
+ const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;
+
+ // Get text as a number, or the default value if conversion did not succeed or object is empty
+ int as_int(int def = 0) const;
+ unsigned int as_uint(unsigned int def = 0) const;
+ double as_double(double def = 0) const;
+ float as_float(float def = 0) const;
- // Get text as bool (returns true if first character is in '1tTyY' set), or false if object is empty
- bool as_bool() const;
+ // 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;
// Set text (returns false if object is empty or there is not enough memory)
bool set(const char_t* rhs);