FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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