Commit 87bab77d authored by mattijs's avatar mattijs
Browse files

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

parent c76acdd6
......@@ -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,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; 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
(
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);
}
......
......@@ -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)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment