ENH: additional fileName methods

- add an extension to the file name
   - remove a file extension
   - check if a file name has an extension
   - check if a file name has a particular extension (as word),
     or matches a particular grouping of extensions (as wordRe).
This commit is contained in:
Mark Olesen
2016-12-18 20:40:03 +01:00
parent b08dadff62
commit e379720053
5 changed files with 270 additions and 27 deletions

View File

@ -37,6 +37,7 @@ Description
#include "IOstreams.H"
#include "OSspecific.H"
#include "POSIX.H"
#include "Switch.H"
#include "etcFiles.H"
using namespace Foam;
@ -47,6 +48,7 @@ using namespace Foam;
int main(int argc, char *argv[])
{
argList::noParallel();
argList::addBoolOption("ext", "test handing of file extensions");
argList::addBoolOption("construct", "test constructors");
argList::addBoolOption("default", "reinstate default tests");
argList::addNote("runs default tests or specified ones only");
@ -108,6 +110,131 @@ int main(int argc, char *argv[])
}
// Test various ext() methods
if (args.optionFound("ext"))
{
Info<<nl << nl << "handling of fileName extension" << nl;
fileName empty;
fileName endWithDot("some.path/name.");
fileName endWithSlash("some.path/");
fileName input0("some.file/with.out/extension");
fileName input1("path.to/media/image.png");
Info<<"File : " << input0 << " ext: "
<< Switch(input0.hasExt())
<< " = " << input0.ext() << nl;
Info<<"File : " << input1 << " ext: "
<< Switch(input1.hasExt())
<< " = " << input1.ext() << nl;
Info<<"File : " << endWithDot << " ext: "
<< Switch(endWithDot.hasExt())
<< " = " << endWithDot.ext() << " <-- perhaps return false?" << nl;
Info<<"File : " << endWithSlash << " ext: "
<< Switch(endWithSlash.hasExt())
<< " = " << endWithSlash.ext() << nl;
Info<<"Remove extension " << (input0.removeExt());
Info<< " now: " << input0 << nl;
Info<<"Remove extension " << (input1.removeExt());
Info<< " now: " << input1 << nl;
Info<<"Remove extension " << (endWithSlash.removeExt());
Info<< " now: " << endWithSlash << nl;
wordList exts{ "jpg", "png", "txt", word::null };
Info<<"Add extension(s): " << input1 << nl;
for (const word& e : exts)
{
Info<<"<" << e << "> -> " << input1.ext(e) << nl;
}
Info<< nl;
Info<<"Test hasExt(word)" << nl
<<"~~~~~~~~~~~~~~~~~" << nl;
Info<<"Has extension(s):" << nl
<< "input: " << input1 << nl;
for (const word& e : exts)
{
Info<<" '" << e << "' -> "
<< Switch(input1.hasExt(e)) << nl;
}
Info<< nl;
Info<<"Has extension(s):" << nl
<< "input: " << endWithDot << nl;
for (const word& e : exts)
{
Info<<" '" << e << "' -> "
<< Switch(endWithDot.hasExt(e)) << nl;
}
Info<< nl;
Info<<"Test hasExt(wordRe)" << nl
<<"~~~~~~~~~~~~~~~~~~~" << nl;
// A regex with a zero length matcher doesn't work at all:
// eg "(png|jpg|txt|)" regex matcher itself
wordRe matcher0("()", wordRe::REGEXP);
wordRe matcher1("(png|jpg|txt)", wordRe::REGEXP);
wordRe matcher2("(png|txt)", wordRe::REGEXP);
Info<<"Has extension(s):" << nl
<< "input: " << endWithDot << nl;
Info<<" " << matcher0 << " -> "
<< Switch(endWithDot.hasExt(matcher0)) << nl;
Info<<" " << matcher1 << " -> "
<< Switch(endWithDot.hasExt(matcher1)) << nl;
Info<<" " << matcher2 << " -> "
<< Switch(endWithDot.hasExt(matcher2)) << nl;
Info<< "input: " << input1 << nl;
Info<<" " << matcher0 << " -> "
<< Switch(input1.hasExt(matcher0)) << nl;
Info<<" " << matcher1 << " -> "
<< Switch(input1.hasExt(matcher1)) << nl;
Info<<" " << matcher2 << " -> "
<< Switch(input1.hasExt(matcher2)) << nl;
Info<< nl;
Info<<"Remove extension(s):" << nl << "input: " << input1 << nl;
while (!input1.empty())
{
if (input1.removeExt())
{
Info<< " -> " << input1 << nl;
}
else
{
Info<< "stop> " << input1 << nl;
break;
}
}
Info<< nl;
input0.clear();
Info<<"test with zero-sized: " << input0 << nl;
Info<<"add extension: " << input0.ext("abc") << nl;
Info<< nl;
input0 = "this/";
Info<<"test add after slash: " << input0 << nl;
Info<<"add extension: " << input0.ext("abc")
<< " <-- avoids accidentally creating hidden files" << nl;
Info<< nl;
input0 = "this.file.";
Info<<"test after dot: " << input0 << nl;
Info<<"add extension: " << input0.ext("abc")
<< " <-- No check for repeated dots (user error!)" << nl;
Info<< nl;
}
if (!defaultTests)
{
return 0;