initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / db / dictionary / entry / entryIO.C
blob830c21cf8a7cf759cc6b00a4c10b35a2e961b098
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "primitiveEntry.H"
28 #include "dictionaryEntry.H"
29 #include "functionEntry.H"
30 #include "includeEntry.H"
31 #include "inputModeEntry.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 bool Foam::entry::getKeyword(word& keyword, Istream& is)
37     token keywordToken;
39     // Read the next valid token discarding spurious ';'s
40     do
41     {
42         if
43         (
44             is.read(keywordToken).bad()
45          || is.eof()
46          || !keywordToken.good()
47         )
48         {
49             return false;
50         }
51     }
52     while (keywordToken == token::END_STATEMENT);
54     // If the token is a valid keyword set 'keyword' return true...
55     if (keywordToken.isWord())
56     {
57         keyword = keywordToken.wordToken();
58         return true;
59     }
60     // If it is the end of the dictionary or file return false...
61     else if (keywordToken == token::END_BLOCK || is.eof())
62     {
63         return false;
64     }
65     // Otherwise the token is invalid
66     else
67     {
68         cerr<< "--> FOAM Warning : " << std::endl
69             << "    From function "
70             << "entry::getKeyword(word& keyword, Istream& is)" << std::endl
71             << "    in file " << __FILE__
72             << " at line " << __LINE__ << std::endl
73             << "    Reading " << is.name().c_str() << std::endl
74             << "    found " << keywordToken << std::endl
75             << "    expected either " << token::END_BLOCK << " or EOF"
76             << std::endl;
78         return false;
79     }
83 bool Foam::entry::New(dictionary& parentDict, Istream& is)
85     is.fatalCheck("entry::New(const dictionary& parentDict, Istream& is)");
87     word keyword;
89     // Get the next keyword and if invalid return false
90     if (!getKeyword(keyword, is))
91     {
92         return false;
93     }
94     else // Keyword starts entry ...
95     {
96         if (keyword[0] == '#')        // ... Function entry
97         {
98             word functionName = keyword(1, keyword.size()-1);
99             return functionEntry::execute(functionName, parentDict, is);
100         }
101         else if (keyword[0] == '$')    // ... Substitution entry
102         {
103             parentDict.substituteKeyword(keyword);
104             return true;
105         }
106         else if (keyword == "include") // ... For backward compatibility
107         {
108             return functionEntries::includeEntry::execute(parentDict, is);
109         }
110         else                           // ... Data entries
111         {
112             token nextToken(is);
113             is.putBack(nextToken);
115             // Deal with duplicate entries
116             bool mergeEntry = false;
118             entry* existingPtr = parentDict.lookupEntryPtr(keyword);
119             if (existingPtr)
120             {
121                 if (functionEntries::inputModeEntry::overwrite())
122                 {
123                     // clear dictionary so merge acts like overwrite
124                     if (existingPtr->isDict())
125                     {
126                         existingPtr->dict().clear();
127                     }
128                     mergeEntry = true;
129                 }
130                 else if (functionEntries::inputModeEntry::merge())
131                 {
132                     mergeEntry = true;
133                 }
134             }
136             if (nextToken == token::BEGIN_BLOCK)
137             {
138                 return parentDict.add
139                 (
140                     new dictionaryEntry(keyword, parentDict, is),
141                     mergeEntry
142                 );
143             }
144             else
145             {
146                 return parentDict.add
147                 (
148                     new primitiveEntry(keyword, parentDict, is),
149                     mergeEntry
150                 );
151             }
152         }
153     }
157 Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is)
159     is.fatalCheck("entry::New(Istream& is)");
161     word keyword;
163     // Get the next keyword and if invalid return false
164     if (!getKeyword(keyword, is))
165     {
166         return autoPtr<entry>(NULL);
167     }
168     else // Keyword starts entry ...
169     {
170         token nextToken(is);
171         is.putBack(nextToken);
173         if (nextToken == token::BEGIN_BLOCK)
174         {
175             return autoPtr<entry>
176             (
177                 new dictionaryEntry(keyword, dictionary::null, is)
178             );
179         }
180         else
181         {
182             return autoPtr<entry>
183             (
184                 new primitiveEntry(keyword, is)
185             );
186         }
187     }
191 // * * * * * * * * * * * * * Ostream operator  * * * * * * * * * * * * * * * //
193 Foam::Ostream& Foam::operator<<(Ostream& os, const entry& e)
195     e.write(os);
196     return os;
200 // ************************************************************************* //