Loading...
Searching...
No Matches
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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "Istream.H"
30#include "ISstream.H"
31
32// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
33
34namespace
35{
36
37// The current get position (std::istream only)
38inline std::streampos stream_tellg(Foam::Istream* isptr)
39{
40 auto* sptr = dynamic_cast<Foam::ISstream*>(isptr);
41 return sptr ? sptr->stdStream().tellg() : std::streampos(0);
43
44} // End anonymous namespace
45
46
47// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48
49const Foam::token& Foam::Istream::peekBack() const noexcept
50{
51 return (putBackAvail_ ? putBackToken_ : token::undefinedToken);
52}
53
54// Return the putback token if available or fetch a new token
55// from the stream.
56//
57// Foam::token& Foam::Istream::peekToken()
58// {
59// if (!putBackAvail_)
60// {
61// putBackToken_.reset();
62// token tok;
63// this->read(tok);
64// putBackToken_ = std::move(tok);
65// }
66//
67// return putBackToken_;
68// }
69
70
72{
73 putBackAvail_ = false;
74 putBackToken_.reset();
75}
76
77
78void Foam::Istream::putBack(const token& tok)
79{
80 if (bad())
81 {
83 << "Attempt to put back onto bad stream"
85 }
86 else if (putBackAvail_)
87 {
89 << "Attempt to put back another token"
91 }
92 else
93 {
94 putBackAvail_ = true;
95 putBackToken_ = tok;
96 }
97}
98
99
100void Foam::Istream::putBack(token&& tok)
101{
102 if (bad())
103 {
105 << "Attempt to put back onto bad stream"
106 << exit(FatalIOError);
107 }
108 else if (putBackAvail_)
109 {
111 << "Attempt to put back another token"
112 << exit(FatalIOError);
113 }
114 else
116 putBackAvail_ = true;
117 putBackToken_ = std::move(tok);
118 }
119}
120
121
122bool Foam::Istream::getBack(token& tok)
123{
124 if (bad())
125 {
127 << "Attempt to get back from bad stream"
128 << exit(FatalIOError);
129 }
130 else if (putBackAvail_)
131 {
132 putBackAvail_ = false;
133 tok = std::move(putBackToken_);
134 return true;
135 }
136
137 return false;
138}
139
140
141bool Foam::Istream::readBegin(const char* funcName)
142{
143 const token delimiter(*this);
144
145 if (delimiter != token::BEGIN_LIST)
146 {
147 setBad();
149 << "Expected a '" << token::BEGIN_LIST
150 << "' while reading " << funcName
151 << ", found " << delimiter.info() << nl
153 }
154
155 return true;
156}
157
158
159bool Foam::Istream::readEnd(const char* funcName)
160{
161 const token delimiter(*this);
162
163 if (delimiter != token::END_LIST)
164 {
165 setBad();
167 << "Expected a '" << token::END_LIST
168 << "' while reading " << funcName
169 << ", found " << delimiter.info()
170 << " at stream position " << stream_tellg(this) << nl
172 }
173
174 return true;
175}
176
177
178char Foam::Istream::readBeginList(const char* funcName)
179{
180 const token delimiter(*this);
181
182 if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
183 {
184 setBad();
186 << "Expected a '" << token::BEGIN_LIST
187 << "' or a '" << token::BEGIN_BLOCK
188 << "' while reading " << funcName
189 << ", found " << delimiter.info()
190 << exit(FatalIOError);
191
192 return '\0';
193 }
194
195 return delimiter.pToken();
196}
197
198
199char Foam::Istream::readEndList(const char* funcName)
200{
201 const token delimiter(*this);
202
203 if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
204 {
205 setBad();
207 << "Expected a '" << token::END_LIST
208 << "' or a '" << token::END_BLOCK
209 << "' while reading " << funcName
210 << ", found " << delimiter.info()
211 << " at stream position " << stream_tellg(this) << nl
212 << exit(FatalIOError);
213
214 return '\0';
215 }
216
217 return delimiter.pToken();
218}
219
222{
223 putBackClear(); // Drop any putback
224}
225
226
228{
229 if (!good())
230 {
232 FatalIOError.exit();
233 }
234
235 return const_cast<Istream&>(*this);
236}
237
238
239// ************************************************************************* //
void setBad() noexcept
Set stream state to be 'bad'.
Definition IOstream.H:488
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
bool bad() const noexcept
True if stream is corrupted.
Definition IOstream.H:305
Generic input stream using a standard (STL) stream.
Definition ISstream.H:54
virtual const std::istream & stdStream() const
Const access to underlying std::istream.
Definition ISstream.H:163
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
bool getBack(token &tok)
Retrieve the put-back token if there is one.
Definition Istream.C:115
Istream & operator()() const
Return a non-const reference to const Istream.
Definition Istream.C:220
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition Istream.C:192
virtual void rewind()=0
Rewind the stream so that it may be read again.
Definition Istream.C:214
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition Istream.C:152
const token & peekBack() const noexcept
Examine putback token without removing it.
Definition Istream.C:42
Istream(const Istream &)=default
Copy construct.
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition Istream.C:171
void putBackClear()
Drop the putback token.
Definition Istream.C:64
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition Istream.C:134
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition Istream.C:71
A token holds an item read from Istream.
Definition token.H:70
@ BEGIN_BLOCK
Begin block [isseparator].
Definition token.H:178
@ END_BLOCK
End block [isseparator].
Definition token.H:179
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
punctuationToken pToken() const
Return punctuation character.
Definition tokenI.H:676
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition tokenI.H:412
static const token undefinedToken
An undefined token.
Definition token.H:570
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition token.H:1253
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define FUNCTION_NAME
static void check(const int retVal, const char *what)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
const direction noexcept
Definition scalarImpl.H:265
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50