From 87bab77daf8fa1a0676839c8baff289189924204 Mon Sep 17 00:00:00 2001 From: mattijs Date: Wed, 12 Sep 2012 09:14:43 +0100 Subject: [PATCH] ENH:dictionary: allow scoped variable lookup (using '.') --- applications/test/dictionary/testDict | 2 +- applications/test/dictionary/testDictRegex | 2 +- .../miscellaneous/foamInfoExec/foamInfoExec.C | 70 +++++++---- src/OpenFOAM/db/dictionary/dictionary.C | 116 +++++++++++++++++- src/OpenFOAM/db/dictionary/dictionary.H | 15 ++- src/OpenFOAM/db/dictionary/entry/entryIO.C | 4 +- .../primitiveEntry/primitiveEntry.C | 4 +- 7 files changed, 179 insertions(+), 34 deletions(-) diff --git a/applications/test/dictionary/testDict b/applications/test/dictionary/testDict index 5fdee9401c..af6c565f6f 100644 --- a/applications/test/dictionary/testDict +++ b/applications/test/dictionary/testDict @@ -51,7 +51,7 @@ boundaryField inlet_4 { $inactive } inlet_5 "a primitiveEntry is squashed by a directory entry"; inlet_5 { $inactive } - inlet_6 { $inactive } + inlet_6 { $.inactive } // Test scoping inlet_7 { $inactive } inlet_8 { $inactive } diff --git a/applications/test/dictionary/testDictRegex b/applications/test/dictionary/testDictRegex index c7eeef2613..4a24a74ef3 100644 --- a/applications/test/dictionary/testDictRegex +++ b/applications/test/dictionary/testDictRegex @@ -24,7 +24,7 @@ keyY parentValue5; "(.*)Dict" { foo subdictValue0; - bar $f.*; // should this really match 'foo'? + //bar $f.*; // should this really match 'foo'? // result is dependent on insert order! "a.*c" subdictValue3; diff --git a/applications/utilities/miscellaneous/foamInfoExec/foamInfoExec.C b/applications/utilities/miscellaneous/foamInfoExec/foamInfoExec.C index 44ba0d624c..5b85aee077 100644 --- a/applications/utilities/miscellaneous/foamInfoExec/foamInfoExec.C +++ b/applications/utilities/miscellaneous/foamInfoExec/foamInfoExec.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -94,41 +94,59 @@ int main(int argc, char *argv[]) if (args.optionFound("entry")) { - wordList entryNames - ( - fileName(args.option("entry")).components(':') - ); + fileName entryName(args.option("entry")); - if (dict.found(entryNames[0])) + const entry* entPtr = NULL; + + if (entryName.find('.') != string::npos) { - const entry* entPtr = &dict.lookupEntry + // New syntax + entPtr = dict.lookupScopedEntryPtr ( - entryNames[0], + entryName, false, true // wildcards ); - - for (int i=1; idict().found(entryNames[i])) + entPtr = &dict.lookupEntry + ( + entryNames[0], + false, + true // wildcards + ); + + for (int i=1; idict().lookupEntry - ( - entryNames[i], - false, - true // wildcards - ); - } - else - { - FatalErrorIn(args.executable()) - << "Cannot find sub-entry " << entryNames[i] - << " in entry " << args["entry"] - << " in dictionary " << dictFileName; - FatalError.exit(3); + if (entPtr->dict().found(entryNames[i])) + { + entPtr = &entPtr->dict().lookupEntry + ( + entryNames[i], + false, + true // wildcards + ); + } + else + { + FatalErrorIn(args.executable()) + << "Cannot find sub-entry " << entryNames[i] + << " in entry " << args["entry"] + << " in dictionary " << dictFileName; + FatalError.exit(3); + } } } + } + + if (entPtr) + { if (args.optionFound("keywords")) { /* @@ -158,7 +176,7 @@ int main(int argc, char *argv[]) { FatalErrorIn(args.executable()) << "Cannot find entry " - << entryNames[0] + << entryName << " in dictionary " << dictFileName; FatalError.exit(2); } diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index a405d153dd..5e67648d17 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -417,6 +417,120 @@ Foam::ITstream& Foam::dictionary::lookup } +const Foam::entry* Foam::dictionary::lookupScopedEntryPtr +( + const word& keyword, + bool recursive, + bool patternMatch +) const +{ + string::size_type dotPos = keyword.find('.'); + + if (dotPos == string::npos) + { + return lookupEntryPtr(keyword, recursive, patternMatch); + } + else + { + if (dotPos == 0) + { + const dictionary* dictPtr = this; + while (&dictPtr->parent_ != &dictionary::null) + { + dictPtr = &dictPtr->parent_; + } + + // At top + return dictPtr->lookupScopedEntryPtr + ( + keyword.substr(1, keyword.size()-1), + false, + patternMatch + ); + } + else + { + wordList entryNames(fileName(keyword).components('.')); + + const entry* entPtr = lookupEntryPtr(entryNames[0], false, true); + + for (int i=1; iisDict()) + { + FatalIOErrorIn + ( + "dictionary::lookupScopedEntryPtr" + "(const word&, bool, bool)", + *this + ) << "Entry " << entPtr->name() + << " is not a dictionary so cannot lookup sub entry " + << entryNames[i] + << exit(FatalIOError); + } + + entPtr = entPtr->dict().lookupEntryPtr + ( + entryNames[i], + false, + true + ); + } + + if (!entPtr) + { + FatalIOErrorIn + ( + "dictionary::lookupScopedEntryPtr" + "(const word&, bool, bool)", + *this + ) << "keyword " << keyword + << " is not a valid scoped entry in dictionary " + << name() + << exit(FatalIOError); + } + return entPtr; + } + } +} + + +bool Foam::dictionary::substituteScopedKeyword(const word& keyword) +{ + word varName = keyword(1, keyword.size()-1); + + // lookup the variable name in the given dictionary + const entry* ePtr = lookupScopedEntryPtr(varName, true, true); + + // if defined insert its entries into this dictionary + if (ePtr != NULL) + { + const dictionary& addDict = ePtr->dict(); + + forAllConstIter(IDLList, addDict, iter) + { + add(iter()); + } + + return true; + } + + return false; +} + + bool Foam::dictionary::isDict(const word& keyword) const { // Find non-recursive with patterns diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 710643c374..17901737ca 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -353,6 +353,15 @@ public: bool patternMatch=true ) const; + //- Find and return an entry data stream pointer if present + // otherwise return NULL. Allows scoping using '.' + const entry* lookupScopedEntryPtr + ( + const word&, + bool recursive, + bool patternMatch + ) const; + //- Check if entry is a sub-dictionary bool isDict(const word&) const; @@ -387,6 +396,10 @@ public: // corresponding sub-dictionary entries bool substituteKeyword(const word& keyword); + //- Substitute the given scoped keyword prepended by '$' with the + // corresponding sub-dictionary entries + bool substituteScopedKeyword(const word& keyword); + //- Add a new entry // With the merge option, dictionaries are interwoven and // primitive entries are overwritten diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index 3807c3bfaf..dd16f4e6ed 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -112,7 +112,7 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is) !disableFunctionEntries && keyword[0] == '$') // ... Substitution entry { - parentDict.substituteKeyword(keyword); + parentDict.substituteScopedKeyword(keyword); return true; } else if diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C index bda2bfef9a..c91f3df5aa 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -52,7 +52,7 @@ bool Foam::primitiveEntry::expandVariable // internalField: // internalField XXX; // boundaryField { ".*" {YYY;} movingWall {value $internalField;} - const entry* ePtr = dict.lookupEntryPtr(varName, true, false); + const entry* ePtr = dict.lookupScopedEntryPtr(varName, true, false); // ...if defined append its tokens into this if (ePtr)