ENH:dictionary: allow scoped variable lookup (using '.')

This commit is contained in:
mattijs
2012-09-12 09:14:43 +01:00
parent c76acdd6ff
commit 87bab77daf
7 changed files with 179 additions and 34 deletions

View File

@ -51,7 +51,7 @@ boundaryField
inlet_4 { $inactive } inlet_4 { $inactive }
inlet_5 "a primitiveEntry is squashed by a directory entry"; inlet_5 "a primitiveEntry is squashed by a directory entry";
inlet_5 { $inactive } inlet_5 { $inactive }
inlet_6 { $inactive } inlet_6 { $.inactive } // Test scoping
inlet_7 { $inactive } inlet_7 { $inactive }
inlet_8 { $inactive } inlet_8 { $inactive }

View File

@ -24,7 +24,7 @@ keyY parentValue5;
"(.*)Dict" "(.*)Dict"
{ {
foo subdictValue0; foo subdictValue0;
bar $f.*; // should this really match 'foo'? //bar $f.*; // should this really match 'foo'?
// result is dependent on insert order! // result is dependent on insert order!
"a.*c" subdictValue3; "a.*c" subdictValue3;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -94,41 +94,59 @@ int main(int argc, char *argv[])
if (args.optionFound("entry")) if (args.optionFound("entry"))
{ {
wordList entryNames fileName entryName(args.option("entry"));
(
fileName(args.option("entry")).components(':')
);
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, false,
true // wildcards true // wildcards
); );
}
for (int i=1; i<entryNames.size(); ++i) else
{
// Old syntax
wordList entryNames(entryName.components(':'));
if (dict.found(entryNames[0]))
{ {
if (entPtr->dict().found(entryNames[i])) entPtr = &dict.lookupEntry
(
entryNames[0],
false,
true // wildcards
);
for (int i=1; i<entryNames.size(); ++i)
{ {
entPtr = &entPtr->dict().lookupEntry if (entPtr->dict().found(entryNames[i]))
( {
entryNames[i], entPtr = &entPtr->dict().lookupEntry
false, (
true // wildcards entryNames[i],
); false,
} true // wildcards
else );
{ }
FatalErrorIn(args.executable()) else
<< "Cannot find sub-entry " << entryNames[i] {
<< " in entry " << args["entry"] FatalErrorIn(args.executable())
<< " in dictionary " << dictFileName; << "Cannot find sub-entry " << entryNames[i]
FatalError.exit(3); << " in entry " << args["entry"]
<< " in dictionary " << dictFileName;
FatalError.exit(3);
}
} }
} }
}
if (entPtr)
{
if (args.optionFound("keywords")) if (args.optionFound("keywords"))
{ {
/* /*
@ -158,7 +176,7 @@ int main(int argc, char *argv[])
{ {
FatalErrorIn(args.executable()) FatalErrorIn(args.executable())
<< "Cannot find entry " << "Cannot find entry "
<< entryNames[0] << entryName
<< " in dictionary " << dictFileName; << " in dictionary " << dictFileName;
FatalError.exit(2); FatalError.exit(2);
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License 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; i<entryNames.size(); ++i)
{
if (!entPtr)
{
FatalIOErrorIn
(
"dictionary::lookupScopedEntryPtr"
"(const word&, bool, bool)",
*this
) << "keyword " << keyword
<< " is undefined in dictionary "
<< name()
<< exit(FatalIOError);
}
if (!entPtr->isDict())
{
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<entry>, addDict, iter)
{
add(iter());
}
return true;
}
return false;
}
bool Foam::dictionary::isDict(const word& keyword) const bool Foam::dictionary::isDict(const word& keyword) const
{ {
// Find non-recursive with patterns // Find non-recursive with patterns

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -353,6 +353,15 @@ public:
bool patternMatch=true bool patternMatch=true
) const; ) 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 //- Check if entry is a sub-dictionary
bool isDict(const word&) const; bool isDict(const word&) const;
@ -387,6 +396,10 @@ public:
// corresponding sub-dictionary entries // corresponding sub-dictionary entries
bool substituteKeyword(const word& keyword); 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 //- Add a new entry
// With the merge option, dictionaries are interwoven and // With the merge option, dictionaries are interwoven and
// primitive entries are overwritten // primitive entries are overwritten

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -112,7 +112,7 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
!disableFunctionEntries !disableFunctionEntries
&& keyword[0] == '$') // ... Substitution entry && keyword[0] == '$') // ... Substitution entry
{ {
parentDict.substituteKeyword(keyword); parentDict.substituteScopedKeyword(keyword);
return true; return true;
} }
else if else if

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -52,7 +52,7 @@ bool Foam::primitiveEntry::expandVariable
// internalField: // internalField:
// internalField XXX; // internalField XXX;
// boundaryField { ".*" {YYY;} movingWall {value $internalField;} // 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 defined append its tokens into this
if (ePtr) if (ePtr)