OpenFOAM: API Guide
v2112
The open source CFD toolbox
exprTools.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) 2019-2021 OpenCFD Ltd.
9
-------------------------------------------------------------------------------
10
License
11
This file is part of OpenFOAM.
12
13
OpenFOAM is free software: you can redistribute it and/or modify it
14
under the terms of the GNU General Public License as published by
15
the Free Software Foundation, either version 3 of the License, or
16
(at your option) any later version.
17
18
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21
for more details.
22
23
You should have received a copy of the GNU General Public License
24
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26
Note
27
Ideas based on swak4Foam driver code (2010-2018)
28
from Bernhard Gschaider <bgschaid@hfd-research.com>
29
30
\*---------------------------------------------------------------------------*/
31
32
#include "
exprTools.H
"
33
#include "
stringOps.H
"
34
#include "
DynamicList.H
"
35
36
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
37
38
namespace
Foam
39
{
40
42
43
// Maximum depth for recursive variable names
44
static
constexpr label maxRecursionDepth_ = 100;
45
46
47
static
List<expressions::exprString> expandExprStrings
48
(
49
const
UList<string>& inputs,
50
const
dictionary&
dict
,
51
bool
mandatory,
52
label recursionDepth
53
)
54
{
56
57
DynamicList<expressions::exprString> result;
58
59
for
(
const
string
&
input
: inputs)
60
{
61
// Allow inline list of semicolon-separated variables
62
const
auto
varExpressions = stringOps::split<string>(
input
,
';'
);
63
64
for
(
const
auto
& subMatch : varExpressions)
65
{
66
string
varExpr(
stringOps::trim
(subMatch.str()));
67
68
if
(varExpr.empty())
69
{
70
continue
;
71
}
72
74
75
// Expand #otherVariable as dictionary lookup
76
if
(varExpr[0] ==
'#'
)
77
{
79
80
List<expressions::exprString> expansions
81
(
82
exprTools::getList
83
(
84
dict
,
85
varExpr.substr(1),
86
mandatory,
87
recursionDepth
88
)
89
);
90
93
94
result.reserve(result.size() + expansions.size());
95
for
(expressions::exprString& str : expansions)
96
{
97
result.append(std::move(str));
98
}
99
}
100
else
101
{
102
result.append(expressions::exprString(varExpr,
dict
));
103
}
104
}
105
}
106
107
result.shrink();
108
return
result;
109
}
110
112
113
}
// End namespace Foam
114
115
116
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
117
118
Foam::List<Foam::expressions::exprString>
119
Foam::exprTools::getList
120
(
121
const
dictionary
&
dict
,
122
const
word
& keyword,
123
bool
mandatory,
124
label recursionDepth
125
)
126
{
127
List<expressions::exprString>
result;
128
129
// Catch empty keyword as a no-op (eg, when called recursively)
130
if
(keyword.empty())
131
{
132
return
result;
133
}
134
135
const
entry
* eptr =
dict
.findEntry(keyword, keyType::LITERAL_RECURSIVE);
136
137
if
(!eptr)
138
{
139
if
(mandatory)
140
{
141
FatalIOErrorInFunction
(
dict
)
142
<<
"Missing mandatory entry: "
<< keyword <<
nl
<<
nl
143
<<
exit
(
FatalIOError
);
144
}
145
146
return
result;
147
}
148
149
if
(++recursionDepth > maxRecursionDepth_)
150
{
151
FatalIOErrorInFunction
(
dict
)
152
<<
"Exceeded recursion depth ("
<< maxRecursionDepth_
153
<<
") while reading list "
<< keyword <<
nl
154
<<
"Likely caused by circular referencing"
<<
nl
155
<<
exit
(
FatalIOError
);
156
}
157
158
159
ITstream
& is = eptr->
stream
();
160
token
tok(is);
161
162
List<string>
list;
163
164
if
(tok.
isLabel
() || tok.
isPunctuation
(token::BEGIN_LIST))
165
{
166
// A list of strings
167
is.
rewind
();
168
is >> list;
169
}
170
else
if
(tok.
isString
())
171
{
172
// A single string
173
list.
resize
(1);
174
list[0] = tok.
stringToken
();
175
}
176
else
177
{
178
FatalIOErrorInFunction
(
dict
)
179
<<
" Entry '"
<< keyword
180
<<
"' not a string or list of strings"
<<
nl
181
<<
exit
(
FatalIOError
);
182
183
return
result;
184
}
185
186
// Check for excess tokens
187
dict
.checkITstream(is, keyword);
188
189
// Expand List<string> to List<expressions::exprString>
190
return
expandExprStrings(list,
dict
, mandatory, recursionDepth);
191
}
192
193
194
// ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition:
entry.H:67
Foam::ITstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition:
ITstream.C:561
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition:
tokenI.H:497
Foam::word
A class for handling words, derived from Foam::string.
Definition:
word.H:65
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition:
ListI.H:139
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition:
tokenI.H:689
Foam::stringOps::trim
string trim(const std::string &s)
Return string trimmed of leading and trailing whitespace.
Definition:
stringOps.C:1046
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition:
token.H:68
Foam::ITstream
An input stream of tokens.
Definition:
ITstream.H:52
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition:
tokenI.H:459
dict
dictionary dict
Definition:
searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition:
dictionary.H:123
Foam
Namespace for OpenFOAM.
Definition:
atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition:
errorManip.H:130
exprTools.H
Foam::nl
constexpr char nl
Definition:
Ostream.H:404
Foam::List< Foam::expressions::exprString >
Foam::token::isString
bool isString() const noexcept
Token is string-variant (STRING, EXPRESSION, VARIABLE, VERBATIM)
Definition:
tokenI.H:653
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition:
IntRanges.C:55
Foam::exprTools::getList
List< expressions::exprString > getList(const dictionary &dict, const word &keyword, bool mandatory=true, label recursionDepth=0)
Get an expression string list from a dictionary.
Definition:
exprTools.C:120
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition:
error.H:473
DynamicList.H
stringOps.H
src
OpenFOAM
expressions
exprTools
exprTools.C
Generated by
1.8.17
OPENFOAM® is a registered
trademark
of OpenCFD Ltd.