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 -------------------------------------------------------------------------------
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 \*---------------------------------------------------------------------------*/
27 
28 #include "dictionaryContent.H"
29 
30 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
35 template<class UnaryPredicate>
36 static dictionary copyFilteredDict
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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::dictionaryContent::copyDict
static dictionary copyDict(const dictionary &input, const wordList &allow=wordList(), const wordList &deny=wordList())
Definition: dictionaryContent.C:78
Foam::wordRes::matched
wordRe::compOption matched(const std::string &text) const
Smart match in the list of matchers, returning the match type.
Definition: wordResI.H:98
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::dictionary::name
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
Foam::wordRes::match
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
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:68
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
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::copyFilteredDict
static dictionary copyFilteredDict(const dictionary &input, const UnaryPredicate &pred)
Definition: dictionaryContent.C:37
dictionaryContent.H
Foam::List< word >
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640