dictionaryContent.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) 2021 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
26\*---------------------------------------------------------------------------*/
27
28#include "dictionaryContent.H"
29
30// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
31
32namespace Foam
33{
34
35template<class UnaryPredicate>
37(
38 const dictionary& input,
39 const UnaryPredicate& pred
40)
41{
43 dict.name() = input.name(); // rename
44
45 for (const entry& e : input)
46 {
47 const keyType& key = e.keyword();
48
49 bool accept = false;
50
51 if (key.isPattern())
52 {
53 // Non-trivial to filter a regex itself - so just accept it
54 // - could also have a "pruneRegex" flag (for example)
55 accept = true;
56 }
57 else
58 {
59 accept = pred(key);
60 }
61
62 if (accept)
63 {
64 dict.add(e, false); // No merge - entries are already unique
65 }
66 }
67
68 return dict;
69}
70
71} // End namespace Foam
72
73
74// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
75
78(
79 const dictionary& input,
80 const wordList& allow,
81 const wordList& deny
82)
83{
84 if (allow.empty())
85 {
86 if (deny.empty())
87 {
88 // Pass-through
89 return dictionary(input);
90 }
91
92 // Deny only
93 return copyFilteredDict
94 (
95 input,
96 [&](const std::string& key) { return !deny.found(key); }
97 );
98 }
99
100 // Allow is non-empty
101
102 // No general case with deny as well
103 return copyFilteredDict
104 (
105 input,
106 [&](const std::string& key) { return allow.found(key); }
107 );
108}
109
110
113(
114 const dictionary& input,
115 const wordRes& allow,
116 const wordRes& deny
117)
118{
119 if (allow.empty())
120 {
121 if (deny.empty())
122 {
123 // Pass-through
124 return dictionary(input);
125 }
126
127 // Deny only
128 return copyFilteredDict
129 (
130 input,
131 [&](const std::string& key) { return !deny.match(key); }
132 );
133 }
134
135 // Allow is non-empty
136
137 if (deny.empty())
138 {
139 return copyFilteredDict
140 (
141 input,
142 [&](const std::string& key) { return allow.match(key); }
143 );
144 }
145
146 // General case - have both deny and allow
147 return copyFilteredDict
148 (
149 input,
150 [&](const std::string& key)
151 {
152 const auto result = allow.matched(key);
153 return
154 (
155 result == wordRe::LITERAL
156 ? true
157 : (result == wordRe::REGEX && !deny.match(key))
158 );
159 }
160 );
161}
162
163
164// ************************************************************************* //
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: UListI.H:265
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
static dictionary copyDict(const dictionary &input, const wordList &allow=wordList(), const wordList &deny=wordList())
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
A class for handling keywords in dictionaries.
Definition: keyType.H:71
@ LITERAL
String literal.
Definition: wordRe.H:104
@ REGEX
Regular expression.
Definition: wordRe.H:105
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:54
wordRe::compOption matched(const std::string &text) const
Smart match in the list of matchers, returning the match type.
Definition: wordResI.H:98
bool match(const std::string &text, bool literal=false) const
Smart match as literal or regex, stopping on the first match.
Definition: wordResI.H:91
Namespace for OpenFOAM.
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
static dictionary copyFilteredDict(const dictionary &input, const UnaryPredicate &pred)
dictionary dict
volScalarField & e
Definition: createFields.H:11