Use istringstream for deserializing config options in order to use the correct locale. #2187
This commit is contained in:
parent
b10917806a
commit
d9f6b0933b
@ -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 ConfigOptionVector<double
|
||||
std::istringstream is(str);
|
||||
std::string item_str;
|
||||
while (std::getline(is, item_str, ',')) {
|
||||
this->values.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<int>
|
||||
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<std::streamsize>::max(), ',');
|
||||
iss.ignore(std::numeric_limits<std::streamsize>::max(), 'x');
|
||||
iss >> this->point.y;
|
||||
return true;
|
||||
};
|
||||
};
|
||||
|
||||
@ -302,16 +300,10 @@ class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector<Pointf
|
||||
std::string point_str;
|
||||
while (std::getline(is, point_str, ',')) {
|
||||
Pointf point;
|
||||
if (strncmp(point_str.c_str(), "0x", 2) == 0) {
|
||||
// if string starts with "0x", only apply sscanf() to the second coordinate
|
||||
// otherwise it would parse the string as a hex number
|
||||
point.x = 0;
|
||||
int res = sscanf(point_str.c_str()+2, "%lf", &point.y);
|
||||
if (res != 1) return false;
|
||||
} else {
|
||||
int res = sscanf(point_str.c_str(), "%lfx%lf", &point.x, &point.y);
|
||||
if (res != 2) return false;
|
||||
}
|
||||
std::istringstream iss(point_str);
|
||||
iss >> point.x;
|
||||
iss.ignore(std::numeric_limits<std::streamsize>::max(), 'x');
|
||||
iss >> point.y;
|
||||
values.push_back(point);
|
||||
}
|
||||
this->values = values;
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user