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 -------------------------------------------------------------------------------
11 License
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 
34 namespace
35 {
36 
37 // The current get position (std::istream only)
38 inline 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 
49 const 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 
70 void Foam::Istream::putBack(const token& tok)
71 {
72  if (bad())
73  {
75  << "Attempt to put back onto bad stream"
76  << exit(FatalIOError);
77  }
78  else if (putBackAvail_)
79  {
81  << "Attempt to put back another token"
82  << exit(FatalIOError);
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"
98  << exit(FatalIOError);
99  }
100  else if (putBackAvail_)
101  {
102  putBackAvail_ = false;
103  tok = putBackToken_;
104  return true;
105  }
106 
107  return false;
108 }
109 
110 
111 bool 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 
129 bool 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 
148 char 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 
169 char 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  {
196  FatalIOError.exit();
197  }
198 
199  return const_cast<Istream&>(*this);
200 }
201 
202 
203 // ************************************************************************* //
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
ISstream.H
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::Istream::readEnd
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition: Istream.C:129
Foam::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:485
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:111
Foam::check
static void check(const int retVal, const char *what)
Definition: ptscotchDecomp.C:80
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::token::reset
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.H:257
Foam::Istream::peekBack
const token & peekBack() const noexcept
Examine putback token without removing it.
Definition: Istream.C:49
Istream.H
Foam::token::END_BLOCK
End block [isseparator].
Definition: token.H:160
Foam::token::undefinedToken
static const token undefinedToken
An undefined token.
Definition: token.H:294
Foam::Istream::operator()
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:191
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:159
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::ISstream::stdStream
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: ISstream.H:216
Foam::Istream::getBack
bool getBack(token &tok)
Get the put-back token if there is one.
Definition: Istream.C:92
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:243
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:156
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155