From dd67b3386abb271d4a201e5f4a97c1d023ad82b7 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 27 Jul 2018 07:15:08 +0200
Subject: [PATCH] ENH: add fileName /= operator (similar to
 std::filesystem::path)

---
 src/OSspecific/POSIX/POSIX.C                  |  6 ++---
 .../primitives/strings/fileName/fileName.C    | 24 +++++++++++++++++++
 .../primitives/strings/fileName/fileName.H    | 15 ++++++++----
 3 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C
index 97ba294bdd9..1953935f3c7 100644
--- a/src/OSspecific/POSIX/POSIX.C
+++ b/src/OSspecific/POSIX/POSIX.C
@@ -824,7 +824,7 @@ bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink)
         // If dest is a directory, create the destination file name.
         if (destFile.type() == fileName::DIRECTORY)
         {
-            destFile = destFile/src.name();
+            destFile /= src.name();
         }
 
         // Make sure the destination directory exists.
@@ -864,7 +864,7 @@ bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink)
         // If dest is a directory, create the destination file name.
         if (destFile.type() == fileName::DIRECTORY)
         {
-            destFile = destFile/src.name();
+            destFile /= src.name();
         }
 
         // Make sure the destination directory exists.
@@ -880,7 +880,7 @@ bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink)
         // If dest is a directory, create the destination file name.
         if (destFile.type() == fileName::DIRECTORY)
         {
-            destFile = destFile/src.components().last();
+            destFile /= src.components().last();
         }
 
         // Make sure the destination directory exists.
diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C
index f77302f521b..009baae7284 100644
--- a/src/OpenFOAM/primitives/strings/fileName/fileName.C
+++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C
@@ -533,6 +533,30 @@ void Foam::fileName::operator=(const char* str)
 }
 
 
+Foam::fileName& Foam::fileName::operator/=(const string& other)
+{
+    fileName& s = *this;
+
+    if (s.size())
+    {
+        if (other.size())
+        {
+            // Two non-empty strings: can concatenate
+
+            s.append("/");
+            s.append(other);
+        }
+    }
+    else if (other.size())
+    {
+        // Or, if the first string is empty
+        s = other;
+    }
+
+    return *this;
+}
+
+
 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
 
 Foam::fileName Foam::operator/(const string& a, const string& b)
diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H
index 437afde91e4..9201a7b2125 100644
--- a/src/OpenFOAM/primitives/strings/fileName/fileName.H
+++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H
@@ -130,7 +130,7 @@ public:
         fileName(Istream& is);
 
 
-    // Member functions
+    // Member Functions
 
         //- Is this character valid for a fileName?
         inline static bool valid(char c);
@@ -188,7 +188,7 @@ public:
         fileName clean() const;
 
 
-      // Interrogation
+    // Interrogation
 
         //- Return the file type: FILE, DIRECTORY, LINK or UNDEFINED.
         //  LINK is only returned if followLink=false
@@ -210,7 +210,7 @@ public:
         inline bool isBackup() const;
 
 
-      // Decomposition
+    // Decomposition
 
         //- Return basename (part beyond last /), including its extension
         //  The result normally corresponds to a Foam::word
@@ -316,7 +316,7 @@ public:
 
     // Member operators
 
-      // Assignment
+    // Assignment
 
         //- Copy, no character validation required
         void operator=(const fileName& str);
@@ -334,6 +334,13 @@ public:
         void operator=(const char* str);
 
 
+    // Other operators
+
+        //- Append a path element with '/' separator.
+        //  No '/' separator is added if this or the argument are empty.
+        fileName& operator/=(const string& other);
+
+
     // IOstream operators
 
         friend Istream& operator>>(Istream& is, fileName& fn);
-- 
GitLab