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
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 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 @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
12 changes: 12 additions & 0 deletions src/StringFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "StringFunctions.h"

#include <locale>

namespace e57
{
template <class FTYPE> std::string floatingPointToStr( FTYPE value, int precision )
Expand Down Expand Up @@ -53,4 +55,14 @@ namespace e57

template std::string floatingPointToStr<float>( float value, int precision );
template std::string floatingPointToStr<double>( 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;
}

}
4 changes: 4 additions & 0 deletions src/StringFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,8 @@ namespace e57

extern template std::string floatingPointToStr<float>( float value, int precision );
extern template std::string floatingPointToStr<double>( 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 );
}