diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index e814be9f5..7a4f46c93 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -7,7 +7,7 @@ use strict; use warnings; require v5.10; -our $VERSION = "1.2.2-dev"; +our $VERSION = VERSION(); our $debug = 0; sub debugf { diff --git a/lib/Slic3r/GCode/PlaceholderParser.pm b/lib/Slic3r/GCode/PlaceholderParser.pm index 0c2dafb82..2f76c4ff4 100644 --- a/lib/Slic3r/GCode/PlaceholderParser.pm +++ b/lib/Slic3r/GCode/PlaceholderParser.pm @@ -5,9 +5,9 @@ use warnings; sub new { # TODO: move this code to C++ constructor, remove this method my ($class) = @_; + my $self = $class->_new; $self->apply_env_variables; - $self->update_timestamp; return $self; } @@ -16,26 +16,6 @@ sub apply_env_variables { $self->_single_set($_, $ENV{$_}) for grep /^SLIC3R_/, keys %ENV; } -sub update_timestamp { - my ($self) = @_; - - my @lt = localtime; $lt[5] += 1900; $lt[4] += 1; - $self->_single_set('timestamp', sprintf '%04d%02d%02d-%02d%02d%02d', @lt[5,4,3,2,1,0]); - $self->_single_set('year', "$lt[5]"); - $self->_single_set('month', "$lt[4]"); - $self->_single_set('day', "$lt[3]"); - $self->_single_set('hour', "$lt[2]"); - $self->_single_set('minute', "$lt[1]"); - $self->_single_set('second', "$lt[0]"); - $self->_single_set('version', $Slic3r::VERSION); -} - -# TODO: or this could be an alias -sub set { - my ($self, $key, $val) = @_; - $self->_single_set($key, $val); -} - sub process { my ($self, $string, $extra) = @_; diff --git a/xs/src/libslic3r/PlaceholderParser.cpp b/xs/src/libslic3r/PlaceholderParser.cpp index c384afa32..31056dd2c 100644 --- a/xs/src/libslic3r/PlaceholderParser.cpp +++ b/xs/src/libslic3r/PlaceholderParser.cpp @@ -1,18 +1,50 @@ #include "PlaceholderParser.hpp" - +#include +#include +#include namespace Slic3r { - PlaceholderParser::PlaceholderParser() { + this->_single["version"] = SLIC3R_VERSION; // TODO: port these methods to C++, then call them here // this->apply_env_variables(); - // this->update_timestamp(); + this->update_timestamp(); } -PlaceholderParser::~PlaceholderParser() +void +PlaceholderParser::update_timestamp() { + time_t rawtime; + time(&rawtime); + struct tm* timeinfo = localtime(&rawtime); + + { + std::ostringstream ss; + ss << (1900 + timeinfo->tm_year); + ss << std::setw(2) << std::setfill('0') << (1 + timeinfo->tm_mon); + ss << std::setw(2) << std::setfill('0') << timeinfo->tm_mday; + ss << "-"; + ss << std::setw(2) << std::setfill('0') << timeinfo->tm_hour; + ss << std::setw(2) << std::setfill('0') << timeinfo->tm_min; + ss << std::setw(2) << std::setfill('0') << timeinfo->tm_sec; + this->_single["timestamp"] = ss.str(); + } + this->_single["year"] = this->_int_to_string(1900 + timeinfo->tm_year); + this->_single["month"] = this->_int_to_string(1 + timeinfo->tm_mon); + this->_single["day"] = this->_int_to_string(timeinfo->tm_mday); + this->_single["hour"] = this->_int_to_string(timeinfo->tm_hour); + this->_single["minute"] = this->_int_to_string(timeinfo->tm_min); + this->_single["second"] = this->_int_to_string(timeinfo->tm_sec); +} + +std::string +PlaceholderParser::_int_to_string(int value) const +{ + std::ostringstream ss; + ss << value; + return ss.str(); } void PlaceholderParser::apply_config(DynamicPrintConfig &config) @@ -85,6 +117,12 @@ void PlaceholderParser::apply_config(DynamicPrintConfig &config) } } +void +PlaceholderParser::set(const std::string &key, const std::string &value) +{ + this->_single[key] = value; +} + std::ostream& operator<<(std::ostream &stm, const Pointf &pointf) { return stm << pointf.x << "," << pointf.y; diff --git a/xs/src/libslic3r/PlaceholderParser.hpp b/xs/src/libslic3r/PlaceholderParser.hpp index e69d6ed93..eb061fab2 100644 --- a/xs/src/libslic3r/PlaceholderParser.hpp +++ b/xs/src/libslic3r/PlaceholderParser.hpp @@ -17,14 +17,15 @@ class PlaceholderParser std::map _multiple; PlaceholderParser(); - ~PlaceholderParser(); - + void update_timestamp(); void apply_config(DynamicPrintConfig &config); + void set(const std::string &key, const std::string &value); private: template void set_multiple_from_vector( const std::string &key, ConfigOptionVector &opt); + std::string _int_to_string(int value) const; }; } diff --git a/xs/src/libslic3r/libslic3r.h b/xs/src/libslic3r/libslic3r.h index 072e97ffb..17a437c3d 100644 --- a/xs/src/libslic3r/libslic3r.h +++ b/xs/src/libslic3r/libslic3r.h @@ -6,6 +6,8 @@ #include #include +#define SLIC3R_VERSION "1.2.2-dev" + #define EPSILON 1e-4 #define SCALING_FACTOR 0.000001 #define PI 3.141592653589793238 diff --git a/xs/xsp/PlaceholderParser.xsp b/xs/xsp/PlaceholderParser.xsp index 4b508f77a..760f3b71f 100644 --- a/xs/xsp/PlaceholderParser.xsp +++ b/xs/xsp/PlaceholderParser.xsp @@ -9,9 +9,11 @@ %name{Slic3r::GCode::PlaceholderParser} class PlaceholderParser { %name{_new} PlaceholderParser(); ~PlaceholderParser(); - + + void update_timestamp(); void apply_config(DynamicPrintConfig *config) - %code%{ THIS->apply_config(*config); %}; + %code%{ THIS->apply_config(*config); %}; + void set(std::string key, std::string value); void _single_set(std::string k, std::string v) %code%{ THIS->_single[k] = v; %}; diff --git a/xs/xsp/XS.xsp b/xs/xsp/XS.xsp index f14af9433..3829994e1 100644 --- a/xs/xsp/XS.xsp +++ b/xs/xsp/XS.xsp @@ -6,3 +6,14 @@ %{ %} + +%package{Slic3r}; +%{ + +SV* +VERSION() + CODE: + RETVAL = newSVpv(SLIC3R_VERSION, 0); + OUTPUT: RETVAL + +%} \ No newline at end of file