tokenIO.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-2015 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 "error.H"
27 #include "token.H"
28 
29 #include "IOstreams.H"
30 #include "scalar.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 :
36  type_(UNDEFINED)
37 {
38  is.read(*this);
39 }
40 
41 
42 // * * * * * * * * * * * * IOstream operators * * * * * * * * * * * * * * * //
43 
45 {
46  t.clear();
47  return is.read(t);
48 }
49 
50 
52 {
53  switch (t.type_)
54  {
55  case token::UNDEFINED:
56  os << "UNDEFINED";
58  << "Undefined token" << endl;
59  break;
60 
61  case token::PUNCTUATION:
62  os << t.punctuationToken_;
63  break;
64 
65  case token::WORD:
66  os << *t.wordTokenPtr_;
67  break;
68 
69  case token::STRING:
71  os << *t.stringTokenPtr_;
72  break;
73 
74  case token::VARIABLE:
75  // Behaviour differs according to stream type
76  os.write(t);
77  break;
78 
79  case token::LABEL:
80  os << t.labelToken_;
81  break;
82 
84  os << t.floatScalarToken_;
85  break;
86 
88  os << t.doubleScalarToken_;
89  break;
90 
91  case token::COMPOUND:
92  os << *t.compoundTokenPtr_;
93  break;
94 
95  case token::ERROR:
96  os << "ERROR";
98  << "Error token" << endl;
99  break;
100 
101  default:
102  os << "UNKNOWN";
104  << "Unknown token"
105  << endl;
106  }
107 
108  // Check state of stream
109  os.check("Ostream& operator<<(Ostream&, const token&)");
110 
111  return os;
112 }
113 
114 
115 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
116 {
117  return os << char(pt);
118 }
119 
120 
122 {
123  return os << char(pt);
124 }
125 
126 
128 {
129  os << ct.type() << token::SPACE;
130  ct.write(os);
131 
132  return os;
133 }
134 
135 
136 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
137 
138 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
139 {
140  const token& t = ip.t_;
141 
142  os << "on line " << t.lineNumber();
143 
144  switch (t.type())
145  {
146  case token::UNDEFINED:
147  os << " an undefined token";
148  break;
149 
150  case token::PUNCTUATION:
151  os << " the punctuation token " << '\'' << t.pToken() << '\'';
152  break;
153 
154  case token::WORD:
155  os << " the word " << '\'' << t.wordToken() << '\'';
156  break;
157 
158  case token::STRING:
159  os << " the string " << t.stringToken();
160  break;
161 
162  case token::VARIABLE:
163  os << " the variable " << t.stringToken();
164  break;
165 
167  os << " the verbatim string " << t.stringToken();
168  break;
169 
170  case token::LABEL:
171  os << " the label " << t.labelToken();
172  break;
173 
174  case token::FLOAT_SCALAR:
175  os << " the floatScalar " << t.floatScalarToken();
176  break;
177 
179  os << " the doubleScalar " << t.doubleScalarToken();
180  break;
181 
182  case token::COMPOUND:
183  {
184  if (t.compoundToken().empty())
185  {
186  os << " the empty compound of type "
187  << t.compoundToken().type();
188  }
189  else
190  {
191  os << " the compound of type "
192  << t.compoundToken().type();
193  }
194  }
195  break;
196 
197  case token::ERROR:
198  os << " an error";
199  break;
200 
201  default:
202  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
203  }
204 
205  return os;
206 }
207 
208 
209 template<>
210 Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<token>& ip)
211 {
212  const token& t = ip.t_;
213 
214  os << "on line " << t.lineNumber();
215 
216  switch (t.type())
217  {
218  case token::UNDEFINED:
219  os << " an undefined token";
220  break;
221 
222  case token::PUNCTUATION:
223  os << " the punctuation token " << '\'' << t.pToken() << '\'';
224  break;
225 
226  case token::WORD:
227  os << " the word " << '\'' << t.wordToken() << '\'';
228  break;
229 
230  case token::STRING:
231  os << " the string " << t.stringToken();
232  break;
233 
234  case token::VARIABLE:
235  os << " the variable " << t.stringToken();
236  break;
237 
239  os << " the verbatim string " << t.stringToken();
240  break;
241 
242  case token::LABEL:
243  os << " the label " << t.labelToken();
244  break;
245 
246  case token::FLOAT_SCALAR:
247  os << " the floatScalar " << t.floatScalarToken();
248  break;
249 
251  os << " the doubleScalar " << t.doubleScalarToken();
252  break;
253 
254  case token::COMPOUND:
255  {
256  if (t.compoundToken().empty())
257  {
258  os << " the empty compound of type "
259  << t.compoundToken().type();
260  }
261  else
262  {
263  os << " the compound of type "
264  << t.compoundToken().type();
265  }
266  }
267  break;
268 
269  case token::ERROR:
270  os << " an error";
271  break;
272 
273  default:
274  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
275  }
276 
277  return os;
278 }
279 
280 
281 // ************************************************************************* //
doubleScalar doubleScalarToken_
Definition: token.H:261
punctuationToken pToken() const
Definition: tokenI.H:200
compound * compoundTokenPtr_
Definition: token.H:262
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
label labelToken_
Definition: token.H:259
const word & wordToken() const
Definition: tokenI.H:218
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:69
string * stringTokenPtr_
Definition: token.H:258
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
word * wordTokenPtr_
Definition: token.H:257
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Abstract base class for complex tokens.
Definition: token.H:124
floatScalar floatScalarToken() const
Definition: tokenI.H:277
virtual Istream & read(token &)=0
Return next token from stream.
Istream & operator>>(Istream &, directionInfo &)
label lineNumber() const
Definition: tokenI.H:373
const compound & compoundToken() const
Definition: tokenI.H:359
bool empty() const
Definition: token.H:185
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual void write(Ostream &) const =0
const string & stringToken() const
Definition: tokenI.H:241
floatScalar floatScalarToken_
Definition: token.H:260
#define WarningInFunction
Report a warning using Foam::Warning.
tokenType type() const
Definition: tokenI.H:170
punctuationToken punctuationToken_
Definition: token.H:256
Ostream & operator<<(Ostream &, const ensightPart &)
label labelToken() const
Definition: tokenI.H:259
token()
Construct null.
Definition: tokenI.H:58
punctuationToken
Standard punctuation tokens.
Definition: token.H:94
virtual Ostream & write(const token &)=0
Write next token to stream.
doubleScalar doubleScalarToken() const
Definition: tokenI.H:296