FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 Description
25  Stream operators for token
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include <OpenFOAM/error.H>
30 #include "token.H"
31 
32 #include <OpenFOAM/IOstreams.H>
33 #include <OpenFOAM/scalar.H>
34 
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 
38 :
39  type_(UNDEFINED)
40 {
41  is.read(*this);
42 }
43 
44 
45 // * * * * * * * * * * * * IOstream operators * * * * * * * * * * * * * * * //
46 
48 {
49  t.clear();
50  return is.read(t);
51 }
52 
53 
55 {
56  switch (t.type_)
57  {
58  case token::UNDEFINED:
59  os << "UNDEFINED";
60  WarningIn("Ostream& operator<<(Ostream&, const token&)")
61  << "Undefined token" << endl;
62  break;
63 
64  case token::PUNCTUATION:
65  os << t.punctuationToken_;
66  break;
67 
68  case token::WORD:
69  os << *t.wordTokenPtr_;
70  break;
71 
72  case token::STRING:
73  os << *t.stringTokenPtr_;
74  break;
75 
76  case token::LABEL:
77  os << t.labelToken_;
78  break;
79 
81  os << t.floatScalarToken_;
82  break;
83 
85  os << t.doubleScalarToken_;
86  break;
87 
88  case token::COMPOUND:
89  os << *t.compoundTokenPtr_;
90  break;
91 
92  case token::ERROR:
93  os << "ERROR";
94  WarningIn("Ostream& operator<<(Ostream&, const token&)")
95  << "Error token" << endl;
96  break;
97 
98  default:
99  os << "UNKNOWN";
100  SeriousErrorIn("Ostream& operator<<(Ostream&, const token&)")
101  << "Unknown token"
102  << endl;
103  }
104 
105  // Check state of stream
106  os.check("Ostream& operator<<(Ostream&, const token&)");
107 
108  return os;
109 }
110 
111 
112 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
113 {
114  return os << char(pt);
115 }
116 
117 
119 {
120  return os << char(pt);
121 }
122 
123 
125 {
126  os << ct.type() << token::SPACE;
127  ct.write(os);
128 
129  return os;
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
134 
135 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
136 {
137  const token& t = ip.t_;
138 
139  os << "on line " << t.lineNumber();
140 
141  switch (t.type())
142  {
143  case token::UNDEFINED:
144  os << " an undefined token";
145  break;
146 
147  case token::PUNCTUATION:
148  os << " the punctuation token " << '\'' << t.pToken() << '\'';
149  break;
150 
151  case token::WORD:
152  os << " the word " << '\'' << t.wordToken() << '\'';
153  break;
154 
155  case token::STRING:
156  os << " the string " << t.stringToken();
157  break;
158 
159  case token::LABEL:
160  os << " the label " << t.labelToken();
161  break;
162 
163  case token::FLOAT_SCALAR:
164  os << " the floatScalar " << t.floatScalarToken();
165  break;
166 
168  os << " the doubleScalar " << t.doubleScalarToken();
169  break;
170 
171  case token::COMPOUND:
172  {
173  if (t.compoundToken().empty())
174  {
175  os << " the empty compound of type "
176  << t.compoundToken().type();
177  }
178  else
179  {
180  os << " the compound of type "
181  << t.compoundToken().type();
182  }
183  }
184  break;
185 
186  case token::ERROR:
187  os << " an error";
188  break;
189 
190  default:
191  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
192  }
193 
194  return os;
195 }
196 
197 
198 // template specialization
199 namespace Foam
200 {
201 
202 #if defined (__GNUC__)
203 template<>
204 #endif
205 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip)
206 {
207  const token& t = ip.t_;
208 
209  os << "on line " << t.lineNumber();
210 
211  switch (t.type())
212  {
213  case token::UNDEFINED:
214  os << " an undefined token";
215  break;
216 
217  case token::PUNCTUATION:
218  os << " the punctuation token " << '\'' << t.pToken() << '\'';
219  break;
220 
221  case token::WORD:
222  os << " the word " << '\'' << t.wordToken() << '\'';
223  break;
224 
225  case token::STRING:
226  os << " the string " << t.stringToken();
227  break;
228 
229  case token::LABEL:
230  os << " the label " << t.labelToken();
231  break;
232 
233  case token::FLOAT_SCALAR:
234  os << " the floatScalar " << t.floatScalarToken();
235  break;
236 
238  os << " the doubleScalar " << t.doubleScalarToken();
239  break;
240 
241  case token::COMPOUND:
242  {
243  if (t.compoundToken().empty())
244  {
245  os << " the empty compound of type "
246  << t.compoundToken().type();
247  }
248  else
249  {
250  os << " the compound of type "
251  << t.compoundToken().type();
252  }
253  }
254  break;
255 
256  case token::ERROR:
257  os << " an error";
258  break;
259 
260  default:
261  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
262  }
263 
264  return os;
265 }
266 
267 
268 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
269 
270 } // End namespace Foam
271 
272 // ************************ vim: set sw=4 sts=4 et: ************************ //