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-2018 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  {
89  << "First token could not be read or is not the keyword 'FoamFile'"
90  << nl << nl << "Check header is of the form:" << nl << endl;
91 
93 
94  return false;
95  }
96 
97  // Check stream is still OK
98  if (is.good())
99  {
100  objState_ = GOOD;
101  }
102  else
103  {
104  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
105  {
107  << " stream failure while reading header"
108  << " on line " << is.lineNumber()
109  << " of file " << is.name()
110  << " for essential object" << name()
111  << exit(FatalIOError);
112  }
113 
114  if (IOobject::debug)
115  {
117  << "Stream failure while reading header"
118  << " on line " << is.lineNumber()
119  << " of file " << is.name() << endl;
120  }
121 
122  objState_ = BAD;
123 
124  return false;
125  }
126 
127  if (IOobject::debug)
128  {
129  Info<< " .... read" << endl;
130  }
131 
132  return true;
133 }
134 
135 
136 // ************************************************************************* //
bool isWord() const
Definition: tokenI.H:230
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
const word & name() const
Return name.
Definition: IOobject.H:295
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:235
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:69
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
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:265
#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:583
IOerror FatalIOError
#define InfoInFunction
Report an information message using Foam::Info.