Skip to content

Commit e3860e4

Browse files
committed
clusttool: Replace strtof by std::stringstream
Using std::stringstream allows conversion of float to string independent of the current locale setting. Signed-off-by: Stefan Weil <[email protected]>
1 parent ed45656 commit e3860e4

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/classify/clusttool.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
//--------------------------Include Files----------------------------------
1919
#include "clusttool.h"
20-
#include <cmath>
20+
#include <cmath> // for std::isnan
21+
#include <locale> // for std::locale::classic
22+
#include <sstream> // for std::stringstream
2123
#include "emalloc.h"
2224

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

56-
char *startptr = line;
57-
for (int i = 0; i < N; i++) {
58-
char *endptr;
59-
Buffer[i] = strtof(startptr, &endptr);
60-
if (endptr == startptr) {
61-
tprintf("Read of %d floats failed!\n", N);
58+
std::stringstream stream(line);
59+
// Use "C" locale (needed for float values Buffer[i]).
60+
stream.imbue(std::locale::classic());
61+
for (uint16_t i = 0; i < N; i++) {
62+
float f = NAN;
63+
stream >> f;
64+
if (std::isnan(f)) {
65+
tprintf("Read of %u floats failed!\n", N);
6266
if (needs_free) Efree(Buffer);
6367
return nullptr;
6468
}
65-
startptr = endptr;
69+
Buffer[i] = f;
6670
}
6771
return Buffer;
6872
}

0 commit comments

Comments
 (0)