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-2025 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::inplaceExpandEntry(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  ) << "Unknown 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  const label keyLineNumber,
113  const keyType& key
114 )
115 :
116  entry(key, keyLineNumber),
117  ITstream(key)
118 {}
119 
120 
122 :
123  entry(key, is.lineNumber()),
124  ITstream(is)
125 {
126  name() += '/' + keyword();
127 }
128 
129 
131 (
132  const label keyLineNumber,
133  const keyType& key,
134  const ITstream& is
135 )
136 :
137  entry(key, keyLineNumber),
138  ITstream(is)
139 {
140  name() += '/' + keyword();
141 }
142 
143 
145 :
146  entry(key, t.lineNumber()),
147  ITstream(key, t)
148 {}
149 
150 
152 (
153  const keyType& key,
154  const UList<token>& tokens
155 )
156 :
157  entry(key, tokens.size() ? tokens[0].lineNumber() : -1),
158  ITstream(key, tokens)
159 {}
160 
161 
163 (
164  const keyType& key,
165  const List<token>& tokens
166 )
167 :
168  entry(key, tokens.size() ? tokens[0].lineNumber() : -1),
169  ITstream(key, tokens)
170 {}
171 
172 
174 (
175  const keyType& key,
176  List<token>&& tokens
177 )
178 :
179  entry(key, tokens.size() ? tokens[0].lineNumber() : -1),
180  ITstream(key, move(tokens))
181 {}
182 
183 
184 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
185 
187 {
188  const tokenList& tokens = *this;
189 
190  if (tokens.empty())
191  {
192  return startLineNumber();
193  }
194  else
195  {
196  return tokens.last().lineNumber();
197  }
198 }
199 
200 
202 {
203  ITstream& is = const_cast<primitiveEntry&>(*this);
204  is.rewind();
205  return is;
206 }
207 
208 
210 {
212  << "Attempt to return primitive entry " << info()
213  << " as a sub-dictionary"
214  << abort(FatalError);
215 
216  return dictionary::null;
217 }
218 
219 
221 {
223  << "Attempt to return primitive entry " << info()
224  << " as a sub-dictionary"
225  << abort(FatalError);
226 
227  return const_cast<dictionary&>(dictionary::null);
228 }
229 
230 
231 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Input token stream.
Definition: ITstream.H:56
virtual Istream & rewind()
Rewind and return the stream so that it may be read again.
Definition: ITstream.C:185
label tokenIndex() const
Return the current token index.
Definition: ITstream.H:181
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
T & newElmt(const label)
Return subscript-checked element of UList.
Definition: ListI.H:152
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
T & last()
Return the last element of the list.
Definition: UListI.H:128
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
const entry * lookupScopedEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present,.
Definition: dictionary.C:696
wordList toc() const
Return the table of contents.
Definition: dictionary.C:981
static const dictionary null
Null dictionary.
Definition: dictionary.H:261
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
const keyType & keyword() const
Return keyword.
Definition: entry.H:136
A class for handling keywords in dictionaries.
Definition: keyType.H:69
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
virtual label endLineNumber() const
Return line number of last token in dictionary.
ITstream & stream() const
Return token stream if this entry is a primitive entry.
const dictionary & dict() const
This entry is not a dictionary,.
const fileName & name() const
Return the dictionary name.
primitiveEntry(const label keyLineNumber, const keyType &key)
Construct from line number and keyword.
A token holds items read from Istream.
Definition: token.H:74
@ BEGIN_BLOCK
Definition: token.H:114
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
string & inplaceExpandEntry(string &s, const dictionary &dict, const bool allowEnvVars, const bool allowEmpty, const char sigil='$')
Inplace expand occurrences of variables according to the dictionary.
Definition: stringOps.C:760
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
errorManip< error > abort(error &err)
Definition: errorManip.H:131
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
IOerror FatalIOError
error FatalError
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:42
dictionary dict