diff options
author | FormerLurker <hochgebe@gmail.com> | 2020-10-18 19:33:13 +0300 |
---|---|---|
committer | FormerLurker <hochgebe@gmail.com> | 2020-10-18 19:33:13 +0300 |
commit | 366a254b190e704b8365fe2d1dcdac0a01da623b (patch) | |
tree | 6a87974376a56e2b6a1b9eed5f6959185d6581dc /GcodeProcessorLib | |
parent | 763d3e1b20d0c677bab8c017ed1ba14ed7c9273a (diff) |
Use rename instead of copying a temp source file. Add segment statistics to console output.
Diffstat (limited to 'GcodeProcessorLib')
-rw-r--r-- | GcodeProcessorLib/utilities.cpp | 66 | ||||
-rw-r--r-- | GcodeProcessorLib/utilities.h | 17 |
2 files changed, 83 insertions, 0 deletions
diff --git a/GcodeProcessorLib/utilities.cpp b/GcodeProcessorLib/utilities.cpp index 0af96a9..19e47ab 100644 --- a/GcodeProcessorLib/utilities.cpp +++ b/GcodeProcessorLib/utilities.cpp @@ -28,6 +28,8 @@ // Had to increase the zero tolerance because prusa slicer doesn't always retract enough while wiping. const double ZERO_TOLERANCE = 0.000005; const std::string utilities::WHITESPACE_ = " \n\r\t\f\v"; +const char utilities::GUID_RANGE[] = "0123456789abcdef"; +const bool utilities::GUID_DASHES[] = { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 }; bool utilities::is_zero(double x) { @@ -284,3 +286,67 @@ int utilities::get_num_digits(double x) { return get_num_digits((int) x); } + +// Nice utility function found here: https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path +std::vector<std::string> utilities::splitpath(const std::string& str) +{ + std::vector<std::string> result; + + char const* pch = str.c_str(); + char const* start = pch; + for (; *pch; ++pch) + { + if (*pch == PATH_SEPARATOR_) + { + if (start != pch) + { + std::string str(start, pch); + result.push_back(str); + } + else + { + result.push_back(""); + } + start = pch + 1; + } + } + result.push_back(start); + + return result; +} + +bool utilities::get_file_path(const std::string& file_path, std::string & path) +{ + std::vector<std::string> file_parts = splitpath(file_path); + if (file_parts.size() == 0) + return false; + for (int index = 0; index < file_parts.size() - 1; index++) + { + path += file_parts[index]; + path += PATH_SEPARATOR_; + } + return true; +} + +std::string utilities::create_uuid() { + std::string res; + for (int i = 0; i < 16; i++) { + if (GUID_DASHES[i]) res += "-"; + res += GUID_RANGE[(int)(rand() % 16)]; + res += GUID_RANGE[(int)(rand() % 16)]; + } + return res; +} + +bool utilities::get_temp_file_path_for_file(const std::string& file_path, std::string& temp_file_path) +{ + temp_file_path = ""; + if (!utilities::get_file_path(file_path, temp_file_path)) + { + return false; + } + temp_file_path = temp_file_path; + temp_file_path += utilities::create_uuid(); + temp_file_path += ".tmp"; + return true; +} diff --git a/GcodeProcessorLib/utilities.h b/GcodeProcessorLib/utilities.h index a6ddbd2..8e54df0 100644 --- a/GcodeProcessorLib/utilities.h +++ b/GcodeProcessorLib/utilities.h @@ -22,6 +22,8 @@ #pragma once #include <string> +#include <vector> +#include <set> class utilities{ public: static bool is_zero(double x); @@ -55,8 +57,23 @@ public: static int get_num_digits(int x); static int get_num_digits(double x); + + static std::vector<std::string> splitpath(const std::string& str); + static bool get_file_path(const std::string& file_path, std::string& path); + static bool get_temp_file_path_for_file(const std::string& file_path, std::string& temp_file_path); + static std::string create_uuid(); + + protected: static const std::string WHITESPACE_; + static const char PATH_SEPARATOR_ = +#ifdef _WIN32 + '\\'; +#else + '/'; +#endif + static const char GUID_RANGE[]; + static const bool GUID_DASHES[]; private: utilities(); |