diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp index bc6aa20fc..06c435809 100644 --- a/src/libslic3r/Utils.hpp +++ b/src/libslic3r/Utils.hpp @@ -93,6 +93,8 @@ namespace PerlUtils { extern std::string path_to_parent_path(const char *src); }; +std::string string_printf(const char *format, ...); + // Standard "generated by Slic3r version xxx timestamp xxx" header string, // to be placed at the top of Slic3r generated files. std::string header_slic3r_generated(); diff --git a/src/libslic3r/libslic3r.h b/src/libslic3r/libslic3r.h index 7d9558f1e..d3e4992ce 100644 --- a/src/libslic3r/libslic3r.h +++ b/src/libslic3r/libslic3r.h @@ -230,23 +230,6 @@ static inline bool is_approx(Number value, Number test_value) return std::fabs(double(value) - double(test_value)) < double(EPSILON); } -template -std::string string_printf(const char *const _fmt, Args &&...args) -{ - static const size_t INITIAL_LEN = 1024; - std::vector buffer(INITIAL_LEN, '\0'); - - auto fmt = std::string("%s") + _fmt; - int bufflen = snprintf(buffer.data(), INITIAL_LEN - 1, fmt.c_str(), "", std::forward(args)...); - - if (bufflen >= int(INITIAL_LEN)) { - buffer.resize(size_t(bufflen) + 1); - snprintf(buffer.data(), buffer.size(), fmt.c_str(), "", std::forward(args)...); - } - - return std::string(buffer.begin(), buffer.begin() + bufflen); -} - } // namespace Slic3r #endif diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index 9f0afa061..f78a2b54d 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -577,6 +577,27 @@ namespace PerlUtils { std::string path_to_parent_path(const char *src) { return boost::filesystem::path(src).parent_path().string(); } }; + +std::string string_printf(const char *format, ...) +{ + va_list args1; + va_start(args1, format); + va_list args2; + va_copy(args2, args1); + + static const size_t INITIAL_LEN = 1024; + std::vector buffer(INITIAL_LEN, '\0'); + + int bufflen = ::vsnprintf(buffer.data(), INITIAL_LEN - 1, format, args1); + + if (bufflen >= int(INITIAL_LEN)) { + buffer.resize(size_t(bufflen) + 1); + ::vsnprintf(buffer.data(), buffer.size(), format, args2); + } + + return std::string(buffer.begin(), buffer.begin() + bufflen); +} + std::string header_slic3r_generated() { return std::string("generated by " SLIC3R_APP_NAME " " SLIC3R_VERSION " on " ) + Utils::utc_timestamp(); diff --git a/tests/libslic3r/libslic3r_tests.cpp b/tests/libslic3r/libslic3r_tests.cpp index b9c615d90..f4dcab42a 100644 --- a/tests/libslic3r/libslic3r_tests.cpp +++ b/tests/libslic3r/libslic3r_tests.cpp @@ -1,6 +1,6 @@ #include -#include "libslic3r/libslic3r.h" +#include "libslic3r/Utils.hpp" namespace {