2021-05-03 14:00:46 +00:00
|
|
|
#include "LocalesUtils.hpp"
|
|
|
|
|
2021-05-10 05:08:38 +00:00
|
|
|
#include <stdexcept>
|
2021-05-03 14:00:46 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
|
|
|
|
CNumericLocalesSetter::CNumericLocalesSetter()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
|
|
|
|
m_orig_numeric_locale = std::setlocale(LC_NUMERIC, nullptr);
|
|
|
|
std::setlocale(LC_NUMERIC, "C");
|
|
|
|
#else
|
|
|
|
m_original_locale = uselocale((locale_t)0);
|
|
|
|
m_new_locale = duplocale(m_original_locale);
|
|
|
|
m_new_locale = newlocale(LC_NUMERIC_MASK, "C", m_new_locale);
|
|
|
|
uselocale(m_new_locale);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CNumericLocalesSetter::~CNumericLocalesSetter()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2021-05-10 10:18:30 +00:00
|
|
|
std::setlocale(LC_NUMERIC, m_orig_numeric_locale.data());
|
2021-05-03 14:00:46 +00:00
|
|
|
#else
|
|
|
|
uselocale(m_original_locale);
|
|
|
|
freelocale(m_new_locale);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool is_decimal_separator_point()
|
|
|
|
{
|
|
|
|
char str[5] = "";
|
|
|
|
sprintf(str, "%.1f", 0.5f);
|
|
|
|
return str[1] == '.';
|
|
|
|
}
|
|
|
|
|
2021-05-10 05:08:38 +00:00
|
|
|
|
|
|
|
double string_to_double_decimal_point(const std::string& str, size_t* pos /* = nullptr*/)
|
|
|
|
{
|
|
|
|
double out;
|
|
|
|
std::istringstream stream(str);
|
|
|
|
if (! (stream >> out))
|
|
|
|
throw std::invalid_argument("string_to_double_decimal_point conversion failed.");
|
|
|
|
if (pos) {
|
|
|
|
if (stream.eof())
|
|
|
|
*pos = str.size();
|
|
|
|
else
|
|
|
|
*pos = stream.tellg();
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-05-03 14:00:46 +00:00
|
|
|
std::string float_to_string_decimal_point(double value, int precision/* = -1*/)
|
|
|
|
{
|
|
|
|
assert(is_decimal_separator_point());
|
|
|
|
std::stringstream buf;
|
|
|
|
if (precision >= 0)
|
|
|
|
buf << std::fixed << std::setprecision(precision);
|
|
|
|
buf << value;
|
|
|
|
return buf.str();
|
|
|
|
}
|
|
|
|
|
2021-05-10 06:13:23 +00:00
|
|
|
//std::string float_to_string_decimal_point(float value, int precision/* = -1*/)
|
|
|
|
//{
|
|
|
|
// return float_to_string_decimal_point(double(value), precision);
|
|
|
|
//}
|
2021-05-03 14:00:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace Slic3r
|
|
|
|
|