BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / dictionary / entry / entryIO.C
blob3807c3bfafc6a1a2b67507894c440d30eefa2377
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "primitiveEntry.H"
27 #include "dictionaryEntry.H"
28 #include "functionEntry.H"
29 #include "includeEntry.H"
30 #include "inputModeEntry.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
36     token keywordToken;
38     // Read the next valid token discarding spurious ';'s
39     do
40     {
41         if
42         (
43             is.read(keywordToken).bad()
44          || is.eof()
45          || !keywordToken.good()
46         )
47         {
48             return false;
49         }
50     }
51     while (keywordToken == token::END_STATEMENT);
53     // If the token is a valid keyword set 'keyword' return true...
54     if (keywordToken.isWord())
55     {
56         keyword = keywordToken.wordToken();
57         return true;
58     }
59     else if (keywordToken.isString())
60     {
61         // Enable wildcards
62         keyword = keywordToken.stringToken();
63         return true;
64     }
65     // If it is the end of the dictionary or file return false...
66     else if (keywordToken == token::END_BLOCK || is.eof())
67     {
68         return false;
69     }
70     // Otherwise the token is invalid
71     else
72     {
73         cerr<< "--> FOAM Warning : " << std::endl
74             << "    From function "
75             << "entry::getKeyword(keyType&, Istream&)" << std::endl
76             << "    in file " << __FILE__
77             << " at line " << __LINE__ << std::endl
78             << "    Reading " << is.name().c_str() << std::endl
79             << "    found " << keywordToken << std::endl
80             << "    expected either " << token::END_BLOCK << " or EOF"
81             << std::endl;
83         return false;
84     }
88 bool Foam::entry::New(dictionary& parentDict, Istream& is)
90     is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)");
92     keyType keyword;
94     // Get the next keyword and if invalid return false
95     if (!getKeyword(keyword, is))
96     {
97         return false;
98     }
99     else  // Keyword starts entry ...
100     {
101         if
102         (
103            !disableFunctionEntries
104          && keyword[0] == '#'
105         )                           // ... Function entry
106         {
107             word functionName = keyword(1, keyword.size()-1);
108             return functionEntry::execute(functionName, parentDict, is);
109         }
110         else if
111         (
112            !disableFunctionEntries
113          && keyword[0] == '$')      // ... Substitution entry
114         {
115             parentDict.substituteKeyword(keyword);
116             return true;
117         }
118         else if
119         (
120            !disableFunctionEntries
121          && keyword == "include"
122         )                           // ... For backward compatibility
123         {
124             return functionEntries::includeEntry::execute(parentDict, is);
125         }
126         else                        // ... Data entries
127         {
128             token nextToken(is);
129             is.putBack(nextToken);
131             // Deal with duplicate entries
132             bool mergeEntry = false;
134             // See (using exact match) if entry already present
135             entry* existingPtr = parentDict.lookupEntryPtr
136             (
137                 keyword,
138                 false,
139                 false
140             );
142             if (existingPtr)
143             {
144                 if (functionEntries::inputModeEntry::merge())
145                 {
146                     mergeEntry = true;
147                 }
148                 else if (functionEntries::inputModeEntry::overwrite())
149                 {
150                     // clear dictionary so merge acts like overwrite
151                     if (existingPtr->isDict())
152                     {
153                         existingPtr->dict().clear();
154                     }
155                     mergeEntry = true;
156                 }
157                 else if (functionEntries::inputModeEntry::protect())
158                 {
159                     // read and discard the entry
160                     if (nextToken == token::BEGIN_BLOCK)
161                     {
162                         dictionaryEntry dummy(keyword, parentDict, is);
163                     }
164                     else
165                     {
166                         primitiveEntry  dummy(keyword, parentDict, is);
167                     }
168                     return true;
169                 }
170                 else if (functionEntries::inputModeEntry::error())
171                 {
172                     FatalIOErrorIn
173                     (
174                         "entry::New(const dictionary& parentDict, Istream&)",
175                         is
176                     )
177                         << "ERROR! duplicate entry: " << keyword
178                         << exit(FatalIOError);
180                     return false;
181                 }
182             }
184             if (nextToken == token::BEGIN_BLOCK)
185             {
186                 return parentDict.add
187                 (
188                     new dictionaryEntry(keyword, parentDict, is),
189                     mergeEntry
190                 );
191             }
192             else
193             {
194                 return parentDict.add
195                 (
196                     new primitiveEntry(keyword, parentDict, is),
197                     mergeEntry
198                 );
199             }
200         }
201     }
205 Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is)
207     is.fatalCheck("entry::New(Istream&)");
209     keyType keyword;
211     // Get the next keyword and if invalid return false
212     if (!getKeyword(keyword, is))
213     {
214         return autoPtr<entry>(NULL);
215     }
216     else // Keyword starts entry ...
217     {
218         token nextToken(is);
219         is.putBack(nextToken);
221         if (nextToken == token::BEGIN_BLOCK)
222         {
223             return autoPtr<entry>
224             (
225                 new dictionaryEntry(keyword, dictionary::null, is)
226             );
227         }
228         else
229         {
230             return autoPtr<entry>
231             (
232                 new primitiveEntry(keyword, is)
233             );
234         }
235     }
239 // * * * * * * * * * * * * * Ostream operator  * * * * * * * * * * * * * * * //
241 Foam::Ostream& Foam::operator<<(Ostream& os, const entry& e)
243     e.write(os);
244     return os;
248 // ************************************************************************* //