functionObject: Added support for scoped keyword lookup in the argument list

functions
{
    #includeFunc        singleGraph(setConfig/axis = y)
    .
    .
    .
}
This commit is contained in:
Henry Weller
2020-09-18 21:39:40 +01:00
parent 312a56e7e3
commit ad33cc2cc8
4 changed files with 121 additions and 128 deletions

View File

@ -203,7 +203,7 @@ IOstream::streamFormat readDict(dictionary& dict, const fileName& dictFileName)
//- Convert keyword syntax to "dot" if the dictionary is "dot" syntax //- Convert keyword syntax to "dot" if the dictionary is "dot" syntax
word scope(const fileName& entryName) word dotToSlash(const fileName& entryName)
{ {
if if
( (
@ -227,67 +227,11 @@ word scope(const fileName& entryName)
} }
//- Extracts dict name and keyword
Pair<word> dictAndKeyword(const word& scopedName)
{
string::size_type i = scopedName.find_last_of
(
functionEntries::inputSyntaxEntry::scopeChar()
);
if (i != string::npos)
{
return Pair<word>
(
scopedName.substr(0, i),
scopedName.substr(i+1, string::npos)
);
}
else
{
return Pair<word>("", scopedName);
}
}
const dictionary& lookupScopedDict
(
const dictionary& dict,
const word& subDictName
)
{
if (subDictName == "")
{
return dict;
}
else
{
const entry* entPtr = dict.lookupScopedEntryPtr
(
subDictName,
false,
false
);
if (!entPtr || !entPtr->isDict())
{
FatalIOErrorInFunction(dict)
<< "keyword " << subDictName
<< " is undefined in dictionary "
<< dict.name() << " or is not a dictionary"
<< endl
<< "Valid keywords are " << dict.keys()
<< exit(FatalIOError);
}
return entPtr->dict();
}
}
void remove(dictionary& dict, const dictionary& removeDict) void remove(dictionary& dict, const dictionary& removeDict)
{ {
forAllConstIter(dictionary, removeDict, iter) forAllConstIter(dictionary, removeDict, iter)
{ {
const entry* entPtr = dict.lookupEntryPtr entry* entPtr = dict.lookupEntryPtr
( (
iter().keyword(), iter().keyword(),
false, false,
@ -300,11 +244,7 @@ void remove(dictionary& dict, const dictionary& removeDict)
{ {
if (iter().isDict()) if (iter().isDict())
{ {
remove remove(entPtr->dict(), iter().dict());
(
const_cast<dictionary&>(entPtr->dict()),
iter().dict()
);
// Check if dictionary is empty // Check if dictionary is empty
if (!entPtr->dict().size()) if (!entPtr->dict().size())
@ -408,13 +348,13 @@ void substitute(dictionary& dict, string substitutions)
forAll(namedArgs, i) forAll(namedArgs, i)
{ {
const Pair<word> dAk(dictAndKeyword(scope(namedArgs[i].first()))); const Pair<word> dAk(dictAndKeyword(dotToSlash(namedArgs[i].first())));
const dictionary& subDict(lookupScopedDict(dict, dAk.first())); dictionary& subDict(dict.scopedDict(dAk.first()));
IStringStream entryStream IStringStream entryStream
( (
dAk.second() + ' ' + namedArgs[i].second() + ';' dAk.second() + ' ' + namedArgs[i].second() + ';'
); );
const_cast<dictionary&>(subDict).set(entry::New(entryStream).ptr()); subDict.set(entry::New(entryStream).ptr());
} }
} }
@ -601,7 +541,7 @@ int main(int argc, char *argv[])
word entryName; word entryName;
if (args.optionReadIfPresent("entry", entryName)) if (args.optionReadIfPresent("entry", entryName))
{ {
const word scopedName(scope(entryName)); const word scopedName(dotToSlash(entryName));
string newValue; string newValue;
if if
@ -615,7 +555,7 @@ int main(int argc, char *argv[])
const bool merge = args.optionFound("merge"); const bool merge = args.optionFound("merge");
const Pair<word> dAk(dictAndKeyword(scopedName)); const Pair<word> dAk(dictAndKeyword(scopedName));
const dictionary& d(lookupScopedDict(dict, dAk.first())); dictionary& subDict(dict.scopedDict(dAk.first()));
entry* ePtr = nullptr; entry* ePtr = nullptr;
@ -654,11 +594,11 @@ int main(int argc, char *argv[])
if (overwrite) if (overwrite)
{ {
Info << "New entry " << *ePtr << endl; Info << "New entry " << *ePtr << endl;
const_cast<dictionary&>(d).set(ePtr); subDict.set(ePtr);
} }
else else
{ {
const_cast<dictionary&>(d).add(ePtr, merge); subDict.add(ePtr, merge);
} }
changed = true; changed = true;
} }
@ -667,8 +607,8 @@ int main(int argc, char *argv[])
// Extract dictionary name and keyword // Extract dictionary name and keyword
const Pair<word> dAk(dictAndKeyword(scopedName)); const Pair<word> dAk(dictAndKeyword(scopedName));
const dictionary& d(lookupScopedDict(dict, dAk.first())); dictionary& subDict(dict.scopedDict(dAk.first()));
const_cast<dictionary&>(d).remove(dAk.second()); subDict.remove(dAk.second());
changed = true; changed = true;
} }
else else
@ -678,27 +618,23 @@ int main(int argc, char *argv[])
{ {
const Pair<word> dAk(dictAndKeyword(scopedName)); const Pair<word> dAk(dictAndKeyword(scopedName));
const dictionary& d(lookupScopedDict(dict, dAk.first())); dictionary& subDict(dict.scopedDict(dAk.first()));
const dictionary& d2(lookupScopedDict(diffDict, dAk.first())); const dictionary& subDict2(diffDict.scopedDict(dAk.first()));
const entry* ePtr = entry* ePtr =
d.lookupEntryPtr(dAk.second(), false, true); subDict.lookupEntryPtr(dAk.second(), false, true);
const entry* e2Ptr = const entry* e2Ptr =
d2.lookupEntryPtr(dAk.second(), false, true); subDict2.lookupEntryPtr(dAk.second(), false, true);
if (ePtr && e2Ptr) if (ePtr && e2Ptr)
{ {
if (*ePtr == *e2Ptr) if (*ePtr == *e2Ptr)
{ {
const_cast<dictionary&>(d).remove(dAk.second()); subDict.remove(dAk.second());
} }
else if (ePtr->isDict() && e2Ptr->isDict()) else if (ePtr->isDict() && e2Ptr->isDict())
{ {
remove remove(ePtr->dict(), e2Ptr->dict());
(
const_cast<dictionary&>(ePtr->dict()),
e2Ptr->dict()
);
} }
} }
} }

View File

@ -82,10 +82,8 @@ const Foam::entry* Foam::dictionary::lookupDotScopedSubEntryPtr
// Go to parent // Go to parent
if (&dictPtr->parent_ == &dictionary::null) if (&dictPtr->parent_ == &dictionary::null)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "No parent of current dictionary"
*this
) << "No parent of current dictionary"
<< " when searching for " << " when searching for "
<< keyword.substr(begVar, keyword.size() - begVar) << keyword.substr(begVar, keyword.size() - begVar)
<< exit(FatalIOError); << exit(FatalIOError);
@ -208,10 +206,8 @@ const Foam::entry* Foam::dictionary::lookupSlashScopedSubEntryPtr
// Go to parent // Go to parent
if (&parent_ == &dictionary::null) if (&parent_ == &dictionary::null)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "No parent of current dictionary"
*this
) << "No parent of current dictionary"
<< " when searching for " << " when searching for "
<< keyword.substr(slashPos, keyword.size() - slashPos) << keyword.substr(slashPos, keyword.size() - slashPos)
<< exit(FatalIOError); << exit(FatalIOError);
@ -339,10 +335,8 @@ const Foam::entry* Foam::dictionary::lookupScopedSubEntryPtr
if (fName == topDict().name()) if (fName == topDict().name())
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "Attempt to re-read current dictionary " << fName
*this
) << "Attempt to re-read current dictionary " << fName
<< " for keyword " << " for keyword "
<< keyword << keyword
<< exit(FatalIOError); << exit(FatalIOError);
@ -362,10 +356,8 @@ const Foam::entry* Foam::dictionary::lookupScopedSubEntryPtr
if (!ifs || !ifs.good()) if (!ifs || !ifs.good())
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "dictionary file " << fName
*this
) << "dictionary file " << fName
<< " cannot be found for keyword " << " cannot be found for keyword "
<< keyword << keyword
<< exit(FatalIOError); << exit(FatalIOError);
@ -382,10 +374,8 @@ const Foam::entry* Foam::dictionary::lookupScopedSubEntryPtr
if (!hmm) if (!hmm)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(dict)
( << "keyword " << localKeyword
dict
) << "keyword " << localKeyword
<< " is undefined in dictionary " << " is undefined in dictionary "
<< dict.name() << dict.name()
<< exit(FatalIOError); << exit(FatalIOError);
@ -796,10 +786,8 @@ const Foam::entry& Foam::dictionary::lookupEntry
if (entryPtr == nullptr) if (entryPtr == nullptr)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "keyword " << keyword << " is undefined in dictionary "
*this
) << "keyword " << keyword << " is undefined in dictionary "
<< name() << name()
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -937,10 +925,8 @@ const Foam::dictionary& Foam::dictionary::subDict(const word& keyword) const
if (entryPtr == nullptr) if (entryPtr == nullptr)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "keyword " << keyword << " is undefined in dictionary "
*this
) << "keyword " << keyword << " is undefined in dictionary "
<< name() << name()
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -954,10 +940,8 @@ Foam::dictionary& Foam::dictionary::subDict(const word& keyword)
if (entryPtr == nullptr) if (entryPtr == nullptr)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "keyword " << keyword << " is undefined in dictionary "
*this
) << "keyword " << keyword << " is undefined in dictionary "
<< name() << name()
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -977,10 +961,8 @@ Foam::dictionary Foam::dictionary::subOrEmptyDict
{ {
if (mustRead) if (mustRead)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "keyword " << keyword << " is undefined in dictionary "
*this
) << "keyword " << keyword << " is undefined in dictionary "
<< name() << name()
<< exit(FatalIOError); << exit(FatalIOError);
return entryPtr->dict(); return entryPtr->dict();
@ -1015,6 +997,44 @@ const Foam::dictionary& Foam::dictionary::optionalSubDict
} }
const Foam::dictionary& Foam::dictionary::scopedDict(const word& keyword) const
{
if (keyword == "")
{
return *this;
}
else
{
const entry* entPtr = lookupScopedEntryPtr
(
keyword,
false,
false
);
if (!entPtr || !entPtr->isDict())
{
FatalIOErrorInFunction(*this)
<< "keyword " << keyword
<< " is undefined in dictionary "
<< name() << " or is not a dictionary"
<< endl
<< "Valid keywords are " << keys()
<< exit(FatalIOError);
}
return entPtr->dict();
}
}
Foam::dictionary& Foam::dictionary::scopedDict(const word& keyword)
{
return const_cast<dictionary&>
(
const_cast<const dictionary*>(this)->scopedDict(keyword)
);
}
Foam::wordList Foam::dictionary::toc() const Foam::wordList Foam::dictionary::toc() const
{ {
wordList keys(size()); wordList keys(size());
@ -1271,10 +1291,8 @@ bool Foam::dictionary::changeKeyword
if (iter()->keyword().isPattern()) if (iter()->keyword().isPattern())
{ {
FatalIOErrorInFunction FatalIOErrorInFunction(*this)
( << "Old keyword "<< oldKeyword
*this
) << "Old keyword "<< oldKeyword
<< " is a pattern." << " is a pattern."
<< "Pattern replacement not yet implemented." << "Pattern replacement not yet implemented."
<< exit(FatalIOError); << exit(FatalIOError);
@ -1536,4 +1554,28 @@ Foam::dictionary Foam::operator|
} }
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
Foam::Pair<Foam::word> Foam::dictAndKeyword(const word& scopedName)
{
string::size_type i = scopedName.find_last_of
(
functionEntries::inputSyntaxEntry::scopeChar()
);
if (i != string::npos)
{
return Pair<word>
(
scopedName.substr(0, i),
scopedName.substr(i+1, string::npos)
);
}
else
{
return Pair<word>("", scopedName);
}
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -58,8 +58,7 @@ SourceFiles
#include "HashTable.H" #include "HashTable.H"
#include "wordList.H" #include "wordList.H"
#include "className.H" #include "className.H"
#include "Pair.H"
#include "VectorSpace.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -465,6 +464,16 @@ public:
// otherwise return this dictionary // otherwise return this dictionary
const dictionary& optionalSubDict(const word&) const; const dictionary& optionalSubDict(const word&) const;
//- Find and return a sub-dictionary by scoped lookup
// i.e. the keyword may contain scope characters.
// If the keyword is null this dictionary is returned
const dictionary& scopedDict(const word&) const;
//- Find and return a sub-dictionary by scoped lookup
// i.e. the keyword may contain scope characters.
// If the keyword is null this dictionary is returned
dictionary& scopedDict(const word&);
//- Return the table of contents //- Return the table of contents
wordList toc() const; wordList toc() const;
@ -625,6 +634,9 @@ dictionary operator|(const dictionary& dict1, const dictionary& dict2);
// Global Functions // Global Functions
//- Extracts dict name and keyword
Pair<word> dictAndKeyword(const word& scopedName);
//- Write a dictionary entry //- Write a dictionary entry
void writeEntry(Ostream& os, const dictionary& dict); void writeEntry(Ostream& os, const dictionary& dict);

View File

@ -292,7 +292,8 @@ bool Foam::functionObjectList::readFunctionObject
} }
else if (c == '=') else if (c == '=')
{ {
argName = string::validate<word>(funcCall(start, i - start)); argName = funcCall(start, i - start);
string::stripInvalid<variable>(argName);
start = i+1; start = i+1;
namedArg = true; namedArg = true;
} }
@ -381,11 +382,13 @@ bool Foam::functionObjectList::readFunctionObject
&& namedArgs[i].first() != "objects" && namedArgs[i].first() != "objects"
) )
{ {
const Pair<word> dAk(dictAndKeyword(namedArgs[i].first()));
dictionary& subDict(funcDict.scopedDict(dAk.first()));
IStringStream entryStream IStringStream entryStream
( (
namedArgs[i].first() + ' ' + namedArgs[i].second() + ';' dAk.second() + ' ' + namedArgs[i].second() + ';'
); );
funcDict.set(entry::New(entryStream).ptr()); subDict.set(entry::New(entryStream).ptr());
} }
} }