From fe6a83a3a8c84f4078ff3d0e7a8be803d73440d3 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 30 Jan 2009 09:06:47 +0100 Subject: [PATCH] moved fileName::IOobjectComponents -> IOobject::fileNameComponents --- applications/test/fileName/fileNameTest.C | 14 +-- src/OpenFOAM/db/IOobject/IOobject.C | 89 ++++++++++++++++++- src/OpenFOAM/db/IOobject/IOobject.H | 16 +++- .../primitives/strings/fileName/fileName.C | 88 ------------------ .../primitives/strings/fileName/fileName.H | 9 -- 5 files changed, 111 insertions(+), 105 deletions(-) diff --git a/applications/test/fileName/fileNameTest.C b/applications/test/fileName/fileNameTest.C index f5a4a93542..c4791f6f38 100644 --- a/applications/test/fileName/fileNameTest.C +++ b/applications/test/fileName/fileNameTest.C @@ -32,6 +32,7 @@ Description #include "fileName.H" #include "SubList.H" +#include "IOobject.H" #include "IOstreams.H" #include "OSspecific.H" @@ -61,7 +62,8 @@ int main() << endl; // try with different combination - for (label start = 0; start < wrdList.size(); ++start) + // The final one should emit warnings + for (label start = 0; start <= wrdList.size(); ++start) { fileName instance, local; word name; @@ -69,26 +71,28 @@ int main() fileName path(SubList(wrdList, wrdList.size()-start, start)); fileName path2 = "." / path; - path.IOobjectComponents + IOobject::fileNameComponents ( + path, instance, local, name ); - Info<< "IOobjectComponents for " << path << nl + Info<< "IOobject::fileNameComponents for " << path << nl << " instance = " << instance << nl << " local = " << local << nl << " name = " << name << endl; - path2.IOobjectComponents + IOobject::fileNameComponents ( + path2, instance, local, name ); - Info<< "IOobjectComponents for " << path2 << nl + Info<< "IOobject::fileNameComponents for " << path2 << nl << " instance = " << instance << nl << " local = " << local << nl << " name = " << name << endl; diff --git a/src/OpenFOAM/db/IOobject/IOobject.C b/src/OpenFOAM/db/IOobject/IOobject.C index fc1c023874..1883f773b9 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.C +++ b/src/OpenFOAM/db/IOobject/IOobject.C @@ -35,6 +35,85 @@ namespace Foam defineTypeNameAndDebug(IOobject, 0); } +// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // + +// 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::IOobject::IOobject::fileNameComponents +( + const fileName& path, + fileName& instance, + fileName& local, + word& name +) +{ + instance.clear(); + local.clear(); + name.clear(); + + // called with directory + if (::Foam::dir(path)) + { + WarningIn("IOobject::fileNameComponents(const fileName&, ...)") + << " called with directory: " << path << "\n"; + return false; + } + + string::size_type first = path.find('/'); + + if (first == 0) + { + // called with absolute path + WarningIn("IOobject::fileNameComponents(const fileName&, ...)") + << "called with absolute path: " << path << "\n"; + return false; + } + + if (first == string::npos) + { + // no '/' found - no instance or local + + // check afterwards + name.string::operator=(path); + } + else + { + instance = path.substr(0, first); + + string::size_type last = path.rfind('/'); + if (last > first) + { + // with local + local = path.substr(first+1, last-first-1); + } + + // check afterwards + name.string::operator=(path.substr(last+1)); + } + + + // check for valid (and stripped) name, regardless of the debug level + if (name.empty() || string::stripInvalid(name)) + { + WarningIn("IOobject::fileNameComponents(const fileName&, ...)") + << "has invalid word for name: \"" << name + << "\"\nwhile processing path: " << path << "\n"; + return false; + } + + return true; +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::IOobject::IOobject @@ -118,7 +197,15 @@ Foam::IOobject::IOobject registerObject_(registerObject), objState_(GOOD) { - path.IOobjectComponents(instance_, local_, name_); + if (!fileNameComponents(path, instance_, local_, name_)) + { + FatalErrorIn + ( + "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)" + ) + << " invalid path specification\n" + << exit(FatalError); + } if (objectRegistry::debug) { diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H index 6ee171423a..5559a2295a 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.H +++ b/src/OpenFOAM/db/IOobject/IOobject.H @@ -149,7 +149,6 @@ private: //- IOobject state objectState objState_; - protected: // Protected member functions @@ -168,6 +167,18 @@ public: TypeName("IOobject"); + // Static Member Functions + + //- Split path into instance, local, name components + static bool fileNameComponents + ( + const fileName& path, + fileName& instance, + fileName& local, + word& name + ); + + // Constructors //- Construct from name, instance, registry, io options @@ -194,6 +205,7 @@ public: ); //- Construct from path, registry, io options + // Uses fileNameComponents() to split path into components. IOobject ( const fileName& path, @@ -215,7 +227,7 @@ public: virtual ~IOobject(); - // Member functions + // Member Functions // General access diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index 3f41e9e692..50d8a29947 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -212,94 +212,6 @@ Foam::word Foam::fileName::component } - -// 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 -{ - 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 0eeb89312e..a8312df3f6 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -163,15 +163,6 @@ public: //- Return a single component of the path word component(const size_type, const char delimiter='/') const; - //- Return path as instance, local, name components for IOobject - bool IOobjectComponents - ( - fileName& instance, - fileName& local, - word& name - ) const; - - // Member operators // Assignment