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 | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Istream.H"
27 
28 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
29 
31 {
32  if (bad())
33  {
34  FatalIOErrorIn("void Istream::putBack(const token&)", *this)
35  << "Attempt to put back onto bad stream"
36  << exit(FatalIOError);
37  }
38  else if (putBack_)
39  {
40  FatalIOErrorIn("void Istream::putBack(const token&)", *this)
41  << "Attempt to put back another token"
42  << exit(FatalIOError);
43  }
44  else
45  {
46  putBackToken_ = t;
47  putBack_ = true;
48  }
49 }
50 
51 
53 {
54  if (bad())
55  {
56  FatalIOErrorIn("void Istream::getBack(token&)", *this)
57  << "Attempt to get back from bad stream"
58  << exit(FatalIOError);
59  }
60  else if (putBack_)
61  {
62  t = putBackToken_;
63  putBack_ = false;
64  return true;
65  }
66 
67  return false;
68 }
69 
70 
72 {
73  if (putBack_)
74  {
75  t = putBackToken_;
76  }
77  else
78  {
80  }
81 
82  return putBack_;
83 }
84 
85 
86 // Functions for reading object delimiters ( ... )
87 
89 {
90  token delimiter(*this);
91  if (delimiter != token::BEGIN_LIST)
92  {
93  setBad();
94  FatalIOErrorIn("Istream::readBegin(const char*)", *this)
95  << "Expected a '" << token::BEGIN_LIST
96  << "' while reading " << funcName
97  << ", found " << delimiter.info()
98  << exit(FatalIOError);
99  }
100 
101  return *this;
102 }
103 
104 
105 Foam::Istream& Foam::Istream::readEnd(const char* funcName)
106 {
107  token delimiter(*this);
108  if (delimiter != token::END_LIST)
109  {
110  setBad();
111  FatalIOErrorIn("Istream::readEnd(const char*)", *this)
112  << "Expected a '" << token::END_LIST
113  << "' while reading " << funcName
114  << ", found " << delimiter.info()
115  << exit(FatalIOError);
116  }
117 
118  return *this;
119 }
120 
121 
123 {
124  readEnd(funcName);
125  return readBegin(funcName);
126 }
127 
128 
129 // Functions for reading List delimiters ( ... ) or { ... }
130 
131 char Foam::Istream::readBeginList(const char* funcName)
132 {
133  token delimiter(*this);
134 
135  if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
136  {
137  setBad();
138  FatalIOErrorIn("Istream::readBeginList(const char*)", *this)
139  << "Expected a '" << token::BEGIN_LIST
140  << "' or a '" << token::BEGIN_BLOCK
141  << "' while reading " << funcName
142  << ", found " << delimiter.info()
143  << exit(FatalIOError);
144 
145  return '\0';
146  }
147 
148  return delimiter.pToken();
149 }
150 
151 
152 char Foam::Istream::readEndList(const char* funcName)
153 {
154  token delimiter(*this);
155 
156  if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
157  {
158  setBad();
159  FatalIOErrorIn("Istream::readEndList(const char*)", *this)
160  << "Expected a '" << token::END_LIST
161  << "' or a '" << token::END_BLOCK
162  << "' while reading " << funcName
163  << ", found " << delimiter.info()
164  << exit(FatalIOError);
165 
166  return '\0';
167  }
168 
169  return delimiter.pToken();
170 }
171 
172 
174 {
175  if (!good())
176  {
177  check("Istream::operator()");
178  FatalIOError.exit();
179  }
180 
181  return const_cast<Istream&>(*this);
182 }
183 
184 
185 // ************************************************************************* //
static token undefinedToken
Static undefined token.
Definition: token.H:241
Istream & readEndBegin(const char *funcName)
Definition: Istream.C:122
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
char readBeginList(const char *funcName)
Definition: Istream.C:131
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool getBack(token &)
Get the put back token if there is one and return true.
Definition: Istream.C:52
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
IOerror FatalIOError
void putBack(const token &)
Put back token.
Definition: Istream.C:30
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
char readEndList(const char *funcName)
Definition: Istream.C:152
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
A token holds items read from Istream.
Definition: token.H:67
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
Istream & readBegin(const char *funcName)
Definition: Istream.C:88
punctuationToken pToken() const
Definition: tokenI.H:208
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
bool peekBack(token &)
Peek at the put back token without removing it.
Definition: Istream.C:71
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:169
Istream & readEnd(const char *funcName)
Definition: Istream.C:105
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:173