All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Istream.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 "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 
87 {
88  token delimiter(*this);
89  if (delimiter != token::BEGIN_LIST)
90  {
91  setBad();
93  << "Expected a '" << token::BEGIN_LIST
94  << "' while reading " << funcName
95  << ", found " << delimiter.info()
96  << exit(FatalIOError);
97  }
98 
99  return *this;
100 }
101 
102 
103 Foam::Istream& Foam::Istream::readEnd(const char* funcName)
104 {
105  token delimiter(*this);
106  if (delimiter != token::END_LIST)
107  {
108  setBad();
110  << "Expected a '" << token::END_LIST
111  << "' while reading " << funcName
112  << ", found " << delimiter.info()
113  << exit(FatalIOError);
114  }
115 
116  return *this;
117 }
118 
119 
121 {
122  readEnd(funcName);
123  return readBegin(funcName);
124 }
125 
126 
127 char Foam::Istream::readBeginList(const char* funcName)
128 {
129  token delimiter(*this);
130 
131  if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
132  {
133  setBad();
135  << "Expected a '" << token::BEGIN_LIST
136  << "' or a '" << token::BEGIN_BLOCK
137  << "' while reading " << funcName
138  << ", found " << delimiter.info()
139  << exit(FatalIOError);
140 
141  return '\0';
142  }
143 
144  return delimiter.pToken();
145 }
146 
147 
148 char Foam::Istream::readEndList(const char* funcName)
149 {
150  token delimiter(*this);
151 
152  if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
153  {
154  setBad();
156  << "Expected a '" << token::END_LIST
157  << "' or a '" << token::END_BLOCK
158  << "' while reading " << funcName
159  << ", found " << delimiter.info()
160  << exit(FatalIOError);
161 
162  return '\0';
163  }
164 
165  return delimiter.pToken();
166 }
167 
168 
170 {
171  if (!good())
172  {
173  check("Istream::operator()");
174  FatalIOError.exit();
175  }
176 
177  return const_cast<Istream&>(*this);
178 }
179 
180 
181 // ************************************************************************* //
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
bool peekBack(token &)
Peek at the put back token without removing it.
Definition: Istream.C:71
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:169
punctuationToken pToken() const
Definition: tokenI.H:248
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
char readEndList(const char *funcName)
Definition: Istream.C:148
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:391
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
void putBack(const token &)
Put back token.
Definition: Istream.C:30
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & readEndBegin(const char *funcName)
Definition: Istream.C:120
static token undefinedToken
Static undefined token.
Definition: token.H:237
char readBeginList(const char *funcName)
Definition: Istream.C:127
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:158
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
bool getBack(token &)
Get the put back token if there is one and return true.
Definition: Istream.C:52
IOerror FatalIOError