From 922ea5667e7603d620fc308559c9a0875c086341 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 19 Jul 2017 12:09:43 +0200 Subject: [PATCH] BUG: IOobject interpretation of ./ in construct-from-fileName (closes #482) - Resolve ambiguity by using the following rules: 1) starts with '/' => absolute file-system path 2) starts with './' or '../' => file-system path relative to CWD 3) otherwise treat as relative to the case STYLE: allow write access to headerClassName --- src/OpenFOAM/db/IOobject/IOobject.C | 24 +++++++------------ src/OpenFOAM/db/IOobject/IOobject.H | 35 +++++++++++++++++++++------- src/OpenFOAM/db/IOobject/IOobjectI.H | 6 +++++ 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/OpenFOAM/db/IOobject/IOobject.C b/src/OpenFOAM/db/IOobject/IOobject.C index d9588c7012..5ec0b29cf0 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.C +++ b/src/OpenFOAM/db/IOobject/IOobject.C @@ -108,28 +108,22 @@ static inline bool isOutsideOfCase(const std::string& file) // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // -// Return components following the IOobject requirements -// -// behaviour -// input IOobject(instance, local, name) -// ----- ------ -// "foo" ("", "", "foo") -// "foo/bar" ("foo", "", "bar") -// "foo/bar/" ERROR - no name -// "foo/xxx/bar" ("foo", "xxx", "bar") -// "foo/xxx/yyy/bar" ("foo", "xxx/yyy", "bar") -// "/xxx/yyy/bar" ("/xxx/yyy", "", "bar") bool Foam::IOobject::fileNameComponents ( - const fileName& rawPath, + const fileName& path, fileName& instance, fileName& local, word& name ) { - // Re-interpret the path as a file-system path. - fileName path(rawPath); - path.toAbsolute(); + // Convert explicit relative file-system path to absolute file-system path. + if (path.startsWith("./") || path.startsWith("../")) + { + fileName absPath = cwd()/path; + absPath.clean(); + + return fileNameComponents(absPath, instance, local, name); + } instance.clear(); local.clear(); diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H index 3ec9b59a79..b41ef980f2 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.H +++ b/src/OpenFOAM/db/IOobject/IOobject.H @@ -174,15 +174,15 @@ protected: // Protected Member Functions //- Construct and return an IFstream for the object. - // The results is nullptr if the stream construction failed + // \Return nullptr if the stream construction failed Istream* objectStream(); - //- Construct and return an IFstream for the object given the - // exact file. The results is nullptr if the stream construction failed - Istream* objectStream(const fileName&); + //- Return an IFstream for the object given the exact file. + // \Return nullptr if the stream construction failed + Istream* objectStream(const fileName& fName); //- Set the object state to bad - void setBad(const string&); + void setBad(const string& s); public: @@ -200,6 +200,20 @@ public: // Static Member Functions //- Split path into instance, local, name components + // + // The splitting behaviour is as follows: + // \verbatim + // input | instance | local | name + // ----------- | ---------- | ----- | ---- + // a | | | a + // a/b | a | | b + // a/b/c/d | a | b/c | d + // /a/b/c | /a/b | | c + // ./a/b/c | PWD/a/b | | c + // ../a/b/c | PWD/../a/b | | c + // a/b/ | ERROR | | + // \endverbatim + // where PWD is the Foam::cwd() current working directory static bool fileNameComponents ( const fileName& path, @@ -240,9 +254,11 @@ public: ); //- Construct from path, registry, io options. - // Uses fileNameComponents() to split path into components. The - // path argument is regarded as a file system path, not a case-relative - // path. Any './' gets expanded to the current working directory. + // Uses fileNameComponents() to split path into components. + // A path that starts with a '/' is regarded as a file system path. + // Paths starting with either './' or '../' are relative to + // current working directory (and replaced with absolute equivalents). + // All other paths are considered to be relative to the case. IOobject ( const fileName& path, @@ -300,6 +316,9 @@ public: //- Return name of the class name read from header inline const word& headerClassName() const; + //- Return non-constant access to the class name read from header + inline word& headerClassName(); + //- Return the optional note inline const string& note() const; diff --git a/src/OpenFOAM/db/IOobject/IOobjectI.H b/src/OpenFOAM/db/IOobject/IOobjectI.H index adce4e7fdc..3a97ffabf9 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectI.H +++ b/src/OpenFOAM/db/IOobject/IOobjectI.H @@ -55,6 +55,12 @@ inline const Foam::word& Foam::IOobject::headerClassName() const } +inline Foam::word& Foam::IOobject::headerClassName() +{ + return headerClassName_; +} + + inline const Foam::string& Foam::IOobject::note() const { return note_;