Commit 7473adc8 authored by Mark Olesen's avatar Mark Olesen
Browse files

writeKeyword spacing for keyType, misc cosmetics changes

parent 599c1c59
......@@ -41,34 +41,44 @@ using namespace Foam;
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);
{
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<< "Wildcard find \"abc\" in top directory : "
Info<< "Pattern find \"abc\" in top directory : "
<< dict.lookup("abc") << endl;
Info<< "Wildcard find \"abc\" in sub directory : "
Info<< "Pattern find \"abc\" in sub directory : "
<< dict.subDict("someDict").lookup("abc")
<< endl;
Info<< "Recursive wildcard find \"def\" in sub directory : "
Info<< "Recursive pattern find \"def\" in sub directory : "
<< dict.subDict("someDict").lookup("def", true)
<< endl;
Info<< "Recursive wildcard find \"foo\" in sub directory : "
Info<< "Recursive pattern find \"foo\" in sub directory : "
<< dict.subDict("someDict").lookup("foo", true)
<< endl;
Info<< "Recursive wildcard find \"fooz\" in sub directory : "
Info<< "Recursive pattern find \"fooz\" in sub directory : "
<< dict.subDict("someDict").lookup("fooz", true)
<< endl;
Info<< "Recursive wildcard find \"bar\" in sub directory : "
Info<< "Recursive pattern find \"bar\" in sub directory : "
<< dict.subDict("someDict").lookup("bar", true)
<< endl;
Info<< "Recursive wildcard find \"xxx\" in sub directory : "
Info<< "Recursive pattern find \"xxx\" in sub directory : "
<< dict.subDict("someDict").lookup("xxx", true)
<< endl;
}
......
......@@ -18,8 +18,10 @@ FoamFile
".*" parentValue1;
"[n-z].*" parentValue2;
"f.*" parentValue3;
keyX parentValue4;
keyY parentValue5;
someDict
"(.*)Dict"
{
foo subdictValue0;
bar $f.*; // should this really match 'foo'?
......@@ -28,7 +30,7 @@ someDict
"a.*c" subdictValue3;
"ab.*" subdictValue2;
"a.*" subdictValue1;
abcd subdictValue4;
abcd \1;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -32,22 +32,22 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
//- Write keyType
Foam::Ostream& Foam::Ostream::write(const keyType& s)
// Write keyType
Foam::Ostream& Foam::Ostream::write(const keyType& kw)
{
// Write as word?
if (s.isWildCard())
// Write as word or string
if (kw.isPattern())
{
return write(static_cast<const string&>(s));
return write(static_cast<const string&>(kw));
}
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()
{
if (indentLevel_ == 0)
......@@ -62,15 +62,26 @@ void Foam::Ostream::decrIndent()
}
// Write the keyword to the Ostream followed by appropriate indentation
Foam::Ostream& Foam::Ostream::writeKeyword(const Foam::keyType& keyword)
// Write the keyword followed by appropriate indentation
Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
{
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));
}
......
......@@ -60,12 +60,11 @@ protected:
//- Number of spaces per indent level
static const unsigned short indentSize_ = 4;
//- Current indent level
unsigned short indentLevel_;
//- Indentation of the entry from the start of the keyword
static const unsigned short entryIndentation_ = 16;
//- Current indent level
unsigned short indentLevel_;
public:
......@@ -148,9 +147,8 @@ public:
//- Decrememt the indent level
void decrIndent();
//- Write the keyword to the Ostream followed by
// appropriate indentation
Ostream& writeKeyword(const keyType& keyword);
//- Write the keyword followed by an appropriate indentation
Ostream& writeKeyword(const keyType&);
// Stream state functions
......
......@@ -38,9 +38,9 @@ const Foam::dictionary Foam::dictionary::null;
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::dictionary::findInWildcards
bool Foam::dictionary::findInPatterns
(
const bool wildCardMatch,
const bool patternMatch,
const word& Keyword,
DLList<entry*>::const_iterator& wcLink,
DLList<autoPtr<regExp> >::const_iterator& reLink
......@@ -50,11 +50,11 @@ bool Foam::dictionary::findInWildcards
{
while (wcLink != wildCardEntries_.end())
{
if (!wildCardMatch && wcLink()->keyword() == Keyword)
{
return true;
}
else if (wildCardMatch && reLink()->match(Keyword))
if
(
patternMatch ? reLink()->match(Keyword)
: wcLink()->keyword() == Keyword
)
{
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,
DLList<entry*>::iterator& wcLink,
DLList<autoPtr<regExp> >::iterator& reLink
......@@ -80,11 +80,11 @@ bool Foam::dictionary::findInWildcards
{
while (wcLink != wildCardEntries_.end())
{
if (!wildCardMatch && wcLink()->keyword() == Keyword)
{
return true;
}
else if (wildCardMatch && reLink()->match(Keyword))
if
(
patternMatch ? reLink()->match(Keyword)
: wcLink()->keyword() == Keyword
)
{
return true;
}
......@@ -125,7 +125,7 @@ Foam::dictionary::dictionary
{
hashedEntries_.insert(iter().keyword(), &iter());
if (iter().keyword().isWildCard())
if (iter().keyword().isPattern())
{
wildCardEntries_.insert(&iter());
wildCardRegexps_.insert
......@@ -155,7 +155,7 @@ Foam::dictionary::dictionary
{
hashedEntries_.insert(iter().keyword(), &iter());
if (iter().keyword().isWildCard())
if (iter().keyword().isPattern())
{
wildCardEntries_.insert(&iter());
wildCardRegexps_.insert
......@@ -223,8 +223,8 @@ bool Foam::dictionary::found(const word& keyword, bool recursive) const
DLList<autoPtr<regExp> >::const_iterator reLink =
wildCardRegexps_.begin();
// Find in wildcards using regular expressions only
if (findInWildcards(true, keyword, wcLink, reLink))
// Find in patterns using regular expressions only
if (findInPatterns(true, keyword, wcLink, reLink))
{
return true;
}
......@@ -246,22 +246,22 @@ const Foam::entry* Foam::dictionary::lookupEntryPtr
(
const word& keyword,
bool recursive,
bool wildCardMatch
bool patternMatch
) const
{
HashTable<entry*>::const_iterator iter = hashedEntries_.find(keyword);
if (iter == hashedEntries_.end())
{
if (wildCardMatch && wildCardEntries_.size() > 0)
if (patternMatch && wildCardEntries_.size() > 0)
{
DLList<entry*>::const_iterator wcLink =
wildCardEntries_.begin();
DLList<autoPtr<regExp> >::const_iterator reLink =
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();
}
......@@ -269,7 +269,7 @@ const Foam::entry* Foam::dictionary::lookupEntryPtr
if (recursive && &parent_ != &dictionary::null)
{
return parent_.lookupEntryPtr(keyword, recursive, wildCardMatch);
return parent_.lookupEntryPtr(keyword, recursive, patternMatch);
}
else
{
......@@ -285,21 +285,22 @@ Foam::entry* Foam::dictionary::lookupEntryPtr
(
const word& keyword,
bool recursive,
bool wildCardMatch
bool patternMatch
)
{
HashTable<entry*>::iterator iter = hashedEntries_.find(keyword);
if (iter == hashedEntries_.end())
{
if (wildCardMatch && wildCardEntries_.size() > 0)
if (patternMatch && wildCardEntries_.size() > 0)
{
DLList<entry*>::iterator wcLink =
wildCardEntries_.begin();
DLList<autoPtr<regExp> >::iterator reLink =
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();
}
......@@ -311,7 +312,7 @@ Foam::entry* Foam::dictionary::lookupEntryPtr
(
keyword,
recursive,
wildCardMatch
patternMatch
);
}
else
......@@ -328,10 +329,10 @@ const Foam::entry& Foam::dictionary::lookupEntry
(
const word& keyword,
bool recursive,
bool wildCardMatch
bool patternMatch
) const
{
const entry* entryPtr = lookupEntryPtr(keyword, recursive, wildCardMatch);
const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
if (entryPtr == NULL)
{
......@@ -352,16 +353,16 @@ Foam::ITstream& Foam::dictionary::lookup
(
const word& keyword,
bool recursive,
bool wildCardMatch
bool patternMatch
) const
{
return lookupEntry(keyword, recursive, wildCardMatch).stream();
return lookupEntry(keyword, recursive, patternMatch).stream();
}
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);
if (entryPtr)
......@@ -430,7 +431,26 @@ Foam::wordList Foam::dictionary::toc() const
{
wordList keys(size());
label i = 0;
label nKeys = 0;
for
(
IDLList<entry>::const_iterator iter = begin();
iter != end();
++iter
)
{
keys[nKeys++] = iter().keyword();
}
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();
......@@ -438,8 +458,12 @@ Foam::wordList Foam::dictionary::toc() const
++iter
)
{
keys[i++] = iter().keyword();
if (iter().keyword().isPattern() ? patterns : !patterns)
{
keys[nKeys++] = iter().keyword();
}
}
keys.setSize(nKeys);
return keys;
}
......@@ -473,7 +497,7 @@ bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
{
entryPtr->name() = name_ + "::" + entryPtr->keyword();
if (entryPtr->keyword().isWildCard())
if (entryPtr->keyword().isPattern())
{
wildCardEntries_.insert(entryPtr);
wildCardRegexps_.insert
......@@ -502,7 +526,7 @@ bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
entryPtr->name() = name_ + "::" + entryPtr->keyword();
IDLList<entry>::append(entryPtr);
if (entryPtr->keyword().isWildCard())
if (entryPtr->keyword().isPattern())
{
wildCardEntries_.insert(entryPtr);
wildCardRegexps_.insert
......@@ -597,13 +621,12 @@ bool Foam::dictionary::remove(const word& Keyword)
if (iter != hashedEntries_.end())
{
// Delete from wildcards first
DLList<entry*>::iterator wcLink =
wildCardEntries_.begin();
// Delete from patterns first
DLList<entry*>::iterator wcLink = wildCardEntries_.begin();
DLList<autoPtr<regExp> >::iterator reLink = wildCardRegexps_.begin();
// Find in wildcards using exact match only
if (findInWildcards(false, Keyword, wcLink, reLink))
// Find in pattern using exact match only
if (findInPatterns(false, Keyword, wcLink, reLink))
{
wildCardEntries_.remove(wcLink);
wildCardRegexps_.remove(reLink);
......@@ -643,14 +666,14 @@ bool Foam::dictionary::changeKeyword
return false;
}
if (iter()->keyword().isWildCard())
if (iter()->keyword().isPattern())
{
FatalErrorIn
(
"dictionary::changeKeyword(const word&, const word&, bool)"
) << "Old keyword "<< oldKeyword
<< " is a wildcard."
<< "Wildcard replacement not yet implemented."
<< " is a pattern."
<< "Pattern replacement not yet implemented."
<< exit(FatalError);
}
......@@ -662,16 +685,16 @@ bool Foam::dictionary::changeKeyword
{
if (forceOverwrite)
{
if (iter2()->keyword().isWildCard())
if (iter2()->keyword().isPattern())
{
// Delete from wildcards first
// Delete from patterns first
DLList<entry*>::iterator wcLink =
wildCardEntries_.begin();
DLList<autoPtr<regExp> >::iterator reLink =
wildCardRegexps_.begin();
// Find in wildcards using exact match only
if (findInWildcards(false, iter2()->keyword(), wcLink, reLink))
// Find in patterns using exact match only
if (findInPatterns(false, iter2()->keyword(), wcLink, reLink))
{
wildCardEntries_.remove(wcLink);
wildCardRegexps_.remove(reLink);
......@@ -701,7 +724,7 @@ bool Foam::dictionary::changeKeyword
hashedEntries_.erase(oldKeyword);
hashedEntries_.insert(newKeyword, iter());
if (newKeyword.isWildCard())
if (newKeyword.isPattern())
{
wildCardEntries_.insert(iter());
wildCardRegexps_.insert
......
......@@ -27,12 +27,12 @@ Class
Description
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
searching is
searching is as follows:
- exact match
- wildcard match (in reverse order)
- optional recursion into subdictionaries
- pattern match (in reverse order)
- optional recursion into the enclosing (parent) dictionaries
The dictionary class is the base class for IOdictionary.
It also serves as a bootstrap dictionary for the objectRegistry data
......@@ -92,29 +92,27 @@ class dictionary
//- Parent dictionary
const dictionary& parent_;
//- Wildcard entries
//- Entries of matching patterns
DLList<entry*> wildCardEntries_;
//- Wildcard precompiled regular expressions
//- Patterns as precompiled regular expressions
DLList<autoPtr<regExp> > wildCardRegexps_;
// Private Member Functions
//- Search wildcard table either for exact match or for regular
// expression match.
bool findInWildcards
//- Search patterns table for exact match or regular expression match
bool findInPatterns
(
const bool wildCardMatch,
const bool patternMatch,
const word& Keyword,
DLList<entry*>::const_iterator& wcLink,
DLList<autoPtr<regExp> >::const_iterator& reLink
) const;
//- Search wildcard table either for exact match or for regular
// expression match.
bool findInWildcards
//- Search patterns table for exact match or regular expression match
bool findInPatterns
(
const bool wildCardMatch,
const bool patternMatch,
const word& Keyword,
DLList<entry*>::iterator& wcLink,
DLList<autoPtr<regExp> >::iterator& reLink
......@@ -210,83 +208,88 @@ public:
// Search and lookup
//- Search dictionary for given keyword
// If recursive search parent dictionaries
// If recursive, search parent dictionaries
bool found(const word&, bool recursive=false) const;
//- Find and return an entry data stream pointer if present
// otherwise return NULL.
// If recursive search parent dictionaries.
// If wildCardMatch use wildcards.
// If recursive, search parent dictionaries.
// If patternMatch, use regular expressions
const entry* lookupEntryPtr
(
const word&,
bool recursive,
bool wildCardMatch
bool patternMatch
) const;
//- Find and return an entry data stream pointer for manipulation
// if present otherwise return NULL.
// If recursive search parent dictionaries.
// If wildCardMatch use wildcards.
// If recursive, search parent dictionaries.
// If patternMatch, use regular expressions.
entry* lookupEntryPtr
(
const word&,
bool recursive,
bool wildCardMatch
bool patternMatch
);
//- Find and return an entry data stream if present otherwise error.
// If recursive search parent dictionaries.
// If wildCardMatch use wildcards.
// If recursive, search parent dictionaries.
// If patternMatch, use regular expressions.
const entry& lookupEntry
(
const word&,
bool recursive,
bool wildCardMatch
bool patternMatch
) const;
//- Find and return an entry data stream
// If recursive search parent dictionaries
// If recursive, search parent dictionaries.
// If patternMatch, use regular expressions.
ITstream& lookup
(
const word&,
bool recursive=false,
bool wildCardMatch=true
bool patternMatch=true
) const;
//- Find and return a T,
// 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>
T lookupOrDefault
(
const word&,
const T&,
bool recursive=false,
bool wildCardMatch=true
bool patternMatch=true
) const;
//- Find and return a T, if not found return the given
// default value, and add to dictionary.
// If recursive search parent dictionaries
// If recursive, search parent dictionaries.
// If patternMatch, use regular expressions.
template<class T>
T lookupOrAddDefault
(
const word&,
const T&,
bool recursive=false,