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 | Copyright (C) 2011-2015 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  Info<< "IOobject::readHeader(Istream&) : reading header for file "
36  << is.name() << endl;
37  }
38 
39  // Check Istream not already bad
40  if (!is.good())
41  {
43  {
45  << " stream not open for reading essential object from file "
46  << is.name()
47  << exit(FatalIOError);
48  }
49 
50  if (IOobject::debug)
51  {
53  << " stream not open for reading from file "
54  << is.name() << endl;
55  }
56 
57  return false;
58  }
59 
60  token firstToken(is);
61 
62  if
63  (
64  is.good()
65  && firstToken.isWord()
66  && firstToken.wordToken() == "FoamFile"
67  )
68  {
69  dictionary headerDict(is);
70 
71  is.version(headerDict.lookup("version"));
72  is.format(headerDict.lookup("format"));
73  headerClassName_ = word(headerDict.lookup("class"));
74 
75  const word headerObject(headerDict.lookup("object"));
76  if (IOobject::debug && headerObject != name())
77  {
79  << " object renamed from "
80  << name() << " to " << headerObject
81  << " for file " << is.name() << endl;
82  }
83 
84  // The note entry is optional
85  headerDict.readIfPresent("note", note_);
86  }
87  else
88  {
90  << "First token could not be read or is not the keyword 'FoamFile'"
91  << nl << nl << "Check header is of the form:" << nl << endl;
92 
94 
95  return false;
96  }
97 
98  // Check stream is still OK
99  if (is.good())
100  {
101  objState_ = GOOD;
102  }
103  else
104  {
106  {
108  << " stream failure while reading header"
109  << " on line " << is.lineNumber()
110  << " of file " << is.name()
111  << " for essential object" << name()
112  << exit(FatalIOError);
113  }
114 
115  if (IOobject::debug)
116  {
117  Info<< "IOobject::readHeader(Istream&) :"
118  << " stream failure while reading header"
119  << " on line " << is.lineNumber()
120  << " of file " << is.name() << endl;
121  }
122 
123  objState_ = BAD;
124 
125  return false;
126  }
127 
128  if (IOobject::debug)
129  {
130  Info<< " .... read" << endl;
131  }
132 
133  return true;
134 }
135 
136 
137 // ************************************************************************* //
SeriousIOErrorInFunction
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
Definition: messageStream.H:249
Foam::IOstream::format
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::IOobject::rOpt_
readOption rOpt_
Read option.
Definition: IOobject.H:145
Foam::dictionary::readIfPresent
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
Definition: dictionaryTemplates.C:94
Foam::token::wordToken
const word & wordToken() const
Definition: tokenI.H:226
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:108
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::IOobject::note_
string note_
Optional note.
Definition: IOobject.H:133
Foam::IOobject::BAD
@ BAD
Definition: IOobject.H:102
Foam::IOobject::MUST_READ_IF_MODIFIED
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:109
Foam::IOobject::writeHeader
bool writeHeader(Ostream &) const
Write header.
Definition: IOobjectWriteHeader.C:67
Foam::IOobject::headerClassName_
word headerClassName_
Class name read from header.
Definition: IOobject.H:130
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobject.H:273
IOobject.H
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::token::isWord
bool isWord() const
Definition: tokenI.H:221
Foam::IOobject::GOOD
@ GOOD
Definition: IOobject.H:101
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::IOstream::version
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
dictionary.H
Foam::IOstream::lineNumber
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::IOobject::readHeader
bool readHeader(Istream &)
Read header.
Definition: IOobjectReadHeader.C:31
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:271
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::IOobject::objState_
objectState objState_
IOobject state.
Definition: IOobject.H:154
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297