Skip to content

Commit

Permalink
Remove redundant file close calls and update error messages for clari…
Browse files Browse the repository at this point in the history
…ty and consistency.
  • Loading branch information
sao05 authored and IrneRacoonovich committed Apr 13, 2024
1 parent d800abd commit aedb28b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
7 changes: 2 additions & 5 deletions source/text_funcs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ bool write_to_file(const std::string& file_path, const std::string& line) {
return false;
}
output_file << line << std::endl;
output_file.close();
return true;
}

Expand All @@ -83,15 +82,14 @@ bool write_to_file(const std::string& file_path, const std::string& line) {
for (const auto& existing_line : existing_lines) {
output_file << existing_line << std::endl;
}
output_file.close();
return true;
}

bool remove_txt(const std::string& file_path, const std::string& pattern) {
std::vector<std::string> lines;
std::ifstream file(file_path);
if (!file.is_open()) {
log("There is no %s in %s",pattern.c_str(), file_path.c_str());
log("File %s not found", file_path.c_str());
return true;
}

Expand All @@ -105,13 +103,12 @@ bool remove_txt(const std::string& file_path, const std::string& pattern) {

std::ofstream output_file(file_path);
if (!output_file.is_open()) {
log("There is no %s in %s",pattern.c_str(), file_path.c_str());
log("File %s can't be created", file_path.c_str());
return true;
}

for (const auto& line : lines) {
output_file << line << std::endl;
}
output_file.close();
return true;
}
26 changes: 12 additions & 14 deletions source/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,25 +508,23 @@ int interpretAndExecuteCommand(const std::vector<std::vector<std::string>>& comm
// Edit command
if (command.size() == 3) {
sourcePath = preprocessPath(command[1]);

bool result = remove_txt(sourcePath, removeQuotes(command[2]));
// log(command[2]);
if (!result && catchErrors) {
log("Error in %s command", commandName.c_str());
return -1;
}
bool result = remove_txt(sourcePath, removeQuotes(command[2]));
// log(command[2]);
if (!result && catchErrors) {
log("Error in %s command", commandName.c_str());
return -1;
}
}
} else if (commandName == "add-txt-str") {
// Edit command
if (command.size() == 3) {
sourcePath = preprocessPath(command[1]);

bool result = write_to_file(sourcePath, removeQuotes(command[2]));
// log(command[2]);
if (!result && catchErrors) {
log("Error in %s command", commandName.c_str());
return -1;
}
bool result = write_to_file(sourcePath, removeQuotes(command[2]));
// log(command[2]);
if (!result && catchErrors) {
log("Error in %s command", commandName.c_str());
return -1;
}
}
} else if (commandName == "hex-by-offset") {
// Edit command
Expand Down

0 comments on commit aedb28b

Please sign in to comment.