From 97da787c69ba59003e8eaececc528733537bd3bd Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 3 Mar 2011 13:48:04 +0100 Subject: [PATCH] ENH: add fileName::name(bool) for returning basename without extension --- applications/test/fileName/Test-fileName.C | 13 ++--- .../primitives/strings/fileName/fileName.C | 52 ++++++++++++++++--- .../primitives/strings/fileName/fileName.H | 5 +- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/applications/test/fileName/Test-fileName.C b/applications/test/fileName/Test-fileName.C index 5bcb5fe845..797055b60e 100644 --- a/applications/test/fileName/Test-fileName.C +++ b/applications/test/fileName/Test-fileName.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -22,10 +22,10 @@ License along with OpenFOAM. If not, see . Application - fileName + Test-fileName Description - + Test some basic fileName functionality \*---------------------------------------------------------------------------*/ @@ -52,9 +52,10 @@ int main() fileName pathName(wrdList); Info<< "pathName = " << pathName << nl - << "pathName.name() = " << pathName.name() << nl - << "pathName.path() = " << pathName.path() << nl - << "pathName.ext() = " << pathName.ext() << endl; + << "pathName.name() = >" << pathName.name() << "<\n" + << "pathName.path() = " << pathName.path() << nl + << "pathName.ext() = >" << pathName.ext() << "<\n" + << "pathName.name(true) = >" << pathName.name(true) << "<\n"; Info<< "pathName.components() = " << pathName.components() << nl << "pathName.component(2) = " << pathName.component(2) << nl diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index a7c5f499cc..ee534bdb1b 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -204,6 +204,42 @@ Foam::word Foam::fileName::name() const } +Foam::word Foam::fileName::name(const bool noExt) const +{ + if (noExt) + { + size_type beg = rfind('/'); + if (beg == npos) + { + beg = 0; + } + else + { + ++beg; + } + + size_type dot = rfind('.'); + if (dot != npos && dot <= beg) + { + dot = npos; + } + + if (dot == npos) + { + return substr(beg, npos); + } + else + { + return substr(beg, dot - beg); + } + } + else + { + return this->name(); + } +} + + // Return directory path name (part before last /) // // behaviour compared to /usr/bin/dirname: @@ -283,22 +319,22 @@ Foam::wordList Foam::fileName::components(const char delimiter) const { DynamicList wrdList(20); - size_type start=0, end=0; + size_type beg=0, end=0; - while ((end = find(delimiter, start)) != npos) + while ((end = find(delimiter, beg)) != npos) { // avoid empty element (caused by doubled slashes) - if (start < end) + if (beg < end) { - wrdList.append(substr(start, end-start)); + wrdList.append(substr(beg, end-beg)); } - start = end + 1; + beg = end + 1; } // avoid empty trailing element - if (start < size()) + if (beg < size()) { - wrdList.append(substr(start, npos)); + wrdList.append(substr(beg, npos)); } // transfer to wordList diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index df5945be99..bdfad0dc14 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -153,6 +153,9 @@ public: //- Return file name (part beyond last /) word name() const; + //- Return file name, optionally without extension + word name(const bool noExt) const; + //- Return directory path name (part before last /) fileName path() const;