Improved error handling when importing configuration from a G-code.

This commit is contained in:
bubnikv 2017-06-14 20:18:46 +02:00
parent f7334f58d3
commit 2ac981e422
5 changed files with 47 additions and 15 deletions

View file

@ -312,7 +312,14 @@ void ConfigBase::load(const std::string &file)
void ConfigBase::load_from_gcode(const std::string &file)
{
// 1) Read a 64k block from the end of the G-code.
boost::nowide::ifstream ifs(file);
boost::nowide::ifstream ifs(file);
{
const char slic3r_gcode_header[] = "; generated by Slic3r ";
std::string firstline;
std::getline(ifs, firstline);
if (strncmp(slic3r_gcode_header, firstline.c_str(), strlen(slic3r_gcode_header)) != 0)
throw std::exception("Not a Slic3r generated g-code.");
}
ifs.seekg(0, ifs.end);
auto file_length = ifs.tellg();
auto data_length = std::min<std::fstream::streampos>(65535, file_length);
@ -325,6 +332,7 @@ void ConfigBase::load_from_gcode(const std::string &file)
char *data_start = data.data();
// boost::nowide::ifstream seems to cook the text data somehow, so less then the 64k of characters may be retrieved.
char *end = data_start + strlen(data.data());
size_t num_key_value_pairs = 0;
for (;;) {
// Extract next line.
for (-- end; end > data_start && (*end == '\r' || *end == '\n'); -- end);
@ -362,11 +370,17 @@ void ConfigBase::load_from_gcode(const std::string &file)
break;
try {
this->set_deserialize(key, value);
++ num_key_value_pairs;
} catch (UnknownOptionException & /* e */) {
// ignore
}
end = start;
}
if (num_key_value_pairs < 90) {
char msg[80];
sprintf(msg, "Suspiciously low number of configuration values extracted: %d", num_key_value_pairs);
throw std::exception(msg);
}
}
void ConfigBase::save(const std::string &file) const