From 21ffc18108d3cdc01fc46ca572726718c645a754 Mon Sep 17 00:00:00 2001 From: nedko Date: Thu, 1 Sep 2022 20:21:37 +0300 Subject: [PATCH] Initial commit --- Makefile | 5 +++ main.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 Makefile create mode 100644 main.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..62c49f8 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + g++ main.cpp -o gcode_extra + +.PHONY: all + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..73da56c --- /dev/null +++ b/main.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +using namespace std; + +bool is_gcode(const string& str) +{ + bool result; + + result = true; + if (str.empty()) + { + result = false; + } + + if (str.find(';') == 0) + { + result = false; + } + + return result; +} + +void usage (const char *name) +{ + cout << "Usage: " << name << " [input]" << endl; + cout << "" << endl; +} + +int main(int argc, char **argv) +{ + string line; + string to_add; + vector lines; + vector indexes; + vector::iterator it; + fstream file; + size_t layer_count; + size_t index; + size_t line_count; + int percent; + + if (argc < 2) + { + usage(argv[0]); + return -1; + } + + layer_count = 0; + index = 0; + line_count = 0; + file.open(argv[1], fstream::in); + while (!file.eof()) + { + getline(file, line); + lines.push_back(line); + + if (is_gcode) + { + ++line_count; + } + + if ((line.size() > 4) && (line.substr(0, 4) == "M117")) + { + indexes.push_back(index); + ++layer_count; + } + + ++index; + } + file.close(); + + to_add = "/"; + to_add += to_string(layer_count - 1); + + for (size_t i = 0; i < layer_count; ++i) + { + line = lines[indexes[i]]; + index = line.find(";"); + if (string::npos == index) + { + index = line.size(); + } + line.insert(index, to_add); + lines[indexes[i]] = line; + } + + file.open(argv[1], fstream::out | fstream::trunc); + for (it = lines.begin(); it != lines.end(); ++it) + { + file << *it << endl; + } + file.close(); + return 0; +}