From f361d8ad4337019af05618dd3dca8d28497c5182 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Wed, 1 Jul 2015 17:56:38 +0200 Subject: [PATCH] Ported PlaceholderParser::apply_env_variables() to XS --- lib/Slic3r/GCode/PlaceholderParser.pm | 14 -------------- xs/src/libslic3r/PlaceholderParser.cpp | 21 +++++++++++++++++++-- xs/src/libslic3r/PlaceholderParser.hpp | 1 + xs/xsp/PlaceholderParser.xsp | 3 ++- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/Slic3r/GCode/PlaceholderParser.pm b/lib/Slic3r/GCode/PlaceholderParser.pm index 2f76c4ff4..1bbb22f67 100644 --- a/lib/Slic3r/GCode/PlaceholderParser.pm +++ b/lib/Slic3r/GCode/PlaceholderParser.pm @@ -2,20 +2,6 @@ package Slic3r::GCode::PlaceholderParser; use strict; use warnings; -sub new { - # TODO: move this code to C++ constructor, remove this method - my ($class) = @_; - - my $self = $class->_new; - $self->apply_env_variables; - return $self; -} - -sub apply_env_variables { - my ($self) = @_; - $self->_single_set($_, $ENV{$_}) for grep /^SLIC3R_/, keys %ENV; -} - sub process { my ($self, $string, $extra) = @_; diff --git a/xs/src/libslic3r/PlaceholderParser.cpp b/xs/src/libslic3r/PlaceholderParser.cpp index 583cf51e6..1d40f94b7 100644 --- a/xs/src/libslic3r/PlaceholderParser.cpp +++ b/xs/src/libslic3r/PlaceholderParser.cpp @@ -2,14 +2,16 @@ #include #include #include +#include // provides **environ + +extern char **environ; namespace Slic3r { PlaceholderParser::PlaceholderParser() { this->_single["version"] = SLIC3R_VERSION; - // TODO: port these methods to C++, then call them here - // this->apply_env_variables(); + this->apply_env_variables(); this->update_timestamp(); } @@ -76,6 +78,21 @@ void PlaceholderParser::apply_config(DynamicPrintConfig &config) } } +void +PlaceholderParser::apply_env_variables() +{ + for (char** env = environ; *env; env++) { + if (strncmp(*env, "SLIC3R_", 7) == 0) { + std::stringstream ss(*env); + std::string key, value; + std::getline(ss, key, '='); + ss >> value; + + this->set(key, value); + } + } +} + void PlaceholderParser::set(const std::string &key, const std::string &value) { diff --git a/xs/src/libslic3r/PlaceholderParser.hpp b/xs/src/libslic3r/PlaceholderParser.hpp index 25d1bcdc3..1488e19f2 100644 --- a/xs/src/libslic3r/PlaceholderParser.hpp +++ b/xs/src/libslic3r/PlaceholderParser.hpp @@ -20,6 +20,7 @@ class PlaceholderParser PlaceholderParser(); void update_timestamp(); void apply_config(DynamicPrintConfig &config); + void apply_env_variables(); void set(const std::string &key, const std::string &value); void set(const std::string &key, int value); void set(const std::string &key, const std::vector &values); diff --git a/xs/xsp/PlaceholderParser.xsp b/xs/xsp/PlaceholderParser.xsp index b36a47189..5a0cccc10 100644 --- a/xs/xsp/PlaceholderParser.xsp +++ b/xs/xsp/PlaceholderParser.xsp @@ -7,10 +7,11 @@ %} %name{Slic3r::GCode::PlaceholderParser} class PlaceholderParser { - %name{_new} PlaceholderParser(); + PlaceholderParser(); ~PlaceholderParser(); void update_timestamp(); + void apply_env_variables(); void apply_config(DynamicPrintConfig *config) %code%{ THIS->apply_config(*config); %}; void set(std::string key, std::string value);