mirror of
https://github.com/QIDITECH/QIDISlicer.git
synced 2026-02-04 18:08:44 +03:00
update bundled_deps
This commit is contained in:
100
bundled_deps/localesutils/LocalesUtils.cpp
Normal file
100
bundled_deps/localesutils/LocalesUtils.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "LocalesUtils.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <charconv>
|
||||
#endif
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
#include <fast_float.h>
|
||||
|
||||
|
||||
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");
|
||||
#elif __APPLE__
|
||||
m_original_locale = uselocale((locale_t)0);
|
||||
m_new_locale = newlocale(LC_NUMERIC_MASK, "C", m_original_locale);
|
||||
uselocale(m_new_locale);
|
||||
#else // linux / BSD
|
||||
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
|
||||
std::setlocale(LC_NUMERIC, m_orig_numeric_locale.data());
|
||||
#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] == '.';
|
||||
}
|
||||
|
||||
template<class T>
|
||||
static T string_to_floating_decimal_point(const std::string_view str, size_t* pos /* = nullptr*/)
|
||||
{
|
||||
T out;
|
||||
size_t p = fast_float::from_chars(str.data(), str.data() + str.size(), out).ptr - str.data();
|
||||
if (pos)
|
||||
*pos = p;
|
||||
return out;
|
||||
}
|
||||
|
||||
double string_to_double_decimal_point(const std::string_view str, size_t* pos /* = nullptr*/)
|
||||
{
|
||||
return string_to_floating_decimal_point<double>(str, pos);
|
||||
}
|
||||
|
||||
float string_to_float_decimal_point(const std::string_view str, size_t* pos /* = nullptr*/)
|
||||
{
|
||||
return string_to_floating_decimal_point<float>(str, pos);
|
||||
}
|
||||
|
||||
std::string float_to_string_decimal_point(double value, int precision/* = -1*/)
|
||||
{
|
||||
// Our Windows build server fully supports C++17 std::to_chars. Let's use it.
|
||||
// Other platforms are behind, fall back to slow stringstreams for now.
|
||||
#ifdef _WIN32
|
||||
constexpr size_t SIZE = 20;
|
||||
char out[SIZE] = "";
|
||||
std::to_chars_result res;
|
||||
if (precision >=0)
|
||||
res = std::to_chars(out, out+SIZE, value, std::chars_format::fixed, precision);
|
||||
else
|
||||
res = std::to_chars(out, out+SIZE, value, std::chars_format::general, 6);
|
||||
if (res.ec == std::errc::value_too_large)
|
||||
throw std::invalid_argument("float_to_string_decimal_point conversion failed.");
|
||||
return std::string(out, res.ptr - out);
|
||||
#else
|
||||
std::stringstream buf;
|
||||
if (precision >= 0)
|
||||
buf << std::fixed << std::setprecision(precision);
|
||||
buf << value;
|
||||
return buf.str();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
} // namespace Slic3r
|
||||
|
||||
68
bundled_deps/localesutils/LocalesUtils.hpp
Normal file
68
bundled_deps/localesutils/LocalesUtils.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#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);
|
||||
float string_to_float_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_
|
||||
Reference in New Issue
Block a user