Fixed second batch of locale-dependent calls

This commit is contained in:
Lukas Matena 2021-05-10 07:08:38 +02:00
parent 9ee2fc8275
commit fef385cd6b
13 changed files with 78 additions and 45 deletions

View File

@ -1,5 +1,4 @@
#include "Arrange.hpp" #include "Arrange.hpp"
#include "SVG.hpp"
#include "BoundingBox.hpp" #include "BoundingBox.hpp"

View File

@ -2,6 +2,7 @@
#include "../Exception.hpp" #include "../Exception.hpp"
#include "../Model.hpp" #include "../Model.hpp"
#include "../Utils.hpp" #include "../Utils.hpp"
#include "../LocalesUtils.hpp"
#include "../GCode.hpp" #include "../GCode.hpp"
#include "../Geometry.hpp" #include "../Geometry.hpp"
#include "../GCode/ThumbnailData.hpp" #include "../GCode/ThumbnailData.hpp"
@ -2408,6 +2409,7 @@ namespace Slic3r {
}; };
auto format_coordinate = [](float f, char *buf) -> char* { auto format_coordinate = [](float f, char *buf) -> char* {
assert(is_decimal_separator_point());
#if EXPORT_3MF_USE_SPIRIT_KARMA_FP #if EXPORT_3MF_USE_SPIRIT_KARMA_FP
// Slightly faster than sprintf("%.9g"), but there is an issue with the karma floating point formatter, // Slightly faster than sprintf("%.9g"), but there is an issue with the karma floating point formatter,
// https://github.com/boostorg/spirit/pull/586 // https://github.com/boostorg/spirit/pull/586
@ -2575,6 +2577,7 @@ namespace Slic3r {
bool _3MF_Exporter::_add_layer_height_profile_file_to_archive(mz_zip_archive& archive, Model& model) bool _3MF_Exporter::_add_layer_height_profile_file_to_archive(mz_zip_archive& archive, Model& model)
{ {
assert(is_decimal_separator_point());
std::string out = ""; std::string out = "";
char buffer[1024]; char buffer[1024];
@ -2666,6 +2669,7 @@ namespace Slic3r {
bool _3MF_Exporter::_add_sla_support_points_file_to_archive(mz_zip_archive& archive, Model& model) bool _3MF_Exporter::_add_sla_support_points_file_to_archive(mz_zip_archive& archive, Model& model)
{ {
assert(is_decimal_separator_point());
std::string out = ""; std::string out = "";
char buffer[1024]; char buffer[1024];
@ -2700,6 +2704,7 @@ namespace Slic3r {
bool _3MF_Exporter::_add_sla_drain_holes_file_to_archive(mz_zip_archive& archive, Model& model) bool _3MF_Exporter::_add_sla_drain_holes_file_to_archive(mz_zip_archive& archive, Model& model)
{ {
assert(is_decimal_separator_point());
const char *const fmt = "object_id=%d|"; const char *const fmt = "object_id=%d|";
std::string out; std::string out;
@ -2750,6 +2755,7 @@ namespace Slic3r {
bool _3MF_Exporter::_add_print_config_file_to_archive(mz_zip_archive& archive, const DynamicPrintConfig &config) bool _3MF_Exporter::_add_print_config_file_to_archive(mz_zip_archive& archive, const DynamicPrintConfig &config)
{ {
assert(is_decimal_separator_point());
char buffer[1024]; char buffer[1024];
sprintf(buffer, "; %s\n\n", header_slic3r_generated().c_str()); sprintf(buffer, "; %s\n\n", header_slic3r_generated().c_str());
std::string out = buffer; std::string out = buffer;
@ -2926,6 +2932,9 @@ bool load_3mf(const char* path, DynamicPrintConfig* config, Model* model, bool c
if (path == nullptr || config == nullptr || model == nullptr) if (path == nullptr || config == nullptr || model == nullptr)
return false; return false;
// All import should use "C" locales for number formatting.
CNumericLocalesSetter locales_setter;
_3MF_Importer importer; _3MF_Importer importer;
bool res = importer.load_model_from_file(path, *model, *config, check_version); bool res = importer.load_model_from_file(path, *model, *config, check_version);
importer.log_errors(); importer.log_errors();
@ -2934,6 +2943,9 @@ bool load_3mf(const char* path, DynamicPrintConfig* config, Model* model, bool c
bool store_3mf(const char* path, Model* model, const DynamicPrintConfig* config, bool fullpath_sources, const ThumbnailData* thumbnail_data, bool zip64) bool store_3mf(const char* path, Model* model, const DynamicPrintConfig* config, bool fullpath_sources, const ThumbnailData* thumbnail_data, bool zip64)
{ {
// All export should use "C" locales for number formatting.
CNumericLocalesSetter locales_setter;
if (path == nullptr || model == nullptr) if (path == nullptr || model == nullptr)
return false; return false;

View File

@ -15,6 +15,7 @@
#include "../I18N.hpp" #include "../I18N.hpp"
#include "../Geometry.hpp" #include "../Geometry.hpp"
#include "../CustomGCode.hpp" #include "../CustomGCode.hpp"
#include "../LocalesUtils.hpp"
#include "AMF.hpp" #include "AMF.hpp"
@ -498,6 +499,7 @@ void AMFParserContext::characters(const XML_Char *s, int len)
void AMFParserContext::endElement(const char * /* name */) void AMFParserContext::endElement(const char * /* name */)
{ {
assert(is_decimal_separator_point());
switch (m_path.back()) { switch (m_path.back()) {
// Constellation transformation: // Constellation transformation:
@ -1052,6 +1054,8 @@ bool load_amf_archive(const char* path, DynamicPrintConfig* config, Model* model
// If config is not a null pointer, updates it if the amf file/archive contains config data // If config is not a null pointer, updates it if the amf file/archive contains config data
bool load_amf(const char* path, DynamicPrintConfig* config, Model* model, bool check_version) bool load_amf(const char* path, DynamicPrintConfig* config, Model* model, bool check_version)
{ {
CNumericLocalesSetter locales_setter; // use "C" locales and point as a decimal separator
if (boost::iends_with(path, ".amf.xml")) if (boost::iends_with(path, ".amf.xml"))
// backward compatibility with older slic3r output // backward compatibility with older slic3r output
return load_amf_file(path, config, model); return load_amf_file(path, config, model);
@ -1251,40 +1255,25 @@ bool store_amf(const char* path, Model* model, const DynamicPrintConfig* config,
stream << " </object>\n"; stream << " </object>\n";
if (!object->instances.empty()) { if (!object->instances.empty()) {
for (ModelInstance *instance : object->instances) { for (ModelInstance *instance : object->instances) {
char buf[512]; std::stringstream buf;
::sprintf(buf, buf << " <instance objectid=\"" << object_id << "\">\n"
" <instance objectid=\"%zu\">\n" << " <deltax>" << instance->get_offset(X) << "</deltax>\n"
" <deltax>%lf</deltax>\n" << " <deltay>" << instance->get_offset(Y) << "</deltay>\n"
" <deltay>%lf</deltay>\n" << " <deltaz>" << instance->get_offset(Z) << "</deltaz>\n"
" <deltaz>%lf</deltaz>\n" << " <rx>" << instance->get_rotation(X) << "</rx>\n"
" <rx>%lf</rx>\n" << " <ry>" << instance->get_rotation(Y) << "</ry>\n"
" <ry>%lf</ry>\n" << " <rz>" << instance->get_rotation(Z) << "</rz>\n"
" <rz>%lf</rz>\n" << " <scalex>" << instance->get_scaling_factor(X) << "</scalex>\n"
" <scalex>%lf</scalex>\n" << " <scaley>" << instance->get_scaling_factor(Y) << "</scaley>\n"
" <scaley>%lf</scaley>\n" << " <scalez>" << instance->get_scaling_factor(Z) << "</scalez>\n"
" <scalez>%lf</scalez>\n" << " <mirrorx>" << instance->get_mirror(X) << "</mirrorx>\n"
" <mirrorx>%lf</mirrorx>\n" << " <mirrory>" << instance->get_mirror(Y) << "</mirrory>\n"
" <mirrory>%lf</mirrory>\n" << " <mirrorz>" << instance->get_mirror(Z) << "</mirrorz>\n"
" <mirrorz>%lf</mirrorz>\n" << " <printable>" << instance->printable << "</printable>\n"
" <printable>%d</printable>\n" << " </instance>\n";
" </instance>\n",
object_id,
instance->get_offset(X),
instance->get_offset(Y),
instance->get_offset(Z),
instance->get_rotation(X),
instance->get_rotation(Y),
instance->get_rotation(Z),
instance->get_scaling_factor(X),
instance->get_scaling_factor(Y),
instance->get_scaling_factor(Z),
instance->get_mirror(X),
instance->get_mirror(Y),
instance->get_mirror(Z),
instance->printable);
//FIXME missing instance->scaling_factor //FIXME missing instance->scaling_factor
instances.append(buf); instances.append(buf.str());
} }
} }
} }

View File

@ -13,6 +13,6 @@ extern bool load_amf(const char* path, DynamicPrintConfig* config, Model* model,
// The model could be modified during the export process if meshes are not repaired or have no shared vertices // The model could be modified during the export process if meshes are not repaired or have no shared vertices
extern bool store_amf(const char* path, Model* model, const DynamicPrintConfig* config, bool fullpath_sources); extern bool store_amf(const char* path, Model* model, const DynamicPrintConfig* config, bool fullpath_sources);
}; // namespace Slic3r } // namespace Slic3r
#endif /* slic3r_Format_AMF_hpp_ */ #endif /* slic3r_Format_AMF_hpp_ */

View File

@ -6,6 +6,8 @@
#include "objparser.hpp" #include "objparser.hpp"
#include "libslic3r/LocalesUtils.hpp"
namespace ObjParser { namespace ObjParser {
static bool obj_parseline(const char *line, ObjData &data) static bool obj_parseline(const char *line, ObjData &data)
@ -15,6 +17,8 @@ static bool obj_parseline(const char *line, ObjData &data)
if (*line == 0) if (*line == 0)
return true; return true;
assert(is_decimal_separator_point());
// Ignore whitespaces at the beginning of the line. // Ignore whitespaces at the beginning of the line.
//FIXME is this a good idea? //FIXME is this a good idea?
EATWS(); EATWS();
@ -322,6 +326,8 @@ static bool obj_parseline(const char *line, ObjData &data)
bool objparse(const char *path, ObjData &data) bool objparse(const char *path, ObjData &data)
{ {
Slic3r::CNumericLocalesSetter locales_setter;
FILE *pFile = boost::nowide::fopen(path, "rt"); FILE *pFile = boost::nowide::fopen(path, "rt");
if (pFile == 0) if (pFile == 0)
return false; return false;
@ -365,6 +371,8 @@ bool objparse(const char *path, ObjData &data)
bool objparse(std::istream &stream, ObjData &data) bool objparse(std::istream &stream, ObjData &data)
{ {
Slic3r::CNumericLocalesSetter locales_setter;
try { try {
char buf[65536 * 2]; char buf[65536 * 2];
size_t len = 0; size_t len = 0;

View File

@ -670,7 +670,7 @@ void GCodeProcessor::TimeProcessor::post_process(const std::string& filename)
std::string time_float_str = format_time_float(time_in_last_minute(it_stop->elapsed_time - it->elapsed_time)); std::string time_float_str = format_time_float(time_in_last_minute(it_stop->elapsed_time - it->elapsed_time));
std::string next_time_float_str = format_time_float(time_in_last_minute(it_stop->elapsed_time - next_it->elapsed_time)); std::string next_time_float_str = format_time_float(time_in_last_minute(it_stop->elapsed_time - next_it->elapsed_time));
is_last |= (std::stof(time_float_str) > 0.0f && std::stof(next_time_float_str) == 0.0f); is_last |= (string_to_double_decimal_point(time_float_str) > 0. && string_to_double_decimal_point(next_time_float_str) == 0.);
} }
if (is_last) { if (is_last) {
@ -1373,7 +1373,7 @@ void GCodeProcessor::apply_config_simplify3d(const std::string& filename)
if (pos != cmt.npos) { if (pos != cmt.npos) {
pos = cmt.find(',', pos); pos = cmt.find(',', pos);
if (pos != cmt.npos) { if (pos != cmt.npos) {
out = std::stod(cmt.substr(pos + 1)); out = string_to_double_decimal_point(cmt.substr(pos+1));
return true; return true;
} }
} }
@ -1523,9 +1523,9 @@ template<typename T>
else if constexpr (std::is_same_v<T, long>) else if constexpr (std::is_same_v<T, long>)
out = std::stol(str, &read); out = std::stol(str, &read);
else if constexpr (std::is_same_v<T, float>) else if constexpr (std::is_same_v<T, float>)
out = std::stof(str, &read); out = string_to_double_decimal_point(str, &read);
else if constexpr (std::is_same_v<T, double>) else if constexpr (std::is_same_v<T, double>)
out = std::stod(str, &read); out = string_to_double_decimal_point(str, &read);
return str.size() == read; return str.size() == read;
} catch (...) { } catch (...) {
return false; return false;

View File

@ -4,6 +4,7 @@
#include "../libslic3r.h" #include "../libslic3r.h"
#include "../PrintConfig.hpp" #include "../PrintConfig.hpp"
#include "../LocalesUtils.hpp"
#include "PressureEqualizer.hpp" #include "PressureEqualizer.hpp"
@ -158,7 +159,7 @@ static inline int parse_int(const char *&line)
static inline float parse_float(const char *&line) static inline float parse_float(const char *&line)
{ {
char *endptr = NULL; char *endptr = NULL;
float result = strtof(line, &endptr); float result = string_to_double_decimal_point(line, &endptr);
if (endptr == NULL || !is_ws_or_eol(*endptr)) if (endptr == NULL || !is_ws_or_eol(*endptr))
throw Slic3r::RuntimeError("PressureEqualizer: Error parsing a float"); throw Slic3r::RuntimeError("PressureEqualizer: Error parsing a float");
line = endptr; line = endptr;

View File

@ -6,6 +6,8 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include "LocalesUtils.hpp"
#include <Shiny/Shiny.h> #include <Shiny/Shiny.h>
namespace Slic3r { namespace Slic3r {
@ -25,6 +27,7 @@ void GCodeReader::apply_config(const DynamicPrintConfig &config)
const char* GCodeReader::parse_line_internal(const char *ptr, GCodeLine &gline, std::pair<const char*, const char*> &command) const char* GCodeReader::parse_line_internal(const char *ptr, GCodeLine &gline, std::pair<const char*, const char*> &command)
{ {
PROFILE_FUNC(); PROFILE_FUNC();
CNumericLocalesSetter locales_setter; // for strtod
// command and args // command and args
const char *c = ptr; const char *c = ptr;
@ -150,6 +153,7 @@ bool GCodeReader::GCodeLine::has(char axis) const
bool GCodeReader::GCodeLine::has_value(char axis, float &value) const bool GCodeReader::GCodeLine::has_value(char axis, float &value) const
{ {
CNumericLocalesSetter locales_setter; // for strtod
const char *c = m_raw.c_str(); const char *c = m_raw.c_str();
// Skip the whitespaces. // Skip the whitespaces.
c = skip_whitespaces(c); c = skip_whitespaces(c);

View File

@ -1,5 +1,6 @@
#include "LocalesUtils.hpp" #include "LocalesUtils.hpp"
#include <stdexcept>
namespace Slic3r { namespace Slic3r {
@ -39,6 +40,22 @@ bool is_decimal_separator_point()
return str[1] == '.'; return str[1] == '.';
} }
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;
}
std::string float_to_string_decimal_point(double value, int precision/* = -1*/) std::string float_to_string_decimal_point(double value, int precision/* = -1*/)
{ {
assert(is_decimal_separator_point()); assert(is_decimal_separator_point());

View File

@ -40,7 +40,7 @@ bool is_decimal_separator_point();
// (We use user C locales and "C" C++ locales in most of the code.) // (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(double value, int precision = -1);
std::string float_to_string_decimal_point(float 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& str, size_t* pos = nullptr);
} // namespace Slic3r } // namespace Slic3r

View File

@ -1215,8 +1215,8 @@ boost::any& Choice::get_value()
return m_value; return m_value;
} }
void Choice::enable() { dynamic_cast<choice_ctrl*>(window)->Enable(); }; void Choice::enable() { dynamic_cast<choice_ctrl*>(window)->Enable(); }
void Choice::disable() { dynamic_cast<choice_ctrl*>(window)->Disable(); }; void Choice::disable() { dynamic_cast<choice_ctrl*>(window)->Disable(); }
void Choice::msw_rescale() void Choice::msw_rescale()
{ {

View File

@ -2,6 +2,8 @@
#include "GUI_App.hpp" #include "GUI_App.hpp"
#include "I18N.hpp" #include "I18N.hpp"
#include "libslic3r/LocalesUtils.hpp"
#include <string> #include <string>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
@ -113,7 +115,7 @@ void change_opt_value(DynamicPrintConfig& config, const t_config_option_key& opt
str.pop_back(); str.pop_back();
percent = true; percent = true;
} }
double val = stod(str); double val = string_to_double_decimal_point(str);
config.set_key_value(opt_key, new ConfigOptionFloatOrPercent(val, percent)); config.set_key_value(opt_key, new ConfigOptionFloatOrPercent(val, percent));
break;} break;}
case coPercent: case coPercent:

View File

@ -11,6 +11,7 @@
#include "Camera.hpp" #include "Camera.hpp"
#include "Plater.hpp" #include "Plater.hpp"
#include "libslic3r/LocalesUtils.hpp"
#include "libslic3r/Model.hpp" #include "libslic3r/Model.hpp"
#if DISABLE_ALLOW_NEGATIVE_Z_FOR_SLA #if DISABLE_ALLOW_NEGATIVE_Z_FOR_SLA
#include "libslic3r/PresetBundle.hpp" #include "libslic3r/PresetBundle.hpp"
@ -1927,7 +1928,7 @@ void Selection::render_sidebar_layers_hints(const std::string& sidebar_field) co
if (pos == std::string::npos) if (pos == std::string::npos)
return; return;
double max_z = std::stod(field.substr(pos + 1)); double max_z = string_to_double_decimal_point(field.substr(pos + 1));
// extract min_z // extract min_z
field = field.substr(0, pos); field = field.substr(0, pos);
@ -1935,7 +1936,7 @@ void Selection::render_sidebar_layers_hints(const std::string& sidebar_field) co
if (pos == std::string::npos) if (pos == std::string::npos)
return; return;
const double min_z = std::stod(field.substr(pos + 1)); const double min_z = string_to_double_decimal_point(field.substr(pos + 1));
// extract type // extract type
field = field.substr(0, pos); field = field.substr(0, pos);