OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

IOobject.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "IOobject.H"
00028 #include "Time.H"
00029 #include "IFstream.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 defineTypeNameAndDebug(Foam::IOobject, 0);
00034 
00035 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
00036 
00037 // Return components following the IOobject requirements
00038 //
00039 // behaviour
00040 //    input               IOobject(instance, local, name)
00041 //    -----               ------
00042 //    "foo"               ("", "", "foo")
00043 //    "foo/bar"           ("foo", "", "bar")
00044 //    "/XXX"              ERROR - no absolute path
00045 //    "foo/bar/"          ERROR - no name
00046 //    "foo/xxx/bar"       ("foo", "xxx", "bar")
00047 //    "foo/xxx/yyy/bar"   ("foo", "xxx/yyy", "bar")
00048 bool Foam::IOobject::IOobject::fileNameComponents
00049 (
00050     const fileName& path,
00051     fileName& instance,
00052     fileName& local,
00053     word& name
00054 )
00055 {
00056     instance.clear();
00057     local.clear();
00058     name.clear();
00059 
00060     // called with directory
00061     if (isDir(path))
00062     {
00063         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
00064             << " called with directory: " << path << "\n";
00065         return false;
00066     }
00067 
00068     string::size_type first = path.find('/');
00069 
00070     if (first == 0)
00071     {
00072         // called with absolute path
00073         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
00074             << "called with absolute path: " << path << "\n";
00075         return false;
00076     }
00077 
00078     if (first == string::npos)
00079     {
00080         // no '/' found - no instance or local
00081 
00082         // check afterwards
00083         name.string::operator=(path);
00084     }
00085     else
00086     {
00087         instance = path.substr(0, first);
00088 
00089         string::size_type last = path.rfind('/');
00090         if (last > first)
00091         {
00092             // with local
00093             local = path.substr(first+1, last-first-1);
00094         }
00095 
00096         // check afterwards
00097         name.string::operator=(path.substr(last+1));
00098     }
00099 
00100 
00101     // check for valid (and stripped) name, regardless of the debug level
00102     if (name.empty() || string::stripInvalid<word>(name))
00103     {
00104         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
00105             << "has invalid word for name: \"" << name
00106             << "\"\nwhile processing path: " << path << "\n";
00107         return false;
00108     }
00109 
00110     return true;
00111 }
00112 
00113 
00114 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00115 
00116 Foam::IOobject::IOobject
00117 (
00118     const word& name,
00119     const fileName& instance,
00120     const objectRegistry& registry,
00121     readOption ro,
00122     writeOption wo,
00123     bool registerObject
00124 )
00125 :
00126     name_(name),
00127     headerClassName_(typeName),
00128     note_(),
00129     instance_(instance),
00130     local_(),
00131     db_(registry),
00132     rOpt_(ro),
00133     wOpt_(wo),
00134     registerObject_(registerObject),
00135     objState_(GOOD)
00136 {
00137     if (objectRegistry::debug)
00138     {
00139         Info<< "Constructing IOobject called " << name_
00140             << " of type " << headerClassName_
00141             << endl;
00142     }
00143 }
00144 
00145 
00146 Foam::IOobject::IOobject
00147 (
00148     const word& name,
00149     const fileName& instance,
00150     const fileName& local,
00151     const objectRegistry& registry,
00152     readOption ro,
00153     writeOption wo,
00154     bool registerObject
00155 )
00156 :
00157     name_(name),
00158     headerClassName_(typeName),
00159     note_(),
00160     instance_(instance),
00161     local_(local),
00162     db_(registry),
00163     rOpt_(ro),
00164     wOpt_(wo),
00165     registerObject_(registerObject),
00166     objState_(GOOD)
00167 {
00168     if (objectRegistry::debug)
00169     {
00170         Info<< "Constructing IOobject called " << name_
00171             << " of type " << headerClassName_
00172             << endl;
00173     }
00174 }
00175 
00176 
00177 Foam::IOobject::IOobject
00178 (
00179     const fileName& path,
00180     const objectRegistry& registry,
00181     readOption ro,
00182     writeOption wo,
00183     bool registerObject
00184 )
00185 :
00186     name_(),
00187     headerClassName_(typeName),
00188     note_(),
00189     instance_(),
00190     local_(),
00191     db_(registry),
00192     rOpt_(ro),
00193     wOpt_(wo),
00194     registerObject_(registerObject),
00195     objState_(GOOD)
00196 {
00197     if (!fileNameComponents(path, instance_, local_, name_))
00198     {
00199         FatalErrorIn
00200         (
00201             "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
00202         )
00203             << " invalid path specification\n"
00204             << exit(FatalError);
00205     }
00206 
00207     if (objectRegistry::debug)
00208     {
00209         Info<< "Constructing IOobject called " << name_
00210             << " of type " << headerClassName_
00211             << endl;
00212     }
00213 }
00214 
00215 
00216 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
00217 
00218 Foam::IOobject::~IOobject()
00219 {}
00220 
00221 
00222 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00223 
00224 const Foam::objectRegistry& Foam::IOobject::db() const
00225 {
00226     return db_;
00227 }
00228 
00229 
00230 const Foam::Time& Foam::IOobject::time() const
00231 {
00232     return db_.time();
00233 }
00234 
00235 
00236 const Foam::fileName& Foam::IOobject::caseName() const
00237 {
00238     return time().caseName();
00239 }
00240 
00241 
00242 const Foam::fileName& Foam::IOobject::rootPath() const
00243 {
00244     return time().rootPath();
00245 }
00246 
00247 
00248 Foam::fileName Foam::IOobject::path() const
00249 {
00250     return rootPath()/caseName()/instance()/db_.dbDir()/local();
00251 }
00252 
00253 
00254 Foam::fileName Foam::IOobject::path
00255 (
00256     const word& instance,
00257     const fileName& local
00258 ) const
00259 {
00260     return rootPath()/caseName()/instance/db_.dbDir()/local;
00261 }
00262 
00263 
00264 Foam::fileName Foam::IOobject::filePath() const
00265 {
00266     fileName path = this->path();
00267     fileName objectPath = path/name();
00268 
00269     if (isFile(objectPath))
00270     {
00271         return objectPath;
00272     }
00273     else
00274     {
00275         if
00276         (
00277             time().processorCase()
00278          && (
00279                 instance() == time().system()
00280              || instance() == time().constant()
00281             )
00282         )
00283         {
00284             fileName parentObjectPath =
00285                 rootPath()/caseName()
00286                /".."/instance()/db_.dbDir()/local()/name();
00287 
00288             if (isFile(parentObjectPath))
00289             {
00290                 return parentObjectPath;
00291             }
00292         }
00293 
00294         if (!isDir(path))
00295         {
00296             word newInstancePath = time().findInstancePath(instant(instance()));
00297 
00298             if (newInstancePath.size())
00299             {
00300                 fileName fName
00301                 (
00302                     rootPath()/caseName()
00303                    /newInstancePath/db_.dbDir()/local()/name()
00304                 );
00305 
00306                 if (isFile(fName))
00307                 {
00308                     return fName;
00309                 }
00310             }
00311         }
00312     }
00313 
00314     return fileName::null;
00315 }
00316 
00317 
00318 Foam::Istream* Foam::IOobject::objectStream()
00319 {
00320     fileName fName = filePath();
00321 
00322     if (fName.size())
00323     {
00324         IFstream* isPtr = new IFstream(fName);
00325 
00326         if (isPtr->good())
00327         {
00328             return isPtr;
00329         }
00330         else
00331         {
00332             delete isPtr;
00333             return NULL;
00334         }
00335     }
00336     else
00337     {
00338         return NULL;
00339     }
00340 }
00341 
00342 
00343 bool Foam::IOobject::headerOk()
00344 {
00345     bool ok = true;
00346 
00347     Istream* isPtr = objectStream();
00348 
00349     // If the stream has failed return
00350     if (!isPtr)
00351     {
00352         if (objectRegistry::debug)
00353         {
00354             Info
00355                 << "IOobject::headerOk() : "
00356                 << "file " << objectPath() << " could not be opened"
00357                 << endl;
00358         }
00359 
00360         ok = false;
00361     }
00362     else
00363     {
00364         // Try reading header
00365         if (!readHeader(*isPtr))
00366         {
00367             if (objectRegistry::debug)
00368             {
00369                 IOWarningIn("IOobject::headerOk()", (*isPtr))
00370                     << "failed to read header of file " << objectPath()
00371                     << endl;
00372             }
00373 
00374             ok = false;
00375         }
00376     }
00377 
00378     delete isPtr;
00379 
00380     return ok;
00381 }
00382 
00383 
00384 void Foam::IOobject::setBad(const string& s)
00385 {
00386     if (objState_ != GOOD)
00387     {
00388         FatalErrorIn("IOobject::setBad(const string&)")
00389             << "recurrent failure for object " << s
00390             << exit(FatalError);
00391     }
00392 
00393     if (error::level)
00394     {
00395         Info<< "IOobject::setBad(const string&) : "
00396             << "broken object " << s << info() << endl;
00397     }
00398 
00399     objState_ = BAD;
00400 }
00401 
00402 
00403 void Foam::IOobject::operator=(const IOobject& io)
00404 {
00405     name_ = io.name_;
00406     headerClassName_ = io.headerClassName_;
00407     note_ = io.note_;
00408     instance_ = io.instance_;
00409     local_ = io.local_;
00410     rOpt_ = io.rOpt_;
00411     wOpt_ = io.wOpt_;
00412     objState_ = io.objState_;
00413 }
00414 
00415 
00416 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd