Debugging
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 2m10s

This commit is contained in:
Your Name
2025-05-25 19:17:51 +12:00
parent 1502d6e3d2
commit 8f06fc31ae
17 changed files with 220 additions and 188 deletions

View File

@@ -40,7 +40,7 @@ bool replace_line_in_file(const std::string& file_path, const std::string& searc
std::string line;
if (!input_file.is_open()) {
std::cerr << "Error: Unable to open file: " << file_path << std::endl;
error << "Unable to open file: " << file_path << std::endl;
return false;
}
@@ -55,7 +55,7 @@ bool replace_line_in_file(const std::string& file_path, const std::string& searc
std::ofstream output_file(file_path);
if (!output_file.is_open())
{
std::cerr << "Error: Unable to open file: " << file_path << std::endl;
error << "Unable to open file: " << file_path << std::endl;
return false;
}
for (const auto& modified_line : file_lines)
@@ -156,7 +156,7 @@ int str2int(const std::string &str)
try {
return std::stoi(str);
} catch (const std::exception& e) {
std::cerr << "Error: Invalid integer string: [" << str << "]" << std::endl;
error << "Invalid integer string: [" << str << "]" << std::endl;
return 0;
}
}
@@ -313,7 +313,8 @@ std::string requote(std::string str) {
int die(const std::string & msg) {
std::cerr << msg << std::endl;
error << "Fatal error:" << std::endl;
error << msg << std::endl;
return 1;
}
@@ -553,23 +554,40 @@ bool match_line(const std::string &line, const std::string &pattern) {
// edits file in-place.
bool file_replace_or_add_segment(std::string filepath, std::string segment)
{
std::string first_line = segment.substr(0, segment.find("\n"));
// look backwards until we get a non-empty line.
size_t last_line_pos = segment.rfind("\n");
while (last_line_pos != std::string::npos) {
std::string last_line = segment.substr(last_line_pos + 1);
if (!trim(last_line).empty()) {
break;
}
last_line_pos = segment.rfind("\n", last_line_pos - 1);
// Create a backup of the original file
std::string backup_path = filepath + ".bak";
try {
std::filesystem::copy_file(filepath, backup_path, std::filesystem::copy_options::overwrite_existing);
} catch (const std::exception& e) {
std::cerr << "Error creating backup file: " << e.what() << std::endl;
return false;
}
// Handle empty segment
if (segment.empty()) {
error << "Empty segment provided" << std::endl;
return false;
}
// split the segment into lines
std::vector<std::string> segment_lines = split(segment, "\n");
// remove empty lines
segment_lines.erase(std::remove_if(segment_lines.begin(), segment_lines.end(), [](const std::string& line) {
return trim(line).empty();
}), segment_lines.end());
// remove any lines that are just whitespace
segment_lines.erase(std::remove_if(segment_lines.begin(), segment_lines.end(), [](const std::string& line) { return trim(line).empty(); }), segment_lines.end());
// check that the segment has at least two lines
if (segment_lines.size() < 2) {
error << "Segment must contain at least two non-empty lines" << std::endl;
return false;
}
std::string last_line = segment.substr(last_line_pos + 1);
// Read the entire file into memory
std::ifstream input_file(filepath);
if (!input_file.is_open()) {
std::cerr << "Error: Unable to open file: " << filepath << std::endl;
error << "Unable to open file: " << filepath << std::endl;
return false;
}
@@ -580,22 +598,21 @@ bool file_replace_or_add_segment(std::string filepath, std::string segment)
}
input_file.close();
// Store original file size for verification
size_t original_size = file_lines.size();
if (original_size == 0) {
warning << "File is empty" << std::endl;
}
// Try to find the matching block
bool found_match = false;
for (size_t i = 0; i < file_lines.size(); i++) {
if (match_line(file_lines[i], first_line)) {
if (match_line(file_lines[i], segment_lines[0])) {
// Found potential start, look for end
for (size_t j = i + 1; j < file_lines.size(); j++) {
if (match_line(file_lines[j], last_line)) {
if (match_line(file_lines[j], segment_lines[segment_lines.size() - 1])) {
// Found matching block, replace it
file_lines.erase(file_lines.begin() + i, file_lines.begin() + j + 1);
// Split segment into lines and insert them
std::vector<std::string> segment_lines;
std::istringstream segment_stream(segment);
while (std::getline(segment_stream, line)) {
segment_lines.push_back(line);
}
file_lines.insert(file_lines.begin() + i, segment_lines.begin(), segment_lines.end());
found_match = true;
@@ -608,16 +625,13 @@ bool file_replace_or_add_segment(std::string filepath, std::string segment)
// If no match found, append the segment
if (!found_match) {
std::istringstream segment_stream(segment);
while (std::getline(segment_stream, line)) {
file_lines.push_back(line);
}
file_lines.insert(file_lines.end(), segment_lines.begin(), segment_lines.end());
}
// Write back to file
std::ofstream output_file(filepath);
if (!output_file.is_open()) {
std::cerr << "Error: Unable to open file for writing: " << filepath << std::endl;
error << "Unable to open file for writing: " << filepath << std::endl;
return false;
}
@@ -626,6 +640,13 @@ bool file_replace_or_add_segment(std::string filepath, std::string segment)
}
output_file.close();
// If everything succeeded, remove the backup
try {
std::filesystem::remove(backup_path);
} catch (const std::exception& e) {
warning << "Could not remove backup file: " << e.what() << std::endl;
}
return true;
}