Skip to content

Commit

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

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed May 2, 2019
1 parent ed45656 commit e3860e4
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/classify/clusttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

//--------------------------Include Files----------------------------------
#include "clusttool.h"
#include <cmath>
#include <cmath> // for std::isnan
#include <locale> // for std::locale::classic
#include <sstream> // for std::stringstream
#include "emalloc.h"

using tesseract::TFile;
Expand Down Expand Up @@ -53,16 +55,18 @@ static float *ReadNFloats(TFile *fp, uint16_t N, float Buffer[]) {
needs_free = true;
}

char *startptr = line;
for (int i = 0; i < N; i++) {
char *endptr;
Buffer[i] = strtof(startptr, &endptr);
if (endptr == startptr) {
tprintf("Read of %d floats failed!\n", N);
std::stringstream stream(line);
// Use "C" locale (needed for float values Buffer[i]).
stream.imbue(std::locale::classic());
for (uint16_t i = 0; i < N; i++) {
float f = NAN;
stream >> f;
if (std::isnan(f)) {
tprintf("Read of %u floats failed!\n", N);
if (needs_free) Efree(Buffer);
return nullptr;
}
startptr = endptr;
Buffer[i] = f;
}
return Buffer;
}
Expand Down

0 comments on commit e3860e4

Please sign in to comment.