fileName gets additional convenience methods

- exists() = forward to OSspecific exists(...)
- isDir()  = forward to OSspecific dir(...)
- isFile() = forward to OSspecific file(...)
- IOobjectComponents() - split into instance, local, name following rules
  set out for IOobject.

- added IOobject(path, registry, ...) constructor that uses
  fileName::IOobjectComponents(). This hides the complexity we otherwise need.
This commit is contained in:
Mark Olesen
2009-01-27 13:28:45 +01:00
parent 295d503d83
commit defe13e205
5 changed files with 225 additions and 31 deletions

View File

@ -31,6 +31,7 @@ Description
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "fileName.H" #include "fileName.H"
#include "SubList.H"
#include "IOstreams.H" #include "IOstreams.H"
#include "OSspecific.H" #include "OSspecific.H"
@ -50,19 +51,55 @@ int main()
fileName pathName(wrdList); fileName pathName(wrdList);
Info<< "pathName = " << pathName << endl; Info<< "pathName = " << pathName << nl
Info<< "pathName.name() = " << pathName.name() << endl; << "pathName.name() = " << pathName.name() << nl
Info<< "pathName.path() = " << pathName.path() << endl; << "pathName.path() = " << pathName.path() << nl
Info<< "pathName.ext() = " << pathName.ext() << endl; << "pathName.ext() = " << pathName.ext() << endl;
Info<< "pathName.components() = " << pathName.components() << endl; Info<< "pathName.components() = " << pathName.components() << nl
Info<< "pathName.component(2) = " << pathName.component(2) << endl; << "pathName.component(2) = " << pathName.component(2) << nl
<< endl;
// try with different combination
for (label start = 0; start < wrdList.size(); ++start)
{
fileName instance, local;
word name;
fileName path(SubList<word>(wrdList, wrdList.size()-start, start));
fileName path2 = "." / path;
path.IOobjectComponents
(
instance,
local,
name
);
Info<< "IOobjectComponents for " << path << nl
<< " instance = " << instance << nl
<< " local = " << local << nl
<< " name = " << name << endl;
path2.IOobjectComponents
(
instance,
local,
name
);
Info<< "IOobjectComponents for " << path2 << nl
<< " instance = " << instance << nl
<< " local = " << local << nl
<< " name = " << name << endl;
}
// test findEtcFile // test findEtcFile
Info<< "\n\nfindEtcFile tests:" << nl Info<< "\n\nfindEtcFile tests:" << nl
<< " 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<< "This should emit a fatal error:" << endl;
Info<< " badName(die) => " << findEtcFile("badName", true) << nl Info<< " badName(die) => " << findEtcFile("badName", true) << nl
<< endl; << endl;

View File

@ -98,6 +98,37 @@ Foam::IOobject::IOobject
} }
Foam::IOobject::IOobject
(
const fileName& path,
const objectRegistry& registry,
readOption ro,
writeOption wo,
bool registerObject
)
:
name_(),
headerClassName_(typeName),
note_(),
instance_(),
local_(),
db_(registry),
rOpt_(ro),
wOpt_(wo),
registerObject_(registerObject),
objState_(GOOD)
{
path.IOobjectComponents(instance_, local_, name_);
if (objectRegistry::debug)
{
Info<< "Constructing IOobject called " << name_
<< " of type " << headerClassName_
<< endl;
}
}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
Foam::IOobject::~IOobject() Foam::IOobject::~IOobject()

View File

