diff --git a/applications/test/dictionaryCopy/Make/files b/applications/test/dictionaryCopy/Make/files new file mode 100644 index 0000000000..11d085174a --- /dev/null +++ b/applications/test/dictionaryCopy/Make/files @@ -0,0 +1,3 @@ +Test-dictionaryCopy.C + +EXE = $(FOAM_USER_APPBIN)/Test-dictionaryCopy diff --git a/applications/test/dictionaryCopy/Make/options b/applications/test/dictionaryCopy/Make/options new file mode 100644 index 0000000000..41306609f2 --- /dev/null +++ b/applications/test/dictionaryCopy/Make/options @@ -0,0 +1 @@ +EXE_INC = diff --git a/applications/test/dictionaryCopy/Test-dictionaryCopy.C b/applications/test/dictionaryCopy/Test-dictionaryCopy.C new file mode 100644 index 0000000000..264c88e3e8 --- /dev/null +++ b/applications/test/dictionaryCopy/Test-dictionaryCopy.C @@ -0,0 +1,98 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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, distributed under GPL-3.0-or-later. + +Application + Test-dictionaryCopy + +Description + Test copying a dictionary with filtering + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "IOobject.H" +#include "IOstreams.H" +#include "IFstream.H" +#include "dictionaryContent.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noParallel(); + argList::addArgument("dict .. dictN"); + argList args(argc, argv, false, true); + + const wordRes allow; + const wordRes deny + ({ + wordRe("boundary.*", wordRe::REGEX) + }); + + Info<< nl + << "allow: " << flatOutput(allow) << nl + << "deny: " << flatOutput(deny) << nl << nl; + + if (args.size() <= 1) + { + const string dictFile = "testDictCopy"; + IFstream is(dictFile); + + dictionary input(is); + + dictionary copied + ( + dictionaryContent::copyDict + ( + input, + allow, + deny + ) + ); + + IOobject::writeDivider(Info); + input.writeEntry("input", Info); + copied.writeEntry("copied", Info); + } + + for (label argi=1; argi < args.size(); ++argi) + { + const string& dictFile = args[argi]; + IFstream is(dictFile); + + dictionary input(is); + + dictionary copied + ( + dictionaryContent::copyDict + ( + input, + allow, + deny + ) + ); + + IOobject::writeDivider(Info); + input.writeEntry("input", Info); + copied.writeEntry("copied", Info); + } + + IOobject::writeEndDivider(Info); + + return 0; +} + + +// ************************************************************************* // diff --git a/applications/test/dictionaryCopy/testDictCopy b/applications/test/dictionaryCopy/testDictCopy new file mode 100644 index 0000000000..979154e5d3 --- /dev/null +++ b/applications/test/dictionaryCopy/testDictCopy @@ -0,0 +1,27 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v2012 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object testDictCopy; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [ 1 2 -2 0 0 0 0 ]; + +internalField uniform 1; + +boundaryField +{ + inlet_1 { type patch; } + inlet_2 { type patch; } +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index ed399ab983..5892fa798b 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -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 diff --git a/src/OpenFOAM/db/dictionary/dictionaryContent/dictionaryContent.C b/src/OpenFOAM/db/dictionary/dictionaryContent/dictionaryContent.C new file mode 100644 index 0000000000..4a7e8e6b51 --- /dev/null +++ b/src/OpenFOAM/db/dictionary/dictionaryContent/dictionaryContent.C @@ -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 . + +\*---------------------------------------------------------------------------*/ + +#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; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/dictionaryContent/dictionaryContent.H b/src/OpenFOAM/db/dictionary/dictionaryContent/dictionaryContent.H index 984011c826..b4c454230c 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryContent/dictionaryContent.H +++ b/src/OpenFOAM/db/dictionary/dictionaryContent/dictionaryContent.H @@ -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 {