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  return typeHeaderOk<IOobject>(false);
34 }
35 
36 
38 {
39  if (IOobject::debug)
40  {
41  InfoInFunction << "Reading header for file " << is.name() << endl;
42  }
43 
44  // Check Istream not already bad
45  if (!is.good())
46  {
47  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
48  {
50  << " stream not open for reading essential object from file "
51  << is.name()
52  << exit(FatalIOError);
53  }
54 
55  if (IOobject::debug)
56  {
58  << " stream not open for reading from file "
59  << is.name() << endl;
60  }
61 
62  return false;
63  }
64 
65  token firstToken(is);
66 
67  if
68  (
69  is.good()
70  && firstToken.isWord()
71  && firstToken.wordToken() == foamFile
72  )
73  {
74  dictionary headerDict(is);
75 
76  is.version
77  (
78  headerDict.lookupOrDefault("version", IOstream::currentVersion)
79  );
80  is.format(headerDict.lookup("format"));
81  headerClassName_ = word(headerDict.lookup("class"));
82 
83  const word headerObject(headerDict.lookup("object"));
84  if (IOobject::debug && headerObject != name())
85  {
87  << " object renamed from "
88  << name() << " to " << headerObject
89  << " for file " << is.name() << endl;
90  }
91 
92  // The note entry is optional
93  headerDict.readIfPresent("note", note_);
94  }
95  else
96  {
97  if (IOobject::debug)
98  {
100  << "First token could not be read "
101  "or is not the keyword 'FoamFile'"
102  << nl << nl << "Check header is of the form:" << nl << endl;
103 
104  writeHeader(Info);
105  }
106 
107  return false;
108  }
109 
110  // Check stream is still OK
111  if (is.good())
112  {
113  objState_ = GOOD;
114  }
115  else
116  {
117  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
118  {
120  << " stream failure while reading header"
121  << " on line " << is.lineNumber()
122  << " of file " << is.name()
123  << " for essential object" << name()
124  << exit(FatalIOError);
125  }
126 
127  if (IOobject::debug)
128  {
130  << "Stream failure while reading header"
131  << " on line " << is.lineNumber()
132  << " of file " << is.name() << endl;
133  }
134 
135  objState_ = BAD;
136 
137  return false;
138  }
139 
140  if (IOobject::debug)
141  {
142  Info<< " .... read" << endl;
143  }
144 
145  return true;
146 }
147 
148 
149 // ************************************************************************* //
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:315
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:104
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:318
bool writeHeader(Ostream &) const
Write header.
messageStream Info
bool headerOk()
Read header of local object without type-checking.
#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:864
IOerror FatalIOError
#define InfoInFunction
Report an information message using Foam::Info.