Add extra boolean parameter (def_set) to xml_text::as_XXX functions to allow checking of whether default value is used or not.
This commit is contained in:
parent
9e6dcc292d
commit
72d57eed1c
158
src/pugixml.cpp
158
src/pugixml.cpp
@ -3709,9 +3709,21 @@ PUGI__NS_BEGIN
|
||||
return (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10;
|
||||
}
|
||||
|
||||
PUGI__FN int get_value_int(const char_t* value, int def)
|
||||
PUGI__FN int get_value_int(const char_t* value, int def, bool* def_set)
|
||||
{
|
||||
if (!value) return def;
|
||||
if (!value)
|
||||
{
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = true;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = false;
|
||||
}
|
||||
|
||||
int base = get_integer_base(value);
|
||||
|
||||
@ -3722,9 +3734,21 @@ PUGI__NS_BEGIN
|
||||
#endif
|
||||
}
|
||||
|
||||
PUGI__FN unsigned int get_value_uint(const char_t* value, unsigned int def)
|
||||
PUGI__FN unsigned int get_value_uint(const char_t* value, unsigned int def, bool* def_set)
|
||||
{
|
||||
if (!value) return def;
|
||||
if (!value)
|
||||
{
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = true;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = false;
|
||||
}
|
||||
|
||||
int base = get_integer_base(value);
|
||||
|
||||
@ -3735,9 +3759,21 @@ PUGI__NS_BEGIN
|
||||
#endif
|
||||
}
|
||||
|
||||
PUGI__FN double get_value_double(const char_t* value, double def)
|
||||
PUGI__FN double get_value_double(const char_t* value, double def, bool* def_set)
|
||||
{
|
||||
if (!value) return def;
|
||||
if (!value)
|
||||
{
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = true;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = false;
|
||||
}
|
||||
|
||||
#ifdef PUGIXML_WCHAR_MODE
|
||||
return wcstod(value, 0);
|
||||
@ -3746,7 +3782,7 @@ PUGI__NS_BEGIN
|
||||
#endif
|
||||
}
|
||||
|
||||
PUGI__FN float get_value_float(const char_t* value, float def)
|
||||
PUGI__FN float get_value_float(const char_t* value, float def, bool* def_set)
|
||||
{
|
||||
if (!value) return def;
|
||||
|
||||
@ -3757,9 +3793,21 @@ PUGI__NS_BEGIN
|
||||
#endif
|
||||
}
|
||||
|
||||
PUGI__FN bool get_value_bool(const char_t* value, bool def)
|
||||
PUGI__FN bool get_value_bool(const char_t* value, bool def, bool* def_set)
|
||||
{
|
||||
if (!value) return def;
|
||||
if (!value)
|
||||
{
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = true;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = false;
|
||||
}
|
||||
|
||||
// only look at first char
|
||||
char_t first = *value;
|
||||
@ -3769,9 +3817,21 @@ PUGI__NS_BEGIN
|
||||
}
|
||||
|
||||
#ifdef PUGIXML_HAS_LONG_LONG
|
||||
PUGI__FN long long get_value_llong(const char_t* value, long long def)
|
||||
PUGI__FN long long get_value_llong(const char_t* value, long long def, bool* def_set)
|
||||
{
|
||||
if (!value) return def;
|
||||
if (!value)
|
||||
{
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = true;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = false;
|
||||
}
|
||||
|
||||
int base = get_integer_base(value);
|
||||
|
||||
@ -3790,9 +3850,21 @@ PUGI__NS_BEGIN
|
||||
#endif
|
||||
}
|
||||
|
||||
PUGI__FN unsigned long long get_value_ullong(const char_t* value, unsigned long long def)
|
||||
PUGI__FN unsigned long long get_value_ullong(const char_t* value, unsigned long long def, bool* def_set)
|
||||
{
|
||||
if (!value) return def;
|
||||
if (!value)
|
||||
{
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = true;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = false;
|
||||
}
|
||||
|
||||
int base = get_integer_base(value);
|
||||
|
||||
@ -4348,38 +4420,38 @@ namespace pugi
|
||||
|
||||
PUGI__FN int xml_attribute::as_int(int def) const
|
||||
{
|
||||
return impl::get_value_int(_attr ? _attr->value : 0, def);
|
||||
return impl::get_value_int(_attr ? _attr->value : 0, def, NULL);
|
||||
}
|
||||
|
||||
PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const
|
||||
{
|
||||
return impl::get_value_uint(_attr ? _attr->value : 0, def);
|
||||
return impl::get_value_uint(_attr ? _attr->value : 0, def, NULL);
|
||||
}
|
||||
|
||||
PUGI__FN double xml_attribute::as_double(double def) const
|
||||
{
|
||||
return impl::get_value_double(_attr ? _attr->value : 0, def);
|
||||
return impl::get_value_double(_attr ? _attr->value : 0, def, NULL);
|
||||
}
|
||||
|
||||
PUGI__FN float xml_attribute::as_float(float def) const
|
||||
{
|
||||
return impl::get_value_float(_attr ? _attr->value : 0, def);
|
||||
return impl::get_value_float(_attr ? _attr->value : 0, def, NULL);
|
||||
}
|
||||
|
||||
PUGI__FN bool xml_attribute::as_bool(bool def) const
|
||||
{
|
||||
return impl::get_value_bool(_attr ? _attr->value : 0, def);
|
||||
return impl::get_value_bool(_attr ? _attr->value : 0, def, NULL);
|
||||
}
|
||||
|
||||
#ifdef PUGIXML_HAS_LONG_LONG
|
||||
PUGI__FN long long xml_attribute::as_llong(long long def) const
|
||||
{
|
||||
return impl::get_value_llong(_attr ? _attr->value : 0, def);
|
||||
return impl::get_value_llong(_attr ? _attr->value : 0, def, NULL);
|
||||
}
|
||||
|
||||
PUGI__FN unsigned long long xml_attribute::as_ullong(unsigned long long def) const
|
||||
{
|
||||
return impl::get_value_ullong(_attr ? _attr->value : 0, def);
|
||||
return impl::get_value_ullong(_attr ? _attr->value : 0, def, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -5441,61 +5513,75 @@ namespace pugi
|
||||
return (d && d->value) ? d->value : PUGIXML_TEXT("");
|
||||
}
|
||||
|
||||
PUGI__FN const char_t* xml_text::as_string(const char_t* def) const
|
||||
PUGI__FN const char_t* xml_text::as_string(const char_t* def, bool* def_set) const
|
||||
{
|
||||
xml_node_struct* d = _data();
|
||||
|
||||
return (d && d->value) ? d->value : def;
|
||||
if(d && d->value)
|
||||
{
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = false;
|
||||
}
|
||||
return d->value;
|
||||
}
|
||||
|
||||
if(def_set)
|
||||
{
|
||||
*def_set = true;
|
||||
}
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
PUGI__FN int xml_text::as_int(int def) const
|
||||
PUGI__FN int xml_text::as_int(int def, bool* def_set) const
|
||||
{
|
||||
xml_node_struct* d = _data();
|
||||
|
||||
return impl::get_value_int(d ? d->value : 0, def);
|
||||
return impl::get_value_int(d ? d->value : 0, def, def_set);
|
||||
}
|
||||
|
||||
PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const
|
||||
PUGI__FN unsigned int xml_text::as_uint(unsigned int def, bool* def_set) const
|
||||
{
|
||||
xml_node_struct* d = _data();
|
||||
|
||||
return impl::get_value_uint(d ? d->value : 0, def);
|
||||
return impl::get_value_uint(d ? d->value : 0, def, def_set);
|
||||
}
|
||||
|
||||
PUGI__FN double xml_text::as_double(double def) const
|
||||
PUGI__FN double xml_text::as_double(double def, bool* def_set) const
|
||||
{
|
||||
xml_node_struct* d = _data();
|
||||
|
||||
return impl::get_value_double(d ? d->value : 0, def);
|
||||
return impl::get_value_double(d ? d->value : 0, def, def_set);
|
||||
}
|
||||
|
||||
PUGI__FN float xml_text::as_float(float def) const
|
||||
PUGI__FN float xml_text::as_float(float def, bool* def_set) const
|
||||
{
|
||||
xml_node_struct* d = _data();
|
||||
|
||||
return impl::get_value_float(d ? d->value : 0, def);
|
||||
return impl::get_value_float(d ? d->value : 0, def, def_set);
|
||||
}
|
||||
|
||||
PUGI__FN bool xml_text::as_bool(bool def) const
|
||||
PUGI__FN bool xml_text::as_bool(bool def, bool* def_set) const
|
||||
{
|
||||
xml_node_struct* d = _data();
|
||||
|
||||
return impl::get_value_bool(d ? d->value : 0, def);
|
||||
return impl::get_value_bool(d ? d->value : 0, def, def_set);
|
||||
}
|
||||
|
||||
#ifdef PUGIXML_HAS_LONG_LONG
|
||||
PUGI__FN long long xml_text::as_llong(long long def) const
|
||||
PUGI__FN long long xml_text::as_llong(long long def, bool* def_set) const
|
||||
{
|
||||
xml_node_struct* d = _data();
|
||||
|
||||
return impl::get_value_llong(d ? d->value : 0, def);
|
||||
return impl::get_value_llong(d ? d->value : 0, def, def_set);
|
||||
}
|
||||
|
||||
PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const
|
||||
PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def, bool* def_set) const
|
||||
{
|
||||
xml_node_struct* d = _data();
|
||||
|
||||
return impl::get_value_ullong(d ? d->value : 0, def);
|
||||
return impl::get_value_ullong(d ? d->value : 0, def, def_set);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@ -666,21 +666,21 @@ namespace pugi
|
||||
const char_t* get() const;
|
||||
|
||||
// Get text, or the default value if object is empty
|
||||
const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;
|
||||
const char_t* as_string(const char_t* def = PUGIXML_TEXT(""), bool* def_set = NULL) 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;
|
||||
int as_int(int def = 0, bool* def_set = NULL) const;
|
||||
unsigned int as_uint(unsigned int def = 0, bool* def_set = NULL) const;
|
||||
double as_double(double def = 0, bool* def_set = NULL) const;
|
||||
float as_float(float def = 0, bool* def_set = NULL) const;
|
||||
|
||||
#ifdef PUGIXML_HAS_LONG_LONG
|
||||
long long as_llong(long long def = 0) const;
|
||||
unsigned long long as_ullong(unsigned long long def = 0) const;
|
||||
long long as_llong(long long def = 0, bool* def_set = NULL) const;
|
||||
unsigned long long as_ullong(unsigned long long def = 0, bool* def_set = NULL) const;
|
||||
#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;
|
||||
bool as_bool(bool def = false, bool* def_set = NULL) const;
|
||||
|
||||
// Set text (returns false if object is empty or there is not enough memory)
|
||||
bool set(const char_t* rhs);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user