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-2019 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 Foam
35 {
36  // Return the current get position for std input stream
37  static inline std::streampos tellg(Istream* isptr)
38  {
39  ISstream* sptr = dynamic_cast<ISstream*>(isptr);
40 
41  if (sptr)
42  {
43  return sptr->stdStream().tellg();
44  }
45 
46  return 0;
47  }
48 }
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
53 void Foam::Istream::putBack(const token& tok)
54 {
55  if (bad())
56  {
58  << "Attempt to put back onto bad stream"
59  << exit(FatalIOError);
60  }
61  else if (putBack_)
62  {
64  << "Attempt to put back another token"
65  << exit(FatalIOError);
66  }
67  else
68  {
69  putBackToken_ = tok;
70  putBack_ = true;
71  }
72 }
73 
74 
76 {
77  if (bad())
78  {
80  << "Attempt to get back from bad stream"
81  << exit(FatalIOError);
82  }
83  else if (putBack_)
84  {
85  tok = putBackToken_;
86  putBack_ = false;
87  return true;
88  }
89 
90  return false;
91 }
92 
93 
95 {
96  if (putBack_)
97  {
98  tok = putBackToken_;
99  }
100  else
101  {
102  tok.reset();
103  }
104 
105  return putBack_;
106 }
107 
108 
109 bool Foam::Istream::readBegin(const char* funcName)
110 {
111  const token delimiter(*this);
112 
113  if (delimiter != token::BEGIN_LIST)
114  {
115  setBad();
117  << "Expected a '" << token::BEGIN_LIST
118  << "' while reading " << funcName
119  << ", found " << delimiter.info() << nl
120  << exit(FatalIOError);
121  }
122 
123  return true;
124 }
125 
126 
127 bool Foam::Istream::readEnd(const char* funcName)
128 {
129  const token delimiter(*this);
130 
131  if (delimiter != token::END_LIST)
132  {
133  setBad();
135  << "Expected a '" << token::END_LIST
136  << "' while reading " << funcName
137  << ", found " << delimiter.info()
138  << " at stream position " << tellg(this) << nl
139  << exit(FatalIOError);
140  }
141 
142  return true;
143 }
144 
145 
146 char Foam::Istream::readBeginList(const char* funcName)
147 {
148  const token delimiter(*this);
149 
150  if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
151  {
152  setBad();
154  << "Expected a '" << token::BEGIN_LIST
155  << "' or a '" << token::BEGIN_BLOCK
156  << "' while reading " << funcName
157  << ", found " << delimiter.info()
158  << exit(FatalIOError);
159 
160  return '\0';
161  }
162 
163  return delimiter.pToken();
164 }
165 
166 
167 char Foam::Istream::readEndList(const char* funcName)
168 {
169  const token delimiter(*this);
170 
171  if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
172  {
173  setBad();
175  << "Expected a '" << token::END_LIST
176  << "' or a '" << token::END_BLOCK
177  << "' while reading " << funcName
178  << ", found " << delimiter.info()
179  << " at stream position " << tellg(this) << nl
180  << exit(FatalIOError);
181 
182  return '\0';
183  }
184 
185  return delimiter.pToken();
186 }
187 
188 
190 {
191  if (!good())
192  {
193  check(FUNCTION_NAME);
194  FatalIOError.exit();
195  }
196 
197  return const_cast<Istream&>(*this);
198 }
199 
200 
201 // ************************************************************************* //
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:146
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:167
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:127
Foam::tellg
static std::streampos tellg(Istream *isptr)
Definition: Istream.C:37
Foam::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:459
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:109
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:551
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:256
Istream.H
Foam::token::END_BLOCK
End block [isseparator].
Definition: token.H:127
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:242
Foam::Istream::operator()
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:189
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:126
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:209
Foam::Istream::getBack
bool getBack(token &tok)
Get the put back token if there is one and return true.
Definition: Istream.C:75
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:249
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:123
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:122
Foam::Istream::peekBack
bool peekBack(token &tok)
Peek at the put back token without removing it.
Definition: Istream.C:94