IOobjectReadHeader.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-2016 OpenFOAM Foundation
9  Copyright (C) 2019-2023 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 "IOobject.H"
30 #include "dictionary.H"
31 #include "foamVersion.H"
32 #include "fileOperation.H"
33 #include "Pstream.H"
34 
35 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36 
38 {
39  IOstreamOption streamOpt; // == (ASCII, currentVersion)
40 
41  // Treat "version" as optional
42  {
43  token tok;
44  if (headerDict.readIfPresent("version", tok))
45  {
46  streamOpt.version(tok);
47  }
48  }
49 
50  // Treat "format" as mandatory, could also as optional
51  streamOpt.format(headerDict.get<word>("format"));
52 
53  headerClassName_ = headerDict.get<word>("class");
54 
55  const word headerObject(headerDict.get<word>("object"));
56 
57  // The "note" entry is optional
58  headerDict.readIfPresent("note", note_);
59 
60  // The "arch" information may be missing
61  string arch;
62  if (headerDict.readIfPresent("arch", arch))
63  {
64  unsigned val = foamVersion::labelByteSize(arch);
65  if (val) sizeofLabel_ = static_cast<unsigned char>(val);
66 
67  val = foamVersion::scalarByteSize(arch);
68  if (val) sizeofScalar_ = static_cast<unsigned char>(val);
69  }
70 
71  return streamOpt;
72 }
73 
74 
75 bool Foam::IOobject::readHeader(dictionary& headerDict, Istream& is)
76 {
77  if (IOobject::debug)
78  {
79  InfoInFunction << "Reading header for file " << is.name() << endl;
80  }
81 
82  // Check Istream not already bad
83  if (!is.good())
84  {
85  if (isReadRequired())
86  {
88  << " stream not open for reading essential object from file "
89  << is.relativeName()
90  << exit(FatalIOError);
91  }
92 
93  if (IOobject::debug)
94  {
96  << " stream not open for reading from file "
97  << is.relativeName() << endl;
98  }
99 
100  return false;
101  }
102 
103  token firstToken(is);
104 
105  if (is.good() && firstToken.isWord("FoamFile"))
106  {
107  headerDict.read(is, false); // Read sub-dictionary content
108 
109  IOstreamOption streamOpt = parseHeader(headerDict);
110 
111  is.format(streamOpt.format());
112  is.version(streamOpt.version());
113  is.setLabelByteSize(sizeofLabel_);
114  is.setScalarByteSize(sizeofScalar_);
115  }
116  else
117  {
119  << "First token could not be read or is not 'FoamFile'"
120  << nl << nl
121  << "Check header is of the form:" << nl << endl;
122 
123  writeHeader(Info);
124 
125  // Mark as not read
126  headerClassName_.clear();
127 
128  return false;
129  }
130 
131  // Check stream is still OK
132  objState_ = (is.good() ? objectState::GOOD : objectState::BAD);
133 
134  if (IOobject::debug)
135  {
136  Info<< " .... read - state: "
137  << (objState_ == objectState::GOOD ? "good" : "bad")
138  << endl;
139 
140  }
141 
142  if (objState_ == objectState::BAD)
143  {
144  if (isReadRequired())
145  {
147  << " stream failure while reading header"
148  << " on line " << is.lineNumber()
149  << " of file " << is.relativeName()
150  << " for essential object:" << name()
151  << exit(FatalIOError);
152  }
153 
154  if (IOobject::debug)
155  {
157  << "Stream failure while reading header"
158  << " on line " << is.lineNumber()
159  << " of file " << is.relativeName() << endl;
160  }
161 
162  return false;
163  }
164 
165  return true;
166 }
167 
168 
169 bool Foam::IOobject::readHeader(Istream& is)
170 {
171  dictionary headerDict;
172  return IOobject::readHeader(headerDict, is);
173 }
174 
175 
176 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
177 
178 bool Foam::IOobject::readAndCheckHeader
179 (
180  const bool isGlobal,
181  const word& typeName,
182  const bool checkType,
183  const bool search,
184  const bool verbose
185 )
186 {
187  // Mark as not yet read. cf, IOobject::readHeader()
188  headerClassName_.clear();
189 
190  // Everyone check or just master
191  const bool masterOnly
192  (
193  isGlobal
194  && (
197  )
198  );
199 
200  const auto& handler = Foam::fileHandler();
201 
202  // Determine local status
203  bool ok = false;
204 
205  if (masterOnly)
206  {
207  if (UPstream::master())
208  {
209  // Force master-only header reading
210  const bool oldParRun = UPstream::parRun(false);
211  const fileName fName
212  (
213  handler.filePath(isGlobal, *this, typeName, search)
214  );
215  ok = handler.readHeader(*this, fName, typeName);
216  UPstream::parRun(oldParRun);
217 
218  if
219  (
220  ok && checkType
221  && !typeName.empty() && headerClassName_ != typeName
222  )
223  {
224  ok = false;
225  if (verbose)
226  {
228  << "Unexpected class name \"" << headerClassName_
229  << "\" expected \"" << typeName
230  << "\" when reading " << fName << endl;
231  }
232  }
233  }
234 
235  // If masterOnly make sure all processors know about the read
236  // information. Note: should ideally be inside fileHandler...
238  (
240  ok,
241  headerClassName_,
242  note_
243  );
244  }
245  else
246  {
247  // All read header
248  const fileName fName
249  (
250  handler.filePath(isGlobal, *this, typeName, search)
251  );
252  ok = handler.readHeader(*this, fName, typeName);
253 
254  if
255  (
256  ok && checkType
257  && !typeName.empty() && headerClassName_ != typeName
258  )
259  {
260  ok = false;
261  if (verbose)
262  {
264  << "Unexpected class name \"" << headerClassName_
265  << "\" expected \"" << typeName
266  << "\" when reading " << fName << endl;
267  }
268  }
269  }
270 
271  return ok;
272 }
273 
274 
275 // ************************************************************************* //
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
static void writeHeader(Ostream &os, const word &fieldName)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
A token holds an item read from Istream.
Definition: token.H:65
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1049
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
A simple container for options an IOstream can normally have.
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:409
IOstreamOption parseHeader(const dictionary &headerDict)
Parse &#39;FoamFile&#39; header contents and set the IOobject characteristics and return the stream character...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
int debug
Static debugging option.
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:343
static void broadcasts(const label comm, Type &arg1, Args &&... args)
Broadcast multiple items to all communicator ranks. Does nothing in non-parallel. ...
unsigned scalarByteSize(const std::string &str)
Extract scalar size (in bytes) from "scalar=" tag in string.
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
versionNumber version() const noexcept
Get the stream version.
unsigned labelByteSize(const std::string &str)
Extract label size (in bytes) from "label=" tag in string.
messageStream Info
Information stream (stdout output on master, null elsewhere)
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:642
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
bool readHeader(Istream &is)
Read header (&#39;FoamFile&#39; dictionary) and set the IOobject and stream characteristics.
streamFormat format() const noexcept
Get the current stream format.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
#define InfoInFunction
Report an information message using Foam::Info.