From 496dc3284ecd37e673f1f4563bca9b3bb3904195 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 26 Jun 2023 15:08:36 +0300 Subject: Always compare file content before replacing it. --- tdtl/td/tl/tl_file_utils.cpp | 13 ++++++++++++- tdtl/td/tl/tl_file_utils.h | 2 +- tdtl/td/tl/tl_generate.cpp | 13 +------------ 3 files changed, 14 insertions(+), 14 deletions(-) (limited to 'tdtl') diff --git a/tdtl/td/tl/tl_file_utils.cpp b/tdtl/td/tl/tl_file_utils.cpp index 91c859d03..a78f8d29f 100644 --- a/tdtl/td/tl/tl_file_utils.cpp +++ b/tdtl/td/tl/tl_file_utils.cpp @@ -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()); diff --git a/tdtl/td/tl/tl_file_utils.h b/tdtl/td/tl/tl_file_utils.h index 6d4cc7132..420d26948 100644 --- a/tdtl/td/tl/tl_file_utils.h +++ b/tdtl/td/tl/tl_file_utils.h @@ -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); diff --git a/tdtl/td/tl/tl_generate.cpp b/tdtl/td/tl/tl_generate.cpp index 62345df79..5222dd318 100644 --- a/tdtl/td/tl/tl_generate.cpp +++ b/tdtl/td/tl/tl_generate.cpp @@ -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 -- cgit v1.2.3