ENH: add fileName::name(bool) for returning basename without extension

This commit is contained in:
Mark Olesen
2011-03-03 13:48:04 +01:00
parent 2fcc1db960
commit 97da787c69
3 changed files with 55 additions and 15 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -22,10 +22,10 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application Application
fileName Test-fileName
Description Description
Test some basic fileName functionality
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -52,9 +52,10 @@ int main()
fileName pathName(wrdList); fileName pathName(wrdList);
Info<< "pathName = " << pathName << nl Info<< "pathName = " << pathName << nl
<< "pathName.name() = " << pathName.name() << nl << "pathName.name() = >" << pathName.name() << "<\n"
<< "pathName.path() = " << pathName.path() << nl << "pathName.path() = " << pathName.path() << nl
<< "pathName.ext() = " << pathName.ext() << endl; << "pathName.ext() = >" << pathName.ext() << "<\n"
<< "pathName.name(true) = >" << pathName.name(true) << "<\n";
Info<< "pathName.components() = " << pathName.components() << nl Info<< "pathName.components() = " << pathName.components() << nl
<< "pathName.component(2) = " << pathName.component(2) << nl << "pathName.component(2) = " << pathName.component(2) << nl

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License 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 /) // Return directory path name (part before last /)
// //
// behaviour compared to /usr/bin/dirname: // behaviour compared to /usr/bin/dirname:
@ -283,22 +319,22 @@ Foam::wordList Foam::fileName::components(const char delimiter) const
{ {
DynamicList<word> wrdList(20); DynamicList<word> 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) // 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 // avoid empty trailing element
if (start < size()) if (beg < size())
{ {
wrdList.append(substr(start, npos)); wrdList.append(substr(beg, npos));
} }
// transfer to wordList // transfer to wordList

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -153,6 +153,9 @@ public:
//- Return file name (part beyond last /) //- Return file name (part beyond last /)
word name() const; word name() const;
//- Return file name, optionally without extension
word name(const bool noExt) const;
//- Return directory path name (part before last /) //- Return directory path name (part before last /)
fileName path() const; fileName path() const;