diff --git a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C index 3644f45a89..42af8f5017 100644 --- a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C +++ b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2016-2020 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2016-2021 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -56,7 +56,10 @@ Usage Adds the entry (should not exist yet) - \par -set \ - Adds or replaces the entry or applies a list of substitutions + Adds or replaces the entry selected by \c -entry + + - \par -set \ + Applies the list of substitutions - \par -merge \ Merges the entry @@ -134,6 +137,11 @@ Usage dictionary with keyword 'entryDDD' where DDD is the position in the dictionary (after ignoring the FoamFile entry). + - Substitute multiple entries: + \verbatim + foamDictionary system/controlDict -set "startTime=2000, endTime=3000" + \endverbatim + \*---------------------------------------------------------------------------*/ #include "argList.H" @@ -267,19 +275,9 @@ void remove(dictionary& dict, const dictionary& removeDict) void substitute(dictionary& dict, string substitutions) { - // Add '()' delimiters to the substitutions if not present - const string whitespace(" \t"); - string::size_type last = substitutions.find_last_not_of(whitespace); - if (substitutions[last] != ')') - { - substitutions = '(' + substitutions + ')'; - } - - word funcName; wordReList args; List> namedArgs; - - dictArgList(substitutions, funcName, args, namedArgs); + dictArgList(substitutions, args, namedArgs); forAll(namedArgs, i) { diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index acd5cbc400..86214dab3e 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -1699,6 +1699,83 @@ void Foam::dictArgList } +void Foam::dictArgList +( + const string& funcArgs, + wordReList& args, + List>& namedArgs +) +{ + int argLevel = 0; + bool namedArg = false; + word argName; + + word::size_type start = 0; + word::size_type i = 0; + + for + ( + word::const_iterator iter = funcArgs.begin(); + iter != funcArgs.end(); + ++iter + ) + { + char c = *iter; + + if (c == '(') + { + ++argLevel; + } + else if (c == ',' || std::next(iter) == funcArgs.end()) + { + if (std::next(iter) == funcArgs.end()) + { + if (c == ')') + { + --argLevel; + } + + ++i; + } + + if (argLevel == 0) + { + if (namedArg) + { + namedArgs.append + ( + Tuple2 + ( + argName, + funcArgs(start, i - start) + ) + ); + namedArg = false; + } + else + { + args.append(wordRe(funcArgs(start, i - start))); + } + start = i+1; + } + } + else if (c == '=') + { + argName = funcArgs(start, i - start); + string::stripInvalid(argName); + start = i+1; + namedArg = true; + } + else if (c == ')') + { + --argLevel; + } + + ++i; + } +} + + Foam::Pair Foam::dictAndKeyword(const word& scopedName) { string::size_type i = scopedName.find_last_of diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 8eec08877b..d2ae911bd9 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -748,6 +748,14 @@ void dictArgList List>& namedArgs ); +//- Parse dictionary substitution argument list +void dictArgList +( + const string& funcArgs, + wordReList& args, + List>& namedArgs +); + //- Extracts dict name and keyword Pair dictAndKeyword(const word& scopedName);