Initial import
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
#include "grbl.h"
|
||||
|
||||
#include <utility>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
#include <iostream>
|
||||
|
||||
grbl::instruction grbl::instruction::new_gcode(size_t line, std::string cmd, std::string comment) {
|
||||
return instruction{
|
||||
.line = line,
|
||||
.type = instruction_type::gcode,
|
||||
.command = std::move(cmd),
|
||||
.comment = std::move(comment),
|
||||
};
|
||||
}
|
||||
|
||||
grbl::instruction grbl::instruction::new_user_message(size_t line, std::string comment) {
|
||||
return instruction{
|
||||
.line = line,
|
||||
.type = instruction_type::user_message,
|
||||
.comment = std::move(comment),
|
||||
};
|
||||
}
|
||||
|
||||
grbl::instruction grbl::instruction::new_comment(size_t line, std::string comment) {
|
||||
return grbl::instruction{
|
||||
.line = line,
|
||||
.type = instruction_type::comment,
|
||||
.comment = std::move(comment),
|
||||
};
|
||||
}
|
||||
|
||||
grbl::program::program(std::string filename) {
|
||||
load(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) {
|
||||
is_loaded = true;
|
||||
size_t line_number = 0;
|
||||
for (std::string line; std::getline(in, line);) {
|
||||
line_number++;
|
||||
if (!line.empty()) {
|
||||
|
||||
std::smatch sm{};
|
||||
if (std::regex_match(line, sm, comment_re)) {
|
||||
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);
|
||||
instructions.emplace_back(instruction::new_gcode(line_number, command, comment));
|
||||
} else {
|
||||
std::cerr << "Failed to parse line " << line << std::endl;
|
||||
is_loaded = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return is_loaded;
|
||||
}
|
||||
|
||||
bool grbl::program::load_from_string(const std::string& content) {
|
||||
std::stringstream in_stream;
|
||||
in_stream << content;
|
||||
|
||||
filename = "";
|
||||
is_loaded = load(in_stream);
|
||||
return is_loaded;
|
||||
}
|
||||
|
||||
bool grbl::program::load(std::string path) {
|
||||
filename = std::move(path);
|
||||
is_loaded = false;
|
||||
|
||||
std::ifstream in_file(filename);
|
||||
if (!in_file) {
|
||||
return false;
|
||||
}
|
||||
is_loaded = load(in_file);
|
||||
return is_loaded;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const grbl::instruction_type& t) {
|
||||
switch (t) {
|
||||
case grbl::instruction_type::gcode:
|
||||
out << "gcode";
|
||||
break;
|
||||
case grbl::instruction_type::comment:
|
||||
out << "comment";
|
||||
break;
|
||||
case grbl::instruction_type::user_message:
|
||||
out << "user_message";
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const grbl::instruction& i) {
|
||||
out << "{.line: " << i.line << ", .type: " << i.type << ", .cmd: " << i.command << ", .comment: " << i.comment << " }";
|
||||
return out;
|
||||
}
|
||||
|
||||
void grbl::program::dump(std::ostream& out) {
|
||||
for (auto& i: instructions) {
|
||||
out << i << std::endl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user