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-2021 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  IOobject::fileModificationChecking == IOobject::timeStampMaster
49  || IOobject::fileModificationChecking == IOobject::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, fmt, typeName);
81  }
82 
83  return false;
84 }
85 
86 
87 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
88 
89 void Foam::regIOobject::readStream(const bool valid)
90 {
91  if (readOpt() == NO_READ)
92  {
94  << "NO_READ specified for read-constructor of object " << name()
95  << " of class " << headerClassName()
96  << abort(FatalError);
97  }
98 
99  // Construct object stream and read header if not already constructed
100  if (!isPtr_)
101  {
102  fileName objPath;
103  if (watchIndices_.size())
104  {
105  // File is being watched. Read exact file that is being watched.
106  objPath = fileHandler().getFile(watchIndices_.last());
107  }
108  else
109  {
110  // Search intelligently for file
111  objPath = filePath();
112 
113  if (IFstream::debug)
114  {
115  Pout<< "regIOobject::readStream() : "
116  << "found object " << name()
117  << " (global " << global() << ")"
118  << " in file " << objPath
119  << endl;
120  }
121  }
122 
123  isPtr_ = fileHandler().readStream(*this, objPath, type(), valid);
124  }
125 }
126 
127 
128 Foam::Istream& Foam::regIOobject::readStream
129 (
130  const word& expectName,
131  const bool valid
132 )
133 {
134  if (IFstream::debug)
135  {
136  Pout<< "regIOobject::readStream(const word&) : "
137  << "reading object " << name()
138  << " of type " << type()
139  << " from file " << filePath()
140  << endl;
141  }
142 
143  // Construct IFstream if not already constructed
144  if (!isPtr_)
145  {
146  readStream(valid);
147 
148  // Check the className of the regIOobject
149  // dictionary is an allowable name in case the actual class
150  // instantiated is a dictionary
151  if
152  (
153  valid
154  && expectName.size()
155  && headerClassName() != expectName
156  && headerClassName() != "dictionary"
157  )
158  {
159  FatalIOErrorInFunction(isPtr_())
160  << "unexpected class name " << headerClassName()
161  << " expected " << expectName << endl
162  << " while reading object " << name()
163  << exit(FatalIOError);
164  }
165  }
166 
167  return *isPtr_;
168 }
169 
170 
172 {
173  if (IFstream::debug)
174  {
175  Pout<< "regIOobject::close() : "
176  << "finished reading "
177  << (isPtr_ ? isPtr_->name() : "dummy")
178  << endl;
179  }
180 
181  isPtr_.reset(nullptr);
182 }
183 
184 
186 {
187  return false;
188 }
189 
190 
192 {
193  // Note: cannot do anything in readStream itself since this is used by
194  // e.g. GeometricField.
195 
196 
197  // Save old watchIndices and clear (so the list of included files can
198  // change)
199  fileNameList oldWatchFiles;
200  if (watchIndices_.size())
201  {
202  oldWatchFiles.setSize(watchIndices_.size());
203  forAll(watchIndices_, i)
204  {
205  oldWatchFiles[i] = fileHandler().getFile(watchIndices_[i]);
206  }
207  forAllReverse(watchIndices_, i)
208  {
209  fileHandler().removeWatch(watchIndices_[i]);
210  }
211  watchIndices_.clear();
212  }
213 
214 
215  // Read
216  bool masterOnly =
217  global()
218  && (
221  );
222 
223  // Note: IOstream::binary flag is for all the processor comms. (Only for
224  // dictionaries should it be ascii)
225  bool ok = fileHandler().read(*this, masterOnly, IOstream::BINARY, type());
226 
227  if (oldWatchFiles.size())
228  {
229  // Re-watch master file
230  addWatch();
231  }
232 
233  return ok;
234 }
235 
236 
238 {
239  forAllReverse(watchIndices_, i)
240  {
241  if (fileHandler().getState(watchIndices_[i]) != fileMonitor::UNMODIFIED)
242  {
243  return true;
244  }
245  }
246 
247  return false;
248 }
249 
250 
252 {
253  // Get index of modified file so we can give nice message. Could instead
254  // just call above modified()
255  label modified = -1;
256  forAllReverse(watchIndices_, i)
257  {
258  if (fileHandler().getState(watchIndices_[i]) != fileMonitor::UNMODIFIED)
259  {
260  modified = watchIndices_[i];
261  break;
262  }
263  }
264 
265  if (modified != -1)
266  {
267  const fileName fName = fileHandler().getFile(watchIndices_.last());
268 
269  if (modified == watchIndices_.last())
270  {
272  << " Re-reading object " << name()
273  << " from file " << fName << endl;
274  }
275  else
276  {
278  << " Re-reading object " << name()
279  << " from file " << fName
280  << " because of modified file "
281  << fileHandler().getFile(modified)
282  << endl;
283  }
284 
285  return read();
286  }
287 
288  return false;
289 }
290 
291 
292 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
regIOobject.H
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
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::IOobject::inotifyMaster
Definition: IOobject.H:204
Foam::IOobject::timeStampMaster
Definition: IOobject.H:202
Foam::regIOobject::read
virtual bool read()
Read object.
Definition: regIOobjectRead.C:191
Foam::IOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:303
Foam::regIOobject::global
virtual bool global() const
Is object global.
Definition: regIOobject.H:354
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1485
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::IOobject::headerClassName
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
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:917
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.
Foam::regIOobject::modified
virtual bool modified() const
Definition: regIOobjectRead.C:237
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
IFstream.H
HashSet.H
Foam::regIOobject::readIfModified
virtual bool readIfModified()
Read object if modified (as set by call to modified)
Definition: regIOobjectRead.C:251
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:857
Foam::IOobject::readOpt
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::regIOobject::close
void close()
Close Istream.
Definition: regIOobjectRead.C:171
Foam::List< fileName >
Foam::fileOperation::read
virtual bool read(regIOobject &, const bool masterOnly, const IOstreamOption::streamFormat format, const word &typeName) const =0
Top-level read.
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:430
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
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:185
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::regIOobject::readHeaderOk
bool readHeaderOk(const IOstreamOption::streamFormat fmt, const word &typeName)
Helper: check readOpt flags and read if necessary.
Definition: regIOobjectRead.C:39
Foam::IOobject::NO_READ
Definition: IOobject.H:188