mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-12-28 03:37:59 +00:00
implement fileName::clean() method
* remove repeated slashes
/abc////def --> /abc/def
* remove '/./'
/abc/def/./ghi/. --> /abc/def/./ghi
abc/def/./ --> abc/def
* remove '/../'
/abc/def/../ghi/jkl/nmo/.. --> /abc/ghi/jkl
abc/../def/ghi/../jkl --> abc/../def/jkl
* remove trailing '/'
This commit is contained in:
@ -40,137 +40,19 @@ Description
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
//
|
||||
// * remove repeated slashes
|
||||
// /abc////def --> /abc/def
|
||||
//
|
||||
// * remove '/./'
|
||||
// /abc/def/./ghi/. --> /abc/def/./ghi
|
||||
// abc/def/./ --> abc/def
|
||||
//
|
||||
// * remove '/../'
|
||||
// /abc/def/../ghi/jkl/nmo/.. --> /abc/ghi/jkl
|
||||
// abc/../def/ghi/../jkl --> abc/../def/jkl
|
||||
//
|
||||
// * remove trailing '/'
|
||||
//
|
||||
bool fileNameCleanThis(fileName& This)
|
||||
{
|
||||
// the top slash - we are never allowed to descend below this one
|
||||
register string::size_type top = This.find('/');
|
||||
|
||||
// no slashes - nothing to do
|
||||
if (top == string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// start with the '/' found:
|
||||
register char prev = '/';
|
||||
register string::size_type nChar = top+1;
|
||||
register string::size_type maxLen = This.size();
|
||||
|
||||
for
|
||||
(
|
||||
register string::size_type src = nChar;
|
||||
src < maxLen;
|
||||
/*nil*/
|
||||
)
|
||||
{
|
||||
register char c = This[src++];
|
||||
|
||||
if (prev == '/')
|
||||
{
|
||||
// repeated '/' - skip it
|
||||
if (c == '/')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// could be '/./' or '/../'
|
||||
if (c == '.')
|
||||
{
|
||||
// found trailing '/.' - skip it
|
||||
if (src >= maxLen)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// peek at the next character
|
||||
register char c1 = This[src];
|
||||
|
||||
// found '/./' - skip it
|
||||
if (c1 == '/')
|
||||
{
|
||||
src++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// it is '/..' or '/../'
|
||||
if (c1 == '.' && (src+1 >= maxLen || This[src+1] == '/'))
|
||||
{
|
||||
// find a candidate for the parent directory
|
||||
// minimum of 3 characters: '/x/../'
|
||||
string::size_type parent =
|
||||
(
|
||||
nChar > 2
|
||||
? This.rfind('/', nChar-2)
|
||||
: string::npos
|
||||
);
|
||||
|
||||
|
||||
// strip parent directory, provided that it isn't below
|
||||
// the current top edit point
|
||||
if (parent != string::npos && parent >= top)
|
||||
{
|
||||
nChar = parent + 1;
|
||||
src += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
// bad resolution, eg 'abc/../../'
|
||||
// retain it, but move the top to avoid it being
|
||||
// considered a valid parent later
|
||||
top += 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
This[nChar++] = prev = c;
|
||||
}
|
||||
|
||||
// remove trailing slash
|
||||
if (nChar > 1 && This[nChar-1] == '/')
|
||||
{
|
||||
nChar--;
|
||||
}
|
||||
|
||||
This.resize(nChar);
|
||||
|
||||
return (nChar != maxLen);
|
||||
}
|
||||
|
||||
|
||||
fileName fileNameClean(fileName& This)
|
||||
{
|
||||
fileName fName(This);
|
||||
fileNameCleanThis(fName);
|
||||
|
||||
return fName;
|
||||
}
|
||||
|
||||
|
||||
void printCleaning(fileName& pathName)
|
||||
{
|
||||
Info<< "fileName = " << pathName << nl
|
||||
<< " path() = " << pathName.path() << nl
|
||||
<< " name() = " << pathName.name() << nl << nl;
|
||||
<< " name() = " << pathName.name() << nl
|
||||
<< " joined = " << pathName.path()/pathName.name() << nl << nl;
|
||||
|
||||
fileNameCleanThis(pathName);
|
||||
pathName.clean();
|
||||
|
||||
Info<< "cleaned = " << pathName << nl
|
||||
<< " path() = " << pathName.path() << nl
|
||||
<< " name() = " << pathName.name() << nl << nl;
|
||||
<< " name() = " << pathName.name() << nl
|
||||
<< " joined = " << pathName.path()/pathName.name() << nl << nl;
|
||||
|
||||
IOobject::writeDivider(Info);
|
||||
}
|
||||
@ -195,7 +77,14 @@ int main(int argc, char *argv[])
|
||||
if (args.optionFound("case"))
|
||||
{
|
||||
fileName pathName = args.option("case");
|
||||
Info<< "-case\n";
|
||||
Info<< nl
|
||||
<< "-case" << nl
|
||||
<< "path = " << args.path() << nl
|
||||
<< "root = " << args.rootPath() << nl
|
||||
<< "case = " << args.caseName() << nl
|
||||
<< "FOAM_CASE=" << getEnv("FOAM_CASE") << nl
|
||||
<< "FOAM_CASENAME=" << getEnv("FOAM_CASENAME") << nl
|
||||
<< endl;
|
||||
|
||||
printCleaning(pathName);
|
||||
}
|
||||
@ -206,7 +95,6 @@ int main(int argc, char *argv[])
|
||||
printCleaning(pathName);
|
||||
}
|
||||
|
||||
|
||||
Info<< "\nEnd" << endl;
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user