ENH: #codeStream: preserve across preprocessing

- new token: token::VERBATIMSTRING
- writing of this type in primitiveEntry
- disabling of all functionEntries in entry
This commit is contained in:
mattijs
2011-02-22 15:29:57 +00:00
parent 869d6fa460
commit 79939b9e18
12 changed files with 121 additions and 20 deletions

View File

@ -60,6 +60,8 @@ Usage
\param -literalRE \n \param -literalRE \n
Do not interpret regular expressions; treat them as any other keyword. Do not interpret regular expressions; treat them as any other keyword.
\param -enableFunctionEntries \n
By default all dictionary preprocessing of fields is disabled
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -255,6 +257,11 @@ int main(int argc, char *argv[])
"literalRE", "literalRE",
"treat regular expressions literally (ie, as a keyword)" "treat regular expressions literally (ie, as a keyword)"
); );
argList::addBoolOption
(
"enableFunctionEntries",
"enable expansion of dictionary directives - #include, #codeStream etc"
);
#include "addRegionOption.H" #include "addRegionOption.H"
#include "setRootCase.H" #include "setRootCase.H"
@ -271,6 +278,20 @@ int main(int argc, char *argv[])
<< " not present." << endl; << " not present." << endl;
} }
const bool enableEntries = args.optionFound("enableFunctionEntries");
if (enableEntries)
{
Info<< "Allowing dictionary preprocessing ('#include', '#codeStream')."
<< endl;
}
int oldFlag = entry::disableFunctionEntries;
if (!enableEntries)
{
// By default disable dictionary expansion for fields
entry::disableFunctionEntries = 1;
}
fileName regionPrefix = ""; fileName regionPrefix = "";
if (regionName != fvMesh::defaultRegion) if (regionName != fvMesh::defaultRegion)
@ -410,6 +431,7 @@ int main(int argc, char *argv[])
false false
) )
); );
const_cast<word&>(IOdictionary::typeName) = oldTypeName; const_cast<word&>(IOdictionary::typeName) = oldTypeName;
// Fake type back to what was in field // Fake type back to what was in field
const_cast<word&>(fieldDict.type()) = fieldDict.headerClassName(); const_cast<word&>(fieldDict.type()) = fieldDict.headerClassName();
@ -429,6 +451,8 @@ int main(int argc, char *argv[])
} }
} }
entry::disableFunctionEntries = oldFlag;
Info<< endl; Info<< endl;
Info<< "End\n" << endl; Info<< "End\n" << endl;

View File

@ -34,6 +34,9 @@ Usage
\param -test \n \param -test \n
Suppress writing the updated files with split cyclics Suppress writing the updated files with split cyclics
\param -enableFunctionEntries \n
By default all dictionary preprocessing of fields is disabled
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "argList.H" #include "argList.H"
@ -391,7 +394,12 @@ int main(int argc, char *argv[])
{ {
timeSelector::addOptions(); timeSelector::addOptions();
argList::addBoolOption("test"); argList::addBoolOption("test", "test only; do not change any files");
argList::addBoolOption
(
"enableFunctionEntries",
"enable expansion of dictionary directives - #include, #codeStream etc"
);
# include "addRegionOption.H" # include "addRegionOption.H"
# include "setRootCase.H" # include "setRootCase.H"
@ -404,6 +412,7 @@ int main(int argc, char *argv[])
{ {
Info<< "-test option: no changes made" << nl << endl; Info<< "-test option: no changes made" << nl << endl;
} }
const bool enableEntries = args.optionFound("enableFunctionEntries");
Foam::word regionName = polyMesh::defaultRegion; Foam::word regionName = polyMesh::defaultRegion;
@ -482,6 +491,13 @@ int main(int argc, char *argv[])
IOobjectList objects(runTime, runTime.timeName()); IOobjectList objects(runTime, runTime.timeName());
int oldFlag = entry::disableFunctionEntries;
if (!enableEntries)
{
// By default disable dictionary expansion for fields
entry::disableFunctionEntries = 1;
}
// volFields // volFields
// ~~~~~~~~~ // ~~~~~~~~~
@ -615,6 +631,8 @@ int main(int argc, char *argv[])
thisNames, thisNames,
nbrNames nbrNames
); );
entry::disableFunctionEntries = oldFlag;
} }
return 0; return 0;

