IOobjectReadHeader.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
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 "IOobject.H"
27 #include "dictionary.H"
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
32 {
33  if (IOobject::debug)
34  {
35  InfoInFunction << "Reading header for file " << is.name() << endl;
36  }
37 
38  // Check Istream not already bad
39  if (!is.good())
40  {
41  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
42  {
44  << " stream not open for reading essential object from file "
45  << is.name()
46  << exit(FatalIOError);
47  }
48 
49  if (IOobject::debug)
50  {
52  << " stream not open for reading from file "
53  << is.name() << endl;
54  }
55 
56  return false;
57  }
58 
59  token firstToken(is);
60 
61  if
62  (
63  is.good()
64  && firstToken.isWord()
65  && firstToken.wordToken() == foamFile
66  )
67  {
68  dictionary headerDict(is);
69 
70  is.version
71  (
72  headerDict.lookupOrDefault("version", IOstream::currentVersion)
73  );
74  is.format(headerDict.lookup("format"));
75  headerClassName_ = word(headerDict.lookup("class"));
76 
77  const word headerObject(headerDict.lookup("object"));
78  if (IOobject::debug && headerObject != name())
79  {
81  << " object renamed from "
82  << name() << " to " << headerObject
83  << " for file " << is.name() << endl;
84  }
85 
86  // The note entry is optional
87  headerDict.readIfPresent("note", note_);
88  }
89  else
90  {
91  if (IOobject::debug)
92  {
94  << "First token could not be read "
95  "or is not the keyword 'FoamFile'"
96  << nl << nl << "Check header is of the form:" << nl << endl;
97 
99  }
100 
101  return false;
102  }
103 
104  // Check stream is still OK
105  if (is.good())
106  {
107  objState_ = GOOD;
108  }
109  else
110  {
111  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
112  {
114  << " stream failure while reading header"
115  << " on line " << is.lineNumber()
116  << " of file " << is.name()
117  << " for essential object" << name()
118  << exit(FatalIOError);
119  }
120 
121  if (IOobject::debug)
122  {
124  << "Stream failure while reading header"
125  << " on line " << is.lineNumber()
126  << " of file " << is.name() << endl;
127  }
128 
129  objState_ = BAD;
130 
131  return false;
132  }
133 
134  if (IOobject::debug)
135  {
136  Info<< " .... read" << endl;
137  }
138 
139  return true;
140 }
141 
142 
143 // ************************************************************************* //
bool isWord() const
Definition: tokenI.H:261
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
const word & name() const
Return name.
Definition: IOobject.H:303
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
const word & wordToken() const
Definition: tokenI.H:266
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:72
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
versionNumber version() const
Return the stream version.
Definition: IOstream.H:396
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
bool readHeader(Istream &)
Read header.
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
static const char nl
Definition: Ostream.H:260
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:98
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
bool writeHeader(Ostream &) const
Write header.
messageStream Info
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:844
IOerror FatalIOError
#define InfoInFunction
Report an information message using Foam::Info.