use vsnprintf instead of snprintf in string_printf function

Also, revert to old location: Utils.hpp and utils.cpp
This commit is contained in:
tamasmeszaros 2020-02-03 11:18:33 +01:00
parent f09bed32b6
commit 0c4797e92e
4 changed files with 24 additions and 18 deletions

View File

@ -93,6 +93,8 @@ namespace PerlUtils {
extern std::string path_to_parent_path(const char *src); 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, // Standard "generated by Slic3r version xxx timestamp xxx" header string,
// to be placed at the top of Slic3r generated files. // to be placed at the top of Slic3r generated files.
std::string header_slic3r_generated(); std::string header_slic3r_generated();

View File

@ -230,23 +230,6 @@ static inline bool is_approx(Number value, Number test_value)
return std::fabs(double(value) - double(test_value)) < double(EPSILON); return std::fabs(double(value) - double(test_value)) < double(EPSILON);
} }
template<class...Args>
std::string string_printf(const char *const _fmt, Args &&...args)
{
static const size_t INITIAL_LEN = 1024;
std::vector<char> buffer(INITIAL_LEN, '\0');
auto fmt = std::string("%s") + _fmt;
int bufflen = snprintf(buffer.data(), INITIAL_LEN - 1, fmt.c_str(), "", std::forward<Args>(args)...);
if (bufflen >= int(INITIAL_LEN)) {
buffer.resize(size_t(bufflen) + 1);
snprintf(buffer.data(), buffer.size(), fmt.c_str(), "", std::forward<Args>(args)...);
}
return std::string(buffer.begin(), buffer.begin() + bufflen);
}
} // namespace Slic3r } // namespace Slic3r
#endif #endif

View File

@ -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 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<char> 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() std::string header_slic3r_generated()
{ {
return std::string("generated by " SLIC3R_APP_NAME " " SLIC3R_VERSION " on " ) + Utils::utc_timestamp(); return std::string("generated by " SLIC3R_APP_NAME " " SLIC3R_VERSION " on " ) + Utils::utc_timestamp();

View File

@ -1,6 +1,6 @@
#include <catch_main.hpp> #include <catch_main.hpp>
#include "libslic3r/libslic3r.h" #include "libslic3r/Utils.hpp"
namespace { namespace {