Skip to content

Commit

Permalink
E57XmlParser: Parse doubles in a local-independent way
Browse files Browse the repository at this point in the history
Fixes #172
  • Loading branch information
Hugal31 committed Nov 7, 2022
1 parent 1108d20 commit b816db9
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/E57XmlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/

#include <limits>
#include <locale>
#include <sstream>

#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
Expand Down Expand Up @@ -83,6 +85,17 @@ inline int64_t convertStrToLL( const std::string &inStr )
#endif
}

/// Parse a double according the the classic ("C") locale.
/// \return The parsed double or 0.0 on error.
inline double convertStrToDouble( const std::string &inStr )
{
std::istringstream iss{ inStr };
iss.imbue( std::locale::classic() );
double res = 0.;
iss >> res;
return res;
}

//=============================================================================
// E57FileInputStream

Expand Down Expand Up @@ -371,7 +384,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 = convertStrToDouble( scale_str ); //??? use exact rounding library
}
else
{
Expand All @@ -382,7 +395,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 = convertStrToDouble( offset_str ); //??? use exact rounding library
}
else
{
Expand Down Expand Up @@ -428,7 +441,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 = convertStrToDouble( minimum_str ); //??? use exact rounding library
}
else
{
Expand All @@ -447,7 +460,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 = convertStrToDouble( maximum_str ); //??? use exact rounding library
}
else
{
Expand Down Expand Up @@ -704,7 +717,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 = convertStrToDouble( pi.childText );
}
else
{
Expand Down

0 comments on commit b816db9

Please sign in to comment.