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);
42}
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
56{
57 if (putBackAvail_)
58 {
59 tok = putBackToken_;
60 }
61 else
62 {
63 tok.reset();
64 }
65
66 return putBackAvail_;
67}
68
69
71{
72 if (bad())
73 {
75 << "Attempt to put back onto bad stream"
77 }
78 else if (putBackAvail_)
79 {
81 << "Attempt to put back another token"
83 }
84 else
85 {
86 putBackAvail_ = true;
87 putBackToken_ = tok;
88 }
89}
90
91
93{
94 if (bad())
95 {
97 << "Attempt to get back from bad stream"
99 }
100 else if (putBackAvail_)
101 {
102 putBackAvail_ = false;
103 tok = putBackToken_;
104 return true;
105 }
106
107 return false;
108}
109
110
111bool Foam::Istream::readBegin(const char* funcName)
112{
113 const token delimiter(*this);
114
115 if (delimiter != token::BEGIN_LIST)
116 {
117 setBad();
119 << "Expected a '" << token::BEGIN_LIST
120 << "' while reading " << funcName
121 << ", found " << delimiter.info() << nl
122 << exit(FatalIOError);
123 }
124
125 return true;
126}
127
128
129bool Foam::Istream::readEnd(const char* funcName)
130{
131 const token delimiter(*this);
132
133 if (delimiter != token::END_LIST)
134 {
135 setBad();
137 << "Expected a '" << token::END_LIST
138 << "' while reading " << funcName
139 << ", found " << delimiter.info()
140 << " at stream position " << stream_tellg(this) << nl
141 << exit(FatalIOError);
142 }
143
144 return true;
145}
146
147
148char Foam::Istream::readBeginList(const char* funcName)
149{
150 const token delimiter(*this);
151
152 if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
153 {
154 setBad();
156 << "Expected a '" << token::BEGIN_LIST
157 << "' or a '" << token::BEGIN_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
169char Foam::Istream::readEndList(const char* funcName)
170{
171 const token delimiter(*this);
172
173 if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
174 {
175 setBad();
177 << "Expected a '" << token::END_LIST
178 << "' or a '" << token::END_BLOCK
179 << "' while reading " << funcName
180 << ", found " << delimiter.info()
181 << " at stream position " << stream_tellg(this) << nl
182 << exit(FatalIOError);
183
184 return '\0';
185 }
186
187 return delimiter.pToken();
188}
189
190
192{
193 if (!good())
194 {
197 }
198
199 return const_cast<Istream&>(*this);
200}
201
202
203// ************************************************************************* //
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:243
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:58
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: ISstream.H:216
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
bool getBack(token &tok)
Get the put-back token if there is one.
Definition: Istream.C:92
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:191
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition: Istream.C:129
const token & peekBack() const noexcept
Examine putback token without removing it.
Definition: Istream.C:49
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:111
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
A token holds an item read from Istream.
Definition: token.H:69
@ BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:159
@ END_BLOCK
End block [isseparator].
Definition: token.H:160
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
@ END_LIST
End list [isseparator].
Definition: token.H:156
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:485
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.H:257
static const token undefinedToken
An undefined token.
Definition: token.H:294
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FUNCTION_NAME
static void check(const int retVal, const char *what)
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53