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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "primitiveEntry.H"
30#include "dictionary.H"
31#include "OSspecific.H"
32#include "stringOps.H"
33
34// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35
36// Find the type/position of the ":-" or ":+" alternative values
37// Returns 0, '-', '+' corresponding to not-found or ':-' or ':+'
38static inline char findParameterAlternative
39(
40 const std::string& s,
41 std::string::size_type& pos,
42 std::string::size_type endPos = std::string::npos
43)
44{
45 while (pos != std::string::npos)
46 {
47 pos = s.find(':', pos);
48 if (pos != std::string::npos)
49 {
50 if (pos < endPos)
51 {
52 // in-range: check for '+' or '-' following the ':'
53 const char altType = s[pos+1];
54 if (altType == '+' || altType == '-')
55 {
56 return altType;
57 }
58
59 ++pos; // unknown/unsupported - continue at next position
60 }
61 else
62 {
63 // out-of-range: abort
64 pos = std::string::npos;
65 }
66 }
67 }
68
69 return 0;
70}
71
72
73// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
74
75bool Foam::primitiveEntry::expandVariable
76(
77 const string& varName,
78 const dictionary& dict
79)
80{
81 char altType = 0; // Type ('-' or '+') for ":-" or ":+" alternatives
82 word expanded;
83 string altValue;
84
85 // Any ${{ expr }} entries have been trapped and processed elsewhere
86
87 if (varName[0] == token::BEGIN_BLOCK && varName.size() > 1)
88 {
89 // Replace content between {} with string expansion and
90 // handle ${parameter:-word} or ${parameter:+word}
91
92 // Copy into a word without stripping
93 expanded.assign(varName, 1, varName.size()-2);
94
95 // Substitute dictionary and environment variables.
96 // - Allow environment.
97 // - No empty substitutions.
98 // - No sub-dictionary lookups
99
100 stringOps::inplaceExpand(expanded, dict, true, false, false);
101
102 // Position of ":-" or ":+" alternative values
103 std::string::size_type altPos = 0;
104
105 // Check for parameter:-word or parameter:+word
106 altType = findParameterAlternative(expanded, altPos);
107
108 if (altType)
109 {
110 altValue = expanded.substr(altPos + 2);
111 expanded.erase(altPos);
112 }
113
114 // Catch really bad expansions and let them die soon after.
115 // Eg, ${:-other} should not be allowed.
116 if (expanded.empty())
117 {
118 altType = 0;
119 altValue.clear();
120 }
121
122 // Fallthrough for further processing
123 }
124
125
126 // Lookup variable name in the given dictionary WITHOUT pattern matching.
127 // Having a pattern match means that in this example:
128 // {
129 // internalField XXX;
130 // boundaryField { ".*" {YYY;} movingWall {value $internalField;}
131 // }
132 // The $internalField would be matched by the ".*" !!!
133
134 // Recursive, non-patterns
135
136 const word& lookupName = (expanded.empty() ? varName : expanded);
137
138 const entry* eptr =
140
141 if (!eptr)
142 {
143 // Not found - revert to environment variable
144 // and parse into a series of tokens.
145
146 // We wish to fail if the environment variable returns
147 // an empty string and there is no alternative given.
148 //
149 // Always allow empty strings as alternative parameters,
150 // since the user provided them for a reason.
151
152 string str(Foam::getEnv(lookupName));
153
154 if (str.empty() ? (altType == '-') : (altType == '+'))
155 {
156 // Not found or empty: use ":-" alternative value
157 // Found and not empty: use ":+" alternative value
158 str = std::move(altValue);
159 }
160 else if (str.empty())
161 {
163 << "Illegal dictionary entry or environment variable name "
164 << lookupName << nl
165 << "Known dictionary entries: " << dict.toc() << nl
166 << exit(FatalIOError);
167
168 return false;
169 }
170
171 // Parse string into a series of tokens
172
173 tokenList toks(ITstream::parse(str)); // ASCII
174
175 ITstream::append(std::move(toks), true); // Lazy resizing
176 }
177 else if (eptr->isDict())
178 {
179 // Found dictionary entry
180
181 tokenList toks(eptr->dict().tokens());
182
183 if (toks.empty() ? (altType == '-') : (altType == '+'))
184 {
185 // Not found or empty: use ":-" alternative value
186 // Found and not empty: use ":+" alternative value
187
188 toks = ITstream::parse(altValue); // ASCII
189 }
190
191 ITstream::append(std::move(toks), true); // Lazy resizing
192 }
193 else
194 {
195 // Found primitive entry - copy tokens
196
197 if (eptr->stream().empty() ? (altType == '-') : (altType == '+'))
198 {
199 // Not found or empty: use ":-" alternative value
200 // Found and not empty: use ":+" alternative value
201
202 tokenList toks(ITstream::parse(altValue)); // ASCII
203
204 ITstream::append(std::move(toks), true); // Lazy resizing
205 }
206 else
207 {
208 ITstream::append(eptr->stream(), true); // Lazy resizing
209 }
210 }
211
212 return true;
213}
214
215
216// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
217
219:
220 entry(key),
221 ITstream(zero{}, key)
222{}
223
224
226:
227 entry(key),
228 ITstream(key, tokenList(one{}, tok))
229{}
230
231
233(
234 const keyType& key,
235 const UList<token>& tokens
236)
237:
238 entry(key),
239 ITstream(key, tokens)
240{}
241
242
244(
245 const keyType& key,
246 List<token>&& tokens
247)
248:
249 entry(key),
250 ITstream(key, std::move(tokens))
251{}
252
253
255:
256 entry(key),
257 ITstream(is)
258{
259 ITstream::name() += '.' + key;
260}
261
262
263// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
264
266{
267 const tokenList& tokens = *this;
268
269 if (tokens.size())
270 {
271 return tokens.first().lineNumber();
272 }
273
274 return -1;
275}
276
277
279{
280 const tokenList& tokens = *this;
281
282 if (tokens.size())
283 {
284 return tokens.last().lineNumber();
285 }
286
287 return -1;
288}
289
290
292{
293 ITstream& is = const_cast<primitiveEntry&>(*this);
294 is.rewind();
295 return is;
296}
297
298
300{
302 << "Attempt to return primitive entry " << info()
303 << " as a sub-dictionary"
304 << abort(FatalError);
305
306 return dictionary::null;
307}
308
309
311{
313 << "Attempt to return primitive entry " << info()
314 << " as a sub-dictionary"
315 << abort(FatalError);
316
317 return const_cast<dictionary&>(dictionary::null);
318}
319
320
321// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
An input stream of tokens.
Definition: ITstream.H:56
virtual const fileName & name() const
Get the name of the stream.
Definition: ITstream.H:235
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:561
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:94
T & first()
Return the first element of the list.
Definition: UListI.H:202
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
T & last()
Return the last element of the list.
Definition: UListI.H:216
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const entry * findScoped(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for a scoped entry (const access) with the given keyword.
Definition: dictionaryI.H:117
wordList toc() const
Return the table of contents.
Definition: dictionary.C:602
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:394
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
entry(const keyType &keyword)
Construct from keyword.
Definition: entry.C:69
A class for handling keywords in dictionaries.
Definition: keyType.H:71
@ LITERAL_RECURSIVE
Definition: keyType.H:86
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
virtual label endLineNumber() const
Return line number of last token in dictionary.
virtual ITstream & stream() const
Return token stream for this primitive entry.
virtual const dictionary & dict() const
This entry is not a dictionary,.
virtual label startLineNumber() const
Return line number of first token in dictionary.
A token holds an item read from Istream.
Definition: token.H:69
@ BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:159
bool append() const noexcept
True if output format uses an append mode.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.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))
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
errorManip< error > abort(error &err)
Definition: errorManip.H:144
IOerror FatalIOError
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:44
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
static char findParameterAlternative(const std::string &s, std::string::size_type &pos, std::string::size_type endPos=std::string::npos)
dictionary dict