mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH:dictionary: allow scoped variable lookup (using '.')
This commit is contained in:
@ -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 }
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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,14 +94,27 @@ int main(int argc, char *argv[])
|
||||
|
||||
if (args.optionFound("entry"))
|
||||
{
|
||||
wordList entryNames
|
||||
(
|
||||
fileName(args.option("entry")).components(':')
|
||||
);
|
||||
fileName entryName(args.option("entry"));
|
||||
|
||||
const entry* entPtr = NULL;
|
||||
|
||||
if (entryName.find('.') != string::npos)
|
||||
{
|
||||
// New syntax
|
||||
entPtr = dict.lookupScopedEntryPtr
|
||||
(
|
||||
entryName,
|
||||
false,
|
||||
true // wildcards
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Old syntax
|
||||
wordList entryNames(entryName.components(':'));
|
||||
if (dict.found(entryNames[0]))
|
||||
{
|
||||
const entry* entPtr = &dict.lookupEntry
|
||||
entPtr = &dict.lookupEntry
|
||||
(
|
||||
entryNames[0],
|
||||
false,
|
||||
@ -128,7 +141,12 @@ int main(int argc, char *argv[])
|
||||
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);
|
||||
}
|
||||
|
||||
@ -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; 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
|
||||
{
|
||||
// Find non-recursive with patterns
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user