summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>2015-01-16 20:54:58 -0800
committerArseny Kapoulkine <arseny.kapoulkine@gmail.com>2015-01-16 20:54:58 -0800
commit550ab4b6356ba203acb6aaebcd3d41acb9e1fa4a (patch)
treeff9bdb4553d7c7c23ecf3e6822d5bd92fdae15fc /src
parentf3e42969a5118247de548c059e9bed69cdf208bb (diff)
parentcf72c20ca173bbad227e8f70bd68a2dbfb5b2890 (diff)
Merge pull request #27 from sdoiel61/master
Make float/double round-trip This change also adds xml_text::set and xml_attribute::set_value overloads for float so that float is only printed using just enough digits to represent float, instead of enough digits to represent double.
Diffstat (limited to 'src')
-rw-r--r--src/pugixml.cpp36
-rw-r--r--src/pugixml.hpp4
2 files changed, 39 insertions, 1 deletions
diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index dd3f427..9760e9f 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -3954,10 +3954,18 @@ PUGI__NS_BEGIN
return set_value_buffer(dest, header, header_mask, buf);
}
+ PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, float value)
+ {
+ char buf[128];
+ sprintf(buf, "%.9g", value);
+
+ return set_value_buffer(dest, header, header_mask, buf);
+ }
+
PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, double value)
{
char buf[128];
- sprintf(buf, "%g", value);
+ sprintf(buf, "%.17g", value);
return set_value_buffer(dest, header, header_mask, buf);
}
@@ -4545,6 +4553,12 @@ namespace pugi
return *this;
}
+ PUGI__FN xml_attribute& xml_attribute::operator=(float rhs)
+ {
+ set_value(rhs);
+ return *this;
+ }
+
PUGI__FN xml_attribute& xml_attribute::operator=(bool rhs)
{
set_value(rhs);
@@ -4600,6 +4614,13 @@ namespace pugi
return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
}
+ PUGI__FN bool xml_attribute::set_value(float rhs)
+ {
+ if (!_attr) return false;
+
+ return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
+ }
+
PUGI__FN bool xml_attribute::set_value(bool rhs)
{
if (!_attr) return false;
@@ -5603,6 +5624,13 @@ namespace pugi
return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
}
+ PUGI__FN bool xml_text::set(float rhs)
+ {
+ xml_node_struct* dn = _data_new();
+
+ return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ }
+
PUGI__FN bool xml_text::set(double rhs)
{
xml_node_struct* dn = _data_new();
@@ -5657,6 +5685,12 @@ namespace pugi
return *this;
}
+ PUGI__FN xml_text& xml_text::operator=(float rhs)
+ {
+ set(rhs);
+ return *this;
+ }
+
PUGI__FN xml_text& xml_text::operator=(bool rhs)
{
set(rhs);
diff --git a/src/pugixml.hpp b/src/pugixml.hpp
index 2432a3f..240b5aa 100644
--- a/src/pugixml.hpp
+++ b/src/pugixml.hpp
@@ -352,6 +352,7 @@ namespace pugi
bool set_value(int rhs);
bool set_value(unsigned int rhs);
bool set_value(double rhs);
+ bool set_value(float rhs);
bool set_value(bool rhs);
#ifdef PUGIXML_HAS_LONG_LONG
@@ -364,6 +365,7 @@ namespace pugi
xml_attribute& operator=(int rhs);
xml_attribute& operator=(unsigned int rhs);
xml_attribute& operator=(double rhs);
+ xml_attribute& operator=(float rhs);
xml_attribute& operator=(bool rhs);
#ifdef PUGIXML_HAS_LONG_LONG
@@ -693,6 +695,7 @@ namespace pugi
// 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(float rhs);
bool set(double rhs);
bool set(bool rhs);
@@ -706,6 +709,7 @@ namespace pugi
xml_text& operator=(int rhs);
xml_text& operator=(unsigned int rhs);
xml_text& operator=(double rhs);
+ xml_text& operator=(float rhs);
xml_text& operator=(bool rhs);
#ifdef PUGIXML_HAS_LONG_LONG