FreeFOAM The Cross-Platform CFD Toolkit
regIOobjectRead.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "regIOobject.H"
27 #include <OpenFOAM/IFstream.H>
28 #include <OpenFOAM/Time.H>
30 
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 Foam::Istream& Foam::regIOobject::readStream()
35 {
36  if (IFstream::debug)
37  {
38  Info<< "regIOobject::readStream() : "
39  << "reading object " << name()
40  << " from file " << objectPath()
41  << endl;
42  }
43 
44  if (readOpt() == NO_READ)
45  {
46  FatalErrorIn("regIOobject::readStream()")
47  << "NO_READ specified for read-constructor of object " << name()
48  << " of class " << headerClassName()
49  << abort(FatalError);
50  }
51 
52  // Construct object stream and read header if not already constructed
53  if (!isPtr_)
54  {
55  if (!(isPtr_ = objectStream()))
56  {
58  (
59  "regIOobject::readStream()",
60  __FILE__,
61  __LINE__,
62  objectPath(),
63  0
64  ) << "cannot open file"
65  << exit(FatalIOError);
66  }
67  else if (!readHeader(*isPtr_))
68  {
69  FatalIOErrorIn("regIOobject::readStream()", *isPtr_)
70  << "problem while reading header for object " << name()
71  << exit(FatalIOError);
72  }
73  }
74 
75  if (!lastModified_)
76  {
77  lastModified_ = lastModified(filePath());
78  }
79 
80  return *isPtr_;
81 }
82 
83 
84 Foam::Istream& Foam::regIOobject::readStream(const word& expectName)
85 {
86  if (IFstream::debug)
87  {
88  Info<< "regIOobject::readStream(const word&) : "
89  << "reading object " << name()
90  << " from file " << objectPath()
91  << endl;
92  }
93 
94  // Construct IFstream if not already constructed
95  if (!isPtr_)
96  {
97  readStream();
98 
99  // Check the className of the regIOobject
100  // dictionary is an allowable name in case the actual class
101  // instantiated is a dictionary
102  if
103  (
104  expectName.size()
105  && headerClassName() != expectName
106  && headerClassName() != "dictionary"
107  )
108  {
109  FatalIOErrorIn("regIOobject::readStream(const word&)", *isPtr_)
110  << "unexpected class name " << headerClassName()
111  << " expected " << expectName << endl
112  << " while reading object " << name()
113  << exit(FatalIOError);
114  }
115  }
116 
117  return *isPtr_;
118 }
119 
120 
122 {
123  if (IFstream::debug)
124  {
125  Info<< "regIOobject::close() : "
126  << "finished reading " << filePath()
127  << endl;
128  }
129 
130  if (isPtr_)
131  {
132  delete isPtr_;
133  isPtr_ = NULL;
134  }
135 }
136 
137 
139 {
140  return false;
141 }
142 
143 
145 {
146  bool ok = readData(readStream(type()));
147  close();
148  return ok;
149 }
150 
151 
153 {
154  return
155  (
156  lastModified_
157  && lastModified(filePath()) > (lastModified_ + fileModificationSkew)
158  );
159 }
160 
161 
163 {
164  if (lastModified_)
165  {
166  time_t newTimeStamp = lastModified(filePath());
167 
168  bool readFile = false;
169 
170  if (newTimeStamp > (lastModified_ + fileModificationSkew))
171  {
172  readFile = true;
173  }
174 
175  if (Pstream::parRun())
176  {
177  bool readFileOnThisProc = readFile;
178  reduce(readFile, andOp<bool>());
179 
180  if (readFileOnThisProc && !readFile)
181  {
182  WarningIn("regIOobject::readIfModified()")
183  << "Delaying reading " << name()
184  << " of class " << headerClassName()
185  << " due to inconsistent "
186  "file time-stamps between processors"
187  << endl;
188  }
189  }
190 
191  if (readFile)
192  {
193  lastModified_ = newTimeStamp;
194  Info<< "regIOobject::readIfModified() : " << nl
195  << " Reading object " << name()
196  << " from file " << filePath() << endl;
197  return read();
198  }
199  else
200  {
201  return false;
202  }
203  }
204  else
205  {
206  return false;
207  }
208 }
209 
210 
211 // ************************ vim: set sw=4 sts=4 et: ************************ //