From 01bbf06e1d95e939b202b3abc7fd499fc6140dd4 Mon Sep 17 00:00:00 2001
From: Tobias Meisel <tobias.meisel@ufz.de>
Date: Thu, 5 Nov 2020 12:05:19 +0100
Subject: [PATCH] [MeL/IO] Use filesystem instead of string

writeMeshToFile is used in about 20 other places (utils)
here we use implicite conversion from string to filesystem
---
 Applications/FileIO/Legacy/createSurface.cpp  |  2 +-
 .../generateStructuredMesh.cpp                |  4 +--
 BaseLib/IO/Writer.cpp                         |  6 ++--
 BaseLib/IO/Writer.h                           |  3 +-
 MeshLib/IO/VtkIO/VtuInterface.cpp             |  6 ++--
 MeshLib/IO/VtkIO/VtuInterface.h               |  3 +-
 MeshLib/IO/XDMF/writeXdmf.cpp                 |  5 ++--
 MeshLib/IO/XDMF/writeXdmf.h                   |  4 +--
 MeshLib/IO/writeMeshToFile.cpp                | 28 +++++++++----------
 MeshLib/IO/writeMeshToFile.h                  |  4 ++-
 10 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/Applications/FileIO/Legacy/createSurface.cpp b/Applications/FileIO/Legacy/createSurface.cpp
index e606f865d93..3e31b0116eb 100644
--- a/Applications/FileIO/Legacy/createSurface.cpp
+++ b/Applications/FileIO/Legacy/createSurface.cpp
@@ -82,7 +82,7 @@ bool createSurface(GeoLib::Polyline const& ply,
     auto geo_file = fs::temp_directory_path() /= BaseLib::randomString(32);
     auto msh_file = fs::temp_directory_path() /= BaseLib::randomString(32);
 
-    gmsh_io.writeToFile(geo_file.string());
+    gmsh_io.writeToFile(geo_file);
     // Using GMSH's vtk output here so we don't have to deal with GMSH and it's
     // various file format versions here
     std::string gmsh_command =
diff --git a/Applications/Utils/SimpleMeshCreation/generateStructuredMesh.cpp b/Applications/Utils/SimpleMeshCreation/generateStructuredMesh.cpp
index c076034198e..6f98f506d5f 100644
--- a/Applications/Utils/SimpleMeshCreation/generateStructuredMesh.cpp
+++ b/Applications/Utils/SimpleMeshCreation/generateStructuredMesh.cpp
@@ -250,9 +250,9 @@ int main (int argc, char* argv[])
              mesh->getNumberOfNodes(), mesh->getNumberOfElements());
 
         // write into a file
-        MeshLib::IO::writeMeshToFile(*(mesh.get()), mesh_out.getValue());
+        MeshLib::IO::writeMeshToFile(
+            *(mesh.get()), std::filesystem::path(mesh_out.getValue()));
     }
 
     return EXIT_SUCCESS;
 }
-
diff --git a/BaseLib/IO/Writer.cpp b/BaseLib/IO/Writer.cpp
index a9d63b552dd..5f2e5d0dc09 100644
--- a/BaseLib/IO/Writer.cpp
+++ b/BaseLib/IO/Writer.cpp
@@ -43,18 +43,18 @@ std::string Writer::writeToString()
     return std::string("");
 }
 
