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

regIOobjectRead.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 "regIOobject.H"
00027 #include <OpenFOAM/IFstream.H>
00028 #include <OpenFOAM/Time.H>
00029 #include <OpenFOAM/PstreamReduceOps.H>
00030 
00031 
00032 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00033 
00034 Foam::Istream& Foam::regIOobject::readStream()
00035 {
00036     if (IFstream::debug)
00037     {
00038         Info<< "regIOobject::readStream() : "
00039             << "reading object " << name()
00040             << " from file " << objectPath()
00041             << endl;
00042     }
00043 
00044     if (readOpt() == NO_READ)
00045     {
00046         FatalErrorIn("regIOobject::readStream()")
00047             << "NO_READ specified for read-constructor of object " << name()
00048             << " of class " << headerClassName()
00049             << abort(FatalError);
00050     }
00051 
00052     // Construct object stream and read header if not already constructed
00053     if (!isPtr_)
00054     {
00055         if (!(isPtr_ = objectStream()))
00056         {
00057             FatalIOError
00058             (
00059                 "regIOobject::readStream()",
00060                 __FILE__,
00061                 __LINE__,
00062                 objectPath(),
00063                 0
00064             )   << "cannot open file"
00065                 << exit(FatalIOError);
00066         }
00067         else if (!readHeader(*isPtr_))
00068         {
00069             FatalIOErrorIn("regIOobject::readStream()", *isPtr_)
00070                 << "problem while reading header for object " << name()
00071                 << exit(FatalIOError);
00072         }
00073     }
00074 
00075     if (!lastModified_)
00076     {
00077         lastModified_ = lastModified(filePath());
00078     }
00079 
00080     return *isPtr_;
00081 }
00082 
00083 
00084 Foam::Istream& Foam::regIOobject::readStream(const word& expectName)
00085 {
00086     if (IFstream::debug)
00087     {
00088         Info<< "regIOobject::readStream(const word&) : "
00089             << "reading object " << name()
00090             << " from file " << objectPath()
00091             << endl;
00092     }
00093 
00094     // Construct IFstream if not already constructed
00095     if (!isPtr_)
00096     {
00097         readStream();
00098 
00099         // Check the className of the regIOobject
00100         // dictionary is an allowable name in case the actual class
00101         // instantiated is a dictionary
00102         if
00103         (
00104             expectName.size()
00105          && headerClassName() != expectName
00106          && headerClassName() != "dictionary"
00107         )
00108         {
00109             FatalIOErrorIn("regIOobject::readStream(const word&)", *isPtr_)
00110                 << "unexpected class name " << headerClassName()
00111                 << " expected " << expectName << endl
00112                 << "    while reading object " << name()
00113                 << exit(FatalIOError);
00114         }
00115     }
00116 
00117     return *isPtr_;
00118 }
00119 
00120 
00121 void Foam::regIOobject::close()
00122 {
00123     if (IFstream::debug)
00124     {
00125         Info<< "regIOobject::close() : "
00126             << "finished reading " << filePath()
00127             << endl;
00128     }
00129 
00130     if (isPtr_)
00131     {
00132         delete isPtr_;
00133         isPtr_ = NULL;
00134     }
00135 }
00136 
00137 
00138 bool Foam::regIOobject::readData(Istream&)
00139 {
00140     return false;
00141 }
00142 
00143 
00144 bool Foam::regIOobject::read()
00145 {
00146     bool ok = readData(readStream(type()));
00147     close();
00148     return ok;
00149 }
00150 
00151 
00152 bool Foam::regIOobject::modified() const
00153 {
00154     return
00155     (
00156         lastModified_
00157      && lastModified(filePath()) > (lastModified_ + fileModificationSkew)
00158     );
00159 }
00160 
00161 
00162 bool Foam::regIOobject::readIfModified()
00163 {
00164     if (lastModified_)
00165     {
00166         time_t newTimeStamp = lastModified(filePath());
00167 
00168         bool readFile = false;
00169 
00170         if (newTimeStamp > (lastModified_ + fileModificationSkew))
00171         {
00172             readFile = true;
00173         }
00174 
00175         if (Pstream::parRun())
00176         {
00177             bool readFileOnThisProc = readFile;
00178             reduce(readFile, andOp<bool>());
00179 
00180             if (readFileOnThisProc && !readFile)
00181             {
00182                 WarningIn("regIOobject::readIfModified()")
00183                     << "Delaying reading " << name()
00184                     << " of class " << headerClassName()
00185                     << " due to inconsistent "
00186                        "file time-stamps between processors"
00187                     << endl;
00188             }
00189         }
00190 
00191         if (readFile)
00192         {
00193             lastModified_ = newTimeStamp;
00194             Info<< "regIOobject::readIfModified() : " << nl
00195                 << "    Reading object " << name()
00196                 << " from file " << filePath() << endl;
00197             return read();
00198         }
00199         else
00200         {
00201             return false;
00202         }
00203     }
00204     else
00205     {
00206         return false;
00207     }
00208 }
00209 
00210 
00211 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines