From 249088b4f8d5ac61c97c6590e6d8b3cb3ba6f88b Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Wed, 1 Jul 2015 18:18:25 +0200 Subject: [PATCH] Ported Config::setenv() to XS --- lib/Slic3r/Config.pm | 8 ------- xs/src/libslic3r/Config.cpp | 46 ++++++++++++++++++++++++++----------- xs/src/libslic3r/Config.hpp | 7 +++--- xs/xsp/Config.xsp | 24 +++++++++---------- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/lib/Slic3r/Config.pm b/lib/Slic3r/Config.pm index bc0455889..23ab7ea89 100644 --- a/lib/Slic3r/Config.pm +++ b/lib/Slic3r/Config.pm @@ -192,14 +192,6 @@ sub save { __PACKAGE__->write_ini($file, $self->as_ini); } -sub setenv { - my $self = shift; - - foreach my $opt_key (@{$self->get_keys}) { - $ENV{"SLIC3R_" . uc $opt_key} = $self->serialize($opt_key); - } -} - # this method is idempotent by design and only applies to ::DynamicConfig or ::Full # objects because it performs cross checks sub validate { diff --git a/xs/src/libslic3r/Config.cpp b/xs/src/libslic3r/Config.cpp index d1c51ac53..07a8e8637 100644 --- a/xs/src/libslic3r/Config.cpp +++ b/xs/src/libslic3r/Config.cpp @@ -1,4 +1,5 @@ #include "Config.hpp" +#include // for setenv() namespace Slic3r { @@ -10,8 +11,7 @@ ConfigBase::has(const t_config_option_key opt_key) { void ConfigBase::apply(const ConfigBase &other, bool ignore_nonexistent) { // get list of option keys to apply - t_config_option_keys opt_keys; - other.keys(&opt_keys); + t_config_option_keys opt_keys = other.keys(); // loop through options and apply them for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) { @@ -37,8 +37,7 @@ t_config_option_keys ConfigBase::diff(ConfigBase &other) { t_config_option_keys diff; - t_config_option_keys my_keys; - this->keys(&my_keys); + t_config_option_keys my_keys = this->keys(); for (t_config_option_keys::const_iterator opt_key = my_keys.begin(); opt_key != my_keys.end(); ++opt_key) { if (other.has(*opt_key) && other.serialize(*opt_key) != this->serialize(*opt_key)) { diff.push_back(*opt_key); @@ -98,14 +97,31 @@ ConfigBase::get_abs_value(const t_config_option_key opt_key, double ratio_over) return opt->get_abs_value(ratio_over); } +void +ConfigBase::setenv_() +{ + 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); + } +} + #ifdef SLIC3RXS SV* ConfigBase::as_hash() { HV* hv = newHV(); - t_config_option_keys opt_keys; - this->keys(&opt_keys); - + t_config_option_keys opt_keys = this->keys(); for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) (void)hv_store( hv, it->c_str(), it->length(), this->get(*it), 0 ); @@ -368,10 +384,12 @@ DynamicConfig::option(const t_config_option_key opt_key) const { return const_cast(this)->option(opt_key, false); } -void -DynamicConfig::keys(t_config_option_keys *keys) const { +t_config_option_keys +DynamicConfig::keys() const { + t_config_option_keys keys; for (t_options_map::const_iterator it = this->options.begin(); it != this->options.end(); ++it) - keys->push_back(it->first); + keys.push_back(it->first); + return keys; } void @@ -379,12 +397,14 @@ DynamicConfig::erase(const t_config_option_key opt_key) { this->options.erase(opt_key); } -void -StaticConfig::keys(t_config_option_keys *keys) const { +t_config_option_keys +StaticConfig::keys() const { + t_config_option_keys keys; for (t_optiondef_map::const_iterator it = this->def->begin(); it != this->def->end(); ++it) { const ConfigOption* opt = this->option(it->first); - if (opt != NULL) keys->push_back(it->first); + if (opt != NULL) keys.push_back(it->first); } + return keys; } const ConfigOption* diff --git a/xs/src/libslic3r/Config.hpp b/xs/src/libslic3r/Config.hpp index 49e999bc0..be433633d 100644 --- a/xs/src/libslic3r/Config.hpp +++ b/xs/src/libslic3r/Config.hpp @@ -515,7 +515,7 @@ class ConfigBase bool has(const t_config_option_key opt_key); virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0; virtual const ConfigOption* option(const t_config_option_key opt_key) const = 0; - virtual void keys(t_config_option_keys *keys) const = 0; + virtual t_config_option_keys keys() const = 0; void apply(const ConfigBase &other, bool ignore_nonexistent = false); bool equals(ConfigBase &other); t_config_option_keys diff(ConfigBase &other); @@ -524,6 +524,7 @@ class ConfigBase void set_ifndef(t_config_option_key opt_key, SV* value, bool deserialize = false); double get_abs_value(const t_config_option_key opt_key); double get_abs_value(const t_config_option_key opt_key, double ratio_over); + void setenv_(); #ifdef SLIC3RXS SV* as_hash(); @@ -545,7 +546,7 @@ class DynamicConfig : public ConfigBase template T* opt(const t_config_option_key opt_key, bool create = false); ConfigOption* option(const t_config_option_key opt_key, bool create = false); const ConfigOption* option(const t_config_option_key opt_key) const; - void keys(t_config_option_keys *keys) const; + t_config_option_keys keys() const; void erase(const t_config_option_key opt_key); private: @@ -556,7 +557,7 @@ class DynamicConfig : public ConfigBase class StaticConfig : public ConfigBase { public: - void keys(t_config_option_keys *keys) const; + t_config_option_keys keys() const; virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0; const ConfigOption* option(const t_config_option_key opt_key) const; diff --git a/xs/xsp/Config.xsp b/xs/xsp/Config.xsp index 4ec282edd..3628d524e 100644 --- a/xs/xsp/Config.xsp +++ b/xs/xsp/Config.xsp @@ -27,10 +27,10 @@ %code{% RETVAL = THIS->equals(*other); %}; void apply_static(FullPrintConfig* other) %code{% THIS->apply(*other, true); %}; - std::vector get_keys() - %code{% THIS->keys(&RETVAL); %}; + %name{get_keys} std::vector keys(); void erase(t_config_option_key opt_key); void normalize(); + %name{setenv} void setenv_(); }; %name{Slic3r::Config::GCode} class GCodeConfig { @@ -51,9 +51,9 @@ %code{% THIS->apply(*other, true); %}; void apply_dynamic(DynamicPrintConfig* other) %code{% THIS->apply(*other, true); %}; - std::vector get_keys() - %code{% THIS->keys(&RETVAL); %}; + %name{get_keys} std::vector keys(); std::string get_extrusion_axis(); + %name{setenv} void setenv_(); }; %name{Slic3r::Config::Print} class PrintConfig { @@ -72,9 +72,9 @@ double get_abs_value(t_config_option_key opt_key, double ratio_over); void apply_dynamic(DynamicPrintConfig* other) %code{% THIS->apply(*other, true); %}; - std::vector get_keys() - %code{% THIS->keys(&RETVAL); %}; + %name{get_keys} std::vector keys(); std::string get_extrusion_axis(); + %name{setenv} void setenv_(); }; %name{Slic3r::Config::PrintRegion} class PrintRegionConfig { @@ -95,8 +95,8 @@ %code{% THIS->apply(*other, true); %}; void apply_dynamic(DynamicPrintConfig* other) %code{% THIS->apply(*other, true); %}; - std::vector get_keys() - %code{% THIS->keys(&RETVAL); %}; + %name{get_keys} std::vector keys(); + %name{setenv} void setenv_(); }; %name{Slic3r::Config::PrintObject} class PrintObjectConfig { @@ -117,8 +117,8 @@ %code{% THIS->apply(*other, true); %}; void apply_dynamic(DynamicPrintConfig* other) %code{% THIS->apply(*other, true); %}; - std::vector get_keys() - %code{% THIS->keys(&RETVAL); %}; + %name{get_keys} std::vector keys(); + %name{setenv} void setenv_(); }; %name{Slic3r::Config::Full} class FullPrintConfig { @@ -143,9 +143,9 @@ %code{% THIS->apply(*other, true); %}; void apply_dynamic(DynamicPrintConfig* other) %code{% THIS->apply(*other, true); %}; - std::vector get_keys() - %code{% THIS->keys(&RETVAL); %}; + %name{get_keys} std::vector keys(); std::string get_extrusion_axis(); + %name{setenv} void setenv_(); }; %package{Slic3r::Config};