# HG changeset patch # User Santeri Piippo # Date 1372688857 -10800 # Node ID 4e2425bd4dc78e63f8e9e3a56d16736dc2424fa6 # Parent c731a22899a3be4a85a4a4960c44430a350eecfa Added an atof overload to convert from string to float, hopefully without any precision error diff -r c731a22899a3 -r 4e2425bd4dc7 src/file.cpp --- a/src/file.cpp Thu Jun 27 14:55:32 2013 +0300 +++ b/src/file.cpp Mon Jul 01 17:27:37 2013 +0300 @@ -570,15 +570,11 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -static vertex parseVertex (QStringList& s, const ushort n) { - // Disable the locale while parsing the line or atof's behavior changes - // between locales (i.e. fails to read decimals properly). That is - // quite undesired... - setlocale (LC_NUMERIC, "C"); - +static vertex parseVertex( QStringList& s, const ushort n ) +{ vertex v; - for (const Axis ax : g_Axes) - v[ax] = s[n + ax].toFloat (); + for( const Axis ax : g_Axes ) + v[ax] = atof( s[n + ax] ); return v; } diff -r c731a22899a3 -r 4e2425bd4dc7 src/misc.cpp --- a/src/misc.cpp Thu Jun 27 14:55:32 2013 +0300 +++ b/src/misc.cpp Mon Jul 01 17:27:37 2013 +0300 @@ -249,6 +249,23 @@ return list.join (delim); } +double atof( str val ) +{ + // Disable the locale while parsing the line or atof's behavior changes + // between locales (i.e. fails to read decimals properly). That is + // quite undesired... + setlocale( LC_NUMERIC, "C" ); + char* buf = new char[val.length()]; + char* bufptr = &buf[0]; + + for( qchar& c : val ) + *bufptr++ = c.toAscii(); + *bufptr = '\0'; + + double fval = atof( buf ); + delete[] buf; + return fval; +} // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * diff -r c731a22899a3 -r 4e2425bd4dc7 src/misc.h --- a/src/misc.h Thu Jun 27 14:55:32 2013 +0300 +++ b/src/misc.h Mon Jul 01 17:27:37 2013 +0300 @@ -37,6 +37,8 @@ // Converts a float value to a string value. str ftoa (double num); +double atof( str val ); + // Simplifies the given fraction. void simplify (short& numer, short& denom);