FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "IOobject.H"
00027 #include <OpenFOAM/Time.H>
00028 #include <OpenFOAM/IFstream.H>
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 defineTypeNameAndDebug(Foam::IOobject, 0);
00033 
00034 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
00035 
00036 // Return components following the IOobject requirements
00037 //
00038 // behaviour
00039 //    input               IOobject(instance, local, name)
00040 //    -----               ------
00041 //    "foo"               ("", "", "foo")
00042 //    "foo/bar"           ("foo", "", "bar")
00043 //    "/XXX"              ERROR - no absolute path
00044 //    "foo/bar/"          ERROR - no name
00045 //    "foo/xxx/bar"       ("foo", "xxx", "bar")
00046 //    "foo/xxx/yyy/bar"   ("foo", "xxx/yyy", "bar")
00047 bool Foam::IOobject::IOobject::fileNameComponents
00048 (
00049     const fileName& path,
00050     fileName& instance,
00051     fileName& local,
00052     word& name
00053 )
00054 {
00055     instance.clear();
00056     local.clear();
00057     name.clear();
00058 
00059     // called with directory
00060     if (isDir(path))
00061     {
00062         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
00063             << " called with directory: " << path << "\n";
00064         return false;
00065     }
00066 
00067     string::size_type first = path.find('/');
00068 
00069     if (first == 0)
00070     {
00071         // called with absolute path
00072         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
00073             << "called with absolute path: " << path << "\n";
00074         return false;
00075     }
00076 
00077     if (first == string::npos)
00078     {
00079         // no '/' found - no instance or local
00080 
00081         // check afterwards
00082         name.string::operator=(path);
00083     }
00084     else
00085     {
00086         instance = path.substr(0, first);
00087 
00088         string::size_type last = path.rfind('/');
00089         if (last > first)
00090         {
00091             // with local
00092             local = path.substr(first+1, last-first-1);
00093         }
00094 
00095         // check afterwards
00096         name.string::operator=(path.substr(last+1));
00097     }
00098 
00099 
00100     // check for valid (and stripped) name, regardless of the debug level
00101     if (name.empty() || string::stripInvalid<word>(name))
00102     {
00103         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
00104             << "has invalid word for name: \"" << name
00105             << "\"\nwhile processing path: " << path << "\n";
00106         return false;
00107     }
00108 
00109     return true;
00110 }
00111 
00112 
00113 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00114 
00115 Foam::IOobject::IOobject
00116 (
00117     const word& name,
00118     const fileName& instance,
00119     const objectRegistry& registry,
00120     readOption ro,
00121     writeOption wo,
00122     bool registerObject
00123 )
00124 :
00125     name_(name),
00126     headerClassName_(typeName),
00127     note_(),
00128     instance_(instance),
00129     local_(),
00130     db_(registry),
00131     rOpt_(ro),
00132     wOpt_(wo),
00133     registerObject_(registerObject),
00134     objState_(GOOD)
00135 {
00136     if (objectRegistry::debug)
00137     {
00138         Info<< "Constructing IOobject called " << name_
00139             << " of type " << headerClassName_
00140             << endl;
00141     }
00142 }
00143 
00144 
00145 Foam::IOobject::IOobject
00146 (
00147     const word& name,
00148     const fileName& instance,
00149     const fileName& local,
00150     const objectRegistry& registry,
00151     readOption ro,
00152     writeOption wo,
00153     bool registerObject
00154 )
00155 :
00156     name_(name),
00157     headerClassName_(typeName),
00158     note_(),
00159     instance_(instance),
00160     local_(local),
00161     db_(registry),
00162     rOpt_(ro),
00163     wOpt_(wo),
00164     registerObject_(registerObject),
00165     objState_(GOOD)
00166 {
00167     if (objectRegistry::debug)
00168     {
00169         Info<< "Constructing IOobject called " << name_
00170             << " of type " << headerClassName_
00171             << endl;
00172     }
00173 }
00174 
00175 
00176 Foam::IOobject::IOobject
00177 (
00178     const fileName& path,
00179     const objectRegistry& registry,
00180     readOption ro,
00181     writeOption wo,
00182     bool registerObject
00183 )
00184 :
00185     name_(),
00186     headerClassName_(typeName),
00187     note_(),
00188     instance_(),
00189     local_(),
00190     db_(registry),
00191     rOpt_(ro),
00192     wOpt_(wo),
00193     registerObject_(registerObject),
00194     objState_(GOOD)
00195 {
00196     if (!fileNameComponents(path, instance_, local_, name_))
00197     {
00198         FatalErrorIn
00199         (
00200             "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
00201         )
00202             << " invalid path specification\n"
00203             << exit(FatalError);
00204     }
00205 
00206     if (objectRegistry::debug)
00207     {
00208         Info<< "Constructing IOobject called " << name_
00209             << " of type " << headerClassName_
00210             << endl;
00211     }
00212 }
00213 
00214 
00215 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
00216 
00217 Foam::IOobject::~IOobject()
00218 {}
00219 
00220 
00221 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00222 
00223 const Foam::objectRegistry& Foam::IOobject::db() const
00224 {
00225     return db_;
00226 }
00227 
00228 
00229 const Foam::Time& Foam::IOobject::time() const
00230 {
00231     return db_.time();
00232 }
00233 
00234 
00235 const Foam::fileName& Foam::IOobject::caseName() const
00236 {
00237     return time().caseName();
00238 }
00239 
00240 
00241 const Foam::fileName& Foam::IOobject::rootPath() const
00242 {
00243     return time().rootPath();
00244 }
00245 
00246 
00247 Foam::fileName Foam::IOobject::path() const
00248 {
00249     return rootPath()/caseName()/instance()/db_.dbDir()/local();
00250 }
00251 
00252 
00253 Foam::fileName Foam::IOobject::path
00254 (
00255     const word& instance,
00256     const fileName& local
00257 ) const
00258 {
00259     return rootPath()/caseName()/instance/db_.dbDir()/local;
00260 }
00261 
00262 
00263 Foam::fileName Foam::IOobject::filePath() const
00264 {
00265     fileName path = this->path();
00266     fileName objectPath = path/name();
00267 
00268     if (isFile(objectPath))
00269     {
00270         return objectPath;
00271     }
00272     else
00273     {
00274         if
00275         (
00276             time().processorCase()
00277          && (
00278                 instance() == time().system()
00279              || instance() == time().constant()
00280             )
00281         )
00282         {
00283             fileName parentObjectPath =
00284                 rootPath()/caseName()
00285                /".."/instance()/db_.dbDir()/local()/name();
00286 
00287             if (isFile(parentObjectPath))
00288             {
00289                 return parentObjectPath;
00290             }
00291         }
00292 
00293         if (!isDir(path))
00294         {
00295             word newInstancePath = time().findInstancePath(instant(instance()));
00296 
00297             if (newInstancePath.size())
00298             {
00299                 fileName fName
00300                 (
00301                     rootPath()/caseName()
00302                    /newInstancePath/db_.dbDir()/local()/name()
00303                 );
00304 
00305                 if (isFile(fName))
00306                 {
00307                     return fName;
00308                 }
00309             }
00310         }
00311     }
00312 
00313     return fileName::null;
00314 }
00315 
00316 
00317 Foam::Istream* Foam::IOobject::objectStream()
00318 {
00319     fileName fName = filePath();
00320 
00321     if (fName.size())
00322     {
00323         IFstream* isPtr = new IFstream(fName);
00324 
00325         if (isPtr->good())
00326         {
00327             return isPtr;
00328         }
00329         else
00330         {
00331             delete isPtr;
00332             return NULL;
00333         }
00334     }
00335     else
00336     {
00337         return NULL;
00338     }
00339 }
00340 
00341 
00342 bool Foam::IOobject::headerOk()
00343 {
00344     bool ok = true;
00345 
00346     Istream* isPtr = objectStream();
00347 
00348     // If the stream has failed return
00349     if (!isPtr)
00350     {
00351         if (objectRegistry::debug)
00352         {
00353             Info
00354                 << "IOobject::headerOk() : "
00355                 << "file " << objectPath() << " could not be opened"
00356                 << endl;
00357         }
00358 
00359         ok = false;
00360     }
00361     else
00362     {
00363         // Try reading header
00364         if (!readHeader(*isPtr))
00365         {
00366             if (objectRegistry::debug)
00367             {
00368                 IOWarningIn("IOobject::headerOk()", (*isPtr))
00369                     << "failed to read header of file " << objectPath()
00370                     << endl;
00371             }
00372 
00373             ok = false;
00374         }
00375     }
00376 
00377     delete isPtr;
00378 
00379     return ok;
00380 }
00381 
00382 
00383 void Foam::IOobject::setBad(const string& s)
00384 {
00385     if (objState_ != GOOD)
00386     {
00387         FatalErrorIn("IOobject::setBad(const string&)")
00388             << "recurrent failure for object " << s
00389             << exit(FatalError);
00390     }
00391 
00392     if (error::level)
00393     {
00394         Info<< "IOobject::setBad(const string&) : "
00395             << "broken object " << s << info() << endl;
00396     }
00397 
00398     objState_ = BAD;
00399 }
00400 
00401 const char* Foam::IOobject::getBannerString(bool noHint)
00402 {
00403     static bool bannerSet = false;
00404     static char spaces[50];
00405 
00406     static string bannerWithHint;
00407     static string bannerNoHint;
00408 
00409     if (!bannerSet)
00410     {
00411         memset(spaces, ' ', 50);
00412         size_t len = strlen(Foam::FOAMfullVersion);
00413         if (len < 48)
00414         {
00415             spaces[48 - len] = '\0';
00416         }
00417         else
00418         {
00419             spaces[0] = '\0';
00420         }
00421         bannerWithHint =
00422         "|               ______                _     ____          __  __              |\n"
00423         "|              |  ____|             _| |_  / __ \\   /\\   |  \\/  |             |\n"
00424         "|              | |__ _ __ ___  ___ /     \\| |  | | /  \\  | \\  / |             |\n"
00425         "|              |  __| '__/ _ \\/ _ ( (| |) ) |  | |/ /\\ \\ | |\\/| |             |\n"
00426         "|              | |  | | |  __/  __/\\_   _/| |__| / ____ \\| |  | |             |\n"
00427         "|              |_|  |_|  \\___|\\___|  |_|   \\____/_/    \\_\\_|  |_|             |\n"
00428         "|                                                                             |\n"
00429         "|                   FreeFOAM: The Cross-Platform CFD Toolkit                  |\n"
00430         "|                   Version:  " + string(FOAMfullVersion) + spaces + "|\n"
00431         "|                   Web:      http://freefoam.sourceforge.net                 |\n"
00432         "\\*---------------------------------------------------------------------------*/\n";
00433         bannerNoHint =
00434             "/*---------------------------------------------------------------------------*\\\n"
00435             + bannerWithHint;
00436         bannerWithHint =
00437             "/*-------------------*- FOAMDict -*-- vim: set ft=foamdict: -----------------*\\\n"
00438             + bannerWithHint;
00439         bannerSet = true;
00440     }
00441 
00442     if (noHint)
00443     {
00444         return bannerNoHint.c_str();
00445     }
00446     else
00447     {
00448         return bannerWithHint.c_str();
00449     }
00450 
00451 }
00452 
00453 
00454 void Foam::IOobject::operator=(const IOobject& io)
00455 {
00456     name_ = io.name_;
00457     headerClassName_ = io.headerClassName_;
00458     note_ = io.note_;
00459     instance_ = io.instance_;
00460     local_ = io.local_;
00461     rOpt_ = io.rOpt_;
00462     wOpt_ = io.wOpt_;
00463     objState_ = io.objState_;
00464 }
00465 
00466 
00467 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines