ENH: add filtering version of dictionary copy

- can be useful when extracting portions of larger field files
This commit is contained in:
Mark Olesen
2021-04-07 14:45:59 +02:00
parent 100ae5c2bd
commit bb86eecdc4
7 changed files with 240 additions and 0 deletions

View File

@ -274,6 +274,8 @@ $(dictionary)/dictionaryIO.C
$(dictionary)/dictionarySearch.C
$(dictionary)/dictionaryCompat.C
$(dictionary)/dictionaryContent/dictionaryContent.C
entry = $(dictionary)/entry
$(entry)/entry.C
$(entry)/entryIO.C

View File

@ -0,0 +1,86 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "dictionaryContent.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::dictionary
Foam::dictionaryContent::copyDict
(
const dictionary& input,
const wordRes& allow,
const wordRes& deny
)
{
if (allow.empty() && deny.empty())
{
return dictionary(input);
}
dictionary dict;
dict.name() = input.name(); // rename
for (const entry& e : input)
{
const keyType& key = e.keyword();
bool accept = false;
if (key.isPattern())
{
// Non-trivial to filter a regex itself - so just accept it
// - could also have a "pruneRegex" flag (for example)
accept = true;
}
else if (allow.size())
{
const auto result = allow.matched(key);
accept =
(
result == wordRe::LITERAL
? true
: (result == wordRe::REGEX && !deny.match(key))
);
}
else
{
accept = !deny.match(key);
}
if (accept)
{
dict.add(e, false); // No merge - entries are already unique
}
}
return dict;
}
// ************************************************************************* //

View File

@ -36,6 +36,7 @@ Description
#define dictionaryContent_H
#include "dictionary.H"
#include "wordRes.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -85,6 +86,28 @@ public:
// Member Functions
//- Copy construct a dictionary,
//- filtered by a combination of allow/deny lists
//
// An empty 'allow' list accepts everything not in the 'deny' list.
// A literal match has higher priority over a regex match.
//
// \verbatim
// input: ( abc apple wall wall1 wall2 )
// allow: ( abc def "wall.*" )
// deny: ( "[ab].*" wall )
//
// result: (abc wall1 wall2)
// \endverbatim
//
// \return filtered dictionary copy
static dictionary copyDict
(
const dictionary& input,
const wordRes& allow = wordRes(),
const wordRes& deny = wordRes()
);
//- Read-access to the content
const dictionary& dict() const noexcept
{