Switch to alameda code, with search for BBGYRS instead of BBGYRO

This commit is contained in:
xp986
2025-10-09 01:21:30 -05:00
parent 2647432b04
commit 55ac790b43

View File

@@ -1,159 +1,181 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <regex> #include <regex>
#include <algorithm> #include <algorithm>
static const std::regex number_regex_full(R"(\d+)"); 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 number_regex_1to3(R"(\b\d{1,3}\b)");
// Find the first number in a line using the given regex // Find the first number in a line using the given regex
bool find_first_number_in_line(const std::string& s, int& out, const std::regex& rx) { bool find_first_number_in_line(const std::string& s, int& out, const std::regex& rx) {
std::smatch m; std::smatch m;
if (std::regex_search(s, m, rx)) { if (std::regex_search(s, m, rx)) {
out = std::stoi(m.str()); out = std::stoi(m.str());
return true; return true;
} }
return false; return false;
} }
// Check if a line contains exactly 24 consecutive '?' // Check if a line contains exactly 24 consecutive '<EFBFBD>'
bool has_exactly_24_A(const std::string& s) { bool has_exactly_24_A(const std::string& s) {
int count = 0; int count = 0;
for (size_t i = 0; i <= s.size(); ++i) { for (size_t i = 0; i <= s.size(); ++i) {
if (i < s.size() && s[i] == (char)196) { if (i < s.size() && s[i] == (char)196) {
count++; count++;
} }
else { else {
if (count == 24) return true; // exact match found if (count == 24) return true; // exact match found
count = 0; // reset counter count = 0; // reset counter
} }
} }
return false; return false;
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc < 3) { if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <inputfile> <outputfile>\n"; std::cerr << "Usage: " << argv[0] << " <inputfile> <outputfile>\n";
return 1; return 1;
} }
std::ifstream infile(argv[1]); // Read entire file into memory as lines
if (!infile) { std::ifstream infile(argv[1], std::ios::binary);
std::cerr << "Error opening input file.\n"; if (!infile) {
return 1; std::cerr << "Error opening input file.\n";
} return 1;
}
std::ofstream outfile(argv[2]);
if (!outfile) { std::ofstream outfile(argv[2], std::ios::binary);
std::cerr << "Error opening output file.\n"; if (!outfile) {
return 1; std::cerr << "Error opening output file.\n";
} return 1;
}
std::vector<std::string> lines;
std::string line; std::vector<std::string> lines;
while (std::getline(infile, line)) { std::string line;
lines.push_back(line); while (std::getline(infile, line)) {
} if (!infile.eof()) line += '\n'; // preserve original line endings
lines.push_back(line);
const std::string marker = "F.L.C.M. %"; }
// === 1) Find marker index === // Detect if document should be modified
bool marker_found = false; bool has_bbgyro = false;
size_t marker_index = 0; bool has_subs = false;
for (size_t i = 0; i < lines.size(); ++i) { for (const auto& l : lines) {
if (lines[i].find(marker) != std::string::npos) { if (!has_bbgyro && l.find("BBGYRS") != std::string::npos) has_bbgyro = true;
marker_found = true; if (!has_subs && l.find("SUBS") != std::string::npos) has_subs = true;
marker_index = i; if (has_bbgyro || has_subs) break;
break; }
}
} // If neither BBGYRO nor SUBS appears, pass file through unmodified
if (!has_bbgyro && !has_subs) {
if (!marker_found) { infile.clear();
for (const auto& l : lines) outfile << l << '\n'; infile.seekg(0, std::ios::beg);
return 0; outfile << infile.rdbuf(); // exact byte-for-byte copy
} return 0;
}
// === 2) Extract gyros from "BBGYRO" line ===
int gyros = 0; const std::string marker = "F.L.C.M. %";
bool gyros_found = false;
size_t bbgyro_index = std::string::npos; // === 1) Find marker index ===
const std::string bbgyro_token = "BBGYRS"; bool marker_found = false;
size_t marker_index = 0;
for (size_t i = 0; i < lines.size(); ++i) { for (size_t i = 0; i < lines.size(); ++i) {
size_t pos = lines[i].find(bbgyro_token); if (lines[i].find(marker) != std::string::npos) {
if (pos != std::string::npos) { marker_found = true;
bbgyro_index = i; marker_index = i;
std::string tail = lines[i].substr(pos + bbgyro_token.size()); break;
if (find_first_number_in_line(tail, gyros, number_regex_full)) { }
gyros_found = true; }
}
break; if (!marker_found) {
} // Marker not found, write original file unchanged
} infile.clear();
infile.seekg(0, std::ios::beg);
// === 3) Extract total_subs === outfile << infile.rdbuf();
int total_subs = 0; return 0;
}
auto find_total_after_start_index = [&](size_t start_index) -> bool {
for (size_t j = start_index; j < lines.size(); ++j) { // === 2) Extract gyros from "BBGYRO" line ===
if (has_exactly_24_A(lines[j])) { int gyros = 0;
if (j + 1 < lines.size()) { bool gyros_found = false;
if (find_first_number_in_line(lines[j + 1], total_subs, number_regex_1to3)) { size_t bbgyro_index = std::string::npos;
return true; 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) {
return false; bbgyro_index = i;
}; std::string tail = lines[i].substr(pos + bbgyro_token.size());
if (find_first_number_in_line(tail, gyros, number_regex_full)) {
if (gyros_found) { gyros_found = true;
find_total_after_start_index(bbgyro_index + 1); }
} break;
else { }
// Fallback: second "SUBS" }
int subs_count = 0;
size_t subs_index = std::string::npos; // === 3) Extract total_subs ===
for (size_t i = 0; i < lines.size(); ++i) { int total_subs = 0;
if (lines[i].find("SUBS") != std::string::npos) {
subs_count++; auto find_total_after_start_index = [&](size_t start_index) -> bool {
if (subs_count == 2) { for (size_t j = start_index; j < lines.size(); ++j) {
subs_index = i; if (has_exactly_24_A(lines[j])) {
break; if (j + 1 < lines.size()) {
} if (find_first_number_in_line(lines[j + 1], total_subs, number_regex_1to3)) {
} return true;
} }
if (subs_index != std::string::npos) { }
find_total_after_start_index(subs_index + 1); }
} }
} return false;
};
// === 4) Truncate document after the marker ===
std::vector<std::string> truncated(lines.begin(), lines.begin() + marker_index + 1); if (gyros_found) {
find_total_after_start_index(bbgyro_index + 1);
// === 5) Insert TOTAL SUBS --- diff before first form feed === }
int diff = total_subs - gyros; else {
const std::string insert_line = "\n\n\r ROLLS USED --- " + std::to_string(diff); // Fallback: second "SUBS"
int subs_count = 0;
bool inserted = false; size_t subs_index = std::string::npos;
for (const auto& l : truncated) { for (size_t i = 0; i < lines.size(); ++i) {
size_t ff_pos = l.find('\f'); if (lines[i].find("SUBS") != std::string::npos) {
if (!inserted && ff_pos != std::string::npos) { subs_count++;
std::string modified = l.substr(0, ff_pos) + insert_line + l.substr(ff_pos); if (subs_count == 2) {
outfile << modified; subs_index = i;
inserted = true; break;
} }
else { }
outfile << l; }
} if (subs_index != std::string::npos) {
} find_total_after_start_index(subs_index + 1);
}
if (inserted) { }
// === 6) Append a form feed to the very end of the truncated document ===
outfile << '\f'; // === 4) Truncate document after the marker ===
} std::vector<std::string> truncated(lines.begin(), lines.begin() + marker_index + 1);
return 0; // === 5) Insert TOTAL SUBS --- diff before first form feed ===
} int diff = total_subs - gyros;
const std::string insert_line = "\n\n\r ROLLS USED --- " + 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;
inserted = true;
}
else {
outfile << l;
}
}
if (inserted) {
// === 6) Append a form feed to the very end of the truncated document ===
outfile << '\f';
}
return 0;
}