gcode-extra/main.cpp
2022-09-01 20:21:37 +03:00

98 lines
1.4 KiB
C++

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
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<string> lines;
vector<size_t> indexes;
vector<string>::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;
}