Always compare file content before replacing it.

This commit is contained in:
levlam 2023-06-26 15:08:36 +03:00
parent c8824c15fd
commit 496dc3284e
4 changed files with 15 additions and 15 deletions

View File

@ -15,7 +15,7 @@ int main(int argc, char *argv[]) {
std::string file_name = argv[i];
std::string old_contents = td::tl::get_file_contents(file_name);
std::string new_contents = td::tl::remove_documentation(old_contents);
if (new_contents != old_contents && !td::tl::put_file_contents(file_name, new_contents)) {
if (!td::tl::put_file_contents(file_name, new_contents, true)) {
std::fprintf(stderr, "Can't write file %s\n", file_name.c_str());
std::abort();
}

View File

@ -44,7 +44,18 @@ std::string get_file_contents(const std::string &file_name) {
return result;
}
bool put_file_contents(const std::string &file_name, const std::string &contents) {
bool put_file_contents(const std::string &file_name, const std::string &contents, bool compare_documentation) {
std::string old_file_contents = get_file_contents(file_name);
if (!compare_documentation) {
old_file_contents = remove_documentation(old_file_contents);
}
if (old_file_contents == contents) {
return true;
}
std::fprintf(stderr, "Write file %s\n", file_name.c_str());
FILE *f = std::fopen(file_name.c_str(), "wb");
if (f == NULL) {
std::fprintf(stderr, "Can't open file \"%s\"\n", file_name.c_str());

View File

@ -13,7 +13,7 @@ namespace tl {
std::string get_file_contents(const std::string &file_name);
bool put_file_contents(const std::string &file_name, const std::string &contents);
bool put_file_contents(const std::string &file_name, const std::string &contents, bool compare_documentation);
std::string remove_documentation(const std::string &str);

View File

@ -883,18 +883,7 @@ tl_config read_tl_config_from_file(const std::string &file_name) {
bool write_tl_to_file(const tl_config &config, const std::string &file_name, const TL_writer &w) {
tl_string_outputer out;
write_tl(config, out, w);
std::string old_file_contents = get_file_contents(file_name);
if (!w.is_documentation_generated()) {
old_file_contents = remove_documentation(old_file_contents);
}
if (old_file_contents != out.get_result()) {
std::fprintf(stderr, "Write tl to file %s\n", file_name.c_str());
return put_file_contents(file_name, out.get_result());
}
return true;
return put_file_contents(file_name, out.get_result(), w.is_documentation_generated());
}
} // namespace tl