Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / src / OpenFOAM / db / regIOobject / regIOobjectRead.C
blobefea9ba783d1ecc0afe96c678f49818b4e1fa83e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "regIOobject.H"
28 #include <OpenFOAM/IFstream.H>
29 #include <OpenFOAM/Time.H>
30 #include <OpenFOAM/PstreamReduceOps.H>
33 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
35 Foam::Istream& Foam::regIOobject::readStream()
37     if (IFstream::debug)
38     {
39         Info<< "regIOobject::readStream() : "
40             << "reading object " << name()
41             << " from file " << objectPath()
42             << endl;
43     }
45     if (readOpt() == NO_READ)
46     {
47         FatalErrorIn("regIOobject::readStream()")
48             << "NO_READ specified for read-constructor of object " << name()
49             << " of class " << headerClassName()
50             << abort(FatalError);
51     }
53     // Construct object stream and read header if not already constructed
54     if (!isPtr_)
55     {
56         if (!(isPtr_ = objectStream()))
57         {
58             FatalIOError
59             (
60                 "regIOobject::readStream()",
61                 __FILE__,
62                 __LINE__,
63                 objectPath(),
64                 0
65             )   << "cannot open file"
66                 << exit(FatalIOError);
67         }
68         else if (!readHeader(*isPtr_))
69         {
70             FatalIOErrorIn("regIOobject::readStream()", *isPtr_)
71                 << "problem while reading header for object " << name()
72                 << exit(FatalIOError);
73         }
74     }
76     if (!lastModified_)
77     {
78         lastModified_ = lastModified(filePath());
79     }
81     return *isPtr_;
85 Foam::Istream& Foam::regIOobject::readStream(const word& expectName)
87     if (IFstream::debug)
88     {
89         Info<< "regIOobject::readStream(const word&) : "
90             << "reading object " << name()
91             << " from file " << objectPath()
92             << endl;
93     }
95     // Construct IFstream if not already constructed
96     if (!isPtr_)
97     {
98         readStream();
100         // Check the className of the regIOobject
101         // dictionary is an allowable name in case the actual class
102         // instantiated is a dictionary
103         if
104         (
105             expectName.size()
106          && headerClassName() != expectName
107          && headerClassName() != "dictionary"
108         )
109         {
110             FatalIOErrorIn("regIOobject::readStream(const word&)", *isPtr_)
111                 << "unexpected class name " << headerClassName()
112                 << " expected " << expectName << endl
113                 << "    while reading object " << name()
114                 << exit(FatalIOError);
115         }
116     }
118     return *isPtr_;
122 void Foam::regIOobject::close()
124     if (IFstream::debug)
125     {
126         Info<< "regIOobject::close() : "
127             << "finished reading " << filePath()
128             << endl;
129     }
131     if (isPtr_)
132     {
133         delete isPtr_;
134         isPtr_ = NULL;
135     }
139 bool Foam::regIOobject::readData(Istream&)
141     return false;
145 bool Foam::regIOobject::read()
147     bool ok = readData(readStream(type()));
148     close();
149     return ok;
153 bool Foam::regIOobject::modified() const
155     return
156     (
157         lastModified_
158      && lastModified(filePath()) > (lastModified_ + fileModificationSkew)
159     );
163 bool Foam::regIOobject::readIfModified()
165     if (lastModified_)
166     {
167         time_t newTimeStamp = lastModified(filePath());
169         bool readFile = false;
171         if (newTimeStamp > (lastModified_ + fileModificationSkew))
172         {
173             readFile = true;
174         }
176         if (Pstream::parRun())
177         {
178             bool readFileOnThisProc = readFile;
179             reduce(readFile, andOp<bool>());
181             if (readFileOnThisProc && !readFile)
182             {
183                 WarningIn("regIOobject::readIfModified()")
184                     << "Delaying reading " << name()
185                     << " of class " << headerClassName()
186                     << " due to inconsistent "
187                        "file time-stamps between processors"
188                     << endl;
189             }
190         }
192         if (readFile)
193         {
194             lastModified_ = newTimeStamp;
195             Info<< "regIOobject::readIfModified() : " << nl
196                 << "    Reading object " << name()
197                 << " from file " << filePath() << endl;
198             return read();
199         }
200         else
201         {
202             return false;
203         }
204     }
205     else
206     {
207         return false;
208     }
212 // ************************ vim: set sw=4 sts=4 et: ************************ //