From 63e71d0df8354e2c0f17de8d43a101fc434912f8 Mon Sep 17 00:00:00 2001
From: "fabian.peller-konrad@kit.edu" <fabian.peller-konrad@kit.edu>
Date: Tue, 13 Oct 2020 14:41:18 +0200
Subject: [PATCH] added comparison between new generation and existing file

---
 .../applications/AronGenerator/main.cpp       | 48 +++++++++++++------
 1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/source/RobotAPI/applications/AronGenerator/main.cpp b/source/RobotAPI/applications/AronGenerator/main.cpp
index afb9a6204..d384cde2b 100644
--- a/source/RobotAPI/applications/AronGenerator/main.cpp
+++ b/source/RobotAPI/applications/AronGenerator/main.cpp
@@ -91,6 +91,11 @@ int main(int argc, char* argv[])
         }
 
         std::filesystem::path input_file(filename);
+        if (!std::filesystem::exists(input_file))
+        {
+            std::cerr << "The input file does not exist!" << std::endl;
+        }
+
         if (input_file.extension().string() != ".xml")
         {
             std::cerr << "The file you passed has the wrong type." << std::endl;
@@ -98,6 +103,8 @@ int main(int argc, char* argv[])
 
         std::string new_name_with_extension = input_file.filename().replace_extension("").string();
         new_name_with_extension += ".aron.generated.h";
+        std::string output_file_path = output + "/" + new_name_with_extension;
+        std::filesystem::path output_file(output_file_path);
 
         reader.parseFile(input_file);
         if (verbose)
@@ -121,14 +128,10 @@ int main(int argc, char* argv[])
             std::cout << "--> Found " << writer.getTypeClasses().size() << " type classes." << std::endl;
         }
 
-        std::ofstream ofs;
-        std::string output_current_file = output + "/" + new_name_with_extension;
-        ofs.open(output_current_file);
-
-        time_t now = time(0);
-        char* dt = ctime(&now);
+        std::stringstream file_new_content;
 
-        ofs << "/* \n\
+        file_new_content << "\
+/* \n\
  * This file is part of ArmarX. \n\
  * \n\
  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), \n\
@@ -151,7 +154,6 @@ int main(int argc, char* argv[])
  ************************************************************************* \n\
  * WARNING: This file is autogenerated. \n\
  * Original file: " + filename + " \n\
- * Timestamp of generation: " + dt + "\
  * Please do not edit since your changes may be overwritten on the next generation \n\
  * If you have any questions please contact: Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) \n\
  ************************************************************************* \n\
@@ -169,7 +171,7 @@ int main(int argc, char* argv[])
             CppWriterPtr w = CppWriterPtr(new CppWriter());
             c->writeCpp(w);
 
-            ofs << w->getString();
+            file_new_content << w->getString();
 
             if (verbose)
             {
@@ -178,17 +180,33 @@ int main(int argc, char* argv[])
 
             if (i != writer.getTypeClasses().size())
             {
-                ofs << "\n";
-                ofs << "// *************\n";
-                ofs << "// Here comes another auto-generated class. Don't mind the duplicated includes - everything is guarded!\n";
-                ofs << "// *************\n";
-                ofs << "\n";
+                file_new_content << "\n";
+                file_new_content << "// *************\n";
+                file_new_content << "// Here comes another auto-generated class. Don't mind the duplicated includes - everything is guarded!\n";
+                file_new_content << "// *************\n";
+                file_new_content << "\n";
             }
+        }
 
+        // check if file already exists
+        if (std::filesystem::exists(output_file))
+        {
+            std::ifstream ifs(output_file_path);
+            std::string file_content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
+
+            if (file_content == file_new_content.str())
+            {
+                std::cout << "File content not changed for <" + output_file_path + ">" << std::endl;
+                exit(0);
+            }
         }
+
+        std::ofstream ofs;
+        ofs.open(output_file_path);
+        ofs << file_new_content.str();
         ofs.close();
 
-        std::cout << "Finished generating <" + output_current_file + ">" << std::endl;
+        std::cout << "Finished generating <" + output_file_path + ">" << std::endl;
     }
     catch (const cxxopts::OptionException& e)
     {
-- 
GitLab