primitiveEntry.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) 2011-2016 OpenFOAM Foundation
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 
26 #include "primitiveEntry.H"
27 #include "dictionary.H"
28 #include "OSspecific.H"
29 #include "stringOps.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 void Foam::primitiveEntry::append(const UList<token>& varTokens)
34 {
35  forAll(varTokens, i)
36  {
37  newElmt(tokenIndex()++) = varTokens[i];
38  }
39 }
40 
41 
42 bool Foam::primitiveEntry::expandVariable
43 (
44  const string& w,
45  const dictionary& dict
46 )
47 {
48  if (w.size() > 2 && w[0] == '$' && w[1] == token::BEGIN_BLOCK)
49  {
50  // Recursive substitution mode. Replace between {} with expansion.
51  string s(w(2, w.size()-3));
52  // Substitute dictionary and environment variables. Do not allow
53  // empty substitutions.
54  stringOps::inplaceExpand(s, dict, true, false);
55  string newW(w);
56  newW.std::string::replace(1, newW.size()-1, s);
57 
58  return expandVariable(newW, dict);
59  }
60  else
61  {
62  string varName = w(1, w.size()-1);
63 
64  // lookup the variable name in the given dictionary....
65  // Note: allow wildcards to match? For now disabled since following
66  // would expand internalField to wildcard match and not expected
67  // internalField:
68  // internalField XXX;
69  // boundaryField { ".*" {YYY;} movingWall {value $internalField;}
70  const entry* ePtr = dict.lookupScopedEntryPtr(varName, true, false);
71 
72  // ...if defined append its tokens into this
73  if (ePtr)
74  {
75  if (ePtr->isDict())
76  {
77  append(ePtr->dict().tokens());
78  }
79  else
80  {
81  append(ePtr->stream());
82  }
83  }
84  else
85  {
86  // not in the dictionary - try an environment variable
87  string envStr = getEnv(varName);
88 
89  if (envStr.empty())
90  {
92  (
93  dict
94  ) << "Illegal dictionary entry or environment variable name "
95  << varName << endl << "Valid dictionary entries are "
96  << dict.toc() << exit(FatalIOError);
97 
98  return false;
99  }
100  append(tokenList(IStringStream('(' + envStr + ')')()));
101  }
102  }
103  return true;
104 }
105 
106 
107 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
108 
110 :
111  entry(key),
112  ITstream(is)
113 {
114  name() += '.' + keyword();
115 }
116 
117 
119 :
120  entry(key),
121  ITstream(key, tokenList(1, t))
122 {}
123 
124 
126 (
127  const keyType& key,
128  const UList<token>& tokens
129 )
130 :
131  entry(key),
132  ITstream(key, tokens)
133 {}
134 
135 
137 (
138  const keyType& key,
139  const Xfer<List<token>>& tokens
140 )
141 :
142  entry(key),
143  ITstream(key, tokens)
144 {}
145 
146 
147 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
148 
150 {
151  const tokenList& tokens = *this;
152 
153  if (tokens.empty())
154  {
155  return -1;
156  }
157  else
158  {
159  return tokens.first().lineNumber();
160  }
161 }
162 
163 
165 {
166  const tokenList& tokens = *this;
167 
168  if (tokens.empty())
169  {
170  return -1;
171  }
172  else
173  {
174  return tokens.last().lineNumber();
175  }
176 }
177 
178 
180 {
181  ITstream& is = const_cast<primitiveEntry&>(*this);
182  is.rewind();
183  return is;
184 }
185 
186 
188 {
190  << "Attempt to return primitive entry " << info()
191  << " as a sub-dictionary"
192  << abort(FatalError);
193 
194  return dictionary::null;
195 }
196 
197 
199 {
201  << "Attempt to return primitive entry " << info()
202  << " as a sub-dictionary"
203  << abort(FatalError);
204 
205  return const_cast<dictionary&>(dictionary::null);
206 }
207 
208 
209 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:104
A class for handling keywords in dictionaries.
Definition: keyType.H:64
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const keyType & keyword() const
Return keyword.
Definition: entry.H:123
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:42
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual Istream & rewind()
Rewind and return the stream so that it may be read again.
Definition: ITstream.C:157
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
InfoProxy< primitiveEntry > info() const
Return info proxy.
label endLineNumber() const
Return line number of last token in dictionary.
static const dictionary null
Null dictionary.
Definition: dictionary.H:202
A token holds items read from Istream.
Definition: token.H:69
T & newElmt(const label)
Return subscript-checked element of UList.
Definition: ListI.H:151
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
T & first()
Return the first element of the list.
Definition: UListI.H:114
A keyword and a list of tokens is a &#39;primitiveEntry&#39;. An primitiveEntry can be read, written and printed, and the types and values of its tokens analysed.
entry(const keyType &)
Construct from keyword.
Definition: entry.C:40
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
label tokenIndex() const
Return the current token index.
Definition: ITstream.H:140
ITstream(const string &name, const UList< token > &tokens, streamFormat format=ASCII, versionNumber version=currentVersion)
Construct from components.
Definition: ITstream.H:69
errorManip< error > abort(error &err)
Definition: errorManip.H:131
ITstream & stream() const
Return token stream if this entry is a primitive entry.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:61
const dictionary & dict() const
This entry is not a dictionary,.
label startLineNumber() const
Return line number of first token in dictionary.
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurences of variables according to the mapping.
Definition: stringOps.C:87
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
primitiveEntry(const keyType &, Istream &)
Construct from keyword and a Istream.
const fileName & name() const
Return the dictionary name.
T & last()
Return the last element of the list.
Definition: UListI.H:128
Input token stream.
Definition: ITstream.H:49
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
IOerror FatalIOError