FreeFOAM The Cross-Platform CFD Toolkit
entryIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
28 #include <OpenFOAM/functionEntry.H>
29 #include <OpenFOAM/includeEntry.H>
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
35 {
36  token keywordToken;
37 
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);
52 
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;
82 
83  return false;
84  }
85 }
86 
87 
88 bool Foam::entry::New(dictionary& parentDict, Istream& is)
89 {
90  is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)");
91 
92  keyType keyword;
93 
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 (keyword[0] == '#') // ... Function entry
102  {
103  word functionName = keyword(1, keyword.size()-1);
104  return functionEntry::execute(functionName, parentDict, is);
105  }
106  else if (keyword[0] == '$') // ... Substitution entry
107  {
108  parentDict.substituteKeyword(keyword);
109  return true;
110  }
111  else if (keyword == "include") // ... For backward compatibility
112  {
113  return functionEntries::includeEntry::execute(parentDict, is);
114  }
115  else // ... Data entries
116  {
117  token nextToken(is);
118  is.putBack(nextToken);
119 
120  // Deal with duplicate entries
121  bool mergeEntry = false;
122 
123  // See (using exact match) if entry already present
124  entry* existingPtr = parentDict.lookupEntryPtr
125  (
126  keyword,
127  false,
128  false
129  );
130 
131  if (existingPtr)
132  {
134  {
135  mergeEntry = true;
136  }
138  {
139  // clear dictionary so merge acts like overwrite
140  if (existingPtr->isDict())
141  {
142  existingPtr->dict().clear();
143  }
144  mergeEntry = true;
145  }
147  {
148  // read and discard the entry
149  if (nextToken == token::BEGIN_BLOCK)
150  {
151  dictionaryEntry dummy(keyword, parentDict, is);
152  }
153  else
154  {
155  primitiveEntry dummy(keyword, parentDict, is);
156  }
157  return true;
158  }
160  {
162  (
163  "entry::New(const dictionary& parentDict, Istream&)",
164  is
165  )
166  << "ERROR! duplicate entry: " << keyword
167  << exit(FatalIOError);
168 
169  return false;
170  }
171  }
172 
173  if (nextToken == token::BEGIN_BLOCK)
174  {
175  return parentDict.add
176  (
177  new dictionaryEntry(keyword, parentDict, is),
178  mergeEntry
179  );
180  }
181  else
182  {
183  return parentDict.add
184  (
185  new primitiveEntry(keyword, parentDict, is),
186  mergeEntry
187  );
188  }
189  }
190  }
191 }
192 
193 
195 {
196  is.fatalCheck("entry::New(Istream&)");
197 
198  keyType keyword;
199 
200  // Get the next keyword and if invalid return false
201  if (!getKeyword(keyword, is))
202  {
203  return autoPtr<entry>(NULL);
204  }
205  else // Keyword starts entry ...
206  {
207  token nextToken(is);
208  is.putBack(nextToken);
209 
210  if (nextToken == token::BEGIN_BLOCK)
211  {
212  return autoPtr<entry>
213  (
214  new dictionaryEntry(keyword, dictionary::null, is)
215  );
216  }
217  else
218  {
219  return autoPtr<entry>
220  (
221  new primitiveEntry(keyword, is)
222  );
223  }
224  }
225 }
226 
227 
228 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
229 
231 {
232  e.write(os);
233  return os;
234 }
235 
236 
237 // ************************ vim: set sw=4 sts=4 et: ************************ //