blob: ce8030cebcdf1c3a04d54d95da050212d6b0f1ff (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#ifndef slic3r_LocalesUtils_hpp_
#define slic3r_LocalesUtils_hpp_
#include <string>
#include <clocale>
#include <iomanip>
#include <cassert>
#include <string_view>
#ifdef __APPLE__
#include <xlocale.h>
#endif
namespace Slic3r {
// RAII wrapper that sets LC_NUMERIC to "C" on construction
// and restores the old value on destruction.
class CNumericLocalesSetter {
public:
CNumericLocalesSetter();
~CNumericLocalesSetter();
private:
#ifdef _WIN32
std::string m_orig_numeric_locale;
#else
locale_t m_original_locale;
locale_t m_new_locale;
#endif
};
// A function to check that current C locale uses decimal point as a separator.
// Intended mostly for asserts.
bool is_decimal_separator_point();
// A substitute for std::to_string that works according to
// C++ locales, not C locale. Meant to be used when we need
// to be sure that decimal point is used as a separator.
// (We use user C locales and "C" C++ locales in most of the code.)
std::string float_to_string_decimal_point(double value, int precision = -1);
//std::string float_to_string_decimal_point(float value, int precision = -1);
double string_to_double_decimal_point(const std::string_view str, size_t* pos = nullptr);
// Set locales to "C".
inline void set_c_locales()
{
#ifdef _WIN32
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
std::setlocale(LC_ALL, "C");
#else
// We are leaking some memory here, because the newlocale() produced memory will never be released.
// This is not a problem though, as there will be a maximum one worker thread created per physical thread.
uselocale(newlocale(
#ifdef __APPLE__
LC_ALL_MASK
#else // some Unix / Linux / BSD
LC_ALL
#endif
, "C", nullptr));
#endif
}
} // namespace Slic3r
#endif // slic3r_LocalesUtils_hpp_
|