From dffb74672c04a02517babf644e962f7ad21f0a60 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Thu, 23 Apr 2020 20:14:56 +0200
Subject: [PATCH] [BL] Refactored copyPathToFileName().

---
 BaseLib/FileTools.cpp                         | 15 ++++++
 BaseLib/FileTools.h                           |  7 +++
 .../TestFilePathStringManipulation.cpp        | 46 +++++++++++++++++++
 3 files changed, 68 insertions(+)
 create mode 100644 Tests/BaseLib/TestFilePathStringManipulation.cpp

diff --git a/BaseLib/FileTools.cpp b/BaseLib/FileTools.cpp
index f8f541a048b..afb53b5fc9d 100644
--- a/BaseLib/FileTools.cpp
+++ b/BaseLib/FileTools.cpp
@@ -86,6 +86,21 @@ bool hasFileExtension(std::string const& extension, std::string const& filename)
     return boost::iequals(extension, getFileExtension(filename));
 }
 
+std::string copyPathToFileName(const std::string &file_name,
+                               const std::string &source)
+{
+    auto filePath = fs::path(file_name);
+    if(filePath.has_parent_path())
+    {
+        return filePath.string();
+    }
+    else
+    {
+        return fs::path(source) /= filePath;
+    }
+
+}
+
 std::string extractPath(std::string const& pathname)
 {
     return fs::path(pathname).parent_path();
diff --git a/BaseLib/FileTools.h b/BaseLib/FileTools.h
index 01168d254bc..daf67284e39 100644
--- a/BaseLib/FileTools.h
+++ b/BaseLib/FileTools.h
@@ -136,6 +136,13 @@ std::string getFileExtension(std::string const& path);
 bool hasFileExtension(std::string const& extension,
                       std::string const& filename);
 
+/**
+ * Checks if file_name already contains a qualified path and if not copies the
+ * path from source.
+ */
+std::string copyPathToFileName(const std::string &file_name,
+                               const std::string &source);
+
 /** Returns a string with file extension as found by getFileExtension()
  * dropped.
  */
diff --git a/Tests/BaseLib/TestFilePathStringManipulation.cpp b/Tests/BaseLib/TestFilePathStringManipulation.cpp
new file mode 100644
index 00000000000..d1014f57d9c
--- /dev/null
+++ b/Tests/BaseLib/TestFilePathStringManipulation.cpp
@@ -0,0 +1,46 @@
+/**
+ * \file
+ * \author
+ * \date
+ * \brief
+ *
+ * \copyright
+ * Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.org/project/license
+ */
+
+#include "gtest/gtest.h"
+
+#include "BaseLib/FileTools.h"
+
+#ifdef WIN32
+TEST(BaseLib, CopyPathToFileNameWin)
+{
+    ASSERT_EQ("extend\\file", BaseLib::copyPathToFileName("file", "extend"));
+    ASSERT_EQ("path\\file",
+              BaseLib::copyPathToFileName("path\\file", "extend"));
+    ASSERT_EQ("extend\\file", BaseLib::copyPathToFileName("file", "extend\\"));
+    ASSERT_EQ("path\\file",
+              BaseLib::copyPathToFileName("path\\file", "extend\\"));
+    ASSERT_EQ("extend\\smth\\file",
+              BaseLib::copyPathToFileName("file", "extend\\smth"));
+    ASSERT_EQ("path\\file",
+              BaseLib::copyPathToFileName("path\\file", "extend\\smth"));
+}
+#else
+TEST(BaseLib, CopyPathToFileNameUnix)
+{
+    ASSERT_EQ("extend/file", BaseLib::copyPathToFileName("file", "extend"));
+    ASSERT_EQ("path/file",
+              BaseLib::copyPathToFileName("path/file", "extend"));
+    ASSERT_EQ("extend/file", BaseLib::copyPathToFileName("file", "extend/"));
+    ASSERT_EQ("path/file", BaseLib::copyPathToFileName("path/file", "extend/"));
+
+    ASSERT_EQ("extend/smth/file",
+              BaseLib::copyPathToFileName("file", "extend/smth"));
+    ASSERT_EQ("path/file",
+              BaseLib::copyPathToFileName("path/file", "extend/smth"));
+}
+#endif
-- 
GitLab