From af39491c04a7d80ca42376cd97ecae6663dadd1b Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Thu, 26 May 2016 21:43:16 +0100 Subject: [PATCH] fileName: Added recursive directory search function //- Recursively search the given directory for the file // returning the path relative to the directory or // fileName::null if not found fileName search(const word& file, const fileName& directory); --- .../primitives/strings/fileName/fileName.C | 29 +++++++++++++++++++ .../primitives/strings/fileName/fileName.H | 6 ++++ 2 files changed, 35 insertions(+) diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index becb5db393..dfbe2d0de7 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -396,4 +396,33 @@ Foam::fileName Foam::operator/(const string& a, const string& b) } +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // + +Foam::fileName Foam::search(const word& file, const fileName& directory) +{ + // Search the current directory for the file + fileNameList files(readDir(directory)); + forAll(files, i) + { + if (files[i] == file) + { + return directory/file; + } + } + + // If not found search each of the sub-directories + fileNameList dirs(readDir(directory, fileName::DIRECTORY)); + forAll(dirs, i) + { + fileName path = search(file, directory/dirs[i]); + if (path != fileName::null) + { + return path; + } + } + + return fileName::null; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index ee0d751fae..7b0c160bda 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -242,6 +242,12 @@ public: fileName operator/(const string&, const string&); +//- Recursively search the given directory for the file +// returning the path relative to the directory or +// fileName::null if not found +fileName search(const word& file, const fileName& directory); + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam