From 771b0067f5ef25f0810d11e632b9561f389d4e7e Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Sat, 20 Mar 2021 18:36:56 +0100
Subject: [PATCH] [BL/IO] Free writeStringToFile function.

---
 BaseLib/IO/Writer.cpp | 36 +++++++++++++++++++++---------------
 BaseLib/IO/Writer.h   |  3 +++
 2 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/BaseLib/IO/Writer.cpp b/BaseLib/IO/Writer.cpp
index ef718453af3..350d98d448b 100644
--- a/BaseLib/IO/Writer.cpp
+++ b/BaseLib/IO/Writer.cpp
@@ -45,25 +45,31 @@ std::string Writer::writeToString()
 
 int Writer::writeToFile(std::filesystem::path const& file_path)
 {
-    std::string file_content = this->writeToString();
-    if (!file_content.empty())
+    return writeStringToFile(writeToString(), file_path);
+}
+
+int writeStringToFile(std::string content,
+                      std::filesystem::path const& file_path)
+{
+    if (content.empty())
     {
-        std::ofstream fileStream;
-        fileStream.open(file_path.c_str());
+        return 0;
+    }
+    std::ofstream fileStream;
+    fileStream.open(file_path.c_str());
 
-        // check file stream
-        if (!fileStream)
-        {
-            ERR("Could not open file '{:s}'!", file_path.string());
-            return 0;
-        }
+    // check file stream
+    if (!fileStream)
+    {
+        ERR("Could not open file '{:s}'!", file_path.string());
+        return 0;
+    }
 
-        fileStream << file_content;
+    fileStream << content;
 
-        fileStream.close();
-        return 1;
-    }
-    return 0;
+    fileStream.close();
+    return 1;
 }
+
 }  // namespace IO
 }  // namespace BaseLib
diff --git a/BaseLib/IO/Writer.h b/BaseLib/IO/Writer.h
index 7937aa93502..a7bbd691fb3 100644
--- a/BaseLib/IO/Writer.h
+++ b/BaseLib/IO/Writer.h
@@ -49,5 +49,8 @@ protected:
     std::ostringstream out;
 };
 
+/// \returns 0 if string is empty, or if there is an error, and 1 otherwise.
+int writeStringToFile(std::string content,
+                      std::filesystem::path const& file_path);
 } // namespace IO
 } // namespace BaseLib
-- 
GitLab