From 947ef878e169e2563b51ce5b3191c740e1198c15 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Fri, 28 Feb 2020 13:42:58 +0100
Subject: [PATCH] Refactor std::tmpnam to std::filesystem::temp_directory_path.

Requires gcc 8.
---
 Applications/FileIO/CMakeLists.txt           |  5 +++
 Applications/FileIO/Legacy/createSurface.cpp | 32 +++++++++-----------
 web/data/versions.json                       |  2 +-
 3 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/Applications/FileIO/CMakeLists.txt b/Applications/FileIO/CMakeLists.txt
index e20207a6897..7a7b311b08a 100644
--- a/Applications/FileIO/CMakeLists.txt
+++ b/Applications/FileIO/CMakeLists.txt
@@ -32,6 +32,11 @@ target_link_libraries(ApplicationsFileIO
                              logog
                       PRIVATE MeshLib GitInfoLib)
 
+# std::filesystem is available on gcc 8 by linking to stdc++fs
+if(COMPILER_IS_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
+    target_link_libraries(ApplicationsFileIO PUBLIC stdc++fs)
+endif()
+
 if(OGS_BUILD_GUI)
     # Needed for the XmlPrjInterface, which links the DE/Base/OGSError.h.
     target_link_libraries(ApplicationsFileIO PUBLIC QtBase)
diff --git a/Applications/FileIO/Legacy/createSurface.cpp b/Applications/FileIO/Legacy/createSurface.cpp
index 8b4453ad7c1..65d335000d2 100644
--- a/Applications/FileIO/Legacy/createSurface.cpp
+++ b/Applications/FileIO/Legacy/createSurface.cpp
@@ -11,6 +11,8 @@
 #include <cstdio>
 #include <list>
 #include <memory>
+#include <filesystem>
+namespace fs = std::filesystem;
 
 #include <logog/include/logog.hpp>
 
@@ -77,20 +79,18 @@ bool createSurface(GeoLib::Polyline const& ply,
         0.0, 0, geo_names, false, false);
     gmsh_io.setPrecision(std::numeric_limits<double>::digits10);
 
-    char file_base_name_c[L_tmpnam];
-    if (! std::tmpnam(file_base_name_c))
-    {
-       OGS_FATAL("Could not create unique file name.");
-    }
-    std::string const file_base_name(file_base_name_c);
-    gmsh_io.writeToFile(file_base_name + ".geo");
-    std::string gmsh_command =
-        gmsh_binary + " -2 -algo meshadapt \"" + file_base_name + ".geo\"";
-    gmsh_command += " -o \"" + file_base_name + ".msh\"";
+    // fs::path const temp_dir = fs::temp_directory_path();
+    auto geo_file = fs::temp_directory_path() /= "createSurface.geo";
+    auto msh_file = fs::temp_directory_path() /= "createSurface.msh";
+
+    gmsh_io.writeToFile(geo_file.string());
     // Newer gmsh versions write a newer file format for meshes per default. At
     // the moment we can't read this new format. This is a switch for gmsh to
     // write the 'old' file format.
-    gmsh_command += " -format msh22";
+    std::string gmsh_command =
+        gmsh_binary + " -2 -algo meshadapt -format msh22 -o "
+        + msh_file.string() + " " + geo_file.string();
+
     int const gmsh_return_value = std::system(gmsh_command.c_str());
     if (gmsh_return_value != 0)
     {
@@ -98,18 +98,14 @@ bool createSurface(GeoLib::Polyline const& ply,
              gmsh_return_value);
     }
     auto surface_mesh =
-        FileIO::GMSH::readGMSHMesh("\"" + file_base_name + ".msh\"");
+        FileIO::GMSH::readGMSHMesh(msh_file.string());
     if (!surface_mesh)
     {
         WARN("The surface mesh could not be created.");
         return false;
     }
-    if (std::remove((file_base_name + ".geo").c_str()) !=0)
-        WARN("Could not remove temporary file '%s'.",
-            (file_base_name + ".geo").c_str());
-    if (std::remove((file_base_name + ".msh").c_str()) !=0)
-        WARN("Could not remove temporary file '%s'.",
-            (file_base_name + ".msh").c_str());
+    if (!(fs::remove(geo_file) && fs::remove(msh_file)))
+        WARN("Could not remove temporary files in createSurface.");
 
     // convert the surface mesh into a geometric surface
     if (!MeshLib::convertMeshToGeo(*surface_mesh, geometries,
diff --git a/web/data/versions.json b/web/data/versions.json
index 7f89598362b..7ccf5fa60e0 100644
--- a/web/data/versions.json
+++ b/web/data/versions.json
@@ -1,6 +1,6 @@
 {
   "minimum_version": {
-    "gcc": "7.3",
+    "gcc": "8.0",
     "clang": "3.5",
     "msvc": {
       "year": "2017",
-- 
GitLab