Skip to content

Commit

Permalink
commandlineflags: Replace strtod by std::stringstream
Browse files Browse the repository at this point in the history
Using std::stringstream allows conversion of double to string
independent of the current locale setting.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed May 2, 2019
1 parent d047fa1 commit 4fbc0a2
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/training/commandlineflags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cmath> // for std::isnan, NAN
#include <locale> // for std::locale::classic
#include <sstream> // for std::stringstream
#include "baseapi.h" // TessBaseAPI::Version
#include "commandlineflags.h"
#include "errcode.h"
Expand Down Expand Up @@ -106,9 +109,17 @@ static bool SafeAtoi(const char* str, int* val) {
}

static bool SafeAtod(const char* str, double* val) {
char* endptr = nullptr;
*val = strtod(str, &endptr);
return endptr != nullptr && *endptr == '\0';
double d = NAN;
std::stringstream stream(str);
// Use "C" locale for reading double value.
stream.imbue(std::locale::classic());
stream >> d;
*val = 0;
bool success = !std::isnan(d);
if (success) {
*val = d;
}
return success;
}

static void PrintCommandLineFlags() {
Expand Down

0 comments on commit 4fbc0a2

Please sign in to comment.