diff options
author | Enrico Turri <enricoturri@seznam.cz> | 2018-02-13 17:19:55 +0300 |
---|---|---|
committer | Enrico Turri <enricoturri@seznam.cz> | 2018-02-13 17:19:55 +0300 |
commit | 8885f5e3447d83e333cf30185189befdee3e9f92 (patch) | |
tree | b79b277fa93291d2d6b81ed9b752e584cb3b3c10 /xs/src/libslic3r/Config.cpp | |
parent | 33553e1c5092f1b0e4fe68b209895d498e91ed66 (diff) |
3mf import/export of config data
Diffstat (limited to 'xs/src/libslic3r/Config.cpp')
-rw-r--r-- | xs/src/libslic3r/Config.cpp | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/xs/src/libslic3r/Config.cpp b/xs/src/libslic3r/Config.cpp index 474be7237..1a0b8fb4a 100644 --- a/xs/src/libslic3r/Config.cpp +++ b/xs/src/libslic3r/Config.cpp @@ -324,7 +324,7 @@ void ConfigBase::setenv_() void ConfigBase::load(const std::string &file) { if (boost::iends_with(file, ".gcode") || boost::iends_with(file, ".g")) - this->load_from_gcode(file); + this->load_from_gcode_file(file); else this->load_from_ini(file); } @@ -349,10 +349,10 @@ void ConfigBase::load(const boost::property_tree::ptree &tree) } } -// Load the config keys from the tail of a G-code. -void ConfigBase::load_from_gcode(const std::string &file) +// Load the config keys from the tail of a G-code file. +void ConfigBase::load_from_gcode_file(const std::string &file) { - // 1) Read a 64k block from the end of the G-code. + // Read a 64k block from the end of the G-code. boost::nowide::ifstream ifs(file); { const char slic3r_gcode_header[] = "; generated by Slic3r "; @@ -365,30 +365,39 @@ void ConfigBase::load_from_gcode(const std::string &file) 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); + std::vector<char> data(size_t(data_length) + 1, 0); + ifs.read(data.data(), data_length); ifs.close(); - // 2) Walk line by line in reverse until a non-configuration key appears. - char *data_start = data.data(); + load_from_gcode_string(data.data()); +} + +// Load the config keys from the given string. +void ConfigBase::load_from_gcode_string(const char* str) +{ + if (str == nullptr) + return; + + // Walk line by line in reverse until a non-configuration key appears. + char *data_start = const_cast<char*>(str); // 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()); + char *end = data_start + strlen(str); size_t num_key_value_pairs = 0; for (;;) { // Extract next line. - for (-- end; end > data_start && (*end == '\r' || *end == '\n'); -- end); + for (--end; end > data_start && (*end == '\r' || *end == '\n'); --end); if (end == data_start) break; char *start = end; - *(++ end) = 0; - for (; start > data_start && *start != '\r' && *start != '\n'; -- start); + *(++end) = 0; + for (; start > data_start && *start != '\r' && *start != '\n'; --start); if (start == data_start) break; // Extracted a line from start to end. Extract the key = value pair. - if (end - (++ start) < 10 || start[0] != ';' || start[1] != ' ') + if (end - (++start) < 10 || start[0] != ';' || start[1] != ' ') break; char *key = start + 2; - if (! (*key >= 'a' && *key <= 'z') || (*key >= 'A' && *key <= 'Z')) + if (!(*key >= 'a' && *key <= 'z') || (*key >= 'A' && *key <= 'Z')) // A key must start with a letter. break; char *sep = strchr(key, '='); @@ -402,8 +411,8 @@ void ConfigBase::load_from_gcode(const std::string &file) break; *key_end = 0; // 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 == '_')) { + 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; } @@ -411,8 +420,9 @@ void ConfigBase::load_from_gcode(const std::string &file) break; try { this->set_deserialize(key, value); - ++ num_key_value_pairs; - } catch (UnknownOptionException & /* e */) { + ++num_key_value_pairs; + } + catch (UnknownOptionException & /* e */) { // ignore } end = start; |