FreeFOAM The Cross-Platform CFD Toolkit
primitiveEntryIO.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 Description
25  PrimitiveEntry constructor from Istream and Ostream output operator.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "primitiveEntry.H"
30 #include <OpenFOAM/OSspecific.H>
31 #include <OpenFOAM/functionEntry.H>
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
35 void Foam::primitiveEntry::append
36 (
37  const token& currToken,
38  const dictionary& dict,
39  Istream& is
40 )
41 {
42  if (currToken.isWord())
43  {
44  const word& w = currToken.wordToken();
45 
46  if
47  (
48  w.size() == 1
49  || (
50  !(w[0] == '$' && expandVariable(w, dict))
51  && !(w[0] == '#' && expandFunction(w, dict, is))
52  )
53  )
54  {
55  newElmt(tokenIndex()++) = currToken;
56  }
57  }
58  else
59  {
60  newElmt(tokenIndex()++) = currToken;
61  }
62 }
63 
64 
65 void Foam::primitiveEntry::append(const tokenList& varTokens)
66 {
67  forAll(varTokens, i)
68  {
69  newElmt(tokenIndex()++) = varTokens[i];
70  }
71 }
72 
73 
74 bool Foam::primitiveEntry::expandVariable
75 (
76  const word& w,
77  const dictionary& dict
78 )
79 {
80  word varName = w(1, w.size()-1);
81 
82  // lookup the variable name in the given dictionary....
83  // Note: allow wildcards to match? For now disabled since following
84  // would expand internalField to wildcard match and not expected
85  // internalField:
86  // internalField XXX;
87  // boundaryField { ".*" {YYY;} movingWall {value $internalField;}
88  const entry* ePtr = dict.lookupEntryPtr(varName, true, false);
89 
90  // ...if defined insert its tokens into this
91  if (ePtr != NULL)
92  {
93  append(ePtr->stream());
94  return true;
95  }
96  else
97  {
98  // if not in the dictionary see if it is an environment
99  // variable
100 
101  string enVarString = getEnv(varName);
102 
103  if (enVarString.size())
104  {
105  append(tokenList(IStringStream('(' + enVarString + ')')()));
106  return true;
107  }
108 
109  return false;
110  }
111 }
112 
113 
114 bool Foam::primitiveEntry::expandFunction
115 (
116  const word& keyword,
117  const dictionary& parentDict,
118  Istream& is
119 )
120 {
121  word functionName = keyword(1, keyword.size()-1);
122  return functionEntry::execute(functionName, parentDict, *this, is);
123 }
124 
125 
127 {
128  is.fatalCheck
129  (
130  "primitiveEntry::readData(const dictionary&, Istream&)"
131  );
132 
133  label blockCount = 0;
134  token currToken;
135 
136  if
137  (
138  !is.read(currToken).bad()
139  && currToken.good()
140  && currToken != token::END_STATEMENT
141  )
142  {
143  append(currToken, dict, is);
144 
145  if
146  (
147  currToken == token::BEGIN_BLOCK
148  || currToken == token::BEGIN_LIST
149  )
150  {
151  blockCount++;
152  }
153 
154  while
155  (
156  !is.read(currToken).bad()
157  && currToken.good()
158  && !(currToken == token::END_STATEMENT && blockCount == 0)
159  )
160  {
161  if
162  (
163  currToken == token::BEGIN_BLOCK
164  || currToken == token::BEGIN_LIST
165  )
166  {
167  blockCount++;
168  }
169  else if
170  (
171  currToken == token::END_BLOCK
172  || currToken == token::END_LIST
173  )
174  {
175  blockCount--;
176  }
177 
178  append(currToken, dict, is);
179  }
180  }
181 
182  is.fatalCheck
183  (
184  "primitiveEntry::readData(const dictionary&, Istream&)"
185  );
186 
187  if (currToken.good())
188  {
189  return true;
190  }
191  else
192  {
193  return false;
194  }
195 }
196 
197 
198 void Foam::primitiveEntry::readEntry(const dictionary& dict, Istream& is)
199 {
200  label keywordLineNumber = is.lineNumber();
201  tokenIndex() = 0;
202 
203  if (read(dict, is))
204  {
205  setSize(tokenIndex());
206  tokenIndex() = 0;
207  }
208  else
209  {
211  (
212  "primitiveEntry::readEntry(const dictionary&, Istream&)",
213  is
214  ) << "ill defined primitiveEntry starting at keyword '"
215  << keyword() << '\''
216  << " on line " << keywordLineNumber
217  << " and ending at line " << is.lineNumber()
218  << exit(FatalIOError);
219  }
220 }
221 
222 
224 (
225  const keyType& key,
226  const dictionary& dict,
227  Istream& is
228 )
229 :
230  entry(key),
231  ITstream
232  (
233  is.name() + "::" + key,
234  tokenList(10),
235  is.format(),
236  is.version()
237  )
238 {
239  readEntry(dict, is);
240 }
241 
242 
244 :
245  entry(key),
246  ITstream
247  (
248  is.name() + "::" + key,
249  tokenList(10),
250  is.format(),
251  is.version()
252  )
253 {
254  readEntry(dictionary::null, is);
255 }
256 
257 
258 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
259 
261 {
262  os.writeKeyword(keyword());
263 
264  for (label i=0; i<size(); i++)
265  {
266  os << operator[](i);
267 
268  if (i < size()-1)
269  {
270  os << token::SPACE;
271  }
272  }
273 
274  os << token::END_STATEMENT << endl;
275 }
276 
277 
278 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
279 
280 template<>
281 Foam::Ostream& Foam::operator<<
282 (
283  Ostream& os,
284  const InfoProxy<primitiveEntry>& ip
285 )
286 {
287  const primitiveEntry& e = ip.t_;
288 
289  e.print(os);
290 
291  const label nPrintTokens = 10;
292 
293  os << " primitiveEntry '" << e.keyword() << "' comprises ";
294 
295  for (label i=0; i<min(e.size(), nPrintTokens); i++)
296  {
297  os << nl << " " << e[i].info();
298  }
299 
300  if (e.size() > nPrintTokens)
301  {
302  os << " ...";
303  }
304 
305  os << endl;
306 
307  return os;
308 }
309 
310 
311 // ************************ vim: set sw=4 sts=4 et: ************************ //