OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

tokenIO.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Stream operators for token
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "error.H"
00031 #include "token.H"
00032 
00033 #include "IOstreams.H"
00034 #include "scalar.H"
00035 
00036 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00037 
00038 Foam::token::token(Istream& is)
00039 :
00040     type_(UNDEFINED)
00041 {
00042     is.read(*this);
00043 }
00044 
00045 
00046 // * * * * * * * * * * * * IOstream operators  * * * * * * * * * * * * * * * //
00047 
00048 Foam::Istream& Foam::operator>>(Istream& is, token& t)
00049 {
00050     t.clear();
00051     return is.read(t);
00052 }
00053 
00054 
00055 Foam::Ostream& Foam::operator<<(Ostream& os, const token& t)
00056 {
00057     switch (t.type_)
00058     {
00059         case token::UNDEFINED:
00060             os << "UNDEFINED";
00061             WarningIn("Ostream& operator<<(Ostream&, const token&)")
00062                 << "Undefined token" << endl;
00063         break;
00064 
00065         case token::PUNCTUATION:
00066             os << t.punctuationToken_;
00067         break;
00068 
00069         case token::WORD:
00070             os << *t.wordTokenPtr_;
00071         break;
00072 
00073         case token::STRING:
00074             os << *t.stringTokenPtr_;
00075         break;
00076 
00077         case token::LABEL:
00078             os << t.labelToken_;
00079         break;
00080 
00081         case token::FLOAT_SCALAR:
00082             os << t.floatScalarToken_;
00083         break;
00084 
00085         case token::DOUBLE_SCALAR:
00086             os << t.doubleScalarToken_;
00087         break;
00088 
00089         case token::COMPOUND:
00090             os << *t.compoundTokenPtr_;
00091         break;
00092 
00093         case token::ERROR:
00094             os << "ERROR";
00095             WarningIn("Ostream& operator<<(Ostream&, const token&)")
00096                 << "Error token" << endl;
00097         break;
00098 
00099         default:
00100             os << "UNKNOWN";
00101             SeriousErrorIn("Ostream& operator<<(Ostream&, const token&)")
00102                 << "Unknown token"
00103                 << endl;
00104     }
00105 
00106     // Check state of stream
00107     os.check("Ostream& operator<<(Ostream&, const token&)");
00108 
00109     return os;
00110 }
00111 
00112 
00113 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
00114 {
00115     return os << char(pt);
00116 }
00117 
00118 
00119 Foam::Ostream& Foam::operator<<(Ostream& os, const token::punctuationToken& pt)
00120 {
00121     return os << char(pt);
00122 }
00123 
00124 
00125 Foam::Ostream& Foam::operator<<(Ostream& os, const token::compound& ct)
00126 {
00127     os << ct.type() << token::SPACE;
00128     ct.write(os);
00129 
00130     return os;
00131 }
00132 
00133 
00134 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00135 
00136 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
00137 {
00138     const token& t = ip.t_;
00139 
00140     os  << "on line " << t.lineNumber();
00141 
00142     switch (t.type())
00143     {
00144         case token::UNDEFINED:
00145             os  << " an undefined token";
00146         break;
00147 
00148         case token::PUNCTUATION:
00149             os  << " the punctuation token " << '\'' << t.pToken() << '\'';
00150         break;
00151 
00152         case token::WORD:
00153             os  << " the word " << '\'' << t.wordToken() << '\'';
00154         break;
00155 
00156         case token::STRING:
00157             os  << " the string " << t.stringToken();
00158         break;
00159 
00160         case token::LABEL:
00161             os  << " the label " << t.labelToken();
00162         break;
00163 
00164         case token::FLOAT_SCALAR:
00165             os  << " the floatScalar " << t.floatScalarToken();
00166         break;
00167 
00168         case token::DOUBLE_SCALAR:
00169             os  << " the doubleScalar " << t.doubleScalarToken();
00170         break;
00171 
00172         case token::COMPOUND:
00173         {
00174             if (t.compoundToken().empty())
00175             {
00176                 os  << " the empty compound of type "
00177                     << t.compoundToken().type();
00178             }
00179             else
00180             {
00181                 os  << " the compound of type "
00182                     << t.compoundToken().type();
00183             }
00184         }
00185         break;
00186 
00187         case token::ERROR:
00188             os  << " an error";
00189         break;
00190 
00191         default:
00192             os  << " an unknown token type " << '\'' << int(t.type()) << '\'';
00193     }
00194 
00195     return os;
00196 }
00197 
00198 
00199 // template specialization
00200 namespace Foam
00201 {
00202 
00203 #if defined (__GNUC__)
00204 template<>
00205 #endif
00206 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip)
00207 {
00208     const token& t = ip.t_;
00209 
00210     os  << "on line " << t.lineNumber();
00211 
00212     switch (t.type())
00213     {
00214         case token::UNDEFINED:
00215             os  << " an undefined token";
00216         break;
00217 
00218         case token::PUNCTUATION:
00219             os  << " the punctuation token " << '\'' << t.pToken() << '\'';
00220         break;
00221 
00222         case token::WORD:
00223             os  << " the word " << '\'' << t.wordToken() << '\'';
00224         break;
00225 
00226         case token::STRING:
00227             os  << " the string " << t.stringToken();
00228         break;
00229 
00230         case token::LABEL:
00231             os  << " the label " << t.labelToken();
00232         break;
00233 
00234         case token::FLOAT_SCALAR:
00235             os  << " the floatScalar " << t.floatScalarToken();
00236         break;
00237 
00238         case token::DOUBLE_SCALAR:
00239             os  << " the doubleScalar " << t.doubleScalarToken();
00240         break;
00241 
00242         case token::COMPOUND:
00243         {
00244             if (t.compoundToken().empty())
00245             {
00246                 os  << " the empty compound of type "
00247                     << t.compoundToken().type();
00248             }
00249             else
00250             {
00251                 os  << " the compound of type "
00252                     << t.compoundToken().type();
00253             }
00254         }
00255         break;
00256 
00257         case token::ERROR:
00258             os  << " an error";
00259         break;
00260 
00261         default:
00262             os  << " an unknown token type "  << '\'' << int(t.type()) << '\'';
00263     }
00264 
00265     return os;
00266 }
00267 
00268 
00269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00270 
00271 } // End namespace Foam
00272 
00273 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd