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-2026 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  // Cache the put back token
47  putBackToken_ = t;
48 
49  // Cache the current stream line number
50  putBackLineNumber_ = lineNumber_;
51 
52  // Reset the stream line number to that of the put back token
53  lineNumber_ = t.lineNumber();
54 
55  putBack_ = true;
56  }
57 }
58 
59 
61 {
62  if (bad())
63  {
65  << "Attempt to get back from bad stream"
66  << exit(FatalIOError);
67  }
68  else if (putBack_)
69  {
70  // Set the token to the current put back token
71  t = putBackToken_;
72 
73  // Reset the stream line number to when the put back token was cached
74  lineNumber_ = putBackLineNumber_;
75 
76  putBack_ = false;
77 
78  return true;
79  }
80 
81  return false;
82 }
83 
84 
86 {
87  return putBack_;
88 }
89 
90 
92 {
93  if (putBack_)
94  {
95  t = putBackToken_;
96  }
97  else
98  {
100  }
101 
102  return putBack_;
103 }
104 
105 
107 {
108  token delimiter(*this);
109  if (delimiter != token::BEGIN_LIST)
110  {
111  setBad();
113  << "Expected a '" << token::BEGIN_LIST
114  << "' while reading " << funcName
115  << ", found " << delimiter.info()
116  << exit(FatalIOError);
117  }
118 
119  return *this;
120 }
121 
122 
123 Foam::Istream& Foam::Istream::readEnd(const char* funcName)
124 {
125  token delimiter(*this);
126  if (delimiter != token::END_LIST)
127  {
128  setBad();
130  << "Expected a '" << token::END_LIST
131  << "' while reading " << funcName
132  << ", found " << delimiter.info()
133  << exit(FatalIOError);
134  }
135 
136  return *this;
137 }
138 
139 
141 {
142  readEnd(funcName);
143  return readBegin(funcName);
144 }
145 
146 
147 char Foam::Istream::readBeginList(const char* funcName)
148 {
149  token delimiter(*this);
150 
151  if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
152  {
153  setBad();
155  << "Expected a '" << token::BEGIN_LIST
156  << "' or a '" << token::BEGIN_BLOCK
157  << "' while reading " << funcName
158  << ", found " << delimiter.info()
159  << exit(FatalIOError);
160 
161  return '\0';
162  }
163 
164  return delimiter.pToken();
165 }
166 
167 
168 char Foam::Istream::readEndList(const char* funcName)
169 {
170  token delimiter(*this);
171 
172  if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
173  {
174  setBad();
176  << "Expected a '" << token::END_LIST
177  << "' or a '" << token::END_BLOCK
178  << "' while reading " << funcName
179  << ", found " << delimiter.info()
180  << exit(FatalIOError);
181 
182  return '\0';
183  }
184 
185  return delimiter.pToken();
186 }
187 
188 
190 {
191  if (!good())
192  {
193  check("Istream::operator()");
194  FatalIOError.exit();
195  }
196 
197  return const_cast<Istream&>(*this);
198 }
199 
200 
201 // ************************************************************************* //
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:158
label lineNumber_
Definition: IOstream.H:229
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Istream & readEnd(const char *funcName)
Definition: Istream.C:123
Istream & readBegin(const char *funcName)
Definition: Istream.C:106
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:189
char readEndList(const char *funcName)
Definition: Istream.C:168
bool peekBack()
Peek at the put back token and return true if available.
Definition: Istream.C:85
char readBeginList(const char *funcName)
Definition: Istream.C:147
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Istream & readEndBegin(const char *funcName)
Definition: Istream.C:140
bool getBack(token &)
Get the put back token if there is one and return true.
Definition: Istream.C:60
A token holds items read from Istream.
Definition: token.H:74
@ BEGIN_BLOCK
Definition: token.H:114
@ END_BLOCK
Definition: token.H:115
@ BEGIN_LIST
Definition: token.H:110
@ END_LIST
Definition: token.H:111
punctuationToken pToken() const
Definition: tokenI.H:329
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:426
static token undefinedToken
Static undefined token.
Definition: token.H:241
label lineNumber() const
Definition: tokenI.H:847
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
IOerror FatalIOError