-int Writer::writeToFile(std::string const& filename)
+int Writer::writeToFile(std::filesystem::path const& file_path)
 {
     std::string file_content = this->writeToString();
     if (!file_content.empty())
     {
         std::ofstream fileStream;
-        fileStream.open (filename.c_str());
+        fileStream.open(file_path.c_str());
 
         // check file stream
         if (!fileStream)
         {
-            ERR("Could not open file '{:s}'!", filename);
+            ERR("Could not open file '{:s}'!", file_path.string());
             return 0;
         }
 
diff --git a/BaseLib/IO/Writer.h b/BaseLib/IO/Writer.h
index e556863b4af..38e20ee57aa 100644
--- a/BaseLib/IO/Writer.h
+++ b/BaseLib/IO/Writer.h
@@ -14,6 +14,7 @@
 
 #pragma once
 
+#include <filesystem.h>
 #include <string>
 #include <sstream>
 
@@ -38,7 +39,7 @@ public:
     std::string writeToString();
 
     /// @brief Writes the object to the given file.
-    int writeToFile(std::string const& filename);
+    int writeToFile(std::filesystem::path const& file_path);
 
     /// @brief Sets the decimal precision.
     void setPrecision(unsigned int precision);
diff --git a/MeshLib/IO/VtkIO/VtuInterface.cpp b/MeshLib/IO/VtkIO/VtuInterface.cpp
index 8c98358c2f3..8c7b2054168 100644
--- a/MeshLib/IO/VtkIO/VtuInterface.cpp
+++ b/MeshLib/IO/VtkIO/VtuInterface.cpp
@@ -132,11 +132,11 @@ std::string getVtuFileNameForPetscOutputWithoutExtension(
 }
 #endif
 
-bool VtuInterface::writeToFile(std::string const &file_name)
+bool VtuInterface::writeToFile(std::filesystem::path const& file_path)
 {
 #ifdef USE_PETSC
     auto const vtu_file_name =
-        getVtuFileNameForPetscOutputWithoutExtension(file_name);
+        getVtuFileNameForPetscOutputWithoutExtension(file_path.string());
     int rank;
     MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
     int mpi_size;
@@ -144,7 +144,7 @@ bool VtuInterface::writeToFile(std::string const &file_name)
     return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
                                                    mpi_size, rank);
 #else
-    return writeVTU<vtkXMLUnstructuredGridWriter>(file_name);
+    return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
 #endif
 }
 
diff --git a/MeshLib/IO/VtkIO/VtuInterface.h b/MeshLib/IO/VtkIO/VtuInterface.h
index c8f9cae2d20..ea6b3336742 100644
--- a/MeshLib/IO/VtkIO/VtuInterface.h
+++ b/MeshLib/IO/VtkIO/VtuInterface.h
@@ -15,6 +15,7 @@
 #pragma once
 
 #include <string>
+#include <filesystem>
 #include <vtkXMLWriter.h>
 
 namespace MeshLib {
@@ -52,7 +53,7 @@ public:
 
     /// Writes the given mesh to file.
     /// \return True on success, false on error
-    bool writeToFile(std::string const &file_name);
+    bool writeToFile(std::filesystem::path const& file_path);
 
     /// Writes the given mesh to vtu file.
     /// \param file_name      File name.
diff --git a/MeshLib/IO/XDMF/writeXdmf.cpp b/MeshLib/IO/XDMF/writeXdmf.cpp
index fa2ecdf6165..cdf1ce6c5fc 100644
--- a/MeshLib/IO/XDMF/writeXdmf.cpp
+++ b/MeshLib/IO/XDMF/writeXdmf.cpp
@@ -23,12 +23,13 @@ namespace MeshLib
 {
 namespace IO
 {
-bool writeXdmf3(const MeshLib::Mesh& mesh, std::string const& file_name)
+bool writeXdmf3(const MeshLib::Mesh& mesh,
+                std::filesystem::path const& file_path)
 {
     vtkSmartPointer<vtkXdmf3Writer> writer =
         vtkSmartPointer<vtkXdmf3Writer>::New();
 
-    writer->SetFileName(file_name.c_str());
+    writer->SetFileName(file_path.string().c_str());
     vtkNew<MeshLib::VtkMappedMeshSource> vtkSource;
     vtkSource->SetMesh(&mesh);
     vtkSource->Update();
diff --git a/MeshLib/IO/XDMF/writeXdmf.h b/MeshLib/IO/XDMF/writeXdmf.h
index b723cde6807..f212f8b505e 100644
--- a/MeshLib/IO/XDMF/writeXdmf.h
+++ b/MeshLib/IO/XDMF/writeXdmf.h
@@ -14,7 +14,7 @@
 
 #pragma once
 
-#include <string>
+#include <filesystem>
 
 namespace MeshLib
 {
@@ -26,7 +26,7 @@ namespace IO
 /// \return True on success, false on error
 /// \param mesh           Mesh holds all data to be written.
 /// \param file_name      File name.
-bool writeXdmf3(MeshLib::Mesh const& mesh, std::string const& file_name);
+bool writeXdmf3(MeshLib::Mesh const& mesh, std::filesystem::path const& file_path);
 
 }  // end namespace IO
 }  // end namespace MeshLib
\ No newline at end of file
diff --git a/MeshLib/IO/writeMeshToFile.cpp b/MeshLib/IO/writeMeshToFile.cpp
index 256680b6a6d..25f23c4a56f 100644
--- a/MeshLib/IO/writeMeshToFile.cpp
+++ b/MeshLib/IO/writeMeshToFile.cpp
@@ -9,52 +9,52 @@
 
 #include "writeMeshToFile.h"
 
-#include "BaseLib/Logging.h"
 #include "BaseLib/FileTools.h"
+#include "BaseLib/Logging.h"
 #include "BaseLib/StringTools.h"
-
-#include "MeshLib/Mesh.h"
 #include "MeshLib/IO/Legacy/MeshIO.h"
 #include "MeshLib/IO/VtkIO/VtuInterface.h"
 #include "MeshLib/IO/XDMF/writeXdmf.h"
-
+#include "MeshLib/Mesh.h"
 
 namespace MeshLib
 {
 namespace IO
 {
-int writeMeshToFile(const MeshLib::Mesh &mesh, const std::string &file_name)
+int writeMeshToFile(const MeshLib::Mesh& mesh,
+                    std::filesystem::path const& file_path)
 {
-    if (BaseLib::hasFileExtension(".msh", file_name))
+    if (file_path.extension().string() == ".msh")
     {
         MeshLib::IO::Legacy::MeshIO meshIO;
         meshIO.setMesh(&mesh);
-        meshIO.writeToFile(file_name);
+        meshIO.writeToFile(file_path);
         return 0;
     }
-    if (BaseLib::hasFileExtension(".vtu", file_name))
+    if (file_path.extension().string() == ".vtu")
     {
         MeshLib::IO::VtuInterface writer(&mesh);
-        auto const result = writer.writeToFile(file_name);
+        auto const result = writer.writeToFile(file_path);
         if (!result)
         {
             ERR("writeMeshToFile(): Could not write mesh to '{:s}'.",
-                file_name);
+                file_path.string());
             return -1;
         }
         return 0;
     }
-    if (BaseLib::hasFileExtension(".xdmf", file_name))
+    if (file_path.extension().string() == ".xdmf")
     {
-        if (auto const result = writeXdmf3(mesh, file_name); !result)
+        if (auto const result = writeXdmf3(mesh, file_path); !result)
         {
             ERR("writeMeshToFile(): Could not write mesh to '{:s}'.",
-                file_name);
+                file_path.string());
             return -1;
         }
         return 0;
     }
-    ERR("writeMeshToFile(): Unknown mesh file format in file {:s}.", file_name);
+    ERR("writeMeshToFile(): Unknown mesh file format in file {:s}.",
+        file_path.string());
     return -1;
 
 }
diff --git a/MeshLib/IO/writeMeshToFile.h b/MeshLib/IO/writeMeshToFile.h
index d2751c7a667..fb3c30223bc 100644
--- a/MeshLib/IO/writeMeshToFile.h
+++ b/MeshLib/IO/writeMeshToFile.h
@@ -9,6 +9,7 @@
 
 #pragma once
 
+#include <filesystem>
 #include <string>
 
 namespace MeshLib
@@ -16,6 +17,7 @@ namespace MeshLib
 class Mesh;
 namespace IO
 {
-int writeMeshToFile(const MeshLib::Mesh &mesh, const std::string &file_name);
+int writeMeshToFile(const MeshLib::Mesh& mesh,
+                    std::filesystem::path const& file_path);
 }
 }
\ No newline at end of file
-- 
GitLab