writeKeyword spacing for keyType, misc cosmetics changes

This commit is contained in:
Mark Olesen
2008-12-10 23:00:39 +01:00
parent 599c1c5976
commit 7473adc89c
10 changed files with 211 additions and 168 deletions

View File

@ -41,34 +41,44 @@ using namespace Foam;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Info<< dictionary(IFstream("testDict")()) << endl; {
dictionary dict(IFstream("testDict")());
Info<< "dict: " << dict << nl
<< "toc: " << dict.toc() << nl
<< "keys: " << dict.keys() << nl
<< "patterns: " << dict.keys(true) << endl;
}
IOobject::writeDivider(Info); IOobject::writeDivider(Info);
{ {
dictionary dict(IFstream("testDictRegex")()); dictionary dict(IFstream("testDictRegex")());
dict.add(keyType("fooba[rz]", true), "anything");
Info<< "dict:" << dict << endl; Info<< "dict:" << dict << nl
<< "toc: " << dict.toc() << nl
<< "keys: " << dict.keys() << nl
<< "patterns: " << dict.keys(true) << endl;
// Wildcard find. Info<< "Pattern find \"abc\" in top directory : "
Info<< "Wildcard find \"abc\" in top directory : "
<< dict.lookup("abc") << endl; << dict.lookup("abc") << endl;
Info<< "Wildcard find \"abc\" in sub directory : " Info<< "Pattern find \"abc\" in sub directory : "
<< dict.subDict("someDict").lookup("abc") << dict.subDict("someDict").lookup("abc")
<< endl; << endl;
Info<< "Recursive wildcard find \"def\" in sub directory : " Info<< "Recursive pattern find \"def\" in sub directory : "
<< dict.subDict("someDict").lookup("def", true) << dict.subDict("someDict").lookup("def", true)
<< endl; << endl;
Info<< "Recursive wildcard find \"foo\" in sub directory : " Info<< "Recursive pattern find \"foo\" in sub directory : "
<< dict.subDict("someDict").lookup("foo", true) << dict.subDict("someDict").lookup("foo", true)
<< endl; << endl;
Info<< "Recursive wildcard find \"fooz\" in sub directory : " Info<< "Recursive pattern find \"fooz\" in sub directory : "
<< dict.subDict("someDict").lookup("fooz", true) << dict.subDict("someDict").lookup("fooz", true)
<< endl; << endl;
Info<< "Recursive wildcard find \"bar\" in sub directory : " Info<< "Recursive pattern find \"bar\" in sub directory : "
<< dict.subDict("someDict").lookup("bar", true) << dict.subDict("someDict").lookup("bar", true)
<< endl; << endl;
Info<< "Recursive wildcard find \"xxx\" in sub directory : " Info<< "Recursive pattern find \"xxx\" in sub directory : "
<< dict.subDict("someDict").lookup("xxx", true) << dict.subDict("someDict").lookup("xxx", true)
<< endl; << endl;
} }

View File

@ -18,8 +18,10 @@ FoamFile
".*" parentValue1; ".*" parentValue1;
"[n-z].*" parentValue2; "[n-z].*" parentValue2;
"f.*" parentValue3; "f.*" parentValue3;
keyX parentValue4;
keyY parentValue5;
someDict "(.*)Dict"
{ {
foo subdictValue0; foo subdictValue0;
bar $f.*; // should this really match 'foo'? bar $f.*; // should this really match 'foo'?
@ -28,7 +30,7 @@ someDict
"a.*c" subdictValue3; "a.*c" subdictValue3;
"ab.*" subdictValue2; "ab.*" subdictValue2;
"a.*" subdictValue1; "a.*" subdictValue1;
abcd subdictValue4; abcd \1;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -32,22 +32,22 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
//- Write keyType // Write keyType
Foam::Ostream& Foam::Ostream::write(const keyType& s) Foam::Ostream& Foam::Ostream::write(const keyType& kw)
{ {
// Write as word? // Write as word or string
if (s.isWildCard()) if (kw.isPattern())
{ {
return write(static_cast<const string&>(s)); return write(static_cast<const string&>(kw));
} }
else else
{ {
return write(static_cast<const word&>(s)); return write(static_cast<const word&>(kw));
} }
} }
//- Decrememt the indent level // Decrement the indent level
void Foam::Ostream::decrIndent() void Foam::Ostream::decrIndent()
{ {
if (indentLevel_ == 0) if (indentLevel_ == 0)
@ -62,15 +62,26 @@ void Foam::Ostream::decrIndent()
} }
// Write the keyword to the Ostream followed by appropriate indentation // Write the keyword followed by appropriate indentation
Foam::Ostream& Foam::Ostream::writeKeyword(const Foam::keyType& keyword) Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
{ {
indent(); indent();
write(keyword); write(kw);
label nSpaces = max(entryIndentation_ - label(keyword.size()), 1); label nSpaces = entryIndentation_ - label(kw.size());
for (label i=0; i<nSpaces; i++) // pattern is surrounded by quotes
if (kw.isPattern())
{
nSpaces -= 2;
}
if (nSpaces < 1)
{
nSpaces = 1;
}
while (nSpaces--)
{ {
write(char(token::SPACE)); write(char(token::SPACE));
} }

View File

@ -60,12 +60,11 @@ protected:
//- Number of spaces per indent level //- Number of spaces per indent level
static const unsigned short indentSize_ = 4; static const unsigned short indentSize_ = 4;
//- Current indent level
unsigned short indentLevel_;
//- Indentation of the entry from the start of the keyword //- Indentation of the entry from the start of the keyword
static const unsigned short entryIndentation_ = 16; static const unsigned short entryIndentation_ = 16;
//- Current indent level
unsigned short indentLevel_;
public: public:
@ -148,9 +147,8 @@ public:
//- Decrememt the indent level //- Decrememt the indent level
void decrIndent(); void decrIndent();
//- Write the keyword to the Ostream followed by //- Write the keyword followed by an appropriate indentation
// appropriate indentation Ostream& writeKeyword(const keyType&);
Ostream& writeKeyword(const keyType& keyword);
// Stream state functions // Stream state functions

View File

@ -38,9 +38,9 @@ const Foam::dictionary Foam::dictionary::null;
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::dictionary::findInWildcards bool Foam::dictionary::findInPatterns
( (
const bool wildCardMatch, const bool patternMatch,
const word& Keyword, const word& Keyword,
DLList<entry*>::const_iterator& wcLink, DLList<entry*>::const_iterator& wcLink,
DLList<autoPtr<regExp> >::const_iterator& reLink DLList<autoPtr<regExp> >::const_iterator& reLink
@ -50,11 +50,11 @@ bool Foam::dictionary::findInWildcards
{ {
while (wcLink != wildCardEntries_.end()) while (wcLink != wildCardEntries_.end())
{ {
if (!wildCardMatch && wcLink()->keyword() == Keyword) if
{ (
return true; patternMatch ? reLink()->match(Keyword)
} : wcLink()->keyword() == Keyword
else if (wildCardMatch && reLink()->match(Keyword)) )
{ {
return true; return true;
} }
@ -68,9 +68,9 @@ bool Foam::dictionary::findInWildcards
} }
bool Foam::dictionary::findInWildcards bool Foam::dictionary::findInPatterns
( (
const bool wildCardMatch, const bool patternMatch,
const word& Keyword, const word& Keyword,
DLList<entry*>::iterator& wcLink, DLList<entry*>::iterator& wcLink,
DLList<autoPtr<regExp> >::iterator& reLink DLList<autoPtr<regExp> >::iterator& reLink
@ -80,11 +80,11 @@ bool Foam::dictionary::findInWildcards
{ {
while (wcLink != wildCardEntries_.end()) while (wcLink != wildCardEntries_.end())
{ {
if (!wildCardMatch && wcLink()->keyword() == Keyword) if
{ (
return true; patternMatch ? reLink()->match(Keyword)
} : wcLink()->keyword() == Keyword
else if (wildCardMatch && reLink()->match(Keyword)) )
{ {
return true; return true;
} }
@ -125,7 +125,7 @@ Foam::dictionary::dictionary
{ {
hashedEntries_.insert(iter().keyword(), &iter()); hashedEntries_.insert(iter().keyword(), &iter());
if (iter().keyword().isWildCard()) if (iter().keyword().isPattern())
{ {
wildCardEntries_.insert(&iter()); wildCardEntries_.insert(&iter());
wildCardRegexps_.insert wildCardRegexps_.insert
@ -155,7 +155,7 @@ Foam::dictionary::dictionary
{ {
hashedEntries_.insert(iter().keyword(), &iter()); hashedEntries_.insert(iter().keyword(), &iter());
if (iter().keyword().isWildCard()) if (iter().keyword().isPattern())
{ {
wildCardEntries_.insert(&iter()); wildCardEntries_.insert(&iter());
wildCardRegexps_.insert wildCardRegexps_.insert
@ -223,8 +223,8 @@ bool Foam::dictionary::found(const word& keyword, bool recursive) const
DLList<autoPtr<regExp> >::const_iterator reLink = DLList<autoPtr<regExp> >::const_iterator reLink =
wildCardRegexps_.begin(); wildCardRegexps_.begin();
// Find in wildcards using regular expressions only // Find in patterns using regular expressions only
if (findInWildcards(true, keyword, wcLink, reLink)) if (findInPatterns(true, keyword, wcLink, reLink))
{ {
return true; return true;
} }
@ -246,22 +246,22 @@ const Foam::entry* Foam::dictionary::lookupEntryPtr
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) const ) const
{ {
HashTable<entry*>::const_iterator iter = hashedEntries_.find(keyword); HashTable<entry*>::const_iterator iter = hashedEntries_.find(keyword);
if (iter == hashedEntries_.end()) if (iter == hashedEntries_.end())
{ {
if (wildCardMatch && wildCardEntries_.size() > 0) if (patternMatch && wildCardEntries_.size() > 0)
{ {
DLList<entry*>::const_iterator wcLink = DLList<entry*>::const_iterator wcLink =
wildCardEntries_.begin(); wildCardEntries_.begin();
DLList<autoPtr<regExp> >::const_iterator reLink = DLList<autoPtr<regExp> >::const_iterator reLink =
wildCardRegexps_.begin(); wildCardRegexps_.begin();
// Find in wildcards using regular expressions only // Find in patterns using regular expressions only
if (findInWildcards(wildCardMatch, keyword, wcLink, reLink)) if (findInPatterns(patternMatch, keyword, wcLink, reLink))
{ {
return wcLink(); return wcLink();
} }
@ -269,7 +269,7 @@ const Foam::entry* Foam::dictionary::lookupEntryPtr
if (recursive && &parent_ != &dictionary::null) if (recursive && &parent_ != &dictionary::null)
{ {
return parent_.lookupEntryPtr(keyword, recursive, wildCardMatch); return parent_.lookupEntryPtr(keyword, recursive, patternMatch);
} }
else else
{ {
@ -285,21 +285,22 @@ Foam::entry* Foam::dictionary::lookupEntryPtr
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) )
{ {
HashTable<entry*>::iterator iter = hashedEntries_.find(keyword); HashTable<entry*>::iterator iter = hashedEntries_.find(keyword);
if (iter == hashedEntries_.end()) if (iter == hashedEntries_.end())
{ {
if (wildCardMatch && wildCardEntries_.size() > 0) if (patternMatch && wildCardEntries_.size() > 0)
{ {
DLList<entry*>::iterator wcLink = DLList<entry*>::iterator wcLink =
wildCardEntries_.begin(); wildCardEntries_.begin();
DLList<autoPtr<regExp> >::iterator reLink = DLList<autoPtr<regExp> >::iterator reLink =
wildCardRegexps_.begin(); wildCardRegexps_.begin();
// Find in wildcards using regular expressions only
if (findInWildcards(wildCardMatch, keyword, wcLink, reLink)) // Find in patterns using regular expressions only
if (findInPatterns(patternMatch, keyword, wcLink, reLink))
{ {
return wcLink(); return wcLink();
} }
@ -311,7 +312,7 @@ Foam::entry* Foam::dictionary::lookupEntryPtr
( (
keyword, keyword,
recursive, recursive,
wildCardMatch patternMatch
); );
} }
else else
@ -328,10 +329,10 @@ const Foam::entry& Foam::dictionary::lookupEntry
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) const ) const
{ {
const entry* entryPtr = lookupEntryPtr(keyword, recursive, wildCardMatch); const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
if (entryPtr == NULL) if (entryPtr == NULL)
{ {
@ -352,16 +353,16 @@ Foam::ITstream& Foam::dictionary::lookup
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) const ) const
{ {
return lookupEntry(keyword, recursive, wildCardMatch).stream(); return lookupEntry(keyword, recursive, patternMatch).stream();
} }
bool Foam::dictionary::isDict(const word& keyword) const bool Foam::dictionary::isDict(const word& keyword) const
{ {
// Find non-recursive with wildcards // Find non-recursive with patterns
const entry* entryPtr = lookupEntryPtr(keyword, false, true); const entry* entryPtr = lookupEntryPtr(keyword, false, true);
if (entryPtr) if (entryPtr)
@ -430,7 +431,7 @@ Foam::wordList Foam::dictionary::toc() const
{ {
wordList keys(size()); wordList keys(size());
label i = 0; label nKeys = 0;
for for
( (
IDLList<entry>::const_iterator iter = begin(); IDLList<entry>::const_iterator iter = begin();
@ -438,13 +439,36 @@ Foam::wordList Foam::dictionary::toc() const
++iter ++iter
) )
{ {
keys[i++] = iter().keyword(); keys[nKeys++] = iter().keyword();
} }
return keys; return keys;
} }
Foam::List<Foam::keyType> Foam::dictionary::keys(bool patterns) const
{
List<keyType> keys(size());
label nKeys = 0;
for
(
IDLList<entry>::const_iterator iter = begin();
iter != end();
++iter
)
{
if (iter().keyword().isPattern() ? patterns : !patterns)
{
keys[nKeys++] = iter().keyword();
}
}
keys.setSize(nKeys);
return keys;
}
bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry) bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
{ {
HashTable<entry*>::iterator iter = hashedEntries_.find HashTable<entry*>::iterator iter = hashedEntries_.find
@ -473,7 +497,7 @@ bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
{ {
entryPtr->name() = name_ + "::" + entryPtr->keyword(); entryPtr->name() = name_ + "::" + entryPtr->keyword();
if (entryPtr->keyword().isWildCard()) if (entryPtr->keyword().isPattern())
{ {
wildCardEntries_.insert(entryPtr); wildCardEntries_.insert(entryPtr);
wildCardRegexps_.insert wildCardRegexps_.insert
@ -502,7 +526,7 @@ bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
entryPtr->name() = name_ + "::" + entryPtr->keyword(); entryPtr->name() = name_ + "::" + entryPtr->keyword();
IDLList<entry>::append(entryPtr); IDLList<entry>::append(entryPtr);
if (entryPtr->keyword().isWildCard()) if (entryPtr->keyword().isPattern())
{ {
wildCardEntries_.insert(entryPtr); wildCardEntries_.insert(entryPtr);
wildCardRegexps_.insert wildCardRegexps_.insert
@ -597,13 +621,12 @@ bool Foam::dictionary::remove(const word& Keyword)
if (iter != hashedEntries_.end()) if (iter != hashedEntries_.end())
{ {
// Delete from wildcards first // Delete from patterns first
DLList<entry*>::iterator wcLink = DLList<entry*>::iterator wcLink = wildCardEntries_.begin();
wildCardEntries_.begin();
DLList<autoPtr<regExp> >::iterator reLink = wildCardRegexps_.begin(); DLList<autoPtr<regExp> >::iterator reLink = wildCardRegexps_.begin();
// Find in wildcards using exact match only // Find in pattern using exact match only
if (findInWildcards(false, Keyword, wcLink, reLink)) if (findInPatterns(false, Keyword, wcLink, reLink))
{ {
wildCardEntries_.remove(wcLink); wildCardEntries_.remove(wcLink);
wildCardRegexps_.remove(reLink); wildCardRegexps_.remove(reLink);
@ -643,14 +666,14 @@ bool Foam::dictionary::changeKeyword
return false; return false;
} }
if (iter()->keyword().isWildCard()) if (iter()->keyword().isPattern())
{ {
FatalErrorIn FatalErrorIn
( (
"dictionary::changeKeyword(const word&, const word&, bool)" "dictionary::changeKeyword(const word&, const word&, bool)"
) << "Old keyword "<< oldKeyword ) << "Old keyword "<< oldKeyword
<< " is a wildcard." << " is a pattern."
<< "Wildcard replacement not yet implemented." << "Pattern replacement not yet implemented."
<< exit(FatalError); << exit(FatalError);
} }
@ -662,16 +685,16 @@ bool Foam::dictionary::changeKeyword
{ {
if (forceOverwrite) if (forceOverwrite)
{ {
if (iter2()->keyword().isWildCard()) if (iter2()->keyword().isPattern())
{ {
// Delete from wildcards first // Delete from patterns first
DLList<entry*>::iterator wcLink = DLList<entry*>::iterator wcLink =
wildCardEntries_.begin(); wildCardEntries_.begin();
DLList<autoPtr<regExp> >::iterator reLink = DLList<autoPtr<regExp> >::iterator reLink =
wildCardRegexps_.begin(); wildCardRegexps_.begin();
// Find in wildcards using exact match only // Find in patterns using exact match only
if (findInWildcards(false, iter2()->keyword(), wcLink, reLink)) if (findInPatterns(false, iter2()->keyword(), wcLink, reLink))
{ {
wildCardEntries_.remove(wcLink); wildCardEntries_.remove(wcLink);
wildCardRegexps_.remove(reLink); wildCardRegexps_.remove(reLink);
@ -701,7 +724,7 @@ bool Foam::dictionary::changeKeyword
hashedEntries_.erase(oldKeyword); hashedEntries_.erase(oldKeyword);
hashedEntries_.insert(newKeyword, iter()); hashedEntries_.insert(newKeyword, iter());
if (newKeyword.isWildCard()) if (newKeyword.isPattern())
{ {
wildCardEntries_.insert(iter()); wildCardEntries_.insert(iter());
wildCardRegexps_.insert wildCardRegexps_.insert

View File

@ -27,12 +27,12 @@ Class
Description Description
A list of keyword definitions, which are a keyword followed by any number A list of keyword definitions, which are a keyword followed by any number
of values (e.g. words and numbers). The keywords can represent wildcards of values (e.g. words and numbers). The keywords can represent patterns
which are matched using Posix regular expressions. The general order for which are matched using Posix regular expressions. The general order for
searching is searching is as follows:
- exact match - exact match
- wildcard match (in reverse order) - pattern match (in reverse order)
- optional recursion into subdictionaries - optional recursion into the enclosing (parent) dictionaries
The dictionary class is the base class for IOdictionary. The dictionary class is the base class for IOdictionary.
It also serves as a bootstrap dictionary for the objectRegistry data It also serves as a bootstrap dictionary for the objectRegistry data
@ -92,29 +92,27 @@ class dictionary
//- Parent dictionary //- Parent dictionary
const dictionary& parent_; const dictionary& parent_;
//- Wildcard entries //- Entries of matching patterns
DLList<entry*> wildCardEntries_; DLList<entry*> wildCardEntries_;
//- Wildcard precompiled regular expressions //- Patterns as precompiled regular expressions
DLList<autoPtr<regExp> > wildCardRegexps_; DLList<autoPtr<regExp> > wildCardRegexps_;
// Private Member Functions // Private Member Functions
//- Search wildcard table either for exact match or for regular //- Search patterns table for exact match or regular expression match
// expression match. bool findInPatterns
bool findInWildcards
( (
const bool wildCardMatch, const bool patternMatch,
const word& Keyword, const word& Keyword,
DLList<entry*>::const_iterator& wcLink, DLList<entry*>::const_iterator& wcLink,
DLList<autoPtr<regExp> >::const_iterator& reLink DLList<autoPtr<regExp> >::const_iterator& reLink
) const; ) const;
//- Search wildcard table either for exact match or for regular //- Search patterns table for exact match or regular expression match
// expression match. bool findInPatterns
bool findInWildcards
( (
const bool wildCardMatch, const bool patternMatch,
const word& Keyword, const word& Keyword,
DLList<entry*>::iterator& wcLink, DLList<entry*>::iterator& wcLink,
DLList<autoPtr<regExp> >::iterator& reLink DLList<autoPtr<regExp> >::iterator& reLink
@ -210,83 +208,88 @@ public:
// Search and lookup // Search and lookup
//- Search dictionary for given keyword //- Search dictionary for given keyword
// If recursive search parent dictionaries // If recursive, search parent dictionaries
bool found(const word&, bool recursive=false) const; bool found(const word&, bool recursive=false) const;
//- Find and return an entry data stream pointer if present //- Find and return an entry data stream pointer if present
// otherwise return NULL. // otherwise return NULL.
// If recursive search parent dictionaries. // If recursive, search parent dictionaries.
// If wildCardMatch use wildcards. // If patternMatch, use regular expressions
const entry* lookupEntryPtr const entry* lookupEntryPtr
( (
const word&, const word&,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) const; ) const;
//- Find and return an entry data stream pointer for manipulation //- Find and return an entry data stream pointer for manipulation
// if present otherwise return NULL. // if present otherwise return NULL.
// If recursive search parent dictionaries. // If recursive, search parent dictionaries.
// If wildCardMatch use wildcards. // If patternMatch, use regular expressions.
entry* lookupEntryPtr entry* lookupEntryPtr
( (
const word&, const word&,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
); );
//- Find and return an entry data stream if present otherwise error. //- Find and return an entry data stream if present otherwise error.
// If recursive search parent dictionaries. // If recursive, search parent dictionaries.
// If wildCardMatch use wildcards. // If patternMatch, use regular expressions.
const entry& lookupEntry const entry& lookupEntry
( (
const word&, const word&,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) const; ) const;
//- Find and return an entry data stream //- Find and return an entry data stream
// If recursive search parent dictionaries // If recursive, search parent dictionaries.
// If patternMatch, use regular expressions.
ITstream& lookup ITstream& lookup
( (
const word&, const word&,
bool recursive=false, bool recursive=false,
bool wildCardMatch=true bool patternMatch=true
) const; ) const;
//- Find and return a T, //- Find and return a T,
// if not found return the given default value // if not found return the given default value
// If recursive search parent dictionaries // If recursive, search parent dictionaries.
// If patternMatch, use regular expressions.
template<class T> template<class T>
T lookupOrDefault T lookupOrDefault
( (
const word&, const word&,
const T&, const T&,
bool recursive=false, bool recursive=false,
bool wildCardMatch=true bool patternMatch=true
) const; ) const;
//- Find and return a T, if not found return the given //- Find and return a T, if not found return the given
// default value, and add to dictionary. // default value, and add to dictionary.
// If recursive search parent dictionaries // If recursive, search parent dictionaries.
// If patternMatch, use regular expressions.
template<class T> template<class T>
T lookupOrAddDefault T lookupOrAddDefault
( (
const word&, const word&,
const T&, const T&,
bool recursive=false, bool recursive=false,
bool wildCardMatch=true bool patternMatch=true
); );
//- Find an entry if present, and assign to T //- Find an entry if present, and assign to T
// Returns true if the entry was found // Returns true if the entry was found.
// If recursive, search parent dictionaries.
// If patternMatch, use regular expressions.
template<class T> template<class T>
bool readIfPresent bool readIfPresent
( (
const word&, const word&,
T&, T&,
bool recursive=false, bool recursive=false,
bool wildCardMatch=true bool patternMatch=true
) const; ) const;
//- Check if entry is a sub-dictionary //- Check if entry is a sub-dictionary
@ -305,6 +308,9 @@ public:
//- Return the table of contents //- Return the table of contents
wordList toc() const; wordList toc() const;
//- Return the list of available keys or patterns
List<keyType> keys(bool patterns=false) const;
// Editing // Editing
//- Add a new entry //- Add a new entry
@ -393,7 +399,7 @@ public:
void operator=(const dictionary&); void operator=(const dictionary&);
//- Include entries from the given dictionary. //- Include entries from the given dictionary.
// Warn, but do not overwrite existing entries // Warn, but do not overwrite existing entries.
void operator+=(const dictionary&); void operator+=(const dictionary&);
//- Conditionally include entries from the given dictionary. //- Conditionally include entries from the given dictionary.
@ -417,13 +423,13 @@ public:
// Global Operators // Global Operators
//- Combine dictionaries starting from the entries in dict1 and then including //- Combine dictionaries.
// those from dict2. // Starting from the entries in dict1 and then including those from dict2.
// Warn, but do not overwrite the entries from dict1. // Warn, but do not overwrite the entries from dict1.
dictionary operator+(const dictionary& dict1, const dictionary& dict2); dictionary operator+(const dictionary& dict1, const dictionary& dict2);
//- Combine dictionaries starting from the entries in dict1 and then including //- Combine dictionaries.
// those from dict2. // Starting from the entries in dict1 and then including those from dict2.
// Do not overwrite the entries from dict1. // Do not overwrite the entries from dict1.
dictionary operator|(const dictionary& dict1, const dictionary& dict2); dictionary operator|(const dictionary& dict1, const dictionary& dict2);

View File

@ -35,18 +35,18 @@ T Foam::dictionary::lookupOrDefault
const word& keyword, const word& keyword,
const T& deflt, const T& deflt,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) const ) const
{ {
const entry* entryPtr = lookupEntryPtr(keyword, recursive, wildCardMatch); const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
if (entryPtr == NULL) if (entryPtr)
{ {
return deflt; return pTraits<T>(entryPtr->stream());
} }
else else
{ {
return pTraits<T>(entryPtr->stream()); return deflt;
} }
} }
@ -57,19 +57,19 @@ T Foam::dictionary::lookupOrAddDefault
const word& keyword, const word& keyword,
const T& deflt, const T& deflt,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) )
{ {
const entry* entryPtr = lookupEntryPtr(keyword, recursive, wildCardMatch); const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
if (entryPtr == NULL) if (entryPtr)
{ {
add(new primitiveEntry(keyword, deflt)); return pTraits<T>(entryPtr->stream());
return deflt;
} }
else else
{ {
return pTraits<T>(entryPtr->stream()); add(new primitiveEntry(keyword, deflt));
return deflt;
} }
} }
@ -80,20 +80,20 @@ bool Foam::dictionary::readIfPresent
const word& k, const word& k,
T& val, T& val,
bool recursive, bool recursive,
bool wildCardMatch bool patternMatch
) const ) const
{ {
const entry* entryPtr = lookupEntryPtr(k, recursive, wildCardMatch); const entry* entryPtr = lookupEntryPtr(k, recursive, patternMatch);
if (entryPtr == NULL) if (entryPtr)
{
return false;
}
else
{ {
entryPtr->stream() >> val; entryPtr->stream() >> val;
return true; return true;
} }
else
{
return false;
}
} }

View File

@ -28,8 +28,8 @@ Class
Description Description
A class for handling keywords in dictionaries. A class for handling keywords in dictionaries.
A keyType is the keyword of a dictionary. It differs from word in that A keyType is the keyword of a dictionary.
it accepts wildcards. It differs from word in that it accepts patterns (regular expressions).
SourceFiles SourceFiles
keyType.C keyType.C
@ -53,7 +53,7 @@ class Ostream;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class keyType Declaration Class keyType Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
class keyType class keyType
@ -78,22 +78,22 @@ public:
inline keyType(); inline keyType();
//- Construct as copy //- Construct as copy
inline keyType(const keyType& s); inline keyType(const keyType&);
//- Construct as copy of word //- Construct as copy of word
inline keyType(const word& s); inline keyType(const word&);
//- Construct as copy of string. Expect it to be regular expression. //- Construct as copy of string. Expect it to be regular expression.
inline keyType(const string& s); inline keyType(const string&);
//- Construct as copy of character array //- Construct as copy of character array
inline keyType(const char* s); inline keyType(const char*);
//- Construct as copy of std::string //- Construct as copy of std::string
inline keyType(const std::string& s, const bool isWildCard); inline keyType(const std::string&, const bool isPattern);
//- Construct from Istream //- Construct from Istream
keyType(Istream& is); keyType(Istream&);
// Member functions // Member functions
@ -101,29 +101,25 @@ public:
//- Is this character valid for a keyType //- Is this character valid for a keyType
inline static bool valid(char c); inline static bool valid(char c);
//- Is the type a wildcard? //- Should be treated as a match rather than a literal string
inline bool isWildCard() const; inline bool isPattern() const;
// Member operators // Member operators
// Assignment // Assignment
inline void operator=(const keyType& s); inline void operator=(const keyType&);
inline void operator=(const word&);
//- Assign from regular expression. //- Assign from regular expression.
inline void operator=(const string& s); inline void operator=(const string&);
inline void operator=(const word& s);
inline void operator=(const char*); inline void operator=(const char*);
// IOstream operators // IOstream operators
friend Istream& operator>>(Istream& is, keyType& w); friend Istream& operator>>(Istream&, keyType&);
friend Ostream& operator<<(Ostream&, const keyType&);
friend Ostream& operator<<(Ostream& os, const keyType& w);
}; };

View File

@ -30,7 +30,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
//- Construct null
inline Foam::keyType::keyType() inline Foam::keyType::keyType()
: :
word(), word(),
@ -38,15 +37,13 @@ inline Foam::keyType::keyType()
{} {}
//- Construct as copy
inline Foam::keyType::keyType(const keyType& s) inline Foam::keyType::keyType(const keyType& s)
: :
word(s, false), word(s, false),
isWildCard_(s.isWildCard()) isWildCard_(s.isPattern())
{} {}
//- Construct as copy of word
inline Foam::keyType::keyType(const word& s) inline Foam::keyType::keyType(const word& s)
: :
word(s, false), word(s, false),
@ -54,7 +51,7 @@ inline Foam::keyType::keyType(const word& s)
{} {}
//- Construct as copy of string. Expect it to be regular expression // Construct as copy of string. Expect it to be regular expression
inline Foam::keyType::keyType(const string& s) inline Foam::keyType::keyType(const string& s)
: :
word(s, false), word(s, false),
@ -62,7 +59,7 @@ inline Foam::keyType::keyType(const string& s)
{} {}
//- Construct as copy of character array // Construct as copy of character array
inline Foam::keyType::keyType(const char* s) inline Foam::keyType::keyType(const char* s)
: :
word(s, false), word(s, false),
@ -74,11 +71,11 @@ inline Foam::keyType::keyType(const char* s)
inline Foam::keyType::keyType inline Foam::keyType::keyType
( (
const std::string& s, const std::string& s,
const bool isWildCard const bool isPattern
) )
: :
word(s, false), word(s, false),
isWildCard_(isWildCard) isWildCard_(isPattern)
{} {}
@ -90,7 +87,7 @@ inline bool Foam::keyType::valid(char c)
} }
bool Foam::keyType::isWildCard() const bool Foam::keyType::isPattern() const
{ {
return isWildCard_; return isWildCard_;
} }
@ -102,7 +99,7 @@ inline void Foam::keyType::operator=(const keyType& s)
{ {
// Bypass checking // Bypass checking
string::operator=(s); string::operator=(s);
isWildCard_ = s.isWildCard(); isWildCard_ = s.isPattern();
} }

View File

@ -87,7 +87,7 @@ public:
inline word(const word&); inline word(const word&);
//- Construct as copy of character array //- Construct as copy of character array
inline word(const char*, const bool doStripInvalid = true); inline word(const char*, const bool doStripInvalid=true);
//- Construct as copy with a maximum number of characters //- Construct as copy with a maximum number of characters
inline word inline word
@ -98,10 +98,10 @@ public:
); );
//- Construct as copy of string //- Construct as copy of string
inline word(const string&, const bool doStripInvalid = true); inline word(const string&, const bool doStripInvalid=true);
//- Construct as copy of std::string //- Construct as copy of std::string
inline word(const std::string&, const bool doStripInvalid = true); inline word(const std::string&, const bool doStripInvalid=true);
//- Construct from Istream //- Construct from Istream
word(Istream&); word(Istream&);