mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add fileName::name(bool) for returning basename without extension
This commit is contained in:
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
||||
|
||||
@ -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<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)
|
||||
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
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user