Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
26Note
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
38namespace Foam
39{
40
42
43// Maximum depth for recursive variable names
44static constexpr label maxRecursionDepth_ = 100;
45
46
47static List<expressions::exprString> expandExprStrings
48(
49 const UList<string>& inputs,
50 const dictionary& dict,
51 bool mandatory,
52 label recursionDepth
53)
54{
56
58
59 for (const string& input : inputs)
60 {
61 // Allow inline list of semicolon-separated variables
62 const auto varExpressions = stringOps::split(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
81 (
83 (
84 varExpr.substr(1),
85 dict,
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
103 (
104 expressions::exprString::toExpr(std::move(varExpr), dict)
105 );
106 }
107 }
108 }
109
110 result.shrink();
111 return result;
112}
113
116} // End namespace Foam
117
118
119// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
120
123(
124 const word& keyword,
125 const dictionary& dict,
126 bool mandatory,
127 label recursionDepth
128)
129{
131
132 // Catch empty keyword as a no-op (eg, when called recursively)
133 if (keyword.empty())
134 {
135 return result;
136 }
137
138 const entry* eptr = dict.findEntry(keyword, keyType::LITERAL_RECURSIVE);
139
140 if (!eptr)
141 {
142 if (mandatory)
143 {
145 << "Missing mandatory entry: " << keyword << nl << nl
146 << exit(FatalIOError);
147 }
148
149 return result;
150 }
151
152 if (++recursionDepth > maxRecursionDepth_)
153 {
155 << "Exceeded recursion depth (" << maxRecursionDepth_
156 << ") while reading list " << keyword << nl
157 << "Likely caused by circular referencing" << nl
158 << exit(FatalIOError);
159 }
160
161
162 ITstream& is = eptr->stream();
163 token tok(is);
164
165 List<string> list;
166
167 if (tok.isLabel() || tok.isPunctuation(token::BEGIN_LIST))
168 {
169 // A list of strings
170 is.rewind();
171 is >> list;
172 }
173 else if (tok.isString())
174 {
175 // A single string
176 list.resize(1);
177 list[0] = tok.stringToken();
178 }
179 else
180 {
182 << " Entry '"<< keyword
183 << "' not a string or list of strings" << nl
184 << exit(FatalIOError);
185
186 return result;
187 }
188
189 // Check for excess tokens
190 dict.checkITstream(is, keyword);
191
192 // Expand List<string> to List<expressions::exprString>
193 return expandExprStrings(list, dict, mandatory, recursionDepth);
194}
195
196
197// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
DynamicList< T, SizeMin > & shrink()
Calls shrink_to_fit() and returns a reference to the DynamicList.
An input stream of tokens.
Definition ITstream.H:56
virtual void rewind() override
Rewind the stream so that it may be read again. Same as seek(0).
Definition ITstream.H:638
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
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:89
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
A variant of Foam::string with expansion of dictionary variables into a comma-separated form.
Definition exprString.H:58
static exprString toExpr(const std::string &str)
Copy convert string to exprString.
@ LITERAL_RECURSIVE
Definition keyType.H:87
A token holds an item read from Istream.
Definition token.H:70
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition tokenI.H:650
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
bool isLabel() const noexcept
Integral token is convertible to Foam::label.
Definition tokenI.H:843
const string & stringToken() const
Return const reference to the string contents.
Definition tokenI.H:1076
bool isString() const noexcept
Token is string-variant (STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA).
Definition tokenI.H:1040
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
List< expressions::exprString > getList(const word &keyword, const dictionary &dict, bool mandatory=true, label recursionDepth=0)
Get an expression string list from a dictionary.
Definition exprTools.C:116
string trim(const std::string &s)
Return string trimmed of leading and trailing whitespace.
Foam::SubStrings split(const std::string &str, const char delim, std::string::size_type pos=0, const bool keepEmpty=false)
Split string into sub-strings at the delimiter character.
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict