Istream.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 "Istream.H"
27 
28 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
29 
31 {
32  if (bad())
33  {
35  << "Attempt to put back onto bad stream"
36  << exit(FatalIOError);
37  }
38  else if (putBack_)
39  {
41  << "Attempt to put back another token"
42  << exit(FatalIOError);
43  }
44  else
45  {
46  putBackToken_ = t;
47  putBack_ = true;
48  }
49 }
50 
51 
53 {
54  if (bad())
55  {
57  << "Attempt to get back from bad stream"
58  << exit(FatalIOError);
59  }
60  else if (putBack_)
61  {
62  t = putBackToken_;
63  putBack_ = false;
64  return true;
65  }
66 
67  return false;
68 }
69 
70 
72 {
73  if (putBack_)
74  {
75  t = putBackToken_;
76  }
77  else
78  {
80  }
81 
82  return putBack_;
83 }
84 
85 
86 // Functions for reading object delimiters ( ... )
87 
89 {
90  token delimiter(*this);
91  if (delimiter != token::BEGIN_LIST)
92  {
93  setBad();
95  << "Expected a '" << token::BEGIN_LIST
96  << "' while reading " << funcName
97  << ", found " << delimiter.info()
98  << exit(FatalIOError);
99  }
100 
101  return *this;
102 }
103 
104 
105 Foam::Istream& Foam::Istream::readEnd(const char* funcName)
106 {
107  token delimiter(*this);
108  if (delimiter != token::END_LIST)
109  {
110  setBad();
112  << "Expected a '" << token::END_LIST
113  << "' while reading " << funcName
114  << ", found " << delimiter.info()
115  << exit(FatalIOError);
116  }
117 
118  return *this;
119 }
120 
121 
123 {
124  readEnd(funcName);
125  return readBegin(funcName);
126 }
127 
128 
129 // Functions for reading List delimiters ( ... ) or { ... }
130 
131 char Foam::Istream::readBeginList(const char* funcName)
132 {
133  token delimiter(*this);
134 
135  if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
136  {
137  setBad();
139  << "Expected a '" << token::BEGIN_LIST
140  << "' or a '" << token::BEGIN_BLOCK
141  << "' while reading " << funcName
142  << ", found " << delimiter.info()
143  << exit(FatalIOError);
144 
145  return '\0';
146  }
147 
148  return delimiter.pToken();
149 }
150 
151 
152 char Foam::Istream::readEndList(const char* funcName)
153 {
154  token delimiter(*this);
155 
156  if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
157  {
158  setBad();
160  << "Expected a '" << token::END_LIST
161  << "' or a '" << token::END_BLOCK
162  << "' while reading " << funcName
163  << ", found " << delimiter.info()
164  << exit(FatalIOError);
165 
166  return '\0';
167  }
168 
169  return delimiter.pToken();
170 }
171 
172 
174 {
175  if (!good())
176  {
177  check("Istream::operator()");
178  FatalIOError.exit();
179  }
180 
181  return const_cast<Istream&>(*this);
182 }
183 
184 
185 // ************************************************************************* //
Foam::Istream::readEnd
Istream & readEnd(const char *funcName)
Definition: Istream.C:105
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Definition: Istream.C:131
Foam::Istream::readEndList
char readEndList(const char *funcName)
Definition: Istream.C:152
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::token::info
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::Istream::peekBack
bool peekBack(token &)
Peek at the put back token without removing it.
Definition: Istream.C:71
Foam::Istream::putBackToken_
token putBackToken_
The last token put back on the stream.
Definition: Istream.H:67
Istream.H
Foam::token::pToken
punctuationToken pToken() const
Definition: tokenI.H:208
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Foam::Istream::operator()
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:173
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:168
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Definition: token.H:100
Foam::token::undefinedToken
static token undefinedToken
Static undefined token.
Definition: token.H:241
Foam::token::END_BLOCK
@ END_BLOCK
Definition: token.H:105
Foam::Istream::putBack
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Foam::Istream::readEndBegin
Istream & readEndBegin(const char *funcName)
Definition: Istream.C:122
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Definition: token.H:104
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::Istream::putBack_
bool putBack_
Has a token been put back on the stream?
Definition: Istream.H:64
Foam::Istream::readBegin
Istream & readBegin(const char *funcName)
Definition: Istream.C:88
Foam::Istream::getBack
bool getBack(token &)
Get the put back token if there is one and return true.
Definition: Istream.C:52
Foam::token::END_LIST
@ END_LIST
Definition: token.H:101