2016-11-24 13:05:06 +00:00
|
|
|
#ifndef slic3r_Utils_hpp_
|
|
|
|
#define slic3r_Utils_hpp_
|
|
|
|
|
2017-10-26 15:17:39 +00:00
|
|
|
#include <locale>
|
2018-11-02 09:31:10 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <functional>
|
2018-11-08 14:19:50 +00:00
|
|
|
#include <type_traits>
|
2019-10-02 11:56:41 +00:00
|
|
|
#include <system_error>
|
2017-10-26 15:17:39 +00:00
|
|
|
|
2018-03-13 11:39:57 +00:00
|
|
|
#include "libslic3r.h"
|
|
|
|
|
2019-02-03 14:30:37 +00:00
|
|
|
namespace boost { namespace filesystem { class directory_entry; }}
|
|
|
|
|
2016-11-24 13:05:06 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
extern void set_logging_level(unsigned int level);
|
2018-12-18 17:40:35 +00:00
|
|
|
extern unsigned get_logging_level();
|
2017-03-03 11:53:05 +00:00
|
|
|
extern void trace(unsigned int level, const char *message);
|
2018-12-18 13:10:31 +00:00
|
|
|
// Format memory allocated, separate thousands by comma.
|
|
|
|
extern std::string format_memsize_MB(size_t n);
|
2018-12-18 10:31:41 +00:00
|
|
|
// Return string to be added to the boost::log output to inform about the current process memory allocation.
|
2019-08-05 17:57:57 +00:00
|
|
|
// The string is non-empty if the loglevel >= info (3) or ignore_loglevel==true.
|
|
|
|
// Latter is used to get the memory info from SysInfoDialog.
|
|
|
|
extern std::string log_memory_info(bool ignore_loglevel = false);
|
2018-09-17 13:12:13 +00:00
|
|
|
extern void disable_multi_threading();
|
2019-07-17 13:48:53 +00:00
|
|
|
// Returns the size of physical memory (RAM) in bytes.
|
|
|
|
extern size_t total_physical_memory();
|
2016-11-24 13:05:06 +00:00
|
|
|
|
2017-10-17 18:00:15 +00:00
|
|
|
// Set a path with GUI resource files.
|
|
|
|
void set_var_dir(const std::string &path);
|
2017-10-25 10:53:31 +00:00
|
|
|
// Return a full path to the GUI resource files.
|
2017-10-17 18:00:15 +00:00
|
|
|
const std::string& var_dir();
|
2017-10-25 10:53:31 +00:00
|
|
|
// Return a full resource path for a file_name.
|
2017-10-17 18:00:15 +00:00
|
|
|
std::string var(const std::string &file_name);
|
|
|
|
|
2017-12-10 12:19:44 +00:00
|
|
|
// Set a path with various static definition data (for example the initial config bundles).
|
|
|
|
void set_resources_dir(const std::string &path);
|
|
|
|
// Return a full path to the resources directory.
|
|
|
|
const std::string& resources_dir();
|
2018-02-12 07:57:32 +00:00
|
|
|
|
|
|
|
// Set a path with GUI localization files.
|
|
|
|
void set_local_dir(const std::string &path);
|
2018-02-07 16:13:52 +00:00
|
|
|
// Return a full path to the localization directory.
|
2018-02-12 07:57:32 +00:00
|
|
|
const std::string& localization_dir();
|
2017-12-10 12:19:44 +00:00
|
|
|
|
2017-10-25 10:53:31 +00:00
|
|
|
// Set a path with preset files.
|
|
|
|
void set_data_dir(const std::string &path);
|
|
|
|
// Return a full path to the GUI resource files.
|
|
|
|
const std::string& data_dir();
|
|
|
|
|
2017-12-21 15:56:33 +00:00
|
|
|
// A special type for strings encoded in the local Windows 8-bit code page.
|
|
|
|
// This type is only needed for Perl bindings to relay to Perl that the string is raw, not UTF-8 encoded.
|
|
|
|
typedef std::string local_encoded_string;
|
|
|
|
|
|
|
|
// Convert an UTF-8 encoded string into local coding.
|
|
|
|
// On Windows, the UTF-8 string is converted to a local 8-bit code page.
|
|
|
|
// On OSX and Linux, this function does no conversion and returns a copy of the source string.
|
|
|
|
extern local_encoded_string encode_path(const char *src);
|
2017-08-03 15:31:31 +00:00
|
|
|
extern std::string decode_path(const char *src);
|
|
|
|
extern std::string normalize_utf8_nfc(const char *src);
|
|
|
|
|
2018-09-14 07:28:00 +00:00
|
|
|
// Safely rename a file even if the target exists.
|
|
|
|
// On Windows, the file explorer (or anti-virus or whatever else) often locks the file
|
|
|
|
// for a short while, so the file may not be movable. Retry while we see recoverable errors.
|
2019-08-20 14:19:30 +00:00
|
|
|
extern std::error_code rename_file(const std::string &from, const std::string &to);
|
2018-09-14 07:28:00 +00:00
|
|
|
|
2020-02-27 09:44:25 +00:00
|
|
|
enum CopyFileResult {
|
|
|
|
SUCCESS = 0,
|
|
|
|
FAIL_COPY_FILE,
|
|
|
|
FAIL_FILES_DIFFERENT,
|
|
|
|
FAIL_RENAMING,
|
|
|
|
FAIL_CHECK_ORIGIN_NOT_OPENED,
|
|
|
|
FAIL_CHECK_TARGET_NOT_OPENED
|
|
|
|
};
|
2018-09-14 07:28:00 +00:00
|
|
|
// Copy a file, adjust the access attributes, so that the target is writable.
|
2020-02-27 09:44:25 +00:00
|
|
|
CopyFileResult copy_file_inner(const std::string &from, const std::string &to);
|
2019-12-22 10:11:48 +00:00
|
|
|
// Copy file to a temp file first, then rename it to the final file name.
|
|
|
|
// If with_check is true, then the content of the copied file is compared to the content
|
|
|
|
// of the source file before renaming.
|
2020-02-27 09:44:25 +00:00
|
|
|
extern CopyFileResult copy_file(const std::string &from, const std::string &to, const bool with_check = false);
|
2018-09-14 07:28:00 +00:00
|
|
|
|
2020-02-27 09:44:25 +00:00
|
|
|
// Compares two files if identical.
|
|
|
|
extern CopyFileResult check_copy(const std::string& origin, const std::string& copy);
|
2018-09-14 07:28:00 +00:00
|
|
|
|
2019-02-03 14:30:37 +00:00
|
|
|
// Ignore system and hidden files, which may be created by the DropBox synchronisation process.
|
2019-05-14 17:46:01 +00:00
|
|
|
// https://github.com/prusa3d/PrusaSlicer/issues/1298
|
2019-02-03 14:30:37 +00:00
|
|
|
extern bool is_plain_file(const boost::filesystem::directory_entry &path);
|
|
|
|
extern bool is_ini_file(const boost::filesystem::directory_entry &path);
|
|
|
|
extern bool is_idx_file(const boost::filesystem::directory_entry &path);
|
|
|
|
|
2017-12-21 15:56:33 +00:00
|
|
|
// File path / name / extension splitting utilities, working with UTF-8,
|
|
|
|
// to be published to Perl.
|
|
|
|
namespace PerlUtils {
|
|
|
|
// Get a file name including the extension.
|
|
|
|
extern std::string path_to_filename(const char *src);
|
|
|
|
// Get a file name without the extension.
|
|
|
|
extern std::string path_to_stem(const char *src);
|
|
|
|
// Get just the extension.
|
|
|
|
extern std::string path_to_extension(const char *src);
|
|
|
|
// Get a directory without the trailing slash.
|
|
|
|
extern std::string path_to_parent_path(const char *src);
|
|
|
|
};
|
|
|
|
|
2020-02-03 10:18:33 +00:00
|
|
|
std::string string_printf(const char *format, ...);
|
|
|
|
|
2017-10-30 17:15:41 +00:00
|
|
|
// Standard "generated by Slic3r version xxx timestamp xxx" header string,
|
|
|
|
// to be placed at the top of Slic3r generated files.
|
2019-09-11 10:13:59 +00:00
|
|
|
std::string header_slic3r_generated();
|
2017-10-30 17:15:41 +00:00
|
|
|
|
2018-04-20 09:05:00 +00:00
|
|
|
// getpid platform wrapper
|
|
|
|
extern unsigned get_current_pid();
|
|
|
|
|
2018-09-21 08:18:56 +00:00
|
|
|
template <typename Real>
|
|
|
|
Real round_nearest(Real value, unsigned int decimals)
|
|
|
|
{
|
|
|
|
Real res = (Real)0;
|
|
|
|
if (decimals == 0)
|
|
|
|
res = ::round(value);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Real power = ::pow((Real)10, (int)decimals);
|
|
|
|
res = ::round(value * power + (Real)0.5) / power;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-28 15:09:57 +00:00
|
|
|
// Compute the next highest power of 2 of 32-bit v
|
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html
|
2018-09-25 09:53:05 +00:00
|
|
|
inline uint16_t next_highest_power_of_2(uint16_t v)
|
2017-03-28 15:09:57 +00:00
|
|
|
{
|
2018-09-25 09:53:05 +00:00
|
|
|
if (v != 0)
|
|
|
|
-- v;
|
2017-03-28 15:09:57 +00:00
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
2018-09-25 09:53:05 +00:00
|
|
|
v |= v >> 8;
|
|
|
|
return ++ v;
|
|
|
|
}
|
|
|
|
inline uint32_t next_highest_power_of_2(uint32_t v)
|
|
|
|
{
|
|
|
|
if (v != 0)
|
|
|
|
-- v;
|
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
|
|
|
return ++ v;
|
|
|
|
}
|
|
|
|
inline uint64_t next_highest_power_of_2(uint64_t v)
|
|
|
|
{
|
|
|
|
if (v != 0)
|
|
|
|
-- v;
|
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
|
|
|
v |= v >> 32;
|
2017-03-28 15:09:57 +00:00
|
|
|
return ++ v;
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:19:50 +00:00
|
|
|
// On some implementations (such as some versions of clang), the size_t is a type of its own, so we need to overload for size_t.
|
|
|
|
// Typically, though, the size_t type aliases to uint64_t / uint32_t.
|
|
|
|
// We distinguish that here and provide implementation for size_t if and only if it is a distinct type
|
|
|
|
template<class T> size_t next_highest_power_of_2(T v,
|
|
|
|
typename std::enable_if<std::is_same<T, size_t>::value, T>::type = 0, // T is size_t
|
|
|
|
typename std::enable_if<!std::is_same<T, uint64_t>::value, T>::type = 0, // T is not uint64_t
|
|
|
|
typename std::enable_if<!std::is_same<T, uint32_t>::value, T>::type = 0, // T is not uint32_t
|
|
|
|
typename std::enable_if<sizeof(T) == 8, T>::type = 0) // T is 64 bits
|
|
|
|
{
|
2018-09-25 10:43:53 +00:00
|
|
|
return next_highest_power_of_2(uint64_t(v));
|
2018-11-08 14:19:50 +00:00
|
|
|
}
|
|
|
|
template<class T> size_t next_highest_power_of_2(T v,
|
|
|
|
typename std::enable_if<std::is_same<T, size_t>::value, T>::type = 0, // T is size_t
|
|
|
|
typename std::enable_if<!std::is_same<T, uint64_t>::value, T>::type = 0, // T is not uint64_t
|
|
|
|
typename std::enable_if<!std::is_same<T, uint32_t>::value, T>::type = 0, // T is not uint32_t
|
|
|
|
typename std::enable_if<sizeof(T) == 4, T>::type = 0) // T is 32 bits
|
|
|
|
{
|
2018-09-25 11:19:50 +00:00
|
|
|
return next_highest_power_of_2(uint32_t(v));
|
2018-09-25 10:43:53 +00:00
|
|
|
}
|
2018-11-08 14:19:50 +00:00
|
|
|
|
2019-11-15 15:20:01 +00:00
|
|
|
template<typename INDEX_TYPE>
|
|
|
|
inline INDEX_TYPE prev_idx_modulo(INDEX_TYPE idx, const INDEX_TYPE count)
|
|
|
|
{
|
|
|
|
if (idx == 0)
|
|
|
|
idx = count;
|
|
|
|
return -- idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename INDEX_TYPE>
|
|
|
|
inline INDEX_TYPE next_idx_modulo(INDEX_TYPE idx, const INDEX_TYPE count)
|
|
|
|
{
|
|
|
|
if (++ idx == count)
|
|
|
|
idx = 0;
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CONTAINER_TYPE>
|
|
|
|
inline typename CONTAINER_TYPE::size_type prev_idx_modulo(typename CONTAINER_TYPE::size_type idx, const CONTAINER_TYPE &container)
|
|
|
|
{
|
|
|
|
return prev_idx_modulo(idx, container.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CONTAINER_TYPE>
|
|
|
|
inline typename CONTAINER_TYPE::size_type next_idx_modulo(typename CONTAINER_TYPE::size_type idx, const CONTAINER_TYPE &container)
|
|
|
|
{
|
|
|
|
return next_idx_modulo(idx, container.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CONTAINER_TYPE>
|
2019-11-16 07:22:39 +00:00
|
|
|
inline const typename CONTAINER_TYPE::value_type& prev_value_modulo(typename CONTAINER_TYPE::size_type idx, const CONTAINER_TYPE &container)
|
2019-11-15 15:20:01 +00:00
|
|
|
{
|
|
|
|
return container[prev_idx_modulo(idx, container.size())];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CONTAINER_TYPE>
|
|
|
|
inline typename CONTAINER_TYPE::value_type& prev_value_modulo(typename CONTAINER_TYPE::size_type idx, CONTAINER_TYPE &container)
|
|
|
|
{
|
|
|
|
return container[prev_idx_modulo(idx, container.size())];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CONTAINER_TYPE>
|
2019-11-16 07:22:39 +00:00
|
|
|
inline const typename CONTAINER_TYPE::value_type& next_value_modulo(typename CONTAINER_TYPE::size_type idx, const CONTAINER_TYPE &container)
|
2019-11-15 15:20:01 +00:00
|
|
|
{
|
|
|
|
return container[next_idx_modulo(idx, container.size())];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CONTAINER_TYPE>
|
|
|
|
inline typename CONTAINER_TYPE::value_type& next_value_modulo(typename CONTAINER_TYPE::size_type idx, CONTAINER_TYPE &container)
|
|
|
|
{
|
|
|
|
return container[next_idx_modulo(idx, container.size())];
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:39:50 +00:00
|
|
|
extern std::string xml_escape(std::string text);
|
|
|
|
|
2018-11-02 09:31:10 +00:00
|
|
|
|
2019-06-25 15:25:24 +00:00
|
|
|
#if defined __GNUC__ && __GNUC__ < 5 && !defined __clang__
|
2019-01-22 10:12:50 +00:00
|
|
|
// Older GCCs don't have std::is_trivially_copyable
|
|
|
|
// cf. https://gcc.gnu.org/onlinedocs/gcc-4.9.4/libstdc++/manual/manual/status.html#status.iso.2011
|
2019-07-23 09:22:54 +00:00
|
|
|
// #warning "GCC version < 5, faking std::is_trivially_copyable"
|
2019-01-22 10:12:50 +00:00
|
|
|
template<typename T> struct IsTriviallyCopyable { static constexpr bool value = true; };
|
|
|
|
#else
|
|
|
|
template<typename T> struct IsTriviallyCopyable : public std::is_trivially_copyable<T> {};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-11-02 09:31:10 +00:00
|
|
|
class ScopeGuard
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef std::function<void()> Closure;
|
|
|
|
private:
|
2019-07-05 09:42:36 +00:00
|
|
|
// bool committed;
|
2018-11-02 09:31:10 +00:00
|
|
|
Closure closure;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopeGuard() {}
|
|
|
|
ScopeGuard(Closure closure) : closure(std::move(closure)) {}
|
|
|
|
ScopeGuard(const ScopeGuard&) = delete;
|
|
|
|
ScopeGuard(ScopeGuard &&other) : closure(std::move(other.closure)) {}
|
|
|
|
|
|
|
|
~ScopeGuard()
|
|
|
|
{
|
|
|
|
if (closure) { closure(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopeGuard& operator=(const ScopeGuard&) = delete;
|
|
|
|
ScopeGuard& operator=(ScopeGuard &&other)
|
|
|
|
{
|
|
|
|
closure = std::move(other.closure);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() { closure = Closure(); }
|
|
|
|
};
|
|
|
|
|
2019-02-12 15:34:42 +00:00
|
|
|
// Shorten the dhms time by removing the seconds, rounding the dhm to full minutes
|
|
|
|
// and removing spaces.
|
2019-03-28 11:37:10 +00:00
|
|
|
inline std::string short_time(const std::string &time)
|
2019-02-12 15:34:42 +00:00
|
|
|
{
|
|
|
|
// Parse the dhms time format.
|
|
|
|
int days = 0;
|
|
|
|
int hours = 0;
|
|
|
|
int minutes = 0;
|
|
|
|
int seconds = 0;
|
|
|
|
if (time.find('d') != std::string::npos)
|
|
|
|
::sscanf(time.c_str(), "%dd %dh %dm %ds", &days, &hours, &minutes, &seconds);
|
|
|
|
else if (time.find('h') != std::string::npos)
|
|
|
|
::sscanf(time.c_str(), "%dh %dm %ds", &hours, &minutes, &seconds);
|
|
|
|
else if (time.find('m') != std::string::npos)
|
|
|
|
::sscanf(time.c_str(), "%dm %ds", &minutes, &seconds);
|
|
|
|
else if (time.find('s') != std::string::npos)
|
|
|
|
::sscanf(time.c_str(), "%ds", &seconds);
|
|
|
|
// Round to full minutes.
|
|
|
|
if (days + hours + minutes > 0 && seconds >= 30) {
|
|
|
|
if (++minutes == 60) {
|
|
|
|
minutes = 0;
|
|
|
|
if (++hours == 24) {
|
|
|
|
hours = 0;
|
|
|
|
++days;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Format the dhm time.
|
|
|
|
char buffer[64];
|
|
|
|
if (days > 0)
|
|
|
|
::sprintf(buffer, "%dd%dh%dm", days, hours, minutes);
|
|
|
|
else if (hours > 0)
|
|
|
|
::sprintf(buffer, "%dh%dm", hours, minutes);
|
|
|
|
else if (minutes > 0)
|
|
|
|
::sprintf(buffer, "%dm", minutes);
|
|
|
|
else
|
|
|
|
::sprintf(buffer, "%ds", seconds);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the given time is seconds in format DDd HHh MMm SSs
|
2019-03-28 11:37:10 +00:00
|
|
|
inline std::string get_time_dhms(float time_in_secs)
|
2019-02-12 15:34:42 +00:00
|
|
|
{
|
|
|
|
int days = (int)(time_in_secs / 86400.0f);
|
|
|
|
time_in_secs -= (float)days * 86400.0f;
|
|
|
|
int hours = (int)(time_in_secs / 3600.0f);
|
|
|
|
time_in_secs -= (float)hours * 3600.0f;
|
|
|
|
int minutes = (int)(time_in_secs / 60.0f);
|
|
|
|
time_in_secs -= (float)minutes * 60.0f;
|
|
|
|
|
|
|
|
char buffer[64];
|
|
|
|
if (days > 0)
|
|
|
|
::sprintf(buffer, "%dd %dh %dm %ds", days, hours, minutes, (int)time_in_secs);
|
|
|
|
else if (hours > 0)
|
|
|
|
::sprintf(buffer, "%dh %dm %ds", hours, minutes, (int)time_in_secs);
|
|
|
|
else if (minutes > 0)
|
|
|
|
::sprintf(buffer, "%dm %ds", minutes, (int)time_in_secs);
|
|
|
|
else
|
|
|
|
::sprintf(buffer, "%ds", (int)time_in_secs);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:05:06 +00:00
|
|
|
} // namespace Slic3r
|
|
|
|
|
2018-12-18 14:55:45 +00:00
|
|
|
#if WIN32
|
|
|
|
#define SLIC3R_STDVEC_MEMSIZE(NAME, TYPE) NAME.capacity() * ((sizeof(TYPE) + __alignof(TYPE) - 1) / __alignof(TYPE)) * __alignof(TYPE)
|
|
|
|
#else
|
|
|
|
#define SLIC3R_STDVEC_MEMSIZE(NAME, TYPE) NAME.capacity() * ((sizeof(TYPE) + alignof(TYPE) - 1) / alignof(TYPE)) * alignof(TYPE)
|
|
|
|
#endif
|
|
|
|
|
2016-11-24 13:05:06 +00:00
|
|
|
#endif // slic3r_Utils_hpp_
|