diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 188ee0f2d1ab373c2a43ccedd89c0b6f564e6b75..68e92ecb74da891e785efbce07dae63ab8b2650b 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -79,36 +79,7 @@ namespace Foam // * * * * * * * * * * * * * * Static Functions * * * * * * * * * * * * * * // -// //! \cond fileScope -// -// Return true if filename appears to be a backup file -// -static inline bool isBackupName(const Foam::fileName& name) -{ - if (name.empty()) - { - return false; - } - else if (name.back() == '~') - { - return true; - } - - // Now check the extension - const Foam::word ext = name.ext(); - if (ext.empty()) - { - return false; - } - - return - ( - ext == "bak" || ext == "BAK" - || ext == "old" || ext == "save" - ); -} - // Like fileName "/" global operator, but retain any invalid characters static inline Foam::fileName fileNameConcat @@ -793,7 +764,7 @@ Foam::fileNameList Foam::readDir else if ( (type == fileName::DIRECTORY) - || (type == fileName::FILE && !isBackupName(name)) + || (type == fileName::FILE && !fileName::isBackup(name)) ) { if ((directory/name).type(followLink) == type) diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index ceba5881162d2103a20ddb94dffd50ecc8709ba4..70e4fc5b8ab8a994085c0449d69be5cdf611cff5 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -122,6 +122,40 @@ bool Foam::fileName::equals(const std::string& s1, const std::string& s2) } +bool Foam::fileName::isBackup(const std::string& str) +{ + if (str.empty()) + { + return false; + } + else if (str.back() == '~') + { + return true; + } + + // Now check the extension + const auto dot = find_ext(str); + + if (dot == npos) + { + return false; + } + + const std::string ending = str.substr(dot+1, npos); + + if (ending.empty()) + { + return false; + } + + return + ( + ending == "bak" || ending == "BAK" + || ending == "old" || ending == "save" + ); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::fileName::fileName(const UList<word>& lst) diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index 8d8d7999b97f90d8c488a24332b3fbde3d7c91bc..f368126216e371309f1513d10b87b5d0acd00f3b 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -83,10 +83,10 @@ public: //- Enumerations to handle file types and modes. enum Type { - UNDEFINED, - FILE, - DIRECTORY, - LINK + UNDEFINED, //!< Undefined file type. + FILE, //!< A file + DIRECTORY, //!< A directory + LINK //!< A symlink }; @@ -190,8 +190,8 @@ public: // Interrogation - //- Return the file type: FILE, DIRECTORY, UNDEFINED or - // LINK (only if followLink=false) + //- Return the file type: FILE, DIRECTORY, LINK or UNDEFINED. + // LINK is only returned if followLink=false Type type(const bool followLink = true) const; //- Return true if string starts with a '/' @@ -203,6 +203,12 @@ public: //- Convert from relative to absolute fileName& toAbsolute(); + //- Return true if string ends with "~", ".bak", ".old", ".save" + static bool isBackup(const std::string& str); + + //- Return true if file name ends with "~", ".bak", ".old", ".save" + inline bool isBackup() const; + // Decomposition diff --git a/src/OpenFOAM/primitives/strings/fileName/fileNameI.H b/src/OpenFOAM/primitives/strings/fileName/fileNameI.H index c3c82cc65d0a619501947ce4d3521a3f2648889a..f626a87d383094732db45f35bfe823dce928d35a 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileNameI.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileNameI.H @@ -128,6 +128,12 @@ inline bool Foam::fileName::isAbsolute() const } +inline bool Foam::fileName::isBackup() const +{ + return isBackup(*this); +} + + inline bool Foam::fileName::hasExt() const { return string::hasExt(); diff --git a/src/OpenFOAM/primitives/strings/string/string.C b/src/OpenFOAM/primitives/strings/string/string.C index ac8beb6f187b66a62d5989400bfadce900fa435e..b95a00086264502e238897b6a4d9c9ca31c36402 100644 --- a/src/OpenFOAM/primitives/strings/string/string.C +++ b/src/OpenFOAM/primitives/strings/string/string.C @@ -39,16 +39,14 @@ const Foam::string Foam::string::null; Foam::word Foam::string::ext() const { - const size_type i = find_ext(); + const auto i = find_ext(); if (i == npos) { return word::null; } - else - { - return substr(i+1, npos); - } + + return substr(i+1, npos); } @@ -68,7 +66,7 @@ bool Foam::string::ext(const Foam::word& ending) bool Foam::string::hasExt(const word& ending) const { - size_type i = find_ext(); + auto i = find_ext(); if (i == npos) { return false; @@ -86,7 +84,7 @@ bool Foam::string::hasExt(const word& ending) const bool Foam::string::hasExt(const wordRe& ending) const { - const size_type i = find_ext(); + const auto i = find_ext(); if (i == npos) { return false; diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index ee5aee9b9c6e7434702a497c74345c549393d308..0b42048412f372d6334ceeebd11101ef11a657b1 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.H @@ -84,7 +84,11 @@ protected: //- Find position of a file extension dot, return npos on failure. // A wrapped version of find_last_of("./") with additional logic. - inline size_type find_ext() const; + inline static std::string::size_type find_ext(const std::string& str); + + //- Find position of a file extension dot, return npos on failure. + // A wrapped version of find_last_of("./") with additional logic. + inline std::string::size_type find_ext() const; //- Return file name extension (part after last .) word ext() const; diff --git a/src/OpenFOAM/primitives/strings/string/stringI.H b/src/OpenFOAM/primitives/strings/string/stringI.H index 7d584d2244aa2e23eca49c8c4703cf6085abf7c7..8e11f78bc67ef0fd5d2bf3dd37e0f1340479c4e9 100644 --- a/src/OpenFOAM/primitives/strings/string/stringI.H +++ b/src/OpenFOAM/primitives/strings/string/stringI.H @@ -26,18 +26,22 @@ License // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // -inline std::string::size_type Foam::string::find_ext() const +inline std::string::size_type Foam::string::find_ext(const std::string& str) { - const size_type i = find_last_of("./"); + const auto i = str.find_last_of("./"); - if (i == npos || i == 0 || operator[](i) == '/') + if (i == npos || i == 0 || str[i] == '/') { return npos; } - else - { - return i; - } + + return i; +} + + +inline std::string::size_type Foam::string::find_ext() const +{ + return find_ext(*this); } @@ -49,17 +53,15 @@ inline bool Foam::string::hasExt() const inline bool Foam::string::removeExt() { - const size_type i = find_ext(); + const auto i = find_ext(); if (i == npos) { return false; } - else - { - this->resize(i); - return true; - } + + this->resize(i); + return true; }