Initial commit with working version.

This commit is contained in:
xp986
2025-10-08 23:44:36 -05:00
parent f8aeb47a77
commit c966d048b6
6 changed files with 951 additions and 0 deletions

143
ReportTruncate_0.2.cpp Normal file
View File

@@ -0,0 +1,143 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>
#include <algorithm>
static const std::regex number_regex_full(R"(\d+)");
static const std::regex number_regex_1to3(R"(\b\d{1,3}\b)");
static const std::regex exact_24_A(R"((?<!Ä)Ä{24}(?!Ä))");
bool find_first_number_in_line(const std::string &s, int &out, const std::regex &rx) {
std::smatch m;
if (std::regex_search(s, m, rx)) {
out = std::stoi(m.str());
return true;
}
return false;
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <inputfile> <outputfile>\n";
return 1;
}
std::ifstream infile(argv[1]);
if (!infile) {
std::cerr << "Error opening input file.\n";
return 1;
}
std::ofstream outfile(argv[2]);
if (!outfile) {
std::cerr << "Error opening output file.\n";
return 1;
}
std::vector<std::string> lines;
std::string line;
while (std::getline(infile, line)) {
lines.push_back(line);
}
const std::string marker = "F.L.C.M. %";
// === 1) Find marker index ===
bool marker_found = false;
size_t marker_index = 0;
for (size_t i = 0; i < lines.size(); ++i) {
if (lines[i].find(marker) != std::string::npos) {
marker_found = true;
marker_index = i;
break;
}
}
if (!marker_found) {
for (const auto &l : lines) outfile << l << '\n';
return 0;
}
// === 2) Extract gyros from "BBGYRO" line ===
int gyros = 0;
bool gyros_found = false;
size_t bbgyro_index = std::string::npos;
const std::string bbgyro_token = "BBGYRO";
for (size_t i = 0; i < lines.size(); ++i) {
size_t pos = lines[i].find(bbgyro_token);
if (pos != std::string::npos) {
bbgyro_index = i;
std::string tail = lines[i].substr(pos + bbgyro_token.size());
if (find_first_number_in_line(tail, gyros, number_regex_full)) {
gyros_found = true;
}
break;
}
}
// === 3) Extract total_subs ===
int total_subs = 0;
bool total_found = false;
auto find_total_after_start_index = [&](size_t start_index)->bool {
for (size_t j = start_index; j < lines.size(); ++j) {
if (std::regex_search(lines[j], exact_24_A)) {
if (j + 1 < lines.size()) {
if (find_first_number_in_line(lines[j + 1], total_subs, number_regex_1to3)) {
return true;
}
}
}
}
return false;
};
if (gyros_found) {
total_found = find_total_after_start_index(bbgyro_index + 1);
} else {
// Fallback: second "SUBS"
int subs_count = 0;
size_t subs_index = std::string::npos;
for (size_t i = 0; i < lines.size(); ++i) {
if (lines[i].find("SUBS") != std::string::npos) {
subs_count++;
if (subs_count == 2) {
subs_index = i;
break;
}
}
}
if (subs_index != std::string::npos) {
total_found = find_total_after_start_index(subs_index + 1);
}
}
// === 4) Truncate document after the marker ===
std::vector<std::string> truncated(lines.begin(), lines.begin() + marker_index + 1);
// === 5) Insert TOTAL SUBS --- diff before first form feed ===
int diff = total_subs - gyros;
const std::string insert_line = "\n\n TOTAL SUBS --- " + std::to_string(diff);
bool inserted = false;
for (const auto &l : truncated) {
size_t ff_pos = l.find('\f');
if (!inserted && ff_pos != std::string::npos) {
std::string modified = l.substr(0, ff_pos) + insert_line + l.substr(ff_pos);
outfile << modified << '\n';
inserted = true;
} else {
outfile << l << '\n';
}
}
if (!inserted) {
outfile << insert_line << '\n';
}
return 0;
}