diff --git a/src/E57XmlParser.cpp b/src/E57XmlParser.cpp index c699b81..ea7ff04 100644 --- a/src/E57XmlParser.cpp +++ b/src/E57XmlParser.cpp @@ -26,6 +26,8 @@ */ #include +#include +#include #include #include @@ -371,7 +373,7 @@ void E57XmlParser::startElement( const XMLCh *const uri, const XMLCh *const loca if ( isAttributeDefined( attributes, att_scale ) ) { ustring scale_str = lookupAttribute( attributes, att_scale ); - pi.scale = atof( scale_str.c_str() ); //??? use exact rounding library + pi.scale = strToDouble( scale_str ); //??? use exact rounding library } else { @@ -382,7 +384,7 @@ void E57XmlParser::startElement( const XMLCh *const uri, const XMLCh *const loca if ( isAttributeDefined( attributes, att_offset ) ) { ustring offset_str = lookupAttribute( attributes, att_offset ); - pi.offset = atof( offset_str.c_str() ); //??? use exact rounding library + pi.offset = strToDouble( offset_str ); //??? use exact rounding library } else { @@ -428,7 +430,7 @@ void E57XmlParser::startElement( const XMLCh *const uri, const XMLCh *const loca if ( isAttributeDefined( attributes, att_minimum ) ) { ustring minimum_str = lookupAttribute( attributes, att_minimum ); - pi.floatMinimum = atof( minimum_str.c_str() ); //??? use exact rounding library + pi.floatMinimum = strToDouble( minimum_str ); //??? use exact rounding library } else { @@ -447,7 +449,7 @@ void E57XmlParser::startElement( const XMLCh *const uri, const XMLCh *const loca if ( isAttributeDefined( attributes, att_maximum ) ) { ustring maximum_str = lookupAttribute( attributes, att_maximum ); - pi.floatMaximum = atof( maximum_str.c_str() ); //??? use exact rounding library + pi.floatMaximum = strToDouble( maximum_str ); //??? use exact rounding library } else { @@ -704,7 +706,7 @@ void E57XmlParser::endElement( const XMLCh *const uri, const XMLCh *const localN double floatValue; if ( pi.childText.length() > 0 ) { - floatValue = atof( pi.childText.c_str() ); + floatValue = strToDouble( pi.childText ); } else { diff --git a/src/StringFunctions.cpp b/src/StringFunctions.cpp index 9b2c410..c52ef36 100644 --- a/src/StringFunctions.cpp +++ b/src/StringFunctions.cpp @@ -1,5 +1,7 @@ #include "StringFunctions.h" +#include + namespace e57 { template std::string floatingPointToStr( FTYPE value, int precision ) @@ -53,4 +55,14 @@ namespace e57 template std::string floatingPointToStr( float value, int precision ); template std::string floatingPointToStr( double value, int precision ); + + double strToDouble( const std::string &inStr ) + { + std::istringstream iss{ inStr }; + iss.imbue( std::locale::classic() ); + double res = 0.; + iss >> res; + return res; + } + } diff --git a/src/StringFunctions.h b/src/StringFunctions.h index 7401a39..ab313ac 100644 --- a/src/StringFunctions.h +++ b/src/StringFunctions.h @@ -197,4 +197,8 @@ namespace e57 extern template std::string floatingPointToStr( float value, int precision ); extern template std::string floatingPointToStr( double value, int precision ); + + /// Parse a double according the the classic ("C") locale. + /// @return The parsed double or 0.0 on error. + double strToDouble( const std::string &inStr ); }