@ -193,6 +193,16 @@ public:
bool registerObject=true bool registerObject=true
); );
//- Construct from path, registry, io options
IOobject
(
const fileName& path,
const objectRegistry& registry,
readOption r=NO_READ,
writeOption w=NO_WRITE,
bool registerObject=true
);
//- Clone //- Clone
Foam::autoPtr<IOobject> clone() const Foam::autoPtr<IOobject> clone() const
{ {

View File

@ -26,6 +26,7 @@ License
#include "fileName.H" #include "fileName.H"
#include "wordList.H" #include "wordList.H"
#include "DynamicList.H"
#include "debug.H" #include "debug.H"
#include "OSspecific.H" #include "OSspecific.H"
@ -48,6 +49,30 @@ Foam::fileName::fileName(const wordList& lst)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::fileName::Type Foam::fileName::type() const
{
return ::Foam::type(*this);
}
bool Foam::fileName::exists() const
{
return ::Foam::exists(*this);
}
bool Foam::fileName::isDir() const
{
return ::Foam::dir(*this);
}
bool Foam::fileName::isFile() const
{
return ::Foam::file(*this);
}
// Return file name (part beyond last /) // Return file name (part beyond last /)
// //
// behaviour compared to /usr/bin/basename: // behaviour compared to /usr/bin/basename:
@ -93,13 +118,13 @@ Foam::fileName Foam::fileName::path() const
{ {
return "."; return ".";
} }
else if (i == 0) else if (i)
{ {
return "/"; return substr(0, i);
} }
else else
{ {
return substr(0, i); return "/";
} }
} }
@ -145,28 +170,22 @@ Foam::word Foam::fileName::ext() const
// ----- ------ // ----- ------
// "foo" 1("foo") // "foo" 1("foo")
// "/foo" 1("foo") // "/foo" 1("foo")
// "foo/bar" 2("foo", "foo") // "foo/bar" 2("foo", "bar")
// "/foo/bar" 2("foo", "foo") // "/foo/bar" 2("foo", "bar")
// "/foo/bar/" 2("foo", "bar") // "/foo/bar/" 2("foo", "bar")
// //
Foam::wordList Foam::fileName::components(const char delimiter) const Foam::wordList Foam::fileName::components(const char delimiter) const
{ {
wordList wrdList(20); DynamicList<word> wrdList(20);
size_type start=0, end=0; size_type start=0, end=0;
label nWords=0;
while ((end = find(delimiter, start)) != npos) while ((end = find(delimiter, start)) != npos)
{ {
// avoid empty element (caused by doubled slashes) // avoid empty element (caused by doubled slashes)
if (start < end) if (start < end)
{ {
wrdList[nWords++] = substr(start, end-start); wrdList.append(substr(start, end-start));
if (nWords == wrdList.size())
{
wrdList.setSize(2*wrdList.size());
}
} }
start = end + 1; start = end + 1;
} }
@ -174,12 +193,11 @@ Foam::wordList Foam::fileName::components(const char delimiter) const
// avoid empty trailing element // avoid empty trailing element
if (start < size()) if (start < size())
{ {
wrdList[nWords++] = substr(start, npos); wrdList.append(substr(start, npos));
} }
wrdList.setSize(nWords); // transfer to wordList
return wordList(wrdList.xfer());
return wrdList;
} }
@ -194,12 +212,94 @@ Foam::word Foam::fileName::component
} }
Foam::fileName::Type Foam::fileName::type() const
// Return components following the IOobject requirements
//
// behaviour
// input IOobject(instance, local, name)
// ----- ------
// "foo" ("", "", "foo")
// "foo/bar" ("foo", "", "bar")
// "/XXX" ERROR - no absolute path
// "foo/bar/" ERROR - no name
// "foo/xxx/bar" ("foo", "xxx", "bar")
// "foo/xxx/yyy/bar" ("foo", "xxx/yyy", "bar")
bool Foam::fileName::IOobjectComponents
(
fileName& instance,
fileName& local,
word& name
)
const
{ {
return ::Foam::type(*this); instance.clear();
local.clear();
name.clear();
// called with directory
if (::Foam::dir(*this))
{
std::cerr
<< "fileName::IOobjectComponents() called with directory: "
<< this->c_str() << std::endl;
std::abort();
return false;
}
size_type first = find('/');
if (first == 0)
{
// called with absolute path
std::cerr
<< "fileName::IOobjectComponents() called with absolute path: "
<< this->c_str() << std::endl;
std::abort();
return false;
}
if (first == npos)
{
// no '/' found - no instance or local
// check afterwards
name.string::operator=(*this);
}
else
{
instance = substr(0, first);
size_type last = rfind('/');
if (last > first)
{
// with local
local = substr(first+1, last-first-1);
}
// check afterwards
name.string::operator=(substr(last+1));
}
// check for valid (and stripped) name, regardless of the debug level
if (name.empty() || string::stripInvalid<word>(name))
{
std::cerr
<< "fileName::IOobjectComponents() has invalid word for name: "
<< name.c_str() << "\nwhile processing "
<< this->c_str() << std::endl;
std::abort();
return false;
}
return true;
} }
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
void Foam::fileName::operator=(const fileName& str) void Foam::fileName::operator=(const fileName& str)

View File

@ -129,6 +129,20 @@ public:
inline static bool valid(char); inline static bool valid(char);
// Interogation
//- Return the file type: FILE, DIRECTORY or UNDEFINED
Type type() const;
//- Does it exist (as FILE or DIRECTORY) in the file system?
bool exists() const;
//- Does it exist as DIRECTORY in the file system?
bool isDir() const;
//- Does it exist as FILE in the file system?
bool isFile() const;
// Decomposition // Decomposition
//- Return file name (part beyond last /) //- Return file name (part beyond last /)
@ -146,14 +160,16 @@ public:
//- Return path components as wordList //- Return path components as wordList
wordList components(const char delimiter='/') const; wordList components(const char delimiter='/') const;
//- Return a component of the path //- Return a single component of the path
word component(const size_type, const char delimiter='/') const; word component(const size_type, const char delimiter='/') const;
//- Return path as instance, local, name components for IOobject
// Interogation bool IOobjectComponents
(
//- Return file type fileName& instance,
Type type() const; fileName& local,
word& name
) const;
// Member operators // Member operators