adjusted dictionary #inputMode directive

- #inputMode error
  now issues a FatalError on duplicate entries

- #inputMode warn
  issues a warning on duplicate entries, corresponds to the
  old behaviour of 'error'

- #inputMode protect
  prevents overwriting existing entries

The 'protect' mode provides a simple mechanism for supplying default values.
eg,
    in file1:
        #inputMode  protect
        intensity       0.1;
        mixingLength    0.005;
        #inputMode  merge

        inlet
        {
            type        turbulentIntensityKineticEnergyInlet;
            intensity   $intensity;
        }

    which is included from file2:

        intensity   0.05;
        #include    "file1"
This commit is contained in:
Mark Olesen
2009-05-05 13:18:29 +02:00
parent 0be0e96fb9
commit 6e10b0defd
10 changed files with 120 additions and 57 deletions

View File

@ -1,23 +1,28 @@
/*-------------------------------*- C++ -*---------------------------------*\ /*--------------------------------*- C++ -*----------------------------------*\
| ========= | | ========= | |
| \\ / OpenFOAM | | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / | | \\ / O peration | Version: Any |
| \\ / The Open Source CFD Toolbox | | \\ / A nd | Web: www.OpenFOAM.org |
| \\/ http://www.OpenFOAM.org | | \\/ M anipulation | |
\*-------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
FoamFile FoamFile
{ {
version 2.0; version 2.0;
format ascii; format ascii;
class dictionary; class dictionary;
object testDict; object testDict;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#inputMode merge #inputMode merge
dimensions [ 0 2 -2 0 0 0 0 ];
internalField uniform 1; internalField uniform 1;
// use 'protect' to supply defaults
#inputMode protect
internalField uniform 10;
dimensions [ 0 2 -2 0 0 0 0 ];
#inputMode merge
active active
{ {
type turbulentIntensityKineticEnergyInlet; type turbulentIntensityKineticEnergyInlet;
@ -31,6 +36,7 @@ inactive
type zeroGradient; type zeroGradient;
} }
boundaryField boundaryField
{ {
Default_Boundary_Region Default_Boundary_Region
@ -101,4 +107,4 @@ baz
// this should work too // this should work too
#remove ( bar baz ) #remove ( bar baz )
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -1,19 +1,18 @@
/*-------------------------------*- C++ -*---------------------------------*\ /*--------------------------------*- C++ -*----------------------------------*\
| ========= | | ========= | |
| \\ / OpenFOAM | | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / | | \\ / O peration | Version: Any |
| \\ / The Open Source CFD Toolbox | | \\ / A nd | Web: www.OpenFOAM.org |
| \\/ http://www.OpenFOAM.org | | \\/ M anipulation | |
\*-------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
FoamFile FoamFile
{ {
version 2.0; version 2.0;
format ascii; format ascii;
class dictionary; class dictionary;
object testDict; object testDict;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
boundaryField boundaryField
{ {
@ -27,4 +26,4 @@ boundaryField
} }
#inputMode overwrite #inputMode overwrite
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -53,7 +53,8 @@ bool Foam::dictionary::findInPatterns
{ {
if if
( (
patternMatch ? reLink()->match(Keyword) patternMatch
? reLink()->match(Keyword)
: wcLink()->keyword() == Keyword : wcLink()->keyword() == Keyword
) )
{ {
@ -83,7 +84,8 @@ bool Foam::dictionary::findInPatterns
{ {
if if
( (
patternMatch ? reLink()->match(Keyword) patternMatch
? reLink()->match(Keyword)
: wcLink()->keyword() == Keyword : wcLink()->keyword() == Keyword
) )
{ {

View File

@ -73,7 +73,7 @@ bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
{ {
cerr<< "--> FOAM Warning : " << std::endl cerr<< "--> FOAM Warning : " << std::endl
<< " From function " << " From function "
<< "entry::getKeyword(keyType& keyword, Istream& is)" << std::endl << "entry::getKeyword(keyType&, Istream&)" << std::endl
<< " in file " << __FILE__ << " in file " << __FILE__
<< " at line " << __LINE__ << std::endl << " at line " << __LINE__ << std::endl
<< " Reading " << is.name().c_str() << std::endl << " Reading " << is.name().c_str() << std::endl
@ -88,7 +88,7 @@ bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
bool Foam::entry::New(dictionary& parentDict, Istream& is) bool Foam::entry::New(dictionary& parentDict, Istream& is)
{ {
is.fatalCheck("entry::New(const dictionary& parentDict, Istream& is)"); is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)");
keyType keyword; keyType keyword;
@ -97,9 +97,9 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
{ {
return false; return false;
} }
else // Keyword starts entry ... else // Keyword starts entry ...
{ {
if (keyword[0] == '#') // ... Function entry if (keyword[0] == '#') // ... Function entry
{ {
word functionName = keyword(1, keyword.size()-1); word functionName = keyword(1, keyword.size()-1);
return functionEntry::execute(functionName, parentDict, is); return functionEntry::execute(functionName, parentDict, is);
@ -128,9 +128,14 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
false, false,
false false
); );
if (existingPtr) if (existingPtr)
{ {
if (functionEntries::inputModeEntry::overwrite()) if (functionEntries::inputModeEntry::merge())
{
mergeEntry = true;
}
else if (functionEntries::inputModeEntry::overwrite())
{ {
// clear dictionary so merge acts like overwrite // clear dictionary so merge acts like overwrite
if (existingPtr->isDict()) if (existingPtr->isDict())
@ -139,9 +144,30 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
} }
mergeEntry = true; mergeEntry = true;
} }
else if (functionEntries::inputModeEntry::merge()) else if (functionEntries::inputModeEntry::protect())
{ {
mergeEntry = true; // read and discard the entry
if (nextToken == token::BEGIN_BLOCK)
{
dictionaryEntry dummy(keyword, parentDict, is);
}
else
{
primitiveEntry dummy(keyword, parentDict, is);
}
return true;
}
else if (functionEntries::inputModeEntry::error())
{
FatalIOErrorIn
(
"entry::New(const dictionary& parentDict, Istream&)",
is
)
<< "ERROR! duplicate entry: " << keyword
<< exit(FatalIOError);
return false;
} }
} }
@ -168,7 +194,7 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is) Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is)
{ {
is.fatalCheck("entry::New(Istream& is)"); is.fatalCheck("entry::New(Istream&)");
keyType keyword; keyType keyword;

View File

@ -58,7 +58,7 @@ bool Foam::functionEntry::execute
is.fatalCheck is.fatalCheck
( (
"functionEntry::execute" "functionEntry::execute"
"(const word& functionName, dictionary& parentDict, Istream& is)" "(const word& functionName, dictionary& parentDict, Istream&)"
); );
if (!executedictionaryIstreamMemberFunctionTablePtr_) if (!executedictionaryIstreamMemberFunctionTablePtr_)

View File

@ -131,7 +131,7 @@ bool Foam::functionEntries::includeEntry::execute
FatalIOErrorIn FatalIOErrorIn
( (
"functionEntries::includeEntry::includeEntry" "functionEntries::includeEntry::includeEntry"
"(dictionary& parentDict, primitiveEntry& entry, Istream&)", "(dictionary& parentDict, primitiveEntry&, Istream&)",
is is
) << "Cannot open include file " << ifs.name() ) << "Cannot open include file " << ifs.name()
<< " while reading dictionary " << parentDict.name() << " while reading dictionary " << parentDict.name()

View File

@ -34,8 +34,8 @@ Description
#include "includeFile" #include "includeFile"
@endverbatim @endverbatim
The usual expansion of environment variables and other constructs (eg, The usual expansion of environment variables and other constructs
the @c ~OpenFOAM/ expansion) is retained. (eg, the @c ~OpenFOAM/ expansion) is retained.
See Also See Also
fileName, string::expand() fileName, string::expand()

View File

@ -72,6 +72,14 @@ void Foam::functionEntries::inputModeEntry::setMode(Istream& is)
{ {
mode_ = OVERWRITE; mode_ = OVERWRITE;
} }
else if (mode == "protect")
{
mode_ = PROTECT;
}
else if (mode == "warn")
{
mode_ = WARN;
}
else if (mode == "error") else if (mode == "error")
{ {
mode_ = ERROR; mode_ = ERROR;
@ -79,7 +87,8 @@ void Foam::functionEntries::inputModeEntry::setMode(Istream& is)
else else
{ {
WarningIn("Foam::functionEntries::inputModeEntry::setMode(Istream&)") WarningIn("Foam::functionEntries::inputModeEntry::setMode(Istream&)")
<< "unsupported input mode " << mode << "unsupported input mode '" << mode
<< "' ... defaulting to 'merge'"
<< endl; << endl;
} }
} }
@ -116,4 +125,15 @@ bool Foam::functionEntries::inputModeEntry::overwrite()
} }
bool Foam::functionEntries::inputModeEntry::protect()
{
return mode_ == PROTECT;
}
bool Foam::functionEntries::inputModeEntry::error()
{
return mode_ == ERROR;
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -37,8 +37,10 @@ Description
The possible input modes: The possible input modes:
@param merge merge sub-dictionaries when possible @param merge merge sub-dictionaries when possible
@param overwrite keep last entry and silently remove previous ones @param overwrite keep last entry and silently remove previous ones
@param error flag duplicate entry as an error @param protect keep initial entry and silently ignore subsequent ones
@param default currently the same as merge @param warn keep initial entry and warn about subsequent ones
@param error issue a FatalError for duplicate entries
@param default currently identical to merge
SourceFiles SourceFiles
inputModeEntry.C inputModeEntry.C
@ -65,15 +67,17 @@ class inputModeEntry
: :
public functionEntry public functionEntry
{ {
//- input mode options //- The input mode options
enum inputMode enum inputMode
{ {
ERROR,
MERGE, MERGE,
OVERWRITE OVERWRITE,
PROTECT,
WARN,
ERROR
}; };
//- current input mode //- The current input mode
static inputMode mode_; static inputMode mode_;
@ -100,7 +104,7 @@ public:
//- Execute the functionEntry in a sub-dict context //- Execute the functionEntry in a sub-dict context
static bool execute(dictionary& parentDict, Istream&); static bool execute(dictionary& parentDict, Istream&);
//- Reset the inputMode to %default //- Reset the inputMode to %default (ie, %merge)
static void clear(); static void clear();
//- Return true if the inputMode is %merge //- Return true if the inputMode is %merge
@ -109,6 +113,12 @@ public:
//- Return true if the inputMode is %overwrite //- Return true if the inputMode is %overwrite
static bool overwrite(); static bool overwrite();
//- Return true if the inputMode is %protect
static bool protect();
//- Return true if the inputMode is %error
static bool error();
}; };

View File

@ -83,7 +83,7 @@ bool Foam::primitiveEntry::expandVariable
// lookup the variable name in the given dictionary.... // lookup the variable name in the given dictionary....
const entry* ePtr = dict.lookupEntryPtr(varName, true, true); const entry* ePtr = dict.lookupEntryPtr(varName, true, true);
// ...if defined insert its tokens into this // ...if defined insert its tokens into this
if (ePtr != NULL) if (ePtr != NULL)
{ {
append(ePtr->stream()); append(ePtr->stream());
@ -91,7 +91,7 @@ bool Foam::primitiveEntry::expandVariable
} }
else else
{ {
// if not in the dictionary see if it is an environment // if not in the dictionary see if it is an environment
// variable // variable
string enVarString = getEnv(varName); string enVarString = getEnv(varName);
@ -123,7 +123,7 @@ bool Foam::primitiveEntry::read(const dictionary& dict, Istream& is)
{ {
is.fatalCheck is.fatalCheck
( (
"primitiveEntry::readData(const dictionary& dict, Istream& is)" "primitiveEntry::readData(const dictionary&, Istream&)"
); );
label blockCount = 0; label blockCount = 0;
@ -177,7 +177,7 @@ bool Foam::primitiveEntry::read(const dictionary& dict, Istream& is)
is.fatalCheck is.fatalCheck
( (
"primitiveEntry::readData(const dictionary& dict, Istream& is)" "primitiveEntry::readData(const dictionary&, Istream&)"
); );
if (currToken.good()) if (currToken.good())
@ -205,7 +205,7 @@ void Foam::primitiveEntry::readEntry(const dictionary& dict, Istream& is)
{ {
FatalIOErrorIn FatalIOErrorIn
( (
"primitiveEntry::readEntry(const dictionary& dict,Istream& is)", "primitiveEntry::readEntry(const dictionary&, Istream&)",
is is
) << "ill defined primitiveEntry starting at keyword '" ) << "ill defined primitiveEntry starting at keyword '"
<< keyword() << '\'' << keyword() << '\''
@ -266,7 +266,7 @@ void Foam::primitiveEntry::write(Ostream& os) const
os << token::SPACE; os << token::SPACE;
} }
} }
os << token::END_STATEMENT << endl; os << token::END_STATEMENT << endl;
} }
@ -297,7 +297,7 @@ Foam::Ostream& Foam::operator<<
{ {
os << " ..."; os << " ...";
} }
os << endl; os << endl;
return os; return os;