From defe13e2054d57d8a395a1d8e807d29d871b9550 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 27 Jan 2009 13:28:45 +0100 Subject: [PATCH] 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. --- applications/test/fileName/fileNameTest.C | 49 ++++++- src/OpenFOAM/db/IOobject/IOobject.C | 31 ++++ src/OpenFOAM/db/IOobject/IOobject.H | 10 ++ .../primitives/strings/fileName/fileName.C | 138 +++++++++++++++--- .../primitives/strings/fileName/fileName.H | 28 +++- 5 files changed, 225 insertions(+), 31 deletions(-) diff --git a/applications/test/fileName/fileNameTest.C b/applications/test/fileName/fileNameTest.C index c48e4ba2fc..f5a4a93542 100644 --- a/applications/test/fileName/fileNameTest.C +++ b/applications/test/fileName/fileNameTest.C @@ -31,6 +31,7 @@ Description \*---------------------------------------------------------------------------*/ #include "fileName.H" +#include "SubList.H" #include "IOstreams.H" #include "OSspecific.H" @@ -50,19 +51,55 @@ int main() fileName pathName(wrdList); - Info<< "pathName = " << pathName << endl; - Info<< "pathName.name() = " << pathName.name() << endl; - Info<< "pathName.path() = " << pathName.path() << endl; - Info<< "pathName.ext() = " << pathName.ext() << endl; + Info<< "pathName = " << pathName << nl + << "pathName.name() = " << pathName.name() << nl + << "pathName.path() = " << pathName.path() << nl + << "pathName.ext() = " << pathName.ext() << endl; - Info<< "pathName.components() = " << pathName.components() << endl; - Info<< "pathName.component(2) = " << pathName.component(2) << endl; + Info<< "pathName.components() = " << pathName.components() << nl + << "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(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 Info<< "\n\nfindEtcFile tests:" << nl << " controlDict => " << findEtcFile("controlDict") << nl << " badName => " << findEtcFile("badName") << endl; + Info<< "This should emit a fatal error:" << endl; Info<< " badName(die) => " << findEtcFile("badName", true) << nl << endl; diff --git a/src/OpenFOAM/db/IOobject/IOobject.C b/src/OpenFOAM/db/IOobject/IOobject.C index aadd9c2464..fc1c023874 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.C +++ b/src/OpenFOAM/db/IOobject/IOobject.C @@ -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 * * * * * * * * * * * * * * * * // Foam::IOobject::~IOobject() diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H index 38c76a2b38..6ee171423a 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.H +++ b/src/OpenFOAM/db/IOobject/IOobject.H @@ -193,6 +193,16 @@ public: 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 Foam::autoPtr clone() const { diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index dbf0a664e8..3f41e9e692 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -26,6 +26,7 @@ License #include "fileName.H" #include "wordList.H" +#include "DynamicList.H" #include "debug.H" #include "OSspecific.H" @@ -48,6 +49,30 @@ Foam::fileName::fileName(const wordList& lst) // * * * * * * * * * * * * * * * 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 /) // // behaviour compared to /usr/bin/basename: @@ -93,13 +118,13 @@ Foam::fileName Foam::fileName::path() const { return "."; } - else if (i == 0) + else if (i) { - return "/"; + return substr(0, i); } else { - return substr(0, i); + return "/"; } } @@ -145,28 +170,22 @@ Foam::word Foam::fileName::ext() const // ----- ------ // "foo" 1("foo") // "/foo" 1("foo") -// "foo/bar" 2("foo", "foo") -// "/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 { - wordList wrdList(20); + DynamicList wrdList(20); size_type start=0, end=0; - label nWords=0; while ((end = find(delimiter, start)) != npos) { // avoid empty element (caused by doubled slashes) if (start < end) { - wrdList[nWords++] = substr(start, end-start); - - if (nWords == wrdList.size()) - { - wrdList.setSize(2*wrdList.size()); - } + wrdList.append(substr(start, end-start)); } start = end + 1; } @@ -174,12 +193,11 @@ Foam::wordList Foam::fileName::components(const char delimiter) const // avoid empty trailing element if (start < size()) { - wrdList[nWords++] = substr(start, npos); + wrdList.append(substr(start, npos)); } - wrdList.setSize(nWords); - - return wrdList; + // transfer to wordList + return wordList(wrdList.xfer()); } @@ -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(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 * * * * * * * * * * * * * // void Foam::fileName::operator=(const fileName& str) diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index 24dd213ef5..0eeb89312e 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -129,6 +129,20 @@ public: 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 //- Return file name (part beyond last /) @@ -146,14 +160,16 @@ public: //- Return path components as wordList 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; - - // Interogation - - //- Return file type - Type type() const; + //- Return path as instance, local, name components for IOobject + bool IOobjectComponents + ( + fileName& instance, + fileName& local, + word& name + ) const; // Member operators