From 899d3b925ccb249de881bbc957f6e4ddc754cbbd Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 20 Aug 2017 00:34:20 +0300 Subject: Add function for read float and double number from xml. --- src/utils/xml/libxml.cpp | 22 +++++++++++++++++++--- src/utils/xml/libxml.h | 9 ++++++++- src/utils/xml/pugixml.cpp | 21 ++++++++++++++++++--- src/utils/xml/pugixml.h | 9 ++++++++- src/utils/xml/tinyxml2.cpp | 21 ++++++++++++++++++--- src/utils/xml/tinyxml2.h | 9 ++++++++- 6 files changed, 79 insertions(+), 12 deletions(-) (limited to 'src/utils') diff --git a/src/utils/xml/libxml.cpp b/src/utils/xml/libxml.cpp index 90204fb49..251f994d5 100644 --- a/src/utils/xml/libxml.cpp +++ b/src/utils/xml/libxml.cpp @@ -205,9 +205,25 @@ namespace XML return ret; } - double getFloatProperty(XmlNodeConstPtr node, - const char *const name, - double def) + float getFloatProperty(XmlNodeConstPtr node, + const char *const name, + float def) + { + float &ret = def; + + xmlChar *const prop = XmlGetProp(node, name); + if (prop != nullptr) + { + ret = atof(reinterpret_cast(prop)); + xmlFree(prop); + } + + return ret; + } + + double getDoubleProperty(XmlNodeConstPtr node, + const char *const name, + double def) { double &ret = def; diff --git a/src/utils/xml/libxml.h b/src/utils/xml/libxml.h index d9f707a5e..6ec0ce08d 100644 --- a/src/utils/xml/libxml.h +++ b/src/utils/xml/libxml.h @@ -98,7 +98,14 @@ namespace XML /** * Gets an floating point property from an XmlNodePtr. */ - double getFloatProperty(XmlNodeConstPtr node, + float getFloatProperty(XmlNodeConstPtr node, + const char *const name, + float def) A_WARN_UNUSED; + + /** + * Gets an double point property from an XmlNodePtr. + */ + double getDoubleProperty(XmlNodeConstPtr node, const char *const name, double def) A_WARN_UNUSED; diff --git a/src/utils/xml/pugixml.cpp b/src/utils/xml/pugixml.cpp index 12ae1d794..12d885a4f 100644 --- a/src/utils/xml/pugixml.cpp +++ b/src/utils/xml/pugixml.cpp @@ -212,9 +212,24 @@ namespace XML return ret; } - double getFloatProperty(XmlNodeConstPtr node, - const char *const name, - double def) + float getFloatProperty(XmlNodeConstPtr node, + const char *const name, + float def) + { + float &ret = def; + + if (!node) + return ret; + const pugi::xml_attribute &attr = node.attribute(name); + if (!attr.empty()) + ret = atof(attr.value()); + + return ret; + } + + double getDoubleProperty(XmlNodeConstPtr node, + const char *const name, + double def) { double &ret = def; diff --git a/src/utils/xml/pugixml.h b/src/utils/xml/pugixml.h index 14a3bdbdf..e4337140e 100644 --- a/src/utils/xml/pugixml.h +++ b/src/utils/xml/pugixml.h @@ -99,7 +99,14 @@ namespace XML /** * Gets an floating point property from an XmlNodePtr. */ - double getFloatProperty(XmlNodeConstPtr node, + float getFloatProperty(XmlNodeConstPtr node, + const char *const name, + float def) A_WARN_UNUSED; + + /** + * Gets an double point property from an XmlNodePtr. + */ + double getDoubleProperty(XmlNodeConstPtr node, const char *const name, double def) A_WARN_UNUSED; diff --git a/src/utils/xml/tinyxml2.cpp b/src/utils/xml/tinyxml2.cpp index 3a1646168..a7275b7f1 100644 --- a/src/utils/xml/tinyxml2.cpp +++ b/src/utils/xml/tinyxml2.cpp @@ -198,9 +198,24 @@ namespace XML return ret; } - double getFloatProperty(XmlNodeConstPtr node, - const char *const name, - double def) + float getFloatProperty(XmlNodeConstPtr node, + const char *const name, + float def) + { + float &ret = def; + + if (!node) + return ret; + const char *attr = node->Attribute(name); + if (attr != nullptr) + ret = atof(attr); + + return ret; + } + + double getDoubleProperty(XmlNodeConstPtr node, + const char *const name, + double def) { double &ret = def; diff --git a/src/utils/xml/tinyxml2.h b/src/utils/xml/tinyxml2.h index e817f7276..f056dc43a 100644 --- a/src/utils/xml/tinyxml2.h +++ b/src/utils/xml/tinyxml2.h @@ -99,7 +99,14 @@ namespace XML /** * Gets an floating point property from an XmlNodePtr. */ - double getFloatProperty(XmlNodeConstPtr node, + float getFloatProperty(XmlNodeConstPtr node, + const char *const name, + float def) A_WARN_UNUSED; + + /** + * Gets an double point property from an XmlNodePtr. + */ + double getDoubleProperty(XmlNodeConstPtr node, const char *const name, double def) A_WARN_UNUSED; -- cgit v1.2.3-70-g09d2