From 79939b9e186b9cb288ecab5cafa6fa5401f3449d Mon Sep 17 00:00:00 2001 From: mattijs Date: Tue, 22 Feb 2011 15:29:57 +0000 Subject: [PATCH] ENH: #codeStream: preserve across preprocessing - new token: token::VERBATIMSTRING - writing of this type in primitiveEntry - disabling of all functionEntries in entry --- .../changeDictionary/changeDictionary.C | 24 +++++++++++++++++++ .../foamUpgradeCyclics/foamUpgradeCyclics.C | 20 +++++++++++++++- doc/changes/codeStream.org | 3 ++- .../db/IOstreams/Pstreams/UIPstream.C | 7 +++++- src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C | 3 ++- src/OpenFOAM/db/IOstreams/token/token.H | 2 ++ src/OpenFOAM/db/IOstreams/token/tokenI.H | 18 ++++++++++---- src/OpenFOAM/db/IOstreams/token/tokenIO.C | 11 ++++++++- src/OpenFOAM/db/dictionary/entry/entry.C | 10 +++++++- src/OpenFOAM/db/dictionary/entry/entry.H | 5 +++- src/OpenFOAM/db/dictionary/entry/entryIO.C | 21 ++++++++++++---- .../primitiveEntry/primitiveEntryIO.C | 17 ++++++++++--- 12 files changed, 121 insertions(+), 20 deletions(-) diff --git a/applications/utilities/preProcessing/changeDictionary/changeDictionary.C b/applications/utilities/preProcessing/changeDictionary/changeDictionary.C index 083107efb9..c5df6ef79f 100644 --- a/applications/utilities/preProcessing/changeDictionary/changeDictionary.C +++ b/applications/utilities/preProcessing/changeDictionary/changeDictionary.C @@ -60,6 +60,8 @@ Usage \param -literalRE \n 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", "treat regular expressions literally (ie, as a keyword)" ); + argList::addBoolOption + ( + "enableFunctionEntries", + "enable expansion of dictionary directives - #include, #codeStream etc" + ); #include "addRegionOption.H" #include "setRootCase.H" @@ -271,6 +278,20 @@ int main(int argc, char *argv[]) << " 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 = ""; if (regionName != fvMesh::defaultRegion) @@ -410,6 +431,7 @@ int main(int argc, char *argv[]) false ) ); + const_cast(IOdictionary::typeName) = oldTypeName; // Fake type back to what was in field const_cast(fieldDict.type()) = fieldDict.headerClassName(); @@ -429,6 +451,8 @@ int main(int argc, char *argv[]) } } + entry::disableFunctionEntries = oldFlag; + Info<< endl; Info<< "End\n" << endl; diff --git a/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C b/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C index dd44b49280..f1a7ab5301 100644 --- a/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C +++ b/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C @@ -34,6 +34,9 @@ Usage \param -test \n Suppress writing the updated files with split cyclics + \param -enableFunctionEntries \n + By default all dictionary preprocessing of fields is disabled + \*---------------------------------------------------------------------------*/ #include "argList.H" @@ -391,7 +394,12 @@ int main(int argc, char *argv[]) { 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 "setRootCase.H" @@ -404,6 +412,7 @@ int main(int argc, char *argv[]) { Info<< "-test option: no changes made" << nl << endl; } + const bool enableEntries = args.optionFound("enableFunctionEntries"); Foam::word regionName = polyMesh::defaultRegion; @@ -482,6 +491,13 @@ int main(int argc, char *argv[]) IOobjectList objects(runTime, runTime.timeName()); + int oldFlag = entry::disableFunctionEntries; + if (!enableEntries) + { + // By default disable dictionary expansion for fields + entry::disableFunctionEntries = 1; + } + // volFields // ~~~~~~~~~ @@ -615,6 +631,8 @@ int main(int argc, char *argv[]) thisNames, nbrNames ); + + entry::disableFunctionEntries = oldFlag; } return 0; diff --git a/doc/changes/codeStream.org b/doc/changes/codeStream.org index 57bd7f46cd..3297d26573 100644 --- a/doc/changes/codeStream.org +++ b/doc/changes/codeStream.org @@ -136,7 +136,8 @@ - =foamFormatConvert= - =changeDictionaryDict= - =foamUpgradeCyclics= - - =fieldToCell= + These applications will usually switch off all '#' processing. + 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 diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C index f0026c2ff0..50728c425c 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -162,11 +162,16 @@ Foam::Istream& Foam::UIPstream::read(token& t) // String case token::STRING : + case token::VERBATIMSTRING : { string* pval = new string; if (read(*pval)) { t = pval; + if (c == token::VERBATIMSTRING) + { + t.type() = token::VERBATIMSTRING; + } } else { diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C index 135f61823a..90e2a46c82 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C @@ -203,7 +203,7 @@ Foam::Istream& Foam::ISstream::read(token& t) return *this; } - // Verbatim string + // Possible verbatim string or dictionary functionEntry case token::HASH : { char nextC; @@ -226,6 +226,7 @@ Foam::Istream& Foam::ISstream::read(token& t) else { t = sPtr; + t.type() = token::VERBATIMSTRING; } return *this; diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H index 7c91758c1a..c48c22b5c3 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.H +++ b/src/OpenFOAM/db/IOstreams/token/token.H @@ -78,6 +78,7 @@ public: PUNCTUATION, WORD, STRING, + VERBATIMSTRING, LABEL, FLOAT_SCALAR, DOUBLE_SCALAR, @@ -318,6 +319,7 @@ public: // Access inline tokenType type() const; + inline tokenType& type(); inline bool good() const; inline bool undefined() const; diff --git a/src/OpenFOAM/db/IOstreams/token/tokenI.H b/src/OpenFOAM/db/IOstreams/token/tokenI.H index 6c3434dead..11124e9e2a 100644 --- a/src/OpenFOAM/db/IOstreams/token/tokenI.H +++ b/src/OpenFOAM/db/IOstreams/token/tokenI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -39,7 +39,7 @@ inline void token::clear() { delete wordTokenPtr_; } - else if (type_ == STRING) + else if (type_ == STRING || type_ == VERBATIMSTRING) { delete stringTokenPtr_; } @@ -88,6 +88,7 @@ inline token::token(const token& t) break; case STRING: + case VERBATIMSTRING: stringTokenPtr_ = new string(*t.stringTokenPtr_); break; @@ -178,6 +179,11 @@ inline token::tokenType token::type() const return type_; } +inline token::tokenType& token::type() +{ + return type_; +} + inline bool token::good() const { return (type_ != ERROR && type_ != UNDEFINED); @@ -231,12 +237,12 @@ inline const word& token::wordToken() const inline bool token::isString() const { - return (type_ == STRING); + return (type_ == STRING || type_ == VERBATIMSTRING); } inline const string& token::stringToken() const { - if (type_ == STRING) + if (type_ == STRING || type_ == VERBATIMSTRING) { return *stringTokenPtr_; } @@ -405,6 +411,7 @@ inline void token::operator=(const token& t) break; case STRING: + case VERBATIMSTRING: stringTokenPtr_ = new string(*t.stringTokenPtr_); break; @@ -511,6 +518,7 @@ inline bool token::operator==(const token& t) const return *wordTokenPtr_ == *t.wordTokenPtr_; case STRING: + case VERBATIMSTRING: return *stringTokenPtr_ == *t.stringTokenPtr_; case LABEL: @@ -544,7 +552,7 @@ inline bool token::operator==(const word& w) 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 diff --git a/src/OpenFOAM/db/IOstreams/token/tokenIO.C b/src/OpenFOAM/db/IOstreams/token/tokenIO.C index bf7cdd081d..23c17e75e5 100644 --- a/src/OpenFOAM/db/IOstreams/token/tokenIO.C +++ b/src/OpenFOAM/db/IOstreams/token/tokenIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -70,6 +70,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& t) break; case token::STRING: + case token::VERBATIMSTRING: os << *t.stringTokenPtr_; break; @@ -156,6 +157,10 @@ ostream& Foam::operator<<(ostream& os, const InfoProxy& ip) os << " the string " << t.stringToken(); break; + case token::VERBATIMSTRING: + os << " the verbatim string " << t.stringToken(); + break; + case token::LABEL: os << " the label " << t.labelToken(); break; @@ -226,6 +231,10 @@ Ostream& operator<<(Ostream& os, const InfoProxy& ip) os << " the string " << t.stringToken(); break; + case token::VERBATIMSTRING: + os << " the verbatim string " << t.stringToken(); + break; + case token::LABEL: os << " the label " << t.labelToken(); break; diff --git a/src/OpenFOAM/db/dictionary/entry/entry.C b/src/OpenFOAM/db/dictionary/entry/entry.C index de62c861a4..3bba611dee 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.C +++ b/src/OpenFOAM/db/dictionary/entry/entry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -27,6 +27,14 @@ License #include "dictionary.H" #include "OStringStream.H" +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +int Foam::entry::disableFunctionEntries +( + Foam::debug::infoSwitch("disableFunctionEntries", 0) +); + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::entry::entry(const keyType& keyword) diff --git a/src/OpenFOAM/db/dictionary/entry/entry.H b/src/OpenFOAM/db/dictionary/entry/entry.H index 21f5634728..93b5a143a2 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.H +++ b/src/OpenFOAM/db/dictionary/entry/entry.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -81,6 +81,9 @@ class entry public: + static int disableFunctionEntries; + + // Constructors //- Construct from keyword diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index c61a97e7c5..1f3172f0e5 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -98,21 +98,32 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is) } else // Keyword starts entry ... { - if (keyword[0] == '#') // ... Function entry + if + ( + !disableFunctionEntries + && keyword[0] == '#' + ) // ... Function entry { word functionName = keyword(1, keyword.size()-1); return functionEntry::execute(functionName, parentDict, is); } - else if (keyword[0] == '$') // ... Substitution entry + else if + ( + !disableFunctionEntries + && keyword[0] == '$') // ... Substitution entry { parentDict.substituteKeyword(keyword); return true; } - else if (keyword == "include") // ... For backward compatibility + else if + ( + !disableFunctionEntries + && keyword == "include" + ) // ... For backward compatibility { return functionEntries::includeEntry::execute(parentDict, is); } - else // ... Data entries + else // ... Data entries { token nextToken(is); is.putBack(nextToken); diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C index a7fcb0c64d..fa9a50efdb 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -44,7 +44,8 @@ void Foam::primitiveEntry::append if ( - w.size() == 1 + disableFunctionEntries + || w.size() == 1 || ( !(w[0] == '$' && expandVariable(w, dict)) && !(w[0] == '#' && expandFunction(w, dict, is)) @@ -215,7 +216,17 @@ void Foam::primitiveEntry::write(Ostream& os) const for (label i=0; i