2013-12-07 15:14:30 +00:00
|
|
|
#include "Config.hpp"
|
2015-12-11 20:36:51 +00:00
|
|
|
#include <assert.h>
|
2017-06-14 15:51:14 +00:00
|
|
|
#include <ctime>
|
|
|
|
#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>
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
2016-11-01 12:53:28 +00:00
|
|
|
#include <string.h>
|
2013-12-07 15:14:30 +00:00
|
|
|
|
2015-11-06 10:34:37 +00:00
|
|
|
#if defined(_WIN32) && !defined(setenv) && defined(_putenv_s)
|
2015-07-10 14:01:45 +00:00
|
|
|
#define setenv(k, v, o) _putenv_s(k, v)
|
|
|
|
#endif
|
|
|
|
|
2013-12-07 15:14:30 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
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];
|
|
|
|
if (c == '\n' || c == '\r') {
|
|
|
|
(*outptr ++) = '\\';
|
|
|
|
(*outptr ++) = 'n';
|
|
|
|
} 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;
|
|
|
|
} else if (c == '\n' || c == '\r') {
|
|
|
|
(*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());
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
if (c == 'n')
|
|
|
|
(*outptr ++) = '\n';
|
|
|
|
} 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];
|
|
|
|
if (c == 'n')
|
|
|
|
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-05-30 18:09:34 +00:00
|
|
|
void ConfigBase::apply(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-05-30 18:09:34 +00:00
|
|
|
for (const t_config_option_key &key : keys) {
|
|
|
|
ConfigOption *my_opt = this->option(key, true);
|
|
|
|
if (my_opt == nullptr) {
|
|
|
|
if (! ignore_nonexistent)
|
|
|
|
throw "Attempt to apply non-existent option";
|
2013-12-21 15:32:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// not the most efficient way, but easier than casting pointers to subclasses
|
2017-05-30 18:09:34 +00:00
|
|
|
if (! my_opt->deserialize(other.option(key)->serialize()))
|
|
|
|
CONFESS((std::string("Unexpected failure when deserializing serialized value for ") + key).c_str());
|
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-05-30 15:04:36 +00:00
|
|
|
for (const t_config_option_key &opt_key : this->keys())
|
|
|
|
if (other.has(opt_key) && other.serialize(opt_key) != this->serialize(opt_key))
|
|
|
|
diff.push_back(opt_key);
|
2014-11-09 11:25:59 +00:00
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
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-06-14 15:51:14 +00:00
|
|
|
bool ConfigBase::set_deserialize(t_config_option_key opt_key, const std::string &str, bool append)
|
2017-05-30 15:04:36 +00:00
|
|
|
{
|
2015-12-07 18:39:49 +00:00
|
|
|
const ConfigOptionDef* optdef = this->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.
|
|
|
|
for (const auto &opt : this->def->options) {
|
|
|
|
for (const t_config_option_key &opt_key2 : opt.second.aliases) {
|
|
|
|
if (opt_key2 == opt_key) {
|
|
|
|
opt_key = opt_key2;
|
|
|
|
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)
|
|
|
|
throw UnknownOptionException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! optdef->shortcut.empty()) {
|
|
|
|
for (const t_config_option_key &shortcut : optdef->shortcut)
|
|
|
|
if (! this->set_deserialize(shortcut, str))
|
|
|
|
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-06-14 15:51:14 +00:00
|
|
|
return opt->deserialize(str, 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
|
|
|
|
{
|
2016-11-01 12:41:24 +00:00
|
|
|
const ConfigOption* opt = this->option(opt_key);
|
|
|
|
if (const ConfigOptionFloatOrPercent* optv = dynamic_cast<const ConfigOptionFloatOrPercent*>(opt)) {
|
2014-01-05 12:16:13 +00:00
|
|
|
// get option definition
|
2015-12-07 18:39:49 +00:00
|
|
|
const ConfigOptionDef* def = this->def->get(opt_key);
|
2017-05-30 15:04:36 +00:00
|
|
|
assert(def != nullptr);
|
2014-01-05 12:16:13 +00:00
|
|
|
// compute absolute value over the absolute value of the base option
|
|
|
|
return optv->get_abs_value(this->get_abs_value(def->ratio_over));
|
2016-11-01 12:41:24 +00:00
|
|
|
} else if (const ConfigOptionFloat* optv = dynamic_cast<const ConfigOptionFloat*>(opt)) {
|
2014-01-05 12:16:13 +00:00
|
|
|
return optv->value;
|
2013-12-20 19:54:11 +00:00
|
|
|
} else {
|
2014-01-05 12:16:13 +00:00
|
|
|
throw "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
|
|
|
|
{
|
2014-01-01 16:29:15 +00:00
|
|
|
// get stored option value
|
2016-11-01 12:41:24 +00:00
|
|
|
const ConfigOptionFloatOrPercent* opt = dynamic_cast<const ConfigOptionFloatOrPercent*>(this->option(opt_key));
|
2017-05-30 15:04:36 +00:00
|
|
|
assert(opt != nullptr);
|
2014-01-01 16:29:15 +00:00
|
|
|
// compute absolute value
|
2014-01-05 12:16:13 +00:00
|
|
|
return 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
|
|
|
{
|
2015-11-06 10:34:37 +00:00
|
|
|
#ifdef 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];
|
|
|
|
|
|
|
|
setenv(envname.c_str(), this->serialize(*it).c_str(), 1);
|
|
|
|
}
|
2015-11-06 10:34:37 +00:00
|
|
|
#endif
|
2015-07-01 16:18:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 15:51:14 +00:00
|
|
|
void ConfigBase::load(const std::string &file)
|
|
|
|
{
|
|
|
|
namespace pt = boost::property_tree;
|
|
|
|
pt::ptree tree;
|
|
|
|
boost::nowide::ifstream ifs(file);
|
|
|
|
pt::read_ini(ifs, tree);
|
|
|
|
for (const pt::ptree::value_type &v : tree) {
|
|
|
|
try {
|
|
|
|
t_config_option_key opt_key = v.first;
|
|
|
|
std::string value = v.second.get_value<std::string>();
|
|
|
|
this->set_deserialize(opt_key, value);
|
|
|
|
} catch (UnknownOptionException & /* e */) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the config keys from the tail of a G-code.
|
|
|
|
void ConfigBase::load_from_gcode(const std::string &file)
|
|
|
|
{
|
|
|
|
// 1) 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);
|
|
|
|
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();
|
|
|
|
|
|
|
|
// 2) Walk line by line in reverse until a non-configuration key appears.
|
|
|
|
char *data_start = data.data();
|
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.
|
|
|
|
char *end = data_start + strlen(data.data());
|
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.
|
|
|
|
for (-- end; end > data_start && (*end == '\r' || *end == '\n'); -- end);
|
|
|
|
if (end == data_start)
|
|
|
|
break;
|
|
|
|
char *start = end;
|
|
|
|
*(++ end) = 0;
|
2017-06-14 16:53:11 +00:00
|
|
|
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.
|
2017-06-14 16:53:11 +00:00
|
|
|
if (end - (++ start) < 10 || start[0] != ';' || start[1] != ' ')
|
2017-06-14 15:51:14 +00:00
|
|
|
break;
|
|
|
|
char *key = start + 2;
|
2017-06-14 16:53:11 +00:00
|
|
|
if (! (*key >= 'a' && *key <= 'z') || (*key >= 'A' && *key <= 'Z'))
|
|
|
|
// 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.
|
|
|
|
for (char *c = key; c != key_end; ++ c)
|
|
|
|
if (! ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9') || *c == '_')) {
|
|
|
|
key = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (key == nullptr)
|
|
|
|
break;
|
2017-06-14 15:51:14 +00:00
|
|
|
try {
|
|
|
|
this->set_deserialize(key, value);
|
2017-06-14 18:18:46 +00:00
|
|
|
++ num_key_value_pairs;
|
2017-06-14 15:51:14 +00:00
|
|
|
} catch (UnknownOptionException & /* e */) {
|
|
|
|
// ignore
|
|
|
|
}
|
2017-06-14 16:53:11 +00:00
|
|
|
end = start;
|
2017-06-14 15:51:14 +00:00
|
|
|
}
|
2017-06-14 18:18:46 +00:00
|
|
|
if (num_key_value_pairs < 90) {
|
|
|
|
char msg[80];
|
|
|
|
sprintf(msg, "Suspiciously low number of configuration values extracted: %d", num_key_value_pairs);
|
2017-06-14 18:33:20 +00:00
|
|
|
throw std::runtime_error(msg);
|
2017-06-14 18:18:46 +00:00
|
|
|
}
|
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-06-14 15:51:14 +00:00
|
|
|
{
|
2017-09-18 07:56:48 +00:00
|
|
|
std::time_t now;
|
2017-06-14 15:51:14 +00:00
|
|
|
time(&now);
|
|
|
|
char buf[sizeof "0000-00-00 00:00:00"];
|
2017-09-18 07:56:48 +00:00
|
|
|
strftime(buf, sizeof(buf), "%F %T", gmtime(&now));
|
|
|
|
c << "# generated by Slic3r " << SLIC3R_VERSION << " on " << buf << std::endl;
|
2017-06-14 15:51:14 +00:00
|
|
|
}
|
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-05-30 15:04:36 +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);
|
|
|
|
if (it == options.end()) {
|
2013-12-20 19:54:11 +00:00
|
|
|
if (create) {
|
2015-12-07 18:39:49 +00:00
|
|
|
const ConfigOptionDef* optdef = this->def->get(opt_key);
|
|
|
|
assert(optdef != NULL);
|
2013-12-20 19:54:11 +00:00
|
|
|
ConfigOption* opt;
|
2013-12-21 15:15:41 +00:00
|
|
|
if (optdef->type == coFloat) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionFloat ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coFloats) {
|
2013-12-21 13:27:58 +00:00
|
|
|
opt = new ConfigOptionFloats ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coInt) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionInt ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coInts) {
|
2013-12-21 13:27:58 +00:00
|
|
|
opt = new ConfigOptionInts ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coString) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionString ();
|
2013-12-21 23:39:03 +00:00
|
|
|
} else if (optdef->type == coStrings) {
|
|
|
|
opt = new ConfigOptionStrings ();
|
2014-03-22 15:23:33 +00:00
|
|
|
} else if (optdef->type == coPercent) {
|
|
|
|
opt = new ConfigOptionPercent ();
|
2017-05-19 17:24:21 +00:00
|
|
|
} else if (optdef->type == coPercents) {
|
|
|
|
opt = new ConfigOptionPercents ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coFloatOrPercent) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionFloatOrPercent ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coPoint) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionPoint ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coPoints) {
|
2013-12-21 13:27:58 +00:00
|
|
|
opt = new ConfigOptionPoints ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coBool) {
|
2013-12-20 20:32:18 +00:00
|
|
|
opt = new ConfigOptionBool ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coBools) {
|
2013-12-21 13:27:58 +00:00
|
|
|
opt = new ConfigOptionBools ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coEnum) {
|
|
|
|
ConfigOptionEnumGeneric* optv = new ConfigOptionEnumGeneric ();
|
|
|
|
optv->keys_map = &optdef->enum_keys_map;
|
|
|
|
opt = static_cast<ConfigOption*>(optv);
|
2013-12-20 19:54:11 +00:00
|
|
|
} else {
|
|
|
|
throw "Unknown option type";
|
|
|
|
}
|
|
|
|
this->options[opt_key] = opt;
|
|
|
|
return opt;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 11:30:00 +00:00
|
|
|
return it->second;
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-26 21:51:58 +00:00
|
|
|
template<class T>
|
2017-05-30 15:04:36 +00:00
|
|
|
T* DynamicConfig::opt(const t_config_option_key &opt_key, bool create) {
|
2014-05-26 21:51:58 +00:00
|
|
|
return dynamic_cast<T*>(this->option(opt_key, create));
|
|
|
|
}
|
2015-08-11 23:03:43 +00:00
|
|
|
template ConfigOptionInt* DynamicConfig::opt<ConfigOptionInt>(const t_config_option_key &opt_key, bool create);
|
|
|
|
template ConfigOptionBool* DynamicConfig::opt<ConfigOptionBool>(const t_config_option_key &opt_key, bool create);
|
|
|
|
template ConfigOptionBools* DynamicConfig::opt<ConfigOptionBools>(const t_config_option_key &opt_key, bool create);
|
|
|
|
template ConfigOptionPercent* DynamicConfig::opt<ConfigOptionPercent>(const t_config_option_key &opt_key, bool create);
|
2014-05-26 21:51:58 +00:00
|
|
|
|
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-05-30 15:04:36 +00:00
|
|
|
if (this->def != nullptr) {
|
|
|
|
for (const std::string &key : this->keys()) {
|
|
|
|
const ConfigOptionDef* def = this->def->get(key);
|
|
|
|
if (def->default_value != nullptr)
|
|
|
|
this->option(key)->set(*def->default_value);
|
|
|
|
}
|
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;
|
2017-05-30 15:04:36 +00:00
|
|
|
assert(this->def != nullptr);
|
|
|
|
for (const auto &opt_def : this->def->options)
|
|
|
|
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
|
|
|
|
|
|
|
}
|