diff options
Diffstat (limited to 'src/libslic3r/Utils.hpp')
-rw-r--r-- | src/libslic3r/Utils.hpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp new file mode 100644 index 000000000..c90ad7650 --- /dev/null +++ b/src/libslic3r/Utils.hpp @@ -0,0 +1,100 @@ +#ifndef slic3r_Utils_hpp_ +#define slic3r_Utils_hpp_ + +#include <locale> + +#include "libslic3r.h" + +namespace Slic3r { + +extern void set_logging_level(unsigned int level); +extern void trace(unsigned int level, const char *message); +extern void disable_multi_threading(); + +// Set a path with GUI resource files. +void set_var_dir(const std::string &path); +// Return a full path to the GUI resource files. +const std::string& var_dir(); +// 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(); + +// Set a path with GUI localization files. +void set_local_dir(const std::string &path); +// Return a full path to the localization directory. +const std::string& localization_dir(); + +// 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); +extern std::string decode_path(const char *src); +extern std::string normalize_utf8_nfc(const char *src); + +// 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(); + +// Compute the next highest power of 2 of 32-bit v +// http://graphics.stanford.edu/~seander/bithacks.html +template<typename T> +inline T next_highest_power_of_2(T v) +{ + if (v != 0) + -- v; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + if (sizeof(T) >= sizeof(uint16_t)) + v |= v >> 8; + if (sizeof(T) >= sizeof(uint32_t)) + v |= v >> 16; + if (sizeof(T) >= sizeof(uint64_t)) + v |= v >> 32; + return ++ v; +} + +extern std::string xml_escape(std::string text); + +} // namespace Slic3r + +#endif // slic3r_Utils_hpp_ |