Files
OrcaSlicer-bambulab/src/libslic3r/Utils.hpp
T

148 lines
4.7 KiB
C++
Raw Normal View History

2016-11-24 14:05:06 +01:00
#ifndef slic3r_Utils_hpp_
#define slic3r_Utils_hpp_
2017-10-26 17:17:39 +02:00
#include <locale>
2018-03-13 12:39:57 +01:00
#include "libslic3r.h"
2016-11-24 14:05:06 +01:00
namespace Slic3r {
extern void set_logging_level(unsigned int level);
2017-03-03 12:53:05 +01:00
extern void trace(unsigned int level, const char *message);
2018-09-17 15:12:13 +02:00
extern void disable_multi_threading();
2016-11-24 14:05:06 +01:00
// Set a path with GUI resource files.
void set_var_dir(const std::string &path);
2017-10-25 12:53:31 +02:00
// Return a full path to the GUI resource files.
const std::string& var_dir();
2017-10-25 12:53:31 +02:00
// Return a full resource path for a file_name.
std::string var(const std::string &file_name);
// 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 08:57:32 +01:00
// Set a path with GUI localization files.
void set_local_dir(const std::string &path);
2018-02-07 17:13:52 +01:00
// Return a full path to the localization directory.
2018-02-12 08:57:32 +01:00
const std::string& localization_dir();
2017-10-25 12:53:31 +02: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();
// 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 17:31:31 +02:00
extern std::string decode_path(const char *src);
extern std::string normalize_utf8_nfc(const char *src);
2018-09-14 09:28:00 +02: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.
extern int rename_file(const std::string &from, const std::string &to);
// Copy a file, adjust the access attributes, so that the target is writable.
extern int copy_file(const std::string &from, const std::string &to);
// 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);
};
// Timestamp formatted for header_slic3r_generated().
extern std::string timestamp_str();
// Standard "generated by Slic3r version xxx timestamp xxx" header string,
// to be placed at the top of Slic3r generated files.
inline std::string header_slic3r_generated() { return std::string("generated by " SLIC3R_FORK_NAME " " SLIC3R_VERSION " " ) + timestamp_str(); }
// getpid platform wrapper
extern unsigned get_current_pid();
2018-09-21 10:18:56 +02: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;
}
// Compute the next highest power of 2 of 32-bit v
// http://graphics.stanford.edu/~seander/bithacks.html
inline uint16_t next_highest_power_of_2(uint16_t v)
{
if (v != 0)
-- v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
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;
return ++ v;
}
2018-09-25 12:48:36 +02:00
#ifdef __clang__
// On clang, the size_t is a type of its own, so we need to overload for size_t.
// On MSC, the size_t type aliases to uint64_t / uint32_t, so the following code
// gives a duplicate symbol error.
2018-09-25 12:43:53 +02:00
inline size_t next_highest_power_of_2(size_t v)
{
#if SSIZE_MAX == 9223372036854775807
static_assert(sizeof(size_t) == sizeof(uint64_t), "sizeof(size_t) == sizeof(uint64_t)");
2018-09-25 12:43:53 +02:00
return next_highest_power_of_2(uint64_t(v));
#else
static_assert(sizeof(size_t) == sizeof(uint32_t), "sizeof(size_t) == sizeof(uint32_t)");
return next_highest_power_of_2(uint32_t(v));
2018-09-25 12:43:53 +02:00
#endif
}
#endif
extern std::string xml_escape(std::string text);
2016-11-24 14:05:06 +01:00
} // namespace Slic3r
#endif // slic3r_Utils_hpp_