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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2018 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "regIOobject.H"
30 #include "IFstream.H"
31 #include "Time.H"
32 #include "Pstream.H"
33 #include "HashSet.H"
34 #include "fileOperation.H"
35 
36 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
37 
39 (
41  const word& typeName
42 )
43 {
44  // Everyone check or just master
45  bool masterOnly =
46  global()
47  && (
48  regIOobject::fileModificationChecking == timeStampMaster
49  || regIOobject::fileModificationChecking == inotifyMaster
50  );
51 
52 
53  // Check if header is ok for READ_IF_PRESENT
54  bool isHeaderOk = false;
55  if (readOpt() == IOobject::READ_IF_PRESENT)
56  {
57  if (masterOnly)
58  {
59  if (Pstream::master())
60  {
61  isHeaderOk = headerOk();
62  }
63  Pstream::scatter(isHeaderOk);
64  }
65  else
66  {
67  isHeaderOk = headerOk();
68  }
69  }
70 
71  if
72  (
73  (
74  readOpt() == IOobject::MUST_READ
75  || readOpt() == IOobject::MUST_READ_IF_MODIFIED
76  )
77  || isHeaderOk
78  )
79  {
80  return fileHandler().read(*this, masterOnly, format, typeName);
81  }
82 
83  return false;
84 }
85 
86 
87 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
88 
89 Foam::Istream& Foam::regIOobject::readStream(const bool valid)
90 {
91  if (IFstream::debug)
92  {
93  Pout<< "regIOobject::readStream() : "
94  << "reading object " << name()
95  << " (global " << global() << ")"
96  << " from file " << objectPath()
97  << endl;
98  }
99 
100  if (readOpt() == NO_READ)
101  {
103  << "NO_READ specified for read-constructor of object " << name()
104  << " of class " << headerClassName()
105  << abort(FatalError);
106  }
107 
108  // Construct object stream and read header if not already constructed
109  if (!isPtr_)
110  {
111  fileName objPath;
112  if (watchIndices_.size())
113  {
114  // File is being watched. Read exact file that is being watched.
115  objPath = fileHandler().getFile(watchIndices_.last());
116  }
117  else
118  {
119  // Search intelligently for file
120  objPath = filePath();
121 
122  if (IFstream::debug)
123  {
124  Pout<< "regIOobject::readStream() : "
125  << "found object " << name()
126  << " (global " << global() << ")"
127  << " in file " << objPath
128  << endl;
129  }
130  }
131 
132  isPtr_ = fileHandler().readStream(*this, objPath, type(), valid);
133  }
134 
135  return *isPtr_;
136 }
137 
138 
139 Foam::Istream& Foam::regIOobject::readStream
140 (
141  const word& expectName,
142  const bool valid
143 )
144 {
145  if (IFstream::debug)
146  {
147  Pout<< "regIOobject::readStream(const word&) : "
148  << "reading object " << name()
149  << " of type " << type()
150  << " from file " << filePath()
151  << endl;
152  }
153 
154  // Construct IFstream if not already constructed
155  if (!isPtr_)
156  {
157  readStream(valid);
158 
159  // Check the className of the regIOobject
160  // dictionary is an allowable name in case the actual class
161  // instantiated is a dictionary
162  if
163  (
164  valid
165  && expectName.size()
166  && headerClassName() != expectName
167  && headerClassName() != "dictionary"
168  )
169  {
170  FatalIOErrorInFunction(isPtr_())
171  << "unexpected class name " << headerClassName()
172  << " expected " << expectName << endl
173  << " while reading object " << name()
174  << exit(FatalIOError);
175  }
176  }
177 
178  return *isPtr_;
179 }
180 
181 
183 {
184  if (IFstream::debug)
185  {
186  Pout<< "regIOobject::close() : "
187  << "finished reading "
188  << (isPtr_ ? isPtr_->name() : "dummy")
189  << endl;
190  }
191 
192  isPtr_.reset(nullptr);
193 }
194 
195 
197 {
198  return false;
199 }
200 
201 
203 {
204  // Note: cannot do anything in readStream itself since this is used by
205  // e.g. GeometricField.
206 
207 
208  // Save old watchIndices and clear (so the list of included files can
209  // change)
210  fileNameList oldWatchFiles;
211  if (watchIndices_.size())
212  {
213  oldWatchFiles.setSize(watchIndices_.size());
214  forAll(watchIndices_, i)
215  {
216  oldWatchFiles[i] = fileHandler().getFile(watchIndices_[i]);
217  }
218  forAllReverse(watchIndices_, i)
219  {
220  fileHandler().removeWatch(watchIndices_[i]);
221  }
222  watchIndices_.clear();
223  }
224 
225 
226  // Read
227  bool masterOnly =
228  global()
229  && (
230  regIOobject::fileModificationChecking == timeStampMaster
231  || regIOobject::fileModificationChecking == inotifyMaster
232  );
233 
234  // Note: IOstream::binary flag is for all the processor comms. (Only for
235  // dictionaries should it be ascii)
236  bool ok = fileHandler().read(*this, masterOnly, IOstream::BINARY, type());
237 
238  if (oldWatchFiles.size())
239  {
240  // Re-watch master file
241  addWatch();
242  }
243 
244  return ok;
245 }
246 
247 
249 {
250  forAllReverse(watchIndices_, i)
251  {
252  if (fileHandler().getState(watchIndices_[i]) != fileMonitor::UNMODIFIED)
253  {
254  return true;
255  }
256  }
257 
258  return false;
259 }
260 
261 
263 {
264  // Get index of modified file so we can give nice message. Could instead
265  // just call above modified()
266  label modified = -1;
267  forAllReverse(watchIndices_, i)
268  {
269  if (fileHandler().getState(watchIndices_[i]) != fileMonitor::UNMODIFIED)
270  {
271  modified = watchIndices_[i];
272  break;
273  }
274  }
275 
276  if (modified != -1)
277  {
278  const fileName fName = fileHandler().getFile(watchIndices_.last());
279 
280  if (modified == watchIndices_.last())
281  {
283  << " Re-reading object " << name()
284  << " from file " << fName << endl;
285  }
286  else
287  {
289  << " Re-reading object " << name()
290  << " from file " << fName
291  << " because of modified file "
292  << fileHandler().getFile(modified)
293  << endl;
294  }
295 
296  return read();
297  }
298 
299  return false;
300 }
301 
302 
303 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
regIOobject.H
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::fileMonitor::UNMODIFIED
Definition: fileMonitor.H:75
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::regIOobject::read
virtual bool read()
Read object.
Definition: regIOobjectRead.C:202
Foam::IOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:211
Foam::regIOobject::global
virtual bool global() const
Is object global.
Definition: regIOobject.H:342
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1354
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::fileOperation::getFile
virtual fileName getFile(const label) const
Get name of file being watched (using handle)
Definition: fileOperation.C:783
Foam::fileOperation::read
virtual bool read(regIOobject &, const bool masterOnly, const IOstream::streamFormat format, const word &typeName) const =0
Top-level read.
Foam::fileOperation::readStream
virtual autoPtr< ISstream > readStream(regIOobject &, const fileName &, const word &typeName, const bool valid=true) const =0
Reads header for regIOobject and returns an ISstream.
format
word format(conversionProperties.get< word >("format"))
Foam::regIOobject::readHeaderOk
bool readHeaderOk(const IOstream::streamFormat PstreamFormat, const word &typeName)
Helper: check readOpt flags and read if necessary.
Definition: regIOobjectRead.C:39
Foam::regIOobject::modified
virtual bool modified() const
Definition: regIOobjectRead.C:248
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
IFstream.H
HashSet.H
Foam::regIOobject::readIfModified
virtual bool readIfModified()
Read object if modified (as set by call to modified)
Definition: regIOobjectRead.C:262
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::FatalError
error FatalError
fileOperation.H
Pstream.H
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::fileOperation::removeWatch
virtual bool removeWatch(const label) const
Remove watch on a file (using handle)
Definition: fileOperation.C:723
Foam::IOobject::headerClassName
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobjectI.H:88
Time.H
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::regIOobject::close
void close()
Close Istream.
Definition: regIOobjectRead.C:182
Foam::List< fileName >
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:165
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::regIOobject::filePath
virtual fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:447
forAllReverse
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: stdFoam.H:309
Foam::regIOobject::readData
virtual bool readData(Istream &)
Virtual readData function.
Definition: regIOobjectRead.C:196
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::IOobject::objectPath
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:209
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::IOobject::NO_READ
Definition: IOobject.H:123