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

primitiveEntryIO.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     PrimitiveEntry constructor from Istream and Ostream output operator.
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "primitiveEntry.H"
00031 #include "OSspecific.H"
00032 #include "functionEntry.H"
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 void Foam::primitiveEntry::append
00037 (
00038     const token& currToken,
00039     const dictionary& dict,
00040     Istream& is
00041 )
00042 {
00043     if (currToken.isWord())
00044     {
00045         const word& w = currToken.wordToken();
00046 
00047         if
00048         (
00049             w.size() == 1
00050          || (
00051                 !(w[0] == '$' && expandVariable(w, dict))
00052              && !(w[0] == '#' && expandFunction(w, dict, is))
00053             )
00054         )
00055         {
00056             newElmt(tokenIndex()++) = currToken;
00057         }
00058     }
00059     else
00060     {
00061         newElmt(tokenIndex()++) = currToken;
00062     }
00063 }
00064 
00065 
00066 void Foam::primitiveEntry::append(const tokenList& varTokens)
00067 {
00068     forAll(varTokens, i)
00069     {
00070         newElmt(tokenIndex()++) = varTokens[i];
00071     }
00072 }
00073 
00074 
00075 bool Foam::primitiveEntry::expandVariable
00076 (
00077     const word& w,
00078     const dictionary& dict
00079 )
00080 {
00081     word varName = w(1, w.size()-1);
00082 
00083     // lookup the variable name in the given dictionary....
00084     const entry* ePtr = dict.lookupEntryPtr(varName, true, true);
00085 
00086     // ...if defined insert its tokens into this
00087     if (ePtr != NULL)
00088     {
00089         append(ePtr->stream());
00090         return true;
00091     }
00092     else
00093     {
00094         // if not in the dictionary see if it is an environment
00095         // variable
00096 
00097         string enVarString = getEnv(varName);
00098 
00099         if (enVarString.size())
00100         {
00101             append(tokenList(IStringStream('(' + enVarString + ')')()));
00102             return true;
00103         }
00104 
00105         return false;
00106     }
00107 }
00108 
00109 
00110 bool Foam::primitiveEntry::expandFunction
00111 (
00112     const word& keyword,
00113     const dictionary& parentDict,
00114     Istream& is
00115 )
00116 {
00117     word functionName = keyword(1, keyword.size()-1);
00118     return functionEntry::execute(functionName, parentDict, *this, is);
00119 }
00120 
00121 
00122 bool Foam::primitiveEntry::read(const dictionary& dict, Istream& is)
00123 {
00124     is.fatalCheck
00125     (
00126         "primitiveEntry::readData(const dictionary&, Istream&)"
00127     );
00128 
00129     label blockCount = 0;
00130     token currToken;
00131 
00132     if
00133     (
00134         !is.read(currToken).bad()
00135      && currToken.good()
00136      && currToken != token::END_STATEMENT
00137     )
00138     {
00139         append(currToken, dict, is);
00140 
00141         if
00142         (
00143             currToken == token::BEGIN_BLOCK
00144          || currToken == token::BEGIN_LIST
00145         )
00146         {
00147             blockCount++;
00148         }
00149 
00150         while
00151         (
00152             !is.read(currToken).bad()
00153          && currToken.good()
00154          && !(currToken == token::END_STATEMENT && blockCount == 0)
00155         )
00156         {
00157             if
00158             (
00159                 currToken == token::BEGIN_BLOCK
00160              || currToken == token::BEGIN_LIST
00161             )
00162             {
00163                 blockCount++;
00164             }
00165             else if
00166             (
00167                 currToken == token::END_BLOCK
00168              || currToken == token::END_LIST
00169             )
00170             {
00171                 blockCount--;
00172             }
00173 
00174             append(currToken, dict, is);
00175         }
00176     }
00177 
00178     is.fatalCheck
00179     (
00180         "primitiveEntry::readData(const dictionary&, Istream&)"
00181     );
00182 
00183     if (currToken.good())
00184     {
00185         return true;
00186     }
00187     else
00188     {
00189         return false;
00190     }
00191 }
00192 
00193 
00194 void Foam::primitiveEntry::readEntry(const dictionary& dict, Istream& is)
00195 {
00196     label keywordLineNumber = is.lineNumber();
00197     tokenIndex() = 0;
00198 
00199     if (read(dict, is))
00200     {
00201         setSize(tokenIndex());
00202         tokenIndex() = 0;
00203     }
00204     else
00205     {
00206         FatalIOErrorIn
00207         (
00208             "primitiveEntry::readEntry(const dictionary&, Istream&)",
00209             is
00210         )   << "ill defined primitiveEntry starting at keyword '"
00211             << keyword() << '\''
00212             << " on line " << keywordLineNumber
00213             << " and ending at line " << is.lineNumber()
00214             << exit(FatalIOError);
00215     }
00216 }
00217 
00218 
00219 Foam::primitiveEntry::primitiveEntry
00220 (
00221     const keyType& key,
00222     const dictionary& dict,
00223     Istream& is
00224 )
00225 :
00226     entry(key),
00227     ITstream
00228     (
00229         is.name() + "::" + key,
00230         tokenList(10),
00231         is.format(),
00232         is.version()
00233     )
00234 {
00235     readEntry(dict, is);
00236 }
00237 
00238 
00239 Foam::primitiveEntry::primitiveEntry(const keyType& key, Istream& is)
00240 :
00241     entry(key),
00242     ITstream
00243     (
00244         is.name() + "::" + key,
00245         tokenList(10),
00246         is.format(),
00247         is.version()
00248     )
00249 {
00250     readEntry(dictionary::null, is);
00251 }
00252 
00253 
00254 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00255 
00256 void Foam::primitiveEntry::write(Ostream& os) const
00257 {
00258     os.writeKeyword(keyword());
00259 
00260     for (label i=0; i<size(); i++)
00261     {
00262         os << operator[](i);
00263 
00264         if (i < size()-1)
00265         {
00266             os << token::SPACE;
00267         }
00268     }
00269 
00270     os << token::END_STATEMENT << endl;
00271 }
00272 
00273 
00274 // * * * * * * * * * * * * * Ostream operator  * * * * * * * * * * * * * * * //
00275 
00276 template<>
00277 Foam::Ostream& Foam::operator<<
00278 (
00279     Ostream& os,
00280     const InfoProxy<primitiveEntry>& ip
00281 )
00282 {
00283     const primitiveEntry& e = ip.t_;
00284 
00285     e.print(os);
00286 
00287     const label nPrintTokens = 10;
00288 
00289     os  << "    primitiveEntry '" << e.keyword() << "' comprises ";
00290 
00291     for (label i=0; i<min(e.size(), nPrintTokens); i++)
00292     {
00293         os  << nl << "        " << e[i].info();
00294     }
00295 
00296     if (e.size() > nPrintTokens)
00297     {
00298         os  << " ...";
00299     }
00300 
00301     os  << endl;
00302 
00303     return os;
00304 }
00305 
00306 
00307 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd