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
word scope(const fileName& entryName)
word dotToSlash(const fileName& entryName)
{
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)
{
forAllConstIter(dictionary, removeDict, iter)
{
const entry* entPtr = dict.lookupEntryPtr
entry* entPtr = dict.lookupEntryPtr
(
iter().keyword(),
false,
@ -300,11 +244,7 @@ void remove(dictionary& dict, const dictionary& removeDict)
{
if (iter().isDict())
{
remove
(
const_cast<dictionary&>(entPtr->dict()),
iter().dict()
);
remove(entPtr->dict(), iter().dict());
// Check if dictionary is empty
if (!entPtr->dict().size())
@ -408,13 +348,13 @@ void substitute(dictionary& dict, string substitutions)
forAll(namedArgs, i)
{
const Pair<word> dAk(dictAndKeyword(scope(namedArgs[i].first())));
const dictionary& subDict(lookupScopedDict(dict, dAk.first()));
const Pair<word> dAk(dictAndKeyword(dotToSlash(namedArgs[i].first())));
dictionary& subDict(dict.scopedDict(dAk.first()));
IStringStream entryStream
(
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;
if (args.optionReadIfPresent("entry", entryName))
{
const word scopedName(scope(entryName));
const word scopedName(dotToSlash(entryName));
string newValue;
if
@ -615,7 +555,7 @@ int main(int argc, char *argv[])
const bool merge = args.optionFound("merge");
const Pair<word> dAk(dictAndKeyword(scopedName));
const dictionary& d(lookupScopedDict(dict, dAk.first()));
dictionary& subDict(dict.scopedDict(dAk.first()));
entry* ePtr = nullptr;
@ -654,11 +594,11 @@ int main(int argc, char *argv[])
if (overwrite)
{
Info << "New entry " << *ePtr << endl;
const_cast<dictionary&>(d).set(ePtr);
subDict.set(ePtr);
}
else
{
const_cast<dictionary&>(d).add(ePtr, merge);
subDict.add(ePtr, merge);
}
changed = true;
}
@ -667,8 +607,8 @@ int main(int argc, char *argv[])
// Extract dictionary name and keyword
const Pair<word> dAk(dictAndKeyword(scopedName));
const dictionary& d(lookupScopedDict(dict, dAk.first()));
const_cast<dictionary&>(d).remove(dAk.second());
dictionary& subDict(dict.scopedDict(dAk.first()));
subDict.remove(dAk.second());
changed = true;
}
else
@ -678,27 +618,23 @@ int main(int argc, char *argv[])
{
const Pair<word> dAk(dictAndKeyword(scopedName));
const dictionary& d(lookupScopedDict(dict, dAk.first()));
const dictionary& d2(lookupScopedDict(diffDict, dAk.first()));
dictionary& subDict(dict.scopedDict(dAk.first()));
const dictionary& subDict2(diffDict.scopedDict(dAk.first()));
const entry* ePtr =
d.lookupEntryPtr(dAk.second(), false, true);
entry* ePtr =
subDict.lookupEntryPtr(dAk.second(), false, true);
const entry* e2Ptr =
d2.lookupEntryPtr(dAk.second(), false, true);
subDict2.lookupEntryPtr(dAk.second(), false, true);
if (ePtr && e2Ptr)
{
if (*ePtr == *e2Ptr)
{
const_cast<dictionary&>(d).remove(dAk.second());
subDict.remove(dAk.second());
}
else if (ePtr->isDict() && e2Ptr->isDict())
{
remove
(
const_cast<dictionary&>(ePtr->dict()),
e2Ptr->dict()
);
remove(ePtr->dict(), e2Ptr->dict());
}
}
}

View File

@ -82,10 +82,8 @@ const Foam::entry* Foam::dictionary::lookupDotScopedSubEntryPtr
// Go to parent
if (&dictPtr->parent_ == &dictionary::null)
{
FatalIOErrorInFunction
(
*this
) << "No parent of current dictionary"
FatalIOErrorInFunction(*this)
<< "No parent of current dictionary"
<< " when searching for "
<< keyword.substr(begVar, keyword.size() - begVar)
<< exit(FatalIOError);
@ -208,10 +206,8 @@ const Foam::entry* Foam::dictionary::lookupSlashScopedSubEntryPtr
// Go to parent
if (&parent_ == &dictionary::null)
{
FatalIOErrorInFunction
(
*this
) << "No parent of current dictionary"
FatalIOErrorInFunction(*this)
<< "No parent of current dictionary"
<< " when searching for "
<< keyword.substr(slashPos, keyword.size() - slashPos)
<< exit(FatalIOError);
@ -339,10 +335,8 @@ const Foam::entry* Foam::dictionary::lookupScopedSubEntryPtr
if (fName == topDict().name())
{
FatalIOErrorInFunction
(
*this
) << "Attempt to re-read current dictionary " << fName
FatalIOErrorInFunction(*this)
<< "Attempt to re-read current dictionary " << fName
<< " for keyword "
<< keyword
<< exit(FatalIOError);
@ -362,10 +356,8 @@ const Foam::entry* Foam::dictionary::lookupScopedSubEntryPtr
if (!ifs || !ifs.good())
{
FatalIOErrorInFunction
(
*this
) << "dictionary file " << fName
FatalIOErrorInFunction(*this)
<< "dictionary file " << fName
<< " cannot be found for keyword "
<< keyword
<< exit(FatalIOError);
@ -382,10 +374,8 @@ const Foam::entry* Foam::dictionary::lookupScopedSubEntryPtr
if (!hmm)
{
FatalIOErrorInFunction
(
dict
) << "keyword " << localKeyword
FatalIOErrorInFunction(dict)
<< "keyword " << localKeyword
<< " is undefined in dictionary "
<< dict.name()
<< exit(FatalIOError);
@ -796,10 +786,8 @@ const Foam::entry& Foam::dictionary::lookupEntry
if (entryPtr == nullptr)
{
FatalIOErrorInFunction
(
*this
) << "keyword " << keyword << " is undefined in dictionary "
FatalIOErrorInFunction(*this)
<< "keyword " << keyword << " is undefined in dictionary "
<< name()
<< exit(FatalIOError);
}
@ -937,10 +925,8 @@ const Foam::dictionary& Foam::dictionary::subDict(const word& keyword) const
if (entryPtr == nullptr)
{
FatalIOErrorInFunction
(
*this
) << "keyword " << keyword << " is undefined in dictionary "
FatalIOErrorInFunction(*this)
<< "keyword " << keyword << " is undefined in dictionary "
<< name()
<< exit(FatalIOError);
}
@ -954,10 +940,8 @@ Foam::dictionary& Foam::dictionary::subDict(const word& keyword)
if (entryPtr == nullptr)
{
FatalIOErrorInFunction
(
*this
) << "keyword " << keyword << " is undefined in dictionary "
FatalIOErrorInFunction(*this)
<< "keyword " << keyword << " is undefined in dictionary "
<< name()
<< exit(FatalIOError);
}
@ -977,10 +961,8 @@ Foam::dictionary Foam::dictionary::subOrEmptyDict
{
if (mustRead)
{
FatalIOErrorInFunction
(
*this
) << "keyword " << keyword << " is undefined in dictionary "
FatalIOErrorInFunction(*this)
<< "keyword " << keyword << " is undefined in dictionary "
<< name()
<< exit(FatalIOError);
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
{
wordList keys(size());
@ -1271,10 +1291,8 @@ bool Foam::dictionary::changeKeyword
if (iter()->keyword().isPattern())
{
FatalIOErrorInFunction
(
*this
) << "Old keyword "<< oldKeyword
FatalIOErrorInFunction(*this)
<< "Old keyword "<< oldKeyword
<< " is a pattern."
<< "Pattern replacement not yet implemented."
<< 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 "wordList.H"
#include "className.H"
#include "VectorSpace.H"
#include "Pair.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -465,6 +464,16 @@ public:
// otherwise return this dictionary
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
wordList toc() const;
@ -625,6 +634,9 @@ dictionary operator|(const dictionary& dict1, const dictionary& dict2);
// Global Functions
//- Extracts dict name and keyword
Pair<word> dictAndKeyword(const word& scopedName);
//- Write a dictionary entry
void writeEntry(Ostream& os, const dictionary& dict);

View File

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