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 
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 
78 void Foam::Istream::putBack(const token& tok)
79 {
80  if (bad())
81  {
83  << "Attempt to put back onto bad stream"
84  << exit(FatalIOError);
85  }
86  else if (putBackAvail_)
87  {
89  << "Attempt to put back another token"
90  << exit(FatalIOError);
91  }
92  else
93  {
94  putBackAvail_ = true;
95  putBackToken_ = tok;
96  }
97 }
98 
99 
100 void 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
115  {
116  putBackAvail_ = true;
117  putBackToken_ = std::move(tok);
118  }
119 }
120 
121 
122 bool 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 
141 bool 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 
159 bool 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 
178 char 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 
199 char 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 
220 
222 {
223  if (!good())
224  {
226  FatalIOError.exit();
227  }
228 
229  return const_cast<Istream&>(*this);
230 }
231 
232 
233 // ************************************************************************* //
Begin block [isseparator].
Definition: token.H:165
static const token undefinedToken
An undefined token.
Definition: token.H:542
const token & peekBack() const noexcept
Examine putback token without removing it.
Definition: Istream.C:42
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:214
bool getBack(token &tok)
Retrieve the put-back token if there is one.
Definition: Istream.C:115
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
virtual const std::istream & stdStream() const
Const access to underlying std::istream.
Definition: ISstream.H:154
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:134
char readEndList(const char *funcName)
End read of list data, ends with &#39;)&#39; or &#39;}&#39;.
Definition: Istream.C:192
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
void putBackClear()
Drop the putback token.
Definition: Istream.C:64
A token holds an item read from Istream.
Definition: token.H:65
Begin list [isseparator].
Definition: token.H:161
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition: Istream.C:71
End list [isseparator].
Definition: token.H:162
char readBeginList(const char *funcName)
Begin read of list data, starts with &#39;(&#39; or &#39;{&#39;.
Definition: Istream.C:171
const direction noexcept
Definition: Scalar.H:258
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:239
bool readEnd(const char *funcName)
End read of data chunk, ends with &#39;)&#39;.
Definition: Istream.C:152
#define FUNCTION_NAME
static void check(const int retVal, const char *what)
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
End block [isseparator].
Definition: token.H:166
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...