2013-12-07 15:14:30 +00:00
|
|
|
#include "Config.hpp"
|
2017-10-30 17:15:41 +00:00
|
|
|
#include "Utils.hpp"
|
2015-12-11 20:36:51 +00:00
|
|
|
#include <assert.h>
|
2017-06-14 15:51:14 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <exception> // std::runtime_error
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
#include <boost/algorithm/string/erase.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
#include <boost/config.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/nowide/cenv.hpp>
|
|
|
|
#include <boost/nowide/fstream.hpp>
|
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
2018-11-02 09:31:10 +00:00
|
|
|
#include <boost/format.hpp>
|
2016-11-01 12:53:28 +00:00
|
|
|
#include <string.h>
|
2013-12-07 15:14:30 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2018-06-28 19:46:23 +00:00
|
|
|
// Escape \n, \r and backslash
|
2016-11-01 12:41:24 +00:00
|
|
|
std::string escape_string_cstyle(const std::string &str)
|
|
|
|
{
|
|
|
|
// Allocate a buffer twice the input string length,
|
|
|
|
// so the output will fit even if all input characters get escaped.
|
|
|
|
std::vector<char> out(str.size() * 2, 0);
|
|
|
|
char *outptr = out.data();
|
|
|
|
for (size_t i = 0; i < str.size(); ++ i) {
|
|
|
|
char c = str[i];
|
2018-06-28 18:13:01 +00:00
|
|
|
if (c == '\r') {
|
|
|
|
(*outptr ++) = '\\';
|
|
|
|
(*outptr ++) = 'r';
|
|
|
|
} else if (c == '\n') {
|
2016-11-01 12:41:24 +00:00
|
|
|
(*outptr ++) = '\\';
|
|
|
|
(*outptr ++) = 'n';
|
2018-06-28 18:13:01 +00:00
|
|
|
} else if (c == '\\') {
|
|
|
|
(*outptr ++) = '\\';
|
|
|
|
(*outptr ++) = '\\';
|
2016-11-01 12:41:24 +00:00
|
|
|
} else
|
|
|
|
(*outptr ++) = c;
|
|
|
|
}
|
|
|
|
return std::string(out.data(), outptr - out.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string escape_strings_cstyle(const std::vector<std::string> &strs)
|
|
|
|
{
|
|
|
|
// 1) Estimate the output buffer size to avoid buffer reallocation.
|
|
|
|
size_t outbuflen = 0;
|
|
|
|
for (size_t i = 0; i < strs.size(); ++ i)
|
|
|
|
// Reserve space for every character escaped + quotes + semicolon.
|
|
|
|
outbuflen += strs[i].size() * 2 + 3;
|
|
|
|
// 2) Fill in the buffer.
|
|
|
|
std::vector<char> out(outbuflen, 0);
|
|
|
|
char *outptr = out.data();
|
|
|
|
for (size_t j = 0; j < strs.size(); ++ j) {
|
|
|
|
if (j > 0)
|
|
|
|
// Separate the strings.
|
|
|
|
(*outptr ++) = ';';
|
|
|
|
const std::string &str = strs[j];
|
|
|
|
// Is the string simple or complex? Complex string contains spaces, tabs, new lines and other
|
|
|
|
// escapable characters. Empty string shall be quoted as well, if it is the only string in strs.
|
|
|
|
bool should_quote = strs.size() == 1 && str.empty();
|
|
|
|
for (size_t i = 0; i < str.size(); ++ i) {
|
|
|
|
char c = str[i];
|
|
|
|
if (c == ' ' || c == '\t' || c == '\\' || c == '"' || c == '\r' || c == '\n') {
|
|
|
|
should_quote = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (should_quote) {
|
|
|
|
(*outptr ++) = '"';
|
|
|
|
for (size_t i = 0; i < str.size(); ++ i) {
|
|
|
|
char c = str[i];
|
|
|
|
if (c == '\\' || c == '"') {
|
|
|
|
(*outptr ++) = '\\';
|
|
|
|
(*outptr ++) = c;
|
2018-06-28 18:13:01 +00:00
|
|
|
} else if (c == '\r') {
|
|
|
|
(*outptr ++) = '\\';
|
|
|
|
(*outptr ++) = 'r';
|
|
|
|
} else if (c == '\n') {
|
2016-11-01 12:41:24 +00:00
|
|
|
(*outptr ++) = '\\';
|
|
|
|
(*outptr ++) = 'n';
|
|
|
|
} else
|
|
|
|
(*outptr ++) = c;
|
|
|
|
}
|
|
|
|
(*outptr ++) = '"';
|
|
|
|
} else {
|
|
|
|
memcpy(outptr, str.data(), str.size());
|
|
|
|
outptr += str.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::string(out.data(), outptr - out.data());
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:46:23 +00:00
|
|
|
// Unescape \n, \r and backslash
|
2016-11-01 12:41:24 +00:00
|
|
|
bool unescape_string_cstyle(const std::string &str, std::string &str_out)
|
|
|
|
{
|
|
|
|
std::vector<char> out(str.size(), 0);
|
|
|
|
char *outptr = out.data();
|
|
|
|
for (size_t i = 0; i < str.size(); ++ i) {
|
|
|
|
char c = str[i];
|
|
|
|
if (c == '\\') {
|
|
|
|
if (++ i == str.size())
|
|
|
|
return false;
|
|
|
|
c = str[i];
|
2018-06-28 18:13:01 +00:00
|
|
|
if (c == 'r')
|
|
|
|
(*outptr ++) = '\r';
|
|
|
|
else if (c == 'n')
|
2016-11-01 12:41:24 +00:00
|
|
|
(*outptr ++) = '\n';
|
2018-06-28 18:13:01 +00:00
|
|
|
else
|
|
|
|
(*outptr ++) = c;
|
2016-11-01 12:41:24 +00:00
|
|
|
} else
|
|
|
|
(*outptr ++) = c;
|
|
|
|
}
|
|
|
|
str_out.assign(out.data(), outptr - out.data());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unescape_strings_cstyle(const std::string &str, std::vector<std::string> &out)
|
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for (;;) {
|
|
|
|
// Skip white spaces.
|
|
|
|
char c = str[i];
|
|
|
|
while (c == ' ' || c == '\t') {
|
|
|
|
if (++ i == str.size())
|
|
|
|
return true;
|
|
|
|
c = str[i];
|
|
|
|
}
|
|
|
|
// Start of a word.
|
|
|
|
std::vector<char> buf;
|
|
|
|
buf.reserve(16);
|
|
|
|
// Is it enclosed in quotes?
|
|
|
|
c = str[i];
|
|
|
|
if (c == '"') {
|
|
|
|
// Complex case, string is enclosed in quotes.
|
|
|
|
for (++ i; i < str.size(); ++ i) {
|
|
|
|
c = str[i];
|
|
|
|
if (c == '"') {
|
|
|
|
// End of string.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == '\\') {
|
|
|
|
if (++ i == str.size())
|
|
|
|
return false;
|
|
|
|
c = str[i];
|
2018-06-28 18:13:01 +00:00
|
|
|
if (c == 'r')
|
|
|
|
c = '\r';
|
|
|
|
else if (c == 'n')
|
2016-11-01 12:41:24 +00:00
|
|
|
c = '\n';
|
|
|
|
}
|
|
|
|
buf.push_back(c);
|
|
|
|
}
|
|
|
|
if (i == str.size())
|
|
|
|
return false;
|
|
|
|
++ i;
|
|
|
|
} else {
|
|
|
|
for (; i < str.size(); ++ i) {
|
|
|
|
c = str[i];
|
|
|
|
if (c == ';')
|
|
|
|
break;
|
|
|
|
buf.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Store the string into the output vector.
|
|
|
|
out.push_back(std::string(buf.data(), buf.size()));
|
|
|
|
if (i == str.size())
|
2016-11-01 13:52:44 +00:00
|
|
|
return true;
|
2016-11-01 12:41:24 +00:00
|
|
|
// Skip white spaces.
|
|
|
|
c = str[i];
|
|
|
|
while (c == ' ' || c == '\t') {
|
|
|
|
if (++ i == str.size())
|
|
|
|
// End of string. This is correct.
|
|
|
|
return true;
|
|
|
|
c = str[i];
|
|
|
|
}
|
|
|
|
if (c != ';')
|
|
|
|
return false;
|
|
|
|
if (++ i == str.size()) {
|
|
|
|
// Emit one additional empty string.
|
|
|
|
out.push_back(std::string());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:01:18 +00:00
|
|
|
void ConfigBase::apply_only(const ConfigBase &other, const t_config_option_keys &keys, bool ignore_nonexistent)
|
2015-12-02 18:32:57 +00:00
|
|
|
{
|
2013-12-20 15:37:28 +00:00
|
|
|
// loop through options and apply them
|
2017-10-17 14:01:18 +00:00
|
|
|
for (const t_config_option_key &opt_key : keys) {
|
|
|
|
// Create a new option with default value for the key.
|
|
|
|
// If the key is not in the parameter definition, or this ConfigBase is a static type and it does not support the parameter,
|
|
|
|
// an exception is thrown if not ignore_nonexistent.
|
|
|
|
ConfigOption *my_opt = this->option(opt_key, true);
|
2017-05-30 18:09:34 +00:00
|
|
|
if (my_opt == nullptr) {
|
2017-10-17 14:01:18 +00:00
|
|
|
// opt_key does not exist in this ConfigBase and it cannot be created, because it is not defined by this->def().
|
|
|
|
// This is only possible if other is of DynamicConfig type.
|
|
|
|
if (ignore_nonexistent)
|
|
|
|
continue;
|
2018-03-09 15:37:33 +00:00
|
|
|
throw UnknownOptionException(opt_key);
|
2013-12-21 15:32:11 +00:00
|
|
|
}
|
2017-10-17 14:01:18 +00:00
|
|
|
const ConfigOption *other_opt = other.option(opt_key);
|
2018-06-26 08:50:50 +00:00
|
|
|
if (other_opt == nullptr) {
|
|
|
|
// The key was not found in the source config, therefore it will not be initialized!
|
|
|
|
// printf("Not found, therefore not initialized: %s\n", opt_key.c_str());
|
|
|
|
} else
|
2017-10-17 14:01:18 +00:00
|
|
|
my_opt->set(other_opt);
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 11:25:59 +00:00
|
|
|
// this will *ignore* options not present in both configs
|
2017-05-30 15:04:36 +00:00
|
|
|
t_config_option_keys ConfigBase::diff(const ConfigBase &other) const
|
|
|
|
{
|
2014-11-09 11:25:59 +00:00
|
|
|
t_config_option_keys diff;
|
2017-10-17 14:01:18 +00:00
|
|
|
for (const t_config_option_key &opt_key : this->keys()) {
|
|
|
|
const ConfigOption *this_opt = this->option(opt_key);
|
|
|
|
const ConfigOption *other_opt = other.option(opt_key);
|
|
|
|
if (this_opt != nullptr && other_opt != nullptr && *this_opt != *other_opt)
|
|
|
|
diff.emplace_back(opt_key);
|
|
|
|
}
|
2014-11-09 11:25:59 +00:00
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2018-03-16 11:56:03 +00:00
|
|
|
t_config_option_keys ConfigBase::equal(const ConfigBase &other) const
|
|
|
|
{
|
|
|
|
t_config_option_keys equal;
|
|
|
|
for (const t_config_option_key &opt_key : this->keys()) {
|
|
|
|
const ConfigOption *this_opt = this->option(opt_key);
|
|
|
|
const ConfigOption *other_opt = other.option(opt_key);
|
|
|
|
if (this_opt != nullptr && other_opt != nullptr && *this_opt == *other_opt)
|
|
|
|
equal.emplace_back(opt_key);
|
|
|
|
}
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
2017-05-30 15:04:36 +00:00
|
|
|
std::string ConfigBase::serialize(const t_config_option_key &opt_key) const
|
|
|
|
{
|
2015-12-02 18:32:57 +00:00
|
|
|
const ConfigOption* opt = this->option(opt_key);
|
2017-05-30 15:04:36 +00:00
|
|
|
assert(opt != nullptr);
|
2013-12-20 19:54:11 +00:00
|
|
|
return opt->serialize();
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:01:18 +00:00
|
|
|
bool ConfigBase::set_deserialize(const t_config_option_key &opt_key_src, const std::string &value_src, bool append)
|
2017-05-30 15:04:36 +00:00
|
|
|
{
|
2017-10-17 14:01:18 +00:00
|
|
|
t_config_option_key opt_key = opt_key_src;
|
|
|
|
std::string value = value_src;
|
|
|
|
// Both opt_key and value may be modified by _handle_legacy().
|
|
|
|
// If the opt_key is no more valid in this version of Slic3r, opt_key is cleared by _handle_legacy().
|
|
|
|
this->handle_legacy(opt_key, value);
|
|
|
|
if (opt_key.empty())
|
|
|
|
// Ignore the option.
|
|
|
|
return true;
|
|
|
|
return this->set_deserialize_raw(opt_key, value, append);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigBase::set_deserialize_raw(const t_config_option_key &opt_key_src, const std::string &value, bool append)
|
|
|
|
{
|
|
|
|
t_config_option_key opt_key = opt_key_src;
|
|
|
|
// Try to deserialize the option by its name.
|
2017-11-17 10:15:46 +00:00
|
|
|
const ConfigDef *def = this->def();
|
|
|
|
if (def == nullptr)
|
2018-03-09 15:37:33 +00:00
|
|
|
throw NoDefinitionException(opt_key);
|
2017-11-17 10:15:46 +00:00
|
|
|
const ConfigOptionDef *optdef = def->get(opt_key);
|
2017-06-14 15:51:14 +00:00
|
|
|
if (optdef == nullptr) {
|
|
|
|
// If we didn't find an option, look for any other option having this as an alias.
|
2017-11-17 10:15:46 +00:00
|
|
|
for (const auto &opt : def->options) {
|
2017-06-14 15:51:14 +00:00
|
|
|
for (const t_config_option_key &opt_key2 : opt.second.aliases) {
|
|
|
|
if (opt_key2 == opt_key) {
|
2017-11-30 12:43:02 +00:00
|
|
|
opt_key = opt.first;
|
2017-06-14 15:51:14 +00:00
|
|
|
optdef = &opt.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (optdef != nullptr)
|
|
|
|
break;
|
2014-03-24 00:07:30 +00:00
|
|
|
}
|
2017-06-14 15:51:14 +00:00
|
|
|
if (optdef == nullptr)
|
2018-03-09 15:37:33 +00:00
|
|
|
throw UnknownOptionException(opt_key);
|
2017-06-14 15:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! optdef->shortcut.empty()) {
|
2017-10-17 14:01:18 +00:00
|
|
|
// Aliasing for example "solid_layers" to "top_solid_layers" and "bottom_solid_layers".
|
2017-06-14 15:51:14 +00:00
|
|
|
for (const t_config_option_key &shortcut : optdef->shortcut)
|
2017-10-17 14:01:18 +00:00
|
|
|
// Recursive call.
|
|
|
|
if (! this->set_deserialize_raw(shortcut, value, append))
|
2017-06-14 15:51:14 +00:00
|
|
|
return false;
|
2014-03-24 00:07:30 +00:00
|
|
|
return true;
|
2014-01-01 16:29:15 +00:00
|
|
|
}
|
2017-06-14 15:51:14 +00:00
|
|
|
|
|
|
|
ConfigOption *opt = this->option(opt_key, true);
|
2017-05-30 15:04:36 +00:00
|
|
|
assert(opt != nullptr);
|
2017-10-17 14:01:18 +00:00
|
|
|
return opt->deserialize(value, append);
|
2013-12-20 19:54:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
// Return an absolute value of a possibly relative config variable.
|
|
|
|
// For example, return absolute infill extrusion width, either from an absolute value, or relative to the layer height.
|
2017-05-30 15:04:36 +00:00
|
|
|
double ConfigBase::get_abs_value(const t_config_option_key &opt_key) const
|
|
|
|
{
|
2017-10-17 14:01:18 +00:00
|
|
|
// Get stored option value.
|
|
|
|
const ConfigOption *raw_opt = this->option(opt_key);
|
|
|
|
assert(raw_opt != nullptr);
|
|
|
|
if (raw_opt->type() == coFloat)
|
|
|
|
return static_cast<const ConfigOptionFloat*>(raw_opt)->value;
|
|
|
|
if (raw_opt->type() == coFloatOrPercent) {
|
|
|
|
// Get option definition.
|
2017-11-17 10:15:46 +00:00
|
|
|
const ConfigDef *def = this->def();
|
|
|
|
if (def == nullptr)
|
2018-03-09 15:37:33 +00:00
|
|
|
throw NoDefinitionException(opt_key);
|
2017-11-17 10:15:46 +00:00
|
|
|
const ConfigOptionDef *opt_def = def->get(opt_key);
|
|
|
|
assert(opt_def != nullptr);
|
2017-10-17 14:01:18 +00:00
|
|
|
// Compute absolute value over the absolute value of the base option.
|
2017-11-17 10:15:46 +00:00
|
|
|
//FIXME there are some ratio_over chains, which end with empty ratio_with.
|
|
|
|
// For example, XXX_extrusion_width parameters are not handled by get_abs_value correctly.
|
|
|
|
return opt_def->ratio_over.empty() ? 0. :
|
|
|
|
static_cast<const ConfigOptionFloatOrPercent*>(raw_opt)->get_abs_value(this->get_abs_value(opt_def->ratio_over));
|
2013-12-20 19:54:11 +00:00
|
|
|
}
|
2017-10-17 14:01:18 +00:00
|
|
|
throw std::runtime_error("ConfigBase::get_abs_value(): Not a valid option type for get_abs_value()");
|
2013-12-20 19:54:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
// Return an absolute value of a possibly relative config variable.
|
|
|
|
// For example, return absolute infill extrusion width, either from an absolute value, or relative to a provided value.
|
2017-05-30 15:04:36 +00:00
|
|
|
double ConfigBase::get_abs_value(const t_config_option_key &opt_key, double ratio_over) const
|
|
|
|
{
|
2017-10-17 14:01:18 +00:00
|
|
|
// Get stored option value.
|
|
|
|
const ConfigOption *raw_opt = this->option(opt_key);
|
|
|
|
assert(raw_opt != nullptr);
|
|
|
|
if (raw_opt->type() != coFloatOrPercent)
|
|
|
|
throw std::runtime_error("ConfigBase::get_abs_value(): opt_key is not of coFloatOrPercent");
|
|
|
|
// Compute absolute value.
|
|
|
|
return static_cast<const ConfigOptionFloatOrPercent*>(raw_opt)->get_abs_value(ratio_over);
|
2014-01-01 16:29:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 15:04:36 +00:00
|
|
|
void ConfigBase::setenv_()
|
2015-07-01 16:18:25 +00:00
|
|
|
{
|
|
|
|
t_config_option_keys opt_keys = this->keys();
|
|
|
|
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) {
|
|
|
|
// prepend the SLIC3R_ prefix
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << "SLIC3R_";
|
|
|
|
ss << *it;
|
|
|
|
std::string envname = ss.str();
|
|
|
|
|
|
|
|
// capitalize environment variable name
|
|
|
|
for (size_t i = 0; i < envname.size(); ++i)
|
|
|
|
envname[i] = (envname[i] <= 'z' && envname[i] >= 'a') ? envname[i]-('a'-'A') : envname[i];
|
|
|
|
|
2018-01-07 21:20:02 +00:00
|
|
|
boost::nowide::setenv(envname.c_str(), this->serialize(*it).c_str(), 1);
|
2015-07-01 16:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 15:51:14 +00:00
|
|
|
void ConfigBase::load(const std::string &file)
|
2017-10-27 14:11:06 +00:00
|
|
|
{
|
|
|
|
if (boost::iends_with(file, ".gcode") || boost::iends_with(file, ".g"))
|
2018-02-13 14:19:55 +00:00
|
|
|
this->load_from_gcode_file(file);
|
2017-10-27 14:11:06 +00:00
|
|
|
else
|
|
|
|
this->load_from_ini(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigBase::load_from_ini(const std::string &file)
|
2017-06-14 15:51:14 +00:00
|
|
|
{
|
2017-11-01 18:30:05 +00:00
|
|
|
boost::property_tree::ptree tree;
|
2017-06-14 15:51:14 +00:00
|
|
|
boost::nowide::ifstream ifs(file);
|
2017-11-01 18:30:05 +00:00
|
|
|
boost::property_tree::read_ini(ifs, tree);
|
|
|
|
this->load(tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigBase::load(const boost::property_tree::ptree &tree)
|
|
|
|
{
|
|
|
|
for (const boost::property_tree::ptree::value_type &v : tree) {
|
2017-06-14 15:51:14 +00:00
|
|
|
try {
|
|
|
|
t_config_option_key opt_key = v.first;
|
2017-10-17 14:01:18 +00:00
|
|
|
this->set_deserialize(opt_key, v.second.get_value<std::string>());
|
2017-06-14 15:51:14 +00:00
|
|
|
} catch (UnknownOptionException & /* e */) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 14:19:55 +00:00
|
|
|
// Load the config keys from the tail of a G-code file.
|
|
|
|
void ConfigBase::load_from_gcode_file(const std::string &file)
|
2017-06-14 15:51:14 +00:00
|
|
|
{
|
2018-02-13 14:19:55 +00:00
|
|
|
// Read a 64k block from the end of the G-code.
|
2017-06-14 18:18:46 +00:00
|
|
|
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)
|
2017-06-14 18:33:20 +00:00
|
|
|
throw std::runtime_error("Not a Slic3r generated g-code.");
|
2017-06-14 18:18:46 +00:00
|
|
|
}
|
2017-06-14 15:51:14 +00:00
|
|
|
ifs.seekg(0, ifs.end);
|
2017-06-14 16:53:11 +00:00
|
|
|
auto file_length = ifs.tellg();
|
|
|
|
auto data_length = std::min<std::fstream::streampos>(65535, file_length);
|
|
|
|
ifs.seekg(file_length - data_length, ifs.beg);
|
2018-02-13 14:19:55 +00:00
|
|
|
std::vector<char> data(size_t(data_length) + 1, 0);
|
|
|
|
ifs.read(data.data(), data_length);
|
2017-06-14 15:51:14 +00:00
|
|
|
ifs.close();
|
|
|
|
|
2018-11-07 13:57:50 +00:00
|
|
|
size_t key_value_pairs = load_from_gcode_string(data.data());
|
|
|
|
if (key_value_pairs < 80)
|
|
|
|
throw std::runtime_error((boost::format("Suspiciously low number of configuration values extracted from %1: %2") % file % key_value_pairs).str());
|
2018-02-13 14:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the config keys from the given string.
|
2018-11-07 13:57:50 +00:00
|
|
|
size_t ConfigBase::load_from_gcode_string(const char* str)
|
2018-02-13 14:19:55 +00:00
|
|
|
{
|
|
|
|
if (str == nullptr)
|
2018-11-07 13:57:50 +00:00
|
|
|
return 0;
|
2018-02-13 14:19:55 +00:00
|
|
|
|
|
|
|
// Walk line by line in reverse until a non-configuration key appears.
|
|
|
|
char *data_start = const_cast<char*>(str);
|
2017-06-14 16:53:11 +00:00
|
|
|
// boost::nowide::ifstream seems to cook the text data somehow, so less then the 64k of characters may be retrieved.
|
2018-02-13 14:19:55 +00:00
|
|
|
char *end = data_start + strlen(str);
|
2017-06-14 18:18:46 +00:00
|
|
|
size_t num_key_value_pairs = 0;
|
2017-06-14 15:51:14 +00:00
|
|
|
for (;;) {
|
|
|
|
// Extract next line.
|
2018-02-13 14:19:55 +00:00
|
|
|
for (--end; end > data_start && (*end == '\r' || *end == '\n'); --end);
|
2017-06-14 15:51:14 +00:00
|
|
|
if (end == data_start)
|
|
|
|
break;
|
|
|
|
char *start = end;
|
2018-02-13 14:19:55 +00:00
|
|
|
*(++end) = 0;
|
|
|
|
for (; start > data_start && *start != '\r' && *start != '\n'; --start);
|
2017-06-14 15:51:14 +00:00
|
|
|
if (start == data_start)
|
|
|
|
break;
|
|
|
|
// Extracted a line from start to end. Extract the key = value pair.
|
2018-02-13 14:19:55 +00:00
|
|
|
if (end - (++start) < 10 || start[0] != ';' || start[1] != ' ')
|
2017-06-14 15:51:14 +00:00
|
|
|
break;
|
|
|
|
char *key = start + 2;
|
2018-02-13 14:19:55 +00:00
|
|
|
if (!(*key >= 'a' && *key <= 'z') || (*key >= 'A' && *key <= 'Z'))
|
2017-06-14 16:53:11 +00:00
|
|
|
// A key must start with a letter.
|
|
|
|
break;
|
2017-06-14 15:51:14 +00:00
|
|
|
char *sep = strchr(key, '=');
|
2017-06-14 16:53:11 +00:00
|
|
|
if (sep == nullptr || sep[-1] != ' ' || sep[1] != ' ')
|
2017-06-14 15:51:14 +00:00
|
|
|
break;
|
|
|
|
char *value = sep + 2;
|
2017-06-14 16:53:11 +00:00
|
|
|
if (value > end)
|
2017-06-14 15:51:14 +00:00
|
|
|
break;
|
|
|
|
char *key_end = sep - 1;
|
|
|
|
if (key_end - key < 3)
|
|
|
|
break;
|
|
|
|
*key_end = 0;
|
2017-06-14 16:53:11 +00:00
|
|
|
// The key may contain letters, digits and underscores.
|
2018-02-13 14:19:55 +00:00
|
|
|
for (char *c = key; c != key_end; ++c)
|
|
|
|
if (!((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9') || *c == '_')) {
|
2017-06-14 16:53:11 +00:00
|
|
|
key = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (key == nullptr)
|
|
|
|
break;
|
2017-06-14 15:51:14 +00:00
|
|
|
try {
|
|
|
|
this->set_deserialize(key, value);
|
2018-02-13 14:19:55 +00:00
|
|
|
++num_key_value_pairs;
|
|
|
|
}
|
|
|
|
catch (UnknownOptionException & /* e */) {
|
2017-06-14 15:51:14 +00:00
|
|
|
// ignore
|
|
|
|
}
|
2017-06-14 16:53:11 +00:00
|
|
|
end = start;
|
2017-06-14 15:51:14 +00:00
|
|
|
}
|
2018-11-07 13:57:50 +00:00
|
|
|
|
|
|
|
return num_key_value_pairs;
|
2017-06-14 15:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigBase::save(const std::string &file) const
|
|
|
|
{
|
|
|
|
boost::nowide::ofstream c;
|
2017-09-18 07:56:48 +00:00
|
|
|
c.open(file, std::ios::out | std::ios::trunc);
|
2017-10-30 17:15:41 +00:00
|
|
|
c << "# " << Slic3r::header_slic3r_generated() << std::endl;
|
2017-09-18 07:56:48 +00:00
|
|
|
for (const std::string &opt_key : this->keys())
|
|
|
|
c << opt_key << " = " << this->serialize(opt_key) << std::endl;
|
2017-06-14 15:51:14 +00:00
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
2017-11-01 18:30:05 +00:00
|
|
|
bool DynamicConfig::operator==(const DynamicConfig &rhs) const
|
|
|
|
{
|
|
|
|
t_options_map::const_iterator it1 = this->options.begin();
|
|
|
|
t_options_map::const_iterator it1_end = this->options.end();
|
|
|
|
t_options_map::const_iterator it2 = rhs.options.begin();
|
|
|
|
t_options_map::const_iterator it2_end = rhs.options.end();
|
|
|
|
for (; it1 != it1_end && it2 != it2_end; ++ it1, ++ it2)
|
|
|
|
if (*it1->second != *it2->second)
|
|
|
|
return false;
|
|
|
|
return it1 == it1_end && it2 == it2_end;
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:01:18 +00:00
|
|
|
ConfigOption* DynamicConfig::optptr(const t_config_option_key &opt_key, bool create)
|
|
|
|
{
|
2016-09-13 11:30:00 +00:00
|
|
|
t_options_map::iterator it = options.find(opt_key);
|
2017-10-17 14:01:18 +00:00
|
|
|
if (it != options.end())
|
|
|
|
// Option was found.
|
|
|
|
return it->second;
|
|
|
|
if (! create)
|
|
|
|
// Option was not found and a new option shall not be created.
|
|
|
|
return nullptr;
|
|
|
|
// Try to create a new ConfigOption.
|
2017-11-17 10:15:46 +00:00
|
|
|
const ConfigDef *def = this->def();
|
|
|
|
if (def == nullptr)
|
2018-03-09 15:37:33 +00:00
|
|
|
throw NoDefinitionException(opt_key);
|
2017-11-17 10:15:46 +00:00
|
|
|
const ConfigOptionDef *optdef = def->get(opt_key);
|
2017-10-17 14:01:18 +00:00
|
|
|
if (optdef == nullptr)
|
|
|
|
// throw std::runtime_error(std::string("Invalid option name: ") + opt_key);
|
|
|
|
// Let the parent decide what to do if the opt_key is not defined by this->def().
|
|
|
|
return nullptr;
|
|
|
|
ConfigOption *opt = nullptr;
|
|
|
|
switch (optdef->type) {
|
|
|
|
case coFloat: opt = new ConfigOptionFloat(); break;
|
|
|
|
case coFloats: opt = new ConfigOptionFloats(); break;
|
|
|
|
case coInt: opt = new ConfigOptionInt(); break;
|
|
|
|
case coInts: opt = new ConfigOptionInts(); break;
|
|
|
|
case coString: opt = new ConfigOptionString(); break;
|
|
|
|
case coStrings: opt = new ConfigOptionStrings(); break;
|
|
|
|
case coPercent: opt = new ConfigOptionPercent(); break;
|
|
|
|
case coPercents: opt = new ConfigOptionPercents(); break;
|
|
|
|
case coFloatOrPercent: opt = new ConfigOptionFloatOrPercent(); break;
|
|
|
|
case coPoint: opt = new ConfigOptionPoint(); break;
|
|
|
|
case coPoints: opt = new ConfigOptionPoints(); break;
|
|
|
|
case coBool: opt = new ConfigOptionBool(); break;
|
|
|
|
case coBools: opt = new ConfigOptionBools(); break;
|
|
|
|
case coEnum: opt = new ConfigOptionEnumGeneric(optdef->enum_keys_map); break;
|
|
|
|
default: throw std::runtime_error(std::string("Unknown option type for option ") + opt_key);
|
2013-12-20 19:54:11 +00:00
|
|
|
}
|
2017-10-17 14:01:18 +00:00
|
|
|
this->options[opt_key] = opt;
|
|
|
|
return opt;
|
2014-05-26 21:51:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 14:48:13 +00:00
|
|
|
void DynamicConfig::read_cli(const std::vector<std::string> &tokens, t_config_option_keys* extra)
|
|
|
|
{
|
|
|
|
std::vector<char*> args;
|
|
|
|
// push a bogus executable name (argv[0])
|
|
|
|
args.emplace_back(const_cast<char*>(""));
|
|
|
|
for (size_t i = 0; i < tokens.size(); ++ i)
|
|
|
|
args.emplace_back(const_cast<char *>(tokens[i].c_str()));
|
|
|
|
this->read_cli(args.size(), &args[0], extra);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DynamicConfig::read_cli(int argc, char** argv, t_config_option_keys* extra)
|
|
|
|
{
|
|
|
|
// cache the CLI option => opt_key mapping
|
|
|
|
std::map<std::string,std::string> opts;
|
|
|
|
for (const auto &oit : this->def()->options) {
|
|
|
|
std::string cli = oit.second.cli;
|
|
|
|
cli = cli.substr(0, cli.find("="));
|
|
|
|
boost::trim_right_if(cli, boost::is_any_of("!"));
|
|
|
|
std::vector<std::string> tokens;
|
|
|
|
boost::split(tokens, cli, boost::is_any_of("|"));
|
|
|
|
for (const std::string &t : tokens)
|
|
|
|
opts[t] = oit.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_options = true;
|
|
|
|
for (int i = 1; i < argc; ++ i) {
|
|
|
|
std::string token = argv[i];
|
|
|
|
// Store non-option arguments in the provided vector.
|
|
|
|
if (! parse_options || ! boost::starts_with(token, "-")) {
|
|
|
|
extra->push_back(token);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Stop parsing tokens as options when -- is supplied.
|
|
|
|
if (token == "--") {
|
|
|
|
parse_options = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Remove leading dashes
|
|
|
|
boost::trim_left_if(token, boost::is_any_of("-"));
|
|
|
|
// Remove the "no-" prefix used to negate boolean options.
|
|
|
|
bool no = false;
|
|
|
|
if (boost::starts_with(token, "no-")) {
|
|
|
|
no = true;
|
|
|
|
boost::replace_first(token, "no-", "");
|
|
|
|
}
|
|
|
|
// Read value when supplied in the --key=value form.
|
|
|
|
std::string value;
|
|
|
|
{
|
|
|
|
size_t equals_pos = token.find("=");
|
|
|
|
if (equals_pos != std::string::npos) {
|
|
|
|
value = token.substr(equals_pos+1);
|
|
|
|
token.erase(equals_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Look for the cli -> option mapping.
|
|
|
|
const auto it = opts.find(token);
|
|
|
|
if (it == opts.end()) {
|
|
|
|
printf("Warning: unknown option --%s\n", token.c_str());
|
|
|
|
// instead of continuing, return false to caller
|
|
|
|
// to stop execution and print usage
|
|
|
|
return false;
|
|
|
|
//continue;
|
|
|
|
}
|
|
|
|
const t_config_option_key opt_key = it->second;
|
|
|
|
const ConfigOptionDef &optdef = this->def()->options.at(opt_key);
|
|
|
|
// If the option type expects a value and it was not already provided,
|
|
|
|
// look for it in the next token.
|
|
|
|
if (optdef.type != coBool && optdef.type != coBools && value.empty()) {
|
|
|
|
if (i == (argc-1)) {
|
|
|
|
printf("No value supplied for --%s\n", token.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
value = argv[++ i];
|
|
|
|
}
|
|
|
|
// Store the option value.
|
|
|
|
const bool existing = this->has(opt_key);
|
|
|
|
if (ConfigOptionBool* opt = this->opt<ConfigOptionBool>(opt_key, true)) {
|
|
|
|
opt->value = !no;
|
|
|
|
} else if (ConfigOptionBools* opt = this->opt<ConfigOptionBools>(opt_key, true)) {
|
|
|
|
if (!existing) opt->values.clear(); // remove the default values
|
|
|
|
opt->values.push_back(!no);
|
|
|
|
} else if (ConfigOptionStrings* opt = this->opt<ConfigOptionStrings>(opt_key, true)) {
|
|
|
|
if (!existing) opt->values.clear(); // remove the default values
|
|
|
|
opt->deserialize(value, true);
|
|
|
|
} else if (ConfigOptionFloats* opt = this->opt<ConfigOptionFloats>(opt_key, true)) {
|
|
|
|
if (!existing) opt->values.clear(); // remove the default values
|
|
|
|
opt->deserialize(value, true);
|
|
|
|
} else if (ConfigOptionPoints* opt = this->opt<ConfigOptionPoints>(opt_key, true)) {
|
|
|
|
if (!existing) opt->values.clear(); // remove the default values
|
|
|
|
opt->deserialize(value, true);
|
|
|
|
} else {
|
|
|
|
this->set_deserialize(opt_key, value, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-30 15:04:36 +00:00
|
|
|
t_config_option_keys DynamicConfig::keys() const
|
|
|
|
{
|
2015-07-01 16:18:25 +00:00
|
|
|
t_config_option_keys keys;
|
2017-05-30 15:04:36 +00:00
|
|
|
keys.reserve(this->options.size());
|
|
|
|
for (const auto &opt : this->options)
|
|
|
|
keys.emplace_back(opt.first);
|
2015-07-01 16:18:25 +00:00
|
|
|
return keys;
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 15:04:36 +00:00
|
|
|
void StaticConfig::set_defaults()
|
2015-12-07 18:39:49 +00:00
|
|
|
{
|
|
|
|
// use defaults from definition
|
2017-10-17 14:01:18 +00:00
|
|
|
auto *defs = this->def();
|
|
|
|
if (defs != nullptr) {
|
2017-05-30 15:04:36 +00:00
|
|
|
for (const std::string &key : this->keys()) {
|
2017-10-17 14:01:18 +00:00
|
|
|
const ConfigOptionDef *def = defs->get(key);
|
|
|
|
ConfigOption *opt = this->option(key);
|
|
|
|
if (def != nullptr && opt != nullptr && def->default_value != nullptr)
|
|
|
|
opt->set(def->default_value);
|
2017-05-30 15:04:36 +00:00
|
|
|
}
|
2015-12-07 18:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 15:04:36 +00:00
|
|
|
t_config_option_keys StaticConfig::keys() const
|
|
|
|
{
|
2015-07-01 16:18:25 +00:00
|
|
|
t_config_option_keys keys;
|
2018-05-23 12:15:10 +00:00
|
|
|
assert(this->def() != nullptr);
|
2017-10-17 14:01:18 +00:00
|
|
|
for (const auto &opt_def : this->def()->options)
|
2017-05-30 15:04:36 +00:00
|
|
|
if (this->option(opt_def.first) != nullptr)
|
|
|
|
keys.push_back(opt_def.first);
|
2015-07-01 16:18:25 +00:00
|
|
|
return keys;
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
2013-12-07 15:14:30 +00:00
|
|
|
|
|
|
|
}
|