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-2019 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(headerDict.lookup("version"));
71  is.format(headerDict.lookup("format"));
72  headerClassName_ = word(headerDict.lookup("class"));
73 
74  const word headerObject(headerDict.lookup("object"));
75  if (IOobject::debug && headerObject != name())
76  {
78  << " object renamed from "
79  << name() << " to " << headerObject
80  << " for file " << is.name() << endl;
81  }
82 
83  // The note entry is optional
84  headerDict.readIfPresent("note", note_);
85  }
86  else
87  {
88  if (IOobject::debug)
89  {
91  << "First token could not be read "
92  "or is not the keyword 'FoamFile'"
93  << nl << nl << "Check header is of the form:" << nl << endl;
94 
96  }
97 
98  return false;
99  }
100 
101  // Check stream is still OK
102  if (is.good())
103  {
104  objState_ = GOOD;
105  }
106  else
107  {
108  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
109  {
111  << " stream failure while reading header"
112  << " on line " << is.lineNumber()
113  << " of file " << is.name()
114  << " for essential object" << name()
115  << exit(FatalIOError);
116  }
117 
118  if (IOobject::debug)
119  {
121  << "Stream failure while reading header"
122  << " on line " << is.lineNumber()
123  << " of file " << is.name() << endl;
124  }
125 
126  objState_ = BAD;
127 
128  return false;
129  }
130 
131  if (IOobject::debug)
132  {
133  Info<< " .... read" << endl;
134  }
135 
136  return true;
137 }
138 
139 
140 // ************************************************************************* //
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:158
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:399
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
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:297
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:377
static const char nl
Definition: Ostream.H:260
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:98
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
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:812
IOerror FatalIOError
#define InfoInFunction
Report an information message using Foam::Info.