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

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