mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
STYLE: update fileName docs and minor code cleanup
This commit is contained in:
@ -47,6 +47,38 @@ using namespace Foam;
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
unsigned testClean(std::initializer_list<Pair<std::string>> tests)
|
||||||
|
{
|
||||||
|
unsigned nFail = 0;
|
||||||
|
|
||||||
|
for (const Pair<std::string>& test : tests)
|
||||||
|
{
|
||||||
|
const std::string& input = test.first();
|
||||||
|
const std::string& expected = test.second();
|
||||||
|
|
||||||
|
fileName cleaned(test.first());
|
||||||
|
cleaned.clean();
|
||||||
|
|
||||||
|
if (cleaned == expected)
|
||||||
|
{
|
||||||
|
Info<< "(pass)"
|
||||||
|
<< " clean " << input << " -> " << cleaned << nl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Info<< "(fail)"
|
||||||
|
<< " clean " << input << " -> " << cleaned
|
||||||
|
<< " expected=" << expected
|
||||||
|
<< nl;
|
||||||
|
|
||||||
|
++nFail;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nFail;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned testStrip
|
unsigned testStrip
|
||||||
(
|
(
|
||||||
const bool doClean,
|
const bool doClean,
|
||||||
@ -184,6 +216,16 @@ unsigned testRelative(std::initializer_list<Pair<std::string>> tests)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void testDirname(const fileName& input)
|
||||||
|
{
|
||||||
|
Info<< "input:" << input
|
||||||
|
<< " path:" << input.path()
|
||||||
|
<< " name:\"" << input.name() << '"'
|
||||||
|
<< " ext:\"" << input.ext() << '"'
|
||||||
|
<< " components: " << flatOutput(input.components()) << nl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
// Main program:
|
// Main program:
|
||||||
|
|
||||||
@ -196,6 +238,8 @@ int main(int argc, char *argv[])
|
|||||||
argList::addBoolOption("relative", "test relative operations");
|
argList::addBoolOption("relative", "test relative operations");
|
||||||
argList::addBoolOption("system", "test filesystem operations");
|
argList::addBoolOption("system", "test filesystem operations");
|
||||||
argList::addBoolOption("default", "reinstate default tests");
|
argList::addBoolOption("default", "reinstate default tests");
|
||||||
|
argList::addBoolOption("clean", "clean()");
|
||||||
|
argList::addBoolOption("dirname", "basename/dirname tables");
|
||||||
argList::addNote("runs default tests or specified ones only");
|
argList::addNote("runs default tests or specified ones only");
|
||||||
|
|
||||||
#include "setRootCase.H"
|
#include "setRootCase.H"
|
||||||
@ -254,6 +298,20 @@ int main(int argc, char *argv[])
|
|||||||
Info<< "All ==> " << file4 << nl;
|
Info<< "All ==> " << file4 << nl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (args.found("dirname"))
|
||||||
|
{
|
||||||
|
testDirname("");
|
||||||
|
testDirname(".");
|
||||||
|
testDirname("abc");
|
||||||
|
testDirname("/");
|
||||||
|
testDirname("/abc");
|
||||||
|
testDirname("abc/def");
|
||||||
|
testDirname("/abc/def");
|
||||||
|
testDirname("/abc/def/");
|
||||||
|
testDirname("/abc///def///");
|
||||||
|
testDirname("/abc/../def");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Test various ext() methods
|
// Test various ext() methods
|
||||||
if (args.found("ext"))
|
if (args.found("ext"))
|
||||||
@ -381,6 +439,35 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (args.found("clean"))
|
||||||
|
{
|
||||||
|
Info<< nl << "Test fileName::clean()" << nl << nl;
|
||||||
|
|
||||||
|
unsigned nFail = testClean
|
||||||
|
({
|
||||||
|
{ "/", "/" },
|
||||||
|
{ "/abc/", "/abc" },
|
||||||
|
{ "/abc////def", "/abc/def" },
|
||||||
|
{ "/abc/def/./ghi/.", "/abc/def/ghi" },
|
||||||
|
{ "abc/def/./", "abc/def" },
|
||||||
|
{ "./abc/", "./abc" },
|
||||||
|
{ "/abc/def/../ghi/jkl/nmo/..", "/abc/ghi/jkl" },
|
||||||
|
{ "abc/../def/ghi/../jkl", "abc/../def/jkl" },
|
||||||
|
});
|
||||||
|
|
||||||
|
Info<< nl;
|
||||||
|
if (nFail)
|
||||||
|
{
|
||||||
|
Info<< "failed " << nFail;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Info<< "passed all";
|
||||||
|
}
|
||||||
|
Info<< " fileName::clean tests" << nl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (args.found("validate"))
|
if (args.found("validate"))
|
||||||
{
|
{
|
||||||
unsigned nFail = 0;
|
unsigned nFail = 0;
|
||||||
@ -677,9 +764,27 @@ int main(int argc, char *argv[])
|
|||||||
<< " controlDict => " << findEtcFile("controlDict") << nl
|
<< " controlDict => " << findEtcFile("controlDict") << nl
|
||||||
<< " badName => " << findEtcFile("badName") << endl;
|
<< " badName => " << findEtcFile("badName") << endl;
|
||||||
|
|
||||||
Info<< "This should emit a fatal error:" << endl;
|
{
|
||||||
Info<< " badName(die) => " << findEtcFile("badName", true) << nl
|
|
||||||
<< endl;
|
Info<< nl << "Expect a FatalError for findEtcFile() with a bad name:"
|
||||||
|
<< nl;
|
||||||
|
|
||||||
|
const bool throwingError = FatalError.throwExceptions();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Info<< " badName(die) => " << flush
|
||||||
|
<< findEtcFile("<very-badName>", true) << nl
|
||||||
|
<< endl;
|
||||||
|
}
|
||||||
|
catch (Foam::error& err)
|
||||||
|
{
|
||||||
|
Info<< nl << "findEtcFile() Caught FatalError "
|
||||||
|
<< err << nl << endl;
|
||||||
|
}
|
||||||
|
FatalError.throwExceptions(throwingError);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Info<< "\nEnd\n" << endl;
|
Info<< "\nEnd\n" << endl;
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -735,10 +735,9 @@ void Foam::argList::setCasePaths()
|
|||||||
caseDir = cwd();
|
caseDir = cwd();
|
||||||
options_.erase("case");
|
options_.erase("case");
|
||||||
}
|
}
|
||||||
else if (!caseDir.isAbsolute())
|
else
|
||||||
{
|
{
|
||||||
caseDir = cwd()/caseDir;
|
caseDir.toAbsolute();
|
||||||
caseDir.clean();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -1126,7 +1125,7 @@ void Foam::argList::parse
|
|||||||
source = options_["decomposeParDict"];
|
source = options_["decomposeParDict"];
|
||||||
if (isDir(source))
|
if (isDir(source))
|
||||||
{
|
{
|
||||||
source = source/"decomposeParDict";
|
source /= "decomposeParDict";
|
||||||
adjustOpt = true;
|
adjustOpt = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -209,15 +209,14 @@ Foam::fileName::Type Foam::fileName::type(const bool followLink) const
|
|||||||
|
|
||||||
Foam::fileName& Foam::fileName::toAbsolute()
|
Foam::fileName& Foam::fileName::toAbsolute()
|
||||||
{
|
{
|
||||||
fileName& f = *this;
|
if (!isAbsolute(*this))
|
||||||
|
|
||||||
if (!f.isAbsolute())
|
|
||||||
{
|
{
|
||||||
|
fileName& f = *this;
|
||||||
f = cwd()/f;
|
f = cwd()/f;
|
||||||
f.clean();
|
f.clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
return f;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -482,9 +481,12 @@ Foam::fileName Foam::operator/(const string& a, const string& b)
|
|||||||
|
|
||||||
Foam::fileName Foam::search(const word& file, const fileName& directory)
|
Foam::fileName Foam::search(const word& file, const fileName& directory)
|
||||||
{
|
{
|
||||||
// Search the current directory for the file
|
// Search current directory for the file
|
||||||
fileNameList files(fileHandler().readDir(directory, fileName::FILE));
|
for
|
||||||
for (const fileName& item : files)
|
(
|
||||||
|
const fileName& item
|
||||||
|
: fileHandler().readDir(directory, fileName::FILE)
|
||||||
|
)
|
||||||
{
|
{
|
||||||
if (item == file)
|
if (item == file)
|
||||||
{
|
{
|
||||||
@ -493,17 +495,20 @@ Foam::fileName Foam::search(const word& file, const fileName& directory)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If not found search each of the sub-directories
|
// If not found search each of the sub-directories
|
||||||
fileNameList dirs(fileHandler().readDir(directory, fileName::DIRECTORY));
|
for
|
||||||
for (const fileName& item : dirs)
|
(
|
||||||
|
const fileName& item
|
||||||
|
: fileHandler().readDir(directory, fileName::DIRECTORY)
|
||||||
|
)
|
||||||
{
|
{
|
||||||
fileName path = search(file, directory/item);
|
fileName path = search(file, directory/item);
|
||||||
if (path != fileName::null)
|
if (!path.empty())
|
||||||
{
|
{
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileName::null;
|
return fileName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -157,7 +157,7 @@ public:
|
|||||||
);
|
);
|
||||||
|
|
||||||
//- This is a specialized (possibly slower) version of compare()
|
//- This is a specialized (possibly slower) version of compare()
|
||||||
// that ignores duplicate or trailing slashes.
|
//- that ignores duplicate or trailing slashes.
|
||||||
static bool equals(const std::string& s1, const std::string& s2);
|
static bool equals(const std::string& s1, const std::string& s2);
|
||||||
|
|
||||||
|
|
||||||
@ -229,14 +229,16 @@ public:
|
|||||||
//
|
//
|
||||||
// Behaviour compared to /usr/bin/basename:
|
// Behaviour compared to /usr/bin/basename:
|
||||||
// \verbatim
|
// \verbatim
|
||||||
// input name() basename
|
// input name() basename
|
||||||
// ----- ------ --------
|
// ----- ------ --------
|
||||||
// "foo" "foo" "foo"
|
// "" "" ""
|
||||||
// "/" "" "/"
|
// "abc" "abc" "abc"
|
||||||
// "/foo" "foo" "foo"
|
// "/" "" "/"
|
||||||
// "foo/bar" "bar" "bar"
|
// "/abc" "abc" "abc"
|
||||||
// "/foo/bar" "bar" "bar"
|
// "abc/def" "def" "def"
|
||||||
// "/foo/bar/" "" "bar"
|
// "/abc/def" "def" "def"
|
||||||
|
// "/abc/def/" "" "def"
|
||||||
|
// "/abc/../def" "def" "def"
|
||||||
// \endverbatim
|
// \endverbatim
|
||||||
inline static std::string name(const std::string& str);
|
inline static std::string name(const std::string& str);
|
||||||
|
|
||||||
@ -261,16 +263,18 @@ public:
|
|||||||
//- Return directory path name (part before last /)
|
//- Return directory path name (part before last /)
|
||||||
// The result normally coresponds to a Foam::fileName
|
// The result normally coresponds to a Foam::fileName
|
||||||
//
|
//
|
||||||
// Behaviour compared to /usr/bin/dirname:
|
// Behaviour compared to /usr/bin/dirname:
|
||||||
// \verbatim
|
// \verbatim
|
||||||
// input path() dirname
|
// input path() dirname
|
||||||
// ----- ------ -------
|
// ----- ------ -------
|
||||||
// "foo" "." "."
|
// "" "." "."
|
||||||
|
// "abc" "." "."
|
||||||
// "/" "/" "/"
|
// "/" "/" "/"
|
||||||
// "/foo" "/" "foo"
|
// "/abc" "/" "/"
|
||||||
// "foo/bar" "foo" "foo"
|
// "abc/def" "abc" "abc"
|
||||||
// "/foo/bar" "/foo" "/foo"
|
// "/abc/def" "/abc" "/abc"
|
||||||
// "/foo/bar/" "/foo/bar/" "/foo"
|
// "/abc/def/" "/abc/def" "/abc"
|
||||||
|
// "/abc/../def" "/abc/.." "/abc/.."
|
||||||
// \endverbatim
|
// \endverbatim
|
||||||
inline static std::string path(const std::string& str);
|
inline static std::string path(const std::string& str);
|
||||||
|
|
||||||
@ -312,16 +316,18 @@ public:
|
|||||||
|
|
||||||
//- Return path components as wordList
|
//- Return path components as wordList
|
||||||
//
|
//
|
||||||
// Behaviour:
|
// Behaviour:
|
||||||
// \verbatim
|
// \verbatim
|
||||||
// Input components()
|
// input components()
|
||||||
// ----- ------
|
// ----- ------------
|
||||||
// "foo" ("foo")
|
// "" ()
|
||||||
// "/foo" ("foo")
|
// "." (".")
|
||||||
// "foo/bar" ("foo", "bar")
|
// "abc" ("abc")
|
||||||
// "/foo/bar" ("foo", "bar")
|
// "/abc" ("abc")
|
||||||
// "/foo/bar/" ("foo", "bar")
|
// "abc/def" ("abc", "def")
|
||||||
// \endverbatim
|
// "/abc/def" ("abc", "def")
|
||||||
|
// "/abc/def/" ("abc", "def")
|
||||||
|
// \endverbatim
|
||||||
wordList components(const char delimiter = '/') const;
|
wordList components(const char delimiter = '/') const;
|
||||||
|
|
||||||
//- Return a single component of the path
|
//- Return a single component of the path
|
||||||
|
|||||||
@ -140,7 +140,7 @@ inline bool Foam::fileName::isAbsolute(const std::string& str)
|
|||||||
|
|
||||||
inline bool Foam::fileName::isAbsolute() const
|
inline bool Foam::fileName::isAbsolute() const
|
||||||
{
|
{
|
||||||
return !empty() && operator[](0) == '/';
|
return isAbsolute(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -46,21 +46,30 @@ Foam::word Foam::string::ext() const
|
|||||||
return word::null;
|
return word::null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return substr(i+1, npos);
|
return substr(i+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Foam::string::ext(const Foam::word& ending)
|
bool Foam::string::ext(const word& ending)
|
||||||
{
|
{
|
||||||
if (!ending.empty() && !empty() && operator[](size()-1) != '/')
|
if (ending.empty() || empty() || back() == '/')
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (ending[0] == '.')
|
||||||
|
{
|
||||||
|
if (ending.size() == 1)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
append(1u, '.');
|
append(1u, '.');
|
||||||
append(ending);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
append(ending);
|
||||||
|
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -90,7 +99,7 @@ bool Foam::string::hasExt(const wordRe& ending) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string end = substr(i+1, npos); // Compare *after* the dot
|
const std::string end = substr(i+1); // Compare *after* the dot
|
||||||
return ending.match(end);
|
return ending.match(end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user