From d9f6b0933b3f76286c8e5b28ef3bacfd0c212b55 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 4 Aug 2014 11:34:53 +0200 Subject: [PATCH] Use istringstream for deserializing config options in order to use the correct locale. #2187 --- xs/src/libslic3r/Config.hpp | 62 ++++++++++++++++--------------------- xs/t/15_config.t | 4 ++- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/xs/src/libslic3r/Config.hpp b/xs/src/libslic3r/Config.hpp index 6533aa65e..437e45ea1 100644 --- a/xs/src/libslic3r/Config.hpp +++ b/xs/src/libslic3r/Config.hpp @@ -58,8 +58,8 @@ class ConfigOptionFloat : public ConfigOption }; bool deserialize(std::string str) { - this->value = ::atof(str.c_str()); - return true; + std::istringstream iss(str); + return iss >> this->value; }; }; @@ -81,7 +81,10 @@ class ConfigOptionFloats : public ConfigOption, public ConfigOptionVectorvalues.push_back(::atof(item_str.c_str())); + std::istringstream iss(item_str); + double value; + iss >> value; + this->values.push_back(value); } return true; }; @@ -104,8 +107,8 @@ class ConfigOptionInt : public ConfigOption }; bool deserialize(std::string str) { - this->value = ::atoi(str.c_str()); - return true; + std::istringstream iss(str); + return iss >> this->value; }; }; @@ -127,7 +130,10 @@ class ConfigOptionInts : public ConfigOption, public ConfigOptionVector std::istringstream is(str); std::string item_str; while (std::getline(is, item_str, ',')) { - this->values.push_back(::atoi(item_str.c_str())); + std::istringstream iss(item_str); + int value; + iss >> value; + this->values.push_back(value); } return true; }; @@ -212,8 +218,8 @@ class ConfigOptionPercent : public ConfigOption bool deserialize(std::string str) { // don't try to parse the trailing % since it's optional - int res = sscanf(str.c_str(), "%lf", &this->value); - return res == 1; + std::istringstream iss(str); + return iss >> this->value; }; }; @@ -241,15 +247,9 @@ class ConfigOptionFloatOrPercent : public ConfigOption }; bool deserialize(std::string str) { - if (str.find_first_of("%") != std::string::npos) { - int res = sscanf(str.c_str(), "%lf%%", &this->value); - if (res == 0) return false; - this->percent = true; - } else { - this->value = ::atof(str.c_str()); - this->percent = false; - } - return true; + this->percent = str.find_first_of("%") != std::string::npos; + std::istringstream iss(str); + return iss >> this->value; }; }; @@ -270,14 +270,12 @@ class ConfigOptionPoint : public ConfigOption }; bool deserialize(std::string str) { - if (strncmp(str.c_str(), "0x", 2) == 0) { - this->point.x = 0; - int res = sscanf(str.c_str()+2, "%lf", &this->point.y); - return res == 1; - } else { - int res = sscanf(str.c_str(), "%lf%*1[,x]%lf", &this->point.x, &this->point.y); - return res == 2; - } + std::istringstream iss(str); + iss >> this->point.x; + iss.ignore(std::numeric_limits::max(), ','); + iss.ignore(std::numeric_limits::max(), 'x'); + iss >> this->point.y; + return true; }; }; @@ -302,16 +300,10 @@ class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector> point.x; + iss.ignore(std::numeric_limits::max(), 'x'); + iss >> point.y; values.push_back(point); } this->values = values; diff --git a/xs/t/15_config.t b/xs/t/15_config.t index 2627c895d..5cc7a021b 100644 --- a/xs/t/15_config.t +++ b/xs/t/15_config.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 103; +use Test::More tests => 105; foreach my $config (Slic3r::Config->new, Slic3r::Config::Full->new) { $config->set('layer_height', 0.3); @@ -63,6 +63,8 @@ foreach my $config (Slic3r::Config->new, Slic3r::Config::Full->new) { is $config->serialize('extruder_offset'), '10x20,30x45', 'serialize points'; $config->set_deserialize('extruder_offset', '20x10'); is_deeply $config->get('extruder_offset'), [[20,10]], 'deserialize points'; + $config->set_deserialize('extruder_offset', '0x0'); + is_deeply $config->get('extruder_offset'), [[0,0]], 'deserialize points'; { my @values = ([10,20]); $values[2] = [10,20]; # implicitely extend array; this is not the same as explicitely assigning undef to second item