Skip to content

Commit a113be0

Browse files
authored
Use C locale for floats in HLSLParser (#435)
1 parent b8fc74a commit a113be0

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/libprojectM/Renderer/hlslparser/src/Engine.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
#include <stdio.h> // vsnprintf
55
#include <string.h> // strcmp, strcasecmp
6-
#include <stdlib.h> // strtod, strtol
7-
6+
#include <stdlib.h> // strtol
7+
#include <locale>
8+
#include <sstream>
89

910
namespace M4 {
1011

@@ -40,7 +41,11 @@ int String_Printf(char * buffer, int size, const char * format, ...) {
4041
}
4142

4243
int String_FormatFloat(char * buffer, int size, float value) {
43-
return String_Printf(buffer, size, "%f", value);
44+
std::ostringstream oss;
45+
oss.imbue(std::locale("C"));
46+
oss << value;
47+
48+
return String_Printf(buffer, size, "%s", oss.str().c_str());
4449
}
4550

4651
bool String_Equal(const char * a, const char * b) {
@@ -59,8 +64,32 @@ bool String_EqualNoCase(const char * a, const char * b) {
5964
#endif
6065
}
6166

67+
static inline double iss_strtod(const char * in, char ** end) {
68+
char * in_var = const_cast<char *>(in);
69+
double df;
70+
std::istringstream iss(in);
71+
iss.imbue(std::locale("C"));
72+
iss >> df;
73+
if(iss.fail()) {
74+
*end = in_var;
75+
return 0.0;
76+
}
77+
if(iss.eof()) {
78+
*end = in_var + strlen(in);
79+
return df;
80+
}
81+
82+
std::streampos pos = iss.tellg();
83+
if(iss.fail()) {
84+
*end = in_var;
85+
return 0.0;
86+
}
87+
*end = in_var + pos;
88+
return df;
89+
}
90+
6291
double String_ToDouble(const char * str, char ** endptr) {
63-
return strtod(str, endptr);
92+
return iss_strtod(str, endptr);
6493
}
6594

6695
int String_ToInteger(const char * str, char ** endptr) {

0 commit comments

Comments
 (0)