Fixed program checking and executing report

This commit is contained in:
2023-04-28 18:42:18 +03:00
parent 0b8f5a6950
commit b361b6b2fe
7 changed files with 159 additions and 48 deletions
+19 -7
View File
@@ -1,4 +1,5 @@
#include "grbl.h"
#include "string_utils.h"
#include <utility>
#include <fstream>
@@ -31,13 +32,15 @@ grbl::instruction grbl::instruction::new_comment(size_t line, std::string commen
}
grbl::program::program(std::string filename) {
load(std::move(filename));
load_from_file(std::move(filename));
}
static auto comment_re = std::regex(R"(\(([^)]*)\))");
static auto gcode_re = std::regex(R"(([a-zA-Z0-9\s.]+).*(\(([^)]*)\))?)");
bool grbl::program::load(std::istream& in) {
bool grbl::program::load_from_stream(std::istream& in) {
instructions.clear();
is_loaded = true;
size_t line_number = 0;
for (std::string line; std::getline(in, line);) {
@@ -49,8 +52,17 @@ bool grbl::program::load(std::istream& in) {
auto comment = sm.str(1);
instructions.emplace_back(instruction::new_comment(line_number, comment));
} else if (std::regex_match(line, sm, gcode_re)) {
auto command = sm.str(1);
auto comment = sm.str(3);
auto command = trim(sm.str(1));
auto comment = trim(sm.str(3));
// // grblHAL does not support feed rates with decimal points,
// // so we remove them if possible (only one feed command in this line)
// static auto decimal_feed_re = std::regex(R"(^(F\d+)\.\d+$)");
// std::smatch feed_matches{};
// if (std::regex_match(command, feed_matches, decimal_feed_re)) {
// command = feed_matches.str(1);
// }
instructions.emplace_back(instruction::new_gcode(line_number, command, comment));
} else {
std::cerr << "Failed to parse line " << line << std::endl;
@@ -67,11 +79,11 @@ bool grbl::program::load_from_string(const std::string& content) {
in_stream << content;
filename = "";
is_loaded = load(in_stream);
is_loaded = load_from_stream(in_stream);
return is_loaded;
}
bool grbl::program::load(std::string path) {
bool grbl::program::load_from_file(std::string path) {
filename = std::move(path);
is_loaded = false;
@@ -79,7 +91,7 @@ bool grbl::program::load(std::string path) {
if (!in_file) {
return false;
}
is_loaded = load(in_file);
is_loaded = load_from_stream(in_file);
return is_loaded;
}