diff options
-rw-r--r-- | ArcWelder/arc_welder.cpp | 6 | ||||
-rw-r--r-- | ArcWelder/arc_welder.h | 7 | ||||
-rw-r--r-- | ArcWelderConsole/ArcWelderConsole.cpp | 48 | ||||
-rw-r--r-- | ArcWelderTest/ArcWelderTest.cpp | 4 | ||||
-rw-r--r-- | ArcWelderTest/ArcWelderTest.h | 2 | ||||
-rw-r--r-- | GcodeProcessorLib/utilities.cpp | 66 | ||||
-rw-r--r-- | GcodeProcessorLib/utilities.h | 17 | ||||
-rw-r--r-- | PyArcWelder/py_arc_welder.cpp | 2 | ||||
-rw-r--r-- | PyArcWelder/py_arc_welder.h | 2 |
9 files changed, 128 insertions, 26 deletions
diff --git a/ArcWelder/arc_welder.cpp b/ArcWelder/arc_welder.cpp index c283e26..01de169 100644 --- a/ArcWelder/arc_welder.cpp +++ b/ArcWelder/arc_welder.cpp @@ -162,10 +162,10 @@ arc_welder_results results; std::stringstream stream; stream << std::fixed << std::setprecision(5); - stream << "py_gcode_arc_converter.ConvertFile - Parameters received: source_file_path: '" << + stream << "arc_welder::process - Parameters received: source_file_path: '" << source_path_ << "', target_file_path:'" << target_path_ << "', resolution_mm:" << resolution_mm_ << "mm (+-" << current_arc_.get_resolution_mm() << "mm), max_radius_mm:" << current_arc_.get_max_radius() - << "mm, g90_91_influences_extruder: " << (p_source_position_->get_g90_91_influences_extruder() ? "True" : "False") << "\n"; + << "mm, g90_91_influences_extruder: " << (p_source_position_->get_g90_91_influences_extruder() ? "True" : "False"); p_logger_->log(logger_type_, INFO, stream.str()); @@ -295,7 +295,7 @@ bool arc_welder::on_progress_(const arc_welder_progress& progress) { if (progress_callback_ != NULL) { - return progress_callback_(progress); + return progress_callback_(progress, p_logger_, logger_type_); } else if (info_logging_enabled_) { diff --git a/ArcWelder/arc_welder.h b/ArcWelder/arc_welder.h index d21c2c8..a6fbc38 100644 --- a/ArcWelder/arc_welder.h +++ b/ArcWelder/arc_welder.h @@ -62,8 +62,6 @@ struct segment_statistic { }; struct source_target_segment_statistics { - - source_target_segment_statistics(const double segment_tracking_lengths[], const int num_lengths, logger* p_logger = NULL) { total_length_source = 0; total_length_target = 0; @@ -301,7 +299,7 @@ struct source_target_segment_statistics { // Add the total percent change row std::string total_percent_change_string = utilities::get_percent_change_string(total_count_source, total_count_target, 1); output_stream << std::setw(totals_row_label_size) << std::right << "Total percent change:"; - output_stream << std::setw(table_width - totals_row_label_size) << std::setfill('.') << std::right << total_percent_change_string << "\n" << std::setfill(' '); + output_stream << std::setw(table_width - totals_row_label_size) << std::setfill('.') << std::right << total_percent_change_string << std::setfill(' '); std::string output_string = output_stream.str(); return output_string; } @@ -361,8 +359,9 @@ struct arc_welder_progress { return stream.str(); } }; + // define the progress callback type -typedef bool(*progress_callback)(arc_welder_progress); +typedef bool(*progress_callback)(arc_welder_progress, logger* p_logger, int logger_type); struct arc_welder_results { arc_welder_results() : progress() diff --git a/ArcWelderConsole/ArcWelderConsole.cpp b/ArcWelderConsole/ArcWelderConsole.cpp index 29187e6..a0e2567 100644 --- a/ArcWelderConsole/ArcWelderConsole.cpp +++ b/ArcWelderConsole/ArcWelderConsole.cpp @@ -152,8 +152,17 @@ int main(int argc, char* argv[]) if (source_file_path == target_file_path) { overwrite_source_file = true; - target_file_path = std::tmpnam(NULL); - log_messages << "Source and target path are the same. The source file will be overwritten. Temporary file path: " << target_file_path << std::endl; + if (!utilities::get_temp_file_path_for_file(source_file_path, target_file_path)); + { + log_messages << "The source and target path are the same, but a temporary file path could not be created. Is the path empty?"; + p_logger->log(0, INFO, log_messages.str()); + log_messages.clear(); + log_messages.str(""); + } + + // create a uuid with a tmp extension for the temporary file + + log_messages << "Source and target path are the same. The source file will be overwritten. Temporary file path: " << target_file_path; p_logger->log(0, INFO, log_messages.str()); log_messages.clear(); log_messages.str(""); @@ -165,12 +174,12 @@ int main(int argc, char* argv[]) log_messages << "\tMaximum Arc Radius in MM : " << max_radius_mm << "\n"; log_messages << "\tG90/G91 Influences Extruder : " << (g90_g91_influences_extruder ? "True" : "False") << "\n"; log_messages << "\tLog Level : " << log_level_string << "\n"; - log_messages << "\tHide Progress Updates : " << (hide_progress ? "True" : "False") << "\n"; + log_messages << "\tHide Progress Updates : " << (hide_progress ? "True" : "False"); p_logger->log(0, INFO, log_messages.str()); arc_welder* p_arc_welder = NULL; if (!hide_progress) - p_arc_welder = new arc_welder(source_file_path, target_file_path, p_logger, resolution_mm, max_radius_mm, g90_g91_influences_extruder, 50, on_progress); + p_arc_welder = new arc_welder(source_file_path, target_file_path, p_logger, resolution_mm, max_radius_mm, g90_g91_influences_extruder, 50); else p_arc_welder = new arc_welder(source_file_path, target_file_path, p_logger, resolution_mm, max_radius_mm, g90_g91_influences_extruder, 50); @@ -185,21 +194,32 @@ int main(int argc, char* argv[]) { log_messages.clear(); log_messages.str(""); - log_messages << "Overwriting source file at '" << source_file_path << "'."; + log_messages << "Deleting the source file at '" << source_file_path << "'."; p_logger->log(0, INFO, log_messages.str()); log_messages.clear(); log_messages.str(""); - std::ifstream src(target_file_path, std::ios::binary); - std::ofstream dst(source_file_path, std::ios::binary); - dst << src.rdbuf(); - src.close(); - dst.close(); - log_messages << "Deleting temporary file at '" << target_file_path << "'."; + std::remove(source_file_path.c_str()); + log_messages << "Renaming temporary file at '" << target_file_path << "' to '" << source_file_path <<"'."; p_logger->log(0, INFO, log_messages.str()); - std::remove(target_file_path.c_str()); - + std::rename(target_file_path.c_str(), source_file_path.c_str()); } - } + log_messages.clear(); + log_messages.str(""); + log_messages << std::endl << results.progress.segment_statistics.str(); + p_logger->log(0, INFO, log_messages.str() ); + + log_messages.clear(); + log_messages.str(""); + log_messages << "Arc Welder process completed successfully."; + p_logger->log(0, INFO, log_messages.str()); + } + else + { + log_messages.clear(); + log_messages.str(""); + log_messages << "File processing failed."; + p_logger->log(0, INFO, log_messages.str()); + } delete p_arc_welder; return 0; diff --git a/ArcWelderTest/ArcWelderTest.cpp b/ArcWelderTest/ArcWelderTest.cpp index b330d98..842d017 100644 --- a/ArcWelderTest/ArcWelderTest.cpp +++ b/ArcWelderTest/ArcWelderTest.cpp @@ -269,9 +269,9 @@ static void TestAntiStutter(std::string filePath) delete p_logger; } -static bool on_progress(arc_welder_progress progress) +static bool on_progress(arc_welder_progress progress, logger * p_logger, int logger_type) { - std::cout << progress.str() << "\r\n"; + p_logger->log(logger_type, INFO, progress.str()); return true; } diff --git a/ArcWelderTest/ArcWelderTest.h b/ArcWelderTest/ArcWelderTest.h index 4ce0ccd..e7cb187 100644 --- a/ArcWelderTest/ArcWelderTest.h +++ b/ArcWelderTest/ArcWelderTest.h @@ -44,7 +44,7 @@ static gcode_position_args get_single_extruder_position_args(); static gcode_position_args get_5_shared_extruder_position_args(); static gcode_position_args get_5_extruder_position_args(); static void TestAntiStutter(std::string filePath); -static bool on_progress(arc_welder_progress progress); +static bool on_progress(arc_welder_progress progress, logger* p_logger, int logger_type); static void TestDoubleToString(); static void TestParsingCase(); 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(); diff --git a/PyArcWelder/py_arc_welder.cpp b/PyArcWelder/py_arc_welder.cpp index 0eacf39..6b47c5f 100644 --- a/PyArcWelder/py_arc_welder.cpp +++ b/PyArcWelder/py_arc_welder.cpp @@ -74,7 +74,7 @@ PyObject* py_arc_welder::build_py_progress(const arc_welder_progress& progress) return py_progress; } -bool py_arc_welder::on_progress_(const arc_welder_progress& progress) +bool py_arc_welder::on_progress_(const arc_welder_progress& progress, logger* p_logger, int logger_type) { PyObject* py_dict = py_arc_welder::build_py_progress(progress); if (py_dict == NULL) diff --git a/PyArcWelder/py_arc_welder.h b/PyArcWelder/py_arc_welder.h index 1b70980..8c4ce72 100644 --- a/PyArcWelder/py_arc_welder.h +++ b/PyArcWelder/py_arc_welder.h @@ -43,7 +43,7 @@ public: } static PyObject* build_py_progress(const arc_welder_progress& progress); protected: - virtual bool on_progress_(const arc_welder_progress& progress); + virtual bool on_progress_(const arc_welder_progress& progress, logger* p_logger, int logger_type); private: PyObject* py_progress_callback_; }; |