primitiveEntry.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 variable& 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 
53  // Substitute dictionary and environment variables. Do not allow
54  // empty substitutions.
55  stringOps::inplaceExpand(s, dict, true, false);
56  variable newW(w);
57  newW.std::string::replace(1, newW.size() - 1, s);
58 
59  return expandVariable(newW, dict);
60  }
61  else
62  {
63  string varName = w(1, w.size() - 1);
64 
65  // Lookup the variable name in the given dictionary....
66  // Note: allow wildcards to match? For now disabled since following
67  // would expand internalField to wildcard match and not expected
68  // internalField:
69  // internalField XXX;
70  // boundaryField { ".*" {YYY;} movingWall {value $internalField;}
71  const entry* ePtr = dict.lookupScopedEntryPtr(varName, true, false);
72 
73  // ...if defined append its tokens into this
74  if (ePtr)
75  {
76  if (ePtr->isDict())
77  {
78  append(ePtr->dict().tokens());
79  }
80  else
81  {
82  append(ePtr->stream());
83  }
84  }
85  else
86  {
87  // Not in the dictionary - try an environment variable
88  string envStr = getEnv(varName);
89 
90  if (envStr.empty())
91  {
93  (
94  dict
95  ) << "Illegal dictionary entry or environment variable name "
96  << varName << endl << "Valid dictionary entries are "
97  << dict.toc() << exit(FatalIOError);
98 
99  return false;
100  }
101  append(tokenList(IStringStream('(' + envStr + ')')()));
102  }
103  }
104  return true;
105 }
106 
107 
108 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
109 
111 :
112  entry(key),
113  ITstream(is)
114 {
115  name() += '/' + keyword();
116 }
117 
118 
120 :
121  entry(key),
122  ITstream(key, tokenList(1, t))
123 {}
124 
125 
127 (
128  const keyType& key,
129  const UList<token>& tokens
130 )
131 :
132  entry(key),
133  ITstream(key, tokens)
134 {}
135 
136 
138 (
139  const keyType& key,
140  List<token>&& tokens
141 )
142 :
143  entry(key),
144  ITstream(key, move(tokens))
145 {}
146 
147 
148 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
149 
151 {
152  const tokenList& tokens = *this;
153 
154  if (tokens.empty())
155  {
156  return -1;
157  }
158  else
159  {
160  return tokens.first().lineNumber();
161  }
162 }
163 
164 
166 {
167  const tokenList& tokens = *this;
168 
169  if (tokens.empty())
170  {
171  return -1;
172  }
173  else
174  {
175  return tokens.last().lineNumber();
176  }
177 }
178 
179 
181 {
182  ITstream& is = const_cast<primitiveEntry&>(*this);
183  is.rewind();
184  return is;
185 }
186 
187 
189 {
191  << "Attempt to return primitive entry " << info()
192  << " as a sub-dictionary"
193  << abort(FatalError);
194 
195  return dictionary::null;
196 }
197 
198 
200 {
202  << "Attempt to return primitive entry " << info()
203  << " as a sub-dictionary"
204  << abort(FatalError);
205 
206  return const_cast<dictionary&>(dictionary::null);
207 }
208 
209 
210 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
A class for handling keywords in dictionaries.
Definition: keyType.H:66
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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:158
#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:164
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
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:241
A token holds items read from Istream.
Definition: token.H:72
T & newElmt(const label)
Return subscript-checked element of UList.
Definition: ListI.H:152
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
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:60
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 occurrences of variables according to the mapping.
Definition: stringOps.C:81
#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