View File

@ -136,7 +136,8 @@
- =foamFormatConvert= - =foamFormatConvert=
- =changeDictionaryDict= - =changeDictionaryDict=
- =foamUpgradeCyclics= - =foamUpgradeCyclics=
- =fieldToCell= These applications will usually switch off all '#' processing.
Note: above field initialisation has the problem that the boundary conditions Note: above field initialisation has the problem that the boundary conditions
are not evaluated so e.g. processor boundaries will not hold the opposite cell are not evaluated so e.g. processor boundaries will not hold the opposite cell

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -162,11 +162,16 @@ Foam::Istream& Foam::UIPstream::read(token& t)
// String // String
case token::STRING : case token::STRING :
case token::VERBATIMSTRING :
{ {
string* pval = new string; string* pval = new string;
if (read(*pval)) if (read(*pval))
{ {
t = pval; t = pval;
if (c == token::VERBATIMSTRING)
{
t.type() = token::VERBATIMSTRING;
}
} }
else else
{ {

View File

@ -203,7 +203,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
return *this; return *this;
} }
// Verbatim string // Possible verbatim string or dictionary functionEntry
case token::HASH : case token::HASH :
{ {
char nextC; char nextC;
@ -226,6 +226,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
else else
{ {
t = sPtr; t = sPtr;
t.type() = token::VERBATIMSTRING;
} }
return *this; return *this;

View File

@ -78,6 +78,7 @@ public:
PUNCTUATION, PUNCTUATION,
WORD, WORD,
STRING, STRING,
VERBATIMSTRING,
LABEL, LABEL,
FLOAT_SCALAR, FLOAT_SCALAR,
DOUBLE_SCALAR, DOUBLE_SCALAR,
@ -318,6 +319,7 @@ public:
// Access // Access
inline tokenType type() const; inline tokenType type() const;
inline tokenType& type();
inline bool good() const; inline bool good() const;
inline bool undefined() const; inline bool undefined() const;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -39,7 +39,7 @@ inline void token::clear()
{ {
delete wordTokenPtr_; delete wordTokenPtr_;
} }
else if (type_ == STRING) else if (type_ == STRING || type_ == VERBATIMSTRING)
{ {
delete stringTokenPtr_; delete stringTokenPtr_;
} }
@ -88,6 +88,7 @@ inline token::token(const token& t)
break; break;
case STRING: case STRING:
case VERBATIMSTRING:
stringTokenPtr_ = new string(*t.stringTokenPtr_); stringTokenPtr_ = new string(*t.stringTokenPtr_);
break; break;
@ -178,6 +179,11 @@ inline token::tokenType token::type() const
return type_; return type_;
} }
inline token::tokenType& token::type()
{
return type_;
}
inline bool token::good() const inline bool token::good() const
{ {
return (type_ != ERROR && type_ != UNDEFINED); return (type_ != ERROR && type_ != UNDEFINED);
@ -231,12 +237,12 @@ inline const word& token::wordToken() const
inline bool token::isString() const inline bool token::isString() const
{ {
return (type_ == STRING); return (type_ == STRING || type_ == VERBATIMSTRING);
} }
inline const string& token::stringToken() const inline const string& token::stringToken() const
{ {
if (type_ == STRING) if (type_ == STRING || type_ == VERBATIMSTRING)
{ {
return *stringTokenPtr_; return *stringTokenPtr_;
} }
@ -405,6 +411,7 @@ inline void token::operator=(const token& t)
break; break;
case STRING: case STRING:
case VERBATIMSTRING:
stringTokenPtr_ = new string(*t.stringTokenPtr_); stringTokenPtr_ = new string(*t.stringTokenPtr_);
break; break;
@ -511,6 +518,7 @@ inline bool token::operator==(const token& t) const
return *wordTokenPtr_ == *t.wordTokenPtr_; return *wordTokenPtr_ == *t.wordTokenPtr_;
case STRING: case STRING:
case VERBATIMSTRING:
return *stringTokenPtr_ == *t.stringTokenPtr_; return *stringTokenPtr_ == *t.stringTokenPtr_;
case LABEL: case LABEL:
@ -544,7 +552,7 @@ inline bool token::operator==(const word& w) const
inline bool token::operator==(const string& s) const inline bool token::operator==(const string& s) const
{ {
return (type_ == STRING && stringToken() == s); return ((type_ == STRING || type_ == VERBATIMSTRING) && stringToken() == s);
} }
inline bool token::operator==(const label l) const inline bool token::operator==(const label l) const

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -70,6 +70,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& t)
break; break;
case token::STRING: case token::STRING:
case token::VERBATIMSTRING:
os << *t.stringTokenPtr_; os << *t.stringTokenPtr_;
break; break;
@ -156,6 +157,10 @@ ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
os << " the string " << t.stringToken(); os << " the string " << t.stringToken();
break; break;
case token::VERBATIMSTRING:
os << " the verbatim string " << t.stringToken();
break;
case token::LABEL: case token::LABEL:
os << " the label " << t.labelToken(); os << " the label " << t.labelToken();
break; break;
@ -226,6 +231,10 @@ Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip)
os << " the string " << t.stringToken(); os << " the string " << t.stringToken();
break; break;
case token::VERBATIMSTRING:
os << " the verbatim string " << t.stringToken();
break;
case token::LABEL: case token::LABEL:
os << " the label " << t.labelToken(); os << " the label " << t.labelToken();
break; break;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -27,6 +27,14 @@ License
#include "dictionary.H" #include "dictionary.H"
#include "OStringStream.H" #include "OStringStream.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
int Foam::entry::disableFunctionEntries
(
Foam::debug::infoSwitch("disableFunctionEntries", 0)
);
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::entry::entry(const keyType& keyword) Foam::entry::entry(const keyType& keyword)

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -81,6 +81,9 @@ class entry
public: public:
static int disableFunctionEntries;
// Constructors // Constructors
//- Construct from keyword //- Construct from keyword

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -98,21 +98,32 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
} }
else // Keyword starts entry ... else // Keyword starts entry ...
{ {
if (keyword[0] == '#') // ... Function entry if
(
!disableFunctionEntries
&& 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);
} }
else if (keyword[0] == '$') // ... Substitution entry else if
(
!disableFunctionEntries
&& keyword[0] == '$') // ... Substitution entry
{ {
parentDict.substituteKeyword(keyword); parentDict.substituteKeyword(keyword);
return true; return true;
} }
else if (keyword == "include") // ... For backward compatibility else if
(
!disableFunctionEntries
&& keyword == "include"
) // ... For backward compatibility
{ {
return functionEntries::includeEntry::execute(parentDict, is); return functionEntries::includeEntry::execute(parentDict, is);
} }
else // ... Data entries else // ... Data entries
{ {
token nextToken(is); token nextToken(is);
is.putBack(nextToken); is.putBack(nextToken);

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -44,7 +44,8 @@ void Foam::primitiveEntry::append
if if
( (
w.size() == 1 disableFunctionEntries
|| w.size() == 1
|| ( || (
!(w[0] == '$' && expandVariable(w, dict)) !(w[0] == '$' && expandVariable(w, dict))
&& !(w[0] == '#' && expandFunction(w, dict, is)) && !(w[0] == '#' && expandFunction(w, dict, is))
@ -215,7 +216,17 @@ void Foam::primitiveEntry::write(Ostream& os) const
for (label i=0; i<size(); ++i) for (label i=0; i<size(); ++i)
{ {
os << operator[](i); const token& t = operator[](i);
if (t.type() == token::VERBATIMSTRING)
{
os << token::HASH << token::BEGIN_BLOCK;
os.writeQuoted(t.stringToken(), false);
os << token::HASH << token::END_BLOCK;
}
else
{
os << t;
}
if (i < size()-1) if (i < size()-1)
{ {