#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; }