Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E57XmlParser: Parse doubles in a local-independent way #173

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 )
Hugal31 marked this conversation as resolved.
Show resolved Hide resolved
{
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