diff --git a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C index 29816597df..79348dcacc 100644 --- a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C +++ b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C @@ -450,7 +450,7 @@ int main(int argc, char *argv[]) } else { - dictPtr = new dictionary; + dictPtr = new dictionary(dictPath); dictFormat = readDict(*dictPtr, dictPath); } diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 31240abae2..bc2d0afd0b 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -108,6 +108,8 @@ $(strings)/word/wordIO.C $(strings)/word/wordIOList.C $(strings)/fileName/fileName.C $(strings)/fileName/fileNameIO.C +$(strings)/variable/variable.C +$(strings)/variable/variableIO.C $(strings)/keyType/keyType.C $(strings)/wordRe/wordRe.C $(strings)/lists/hashedWordList.C @@ -237,13 +239,13 @@ db/IOobjects/GlobalIOField/GlobalIOFields.C IOobject = db/IOobject -/* $(IOobject)/IOobject.C in global.Cver */ +$(IOobject)/IOobject.C $(IOobject)/IOobjectIO.C $(IOobject)/IOobjectReadHeader.C $(IOobject)/IOobjectWriteHeader.C regIOobject = db/regIOobject -/* $(regIOobject)/regIOobject.C in global.Cver */ +$(regIOobject)/regIOobject.C $(regIOobject)/regIOobjectRead.C $(regIOobject)/regIOobjectWrite.C diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C index abda94d8cf..521a1c54a8 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -241,40 +241,32 @@ Foam::Istream& Foam::ISstream::read(token& t) } } - case '$': + case '$' : { // Look ahead char nextC; if (read(nextC).bad()) { - // Return $ as word - t = token(word(c)); - return *this; - } - else if (nextC == token::BEGIN_BLOCK) - { - putback(nextC); - putback(c); - - string* sPtr = new string; - - if (readVariable(*sPtr).bad()) - { - delete sPtr; - t.setBad(); - } - else - { - t = sPtr; - t.type() = token::VARIABLE; - } + // Return $ as a variable + t = token(variable(c)); return *this; } else { putback(nextC); putback(c); - readWordToken(t); + + variable* vPtr = new variable; + + if (readVariable(*vPtr).bad()) + { + delete vPtr; + t.setBad(); + } + else + { + t = vPtr; + } return *this; } } @@ -610,7 +602,7 @@ Foam::Istream& Foam::ISstream::readVariable(string& str) && ( c == token::BEGIN_BLOCK || c == token::END_BLOCK - || word::valid(c) + || variable::valid(c) ) ) { @@ -620,7 +612,7 @@ Foam::Istream& Foam::ISstream::readVariable(string& str) buf[errLen] = '\0'; FatalIOErrorInFunction(*this) - << "word '" << buf << "...'\n" + << "variable '" << buf << "...'\n" << " is too long (max. " << maxLen << " characters)" << exit(FatalIOError); @@ -647,16 +639,33 @@ Foam::Istream& Foam::ISstream::readVariable(string& str) else { buf[nChar++] = c; + int listDepth = 0; - while (get(c) && word::valid(c)) + while (get(c) && variable::valid(c)) { + if (c == token::BEGIN_LIST) + { + listDepth++; + } + else if (c == token::END_LIST) + { + if (listDepth) + { + listDepth--; + } + else + { + break; + } + } + buf[nChar++] = c; if (nChar == maxLen) { buf[errLen] = '\0'; FatalIOErrorInFunction(*this) - << "word '" << buf << "...'\n" + << "variable '" << buf << "...'\n" << " is too long (max. " << maxLen << " characters)" << exit(FatalIOError); @@ -689,7 +698,7 @@ Foam::Istream& Foam::ISstream::readVariable(string& str) buf[nChar] = '\0'; str = buf; - // Note: check if we exited due to '}' or just !word::valid. + // Note: check if we exited due to '}' or just !variable::valid. if (c != token::END_BLOCK) { putback(c); diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H index 933d068831..6dbb84c31c 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.H +++ b/src/OpenFOAM/db/IOstreams/token/token.H @@ -41,6 +41,7 @@ SourceFiles #include "uLabel.H" #include "scalar.H" #include "word.H" +#include "variable.H" #include "InfoProxy.H" #include "refCount.H" #include "typeInfo.H" @@ -246,6 +247,7 @@ private: { punctuationToken punctuationToken_; word* wordTokenPtr_; + variable* variableTokenPtr_; string* stringTokenPtr_; label labelToken_; floatScalar floatScalarToken_; @@ -329,6 +331,7 @@ public: inline const word& wordToken() const; inline bool isVariable() const; + inline const variable& variableToken() const; inline bool isString() const; inline const string& stringToken() const; @@ -386,6 +389,9 @@ public: inline void operator=(word*); inline void operator=(const word&); + inline void operator=(variable*); + inline void operator=(const variable&); + inline void operator=(string*); inline void operator=(const string&); @@ -402,6 +408,7 @@ public: inline bool operator==(const token&) const; inline bool operator==(const punctuationToken) const; inline bool operator==(const word&) const; + inline bool operator==(const variable&) const; inline bool operator==(const string&) const; inline bool operator==(const label) const; inline bool operator==(const floatScalar) const; @@ -414,6 +421,7 @@ public: inline bool operator!=(const token&) const; inline bool operator!=(const punctuationToken) const; inline bool operator!=(const word&) const; + inline bool operator!=(const variable&) const; inline bool operator!=(const string&) const; inline bool operator!=(const label) const; inline bool operator!=(const floatScalar) const; diff --git a/src/OpenFOAM/db/IOstreams/token/tokenI.H b/src/OpenFOAM/db/IOstreams/token/tokenI.H index ad520fb2e2..a909d6bfd1 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 | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -33,7 +33,11 @@ inline void Foam::token::clear() { delete wordTokenPtr_; } - else if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING) + else if (type_ == VARIABLE) + { + delete variableTokenPtr_; + } + else if (type_ == STRING || type_ == VERBATIMSTRING) { delete stringTokenPtr_; } @@ -84,8 +88,11 @@ inline Foam::token::token(const token& t) wordTokenPtr_ = new word(*t.wordTokenPtr_); break; - case STRING: case VARIABLE: + variableTokenPtr_ = new variable(*t.variableTokenPtr_); + break; + + case STRING: case VERBATIMSTRING: stringTokenPtr_ = new string(*t.stringTokenPtr_); break; @@ -250,14 +257,27 @@ inline bool Foam::token::isVariable() const return (type_ == VARIABLE); } +inline const Foam::variable& Foam::token::variableToken() const +{ + if (type_ == VARIABLE) + { + return *variableTokenPtr_; + } + else + { + parseError(variable::typeName); + return variable::null; + } +} + inline bool Foam::token::isString() const { - return (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING); + return (type_ == STRING || type_ == VERBATIMSTRING); } inline const Foam::string& Foam::token::stringToken() const { - if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING) + if (type_ == STRING || type_ == VERBATIMSTRING) { return *stringTokenPtr_; } @@ -453,8 +473,11 @@ inline void Foam::token::operator=(const token& t) wordTokenPtr_ = new word(*t.wordTokenPtr_); break; - case STRING: case VARIABLE: + variableTokenPtr_ = new variable(*t.variableTokenPtr_); + break; + + case STRING: case VERBATIMSTRING: stringTokenPtr_ = new string(*t.stringTokenPtr_); break; @@ -507,6 +530,18 @@ inline void Foam::token::operator=(const word& w) operator=(new word(w)); } +inline void Foam::token::operator=(variable* vPtr) +{ + clear(); + type_ = VARIABLE; + variableTokenPtr_ = vPtr; +} + +inline void Foam::token::operator=(const variable& v) +{ + operator=(new variable(v)); +} + inline void Foam::token::operator=(string* sPtr) { clear(); @@ -573,8 +608,10 @@ inline bool Foam::token::operator==(const token& t) const case WORD: return *wordTokenPtr_ == *t.wordTokenPtr_; - case STRING: case VARIABLE: + return *variableTokenPtr_ == *t.variableTokenPtr_; + + case STRING: case VERBATIMSTRING: return *stringTokenPtr_ == *t.stringTokenPtr_; @@ -614,11 +651,16 @@ inline bool Foam::token::operator==(const word& w) const return (type_ == WORD && wordToken() == w); } +inline bool Foam::token::operator==(const variable& v) const +{ + return (type_ == VARIABLE && variableToken() == v); +} + inline bool Foam::token::operator==(const string& s) const { return ( - (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING) + (type_ == STRING || type_ == VERBATIMSTRING) && stringToken() == s ); } diff --git a/src/OpenFOAM/db/IOstreams/token/tokenIO.C b/src/OpenFOAM/db/IOstreams/token/tokenIO.C index a2dab7c7d6..c2d2f68135 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 | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -66,16 +66,15 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& t) os << *t.wordTokenPtr_; break; + case token::VARIABLE: + os << *t.variableTokenPtr_; + break; + case token::STRING: case token::VERBATIMSTRING: os << *t.stringTokenPtr_; break; - case token::VARIABLE: - // Behaviour differs according to stream type - os.write(t); - break; - case token::LABEL: os << t.labelToken_; break; @@ -164,7 +163,7 @@ ostream& Foam::operator<<(ostream& os, const InfoProxy& ip) break; case token::VARIABLE: - os << " the variable " << t.stringToken(); + os << " the variable " << t.variableToken(); break; case token::VERBATIMSTRING: @@ -240,7 +239,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy& ip) break; case token::VARIABLE: - os << " the variable " << t.stringToken(); + os << " the variable " << t.variableToken(); break; case token::VERBATIMSTRING: diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index 53547bc943..3f84c98724 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -59,8 +59,7 @@ bool Foam::entry::getKeyword(keyType& keyword, token& keywordToken, Istream& is) } else if (keywordToken.isVariable()) { - // Disable wildcards for variables - keyword = keyType(keywordToken.stringToken(), false); + keyword = keywordToken.variableToken(); return true; } else if (keywordToken.isString()) diff --git a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C index 312bd1ed59..22f0f6348e 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2018-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -73,43 +73,31 @@ void Foam::functionEntries::ifeqEntry::readToken(token& t, Istream& is) Foam::token Foam::functionEntries::ifeqEntry::expand ( const dictionary& dict, - const string& keyword, + const variable& var, const token& t ) { - if (keyword[0] == '$') - { - word varName = keyword(1, keyword.size()-1); + word varName = var(1, var.size()-1); - // lookup the variable name in the given dictionary - const entry* ePtr = dict.lookupScopedEntryPtr - ( - varName, - true, - true - ); - if (ePtr) - { - return token(ePtr->stream()); - } - else - { - // String expansion. Allow unset variables - string expanded(keyword); - stringOps::inplaceExpand(expanded, dict, true, true); - - // Re-form as a string token so we can compare to string - return token(expanded, t.lineNumber()); - } - } - else if (!t.isString()) + // lookup the variable name in the given dictionary + const entry* ePtr = dict.lookupScopedEntryPtr + ( + varName, + true, + true + ); + if (ePtr) { - // Re-form as a string token so we can compare to string - return token(keyword, t.lineNumber()); + return token(ePtr->stream()); } else { - return t; + // String expansion. Allow unset variables + string expanded(var); + stringOps::inplaceExpand(expanded, dict, true, true); + + // Re-form as a string token so we can compare to string + return token(expanded, t.lineNumber()); } } @@ -122,15 +110,12 @@ Foam::token Foam::functionEntries::ifeqEntry::expand { if (t.isWord()) { - return expand(dict, t.wordToken(), t); + // Re-form as a string token so we can compare to string + return string(t.wordToken()); } else if (t.isVariable()) { - return expand(dict, t.stringToken(), t); - } - else if (t.isString()) - { - return expand(dict, t.stringToken(), t); + return expand(dict, t.variableToken(), t); } else { diff --git a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.H index 7b4f4ac02a..1bc7e9bc7d 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.H @@ -97,11 +97,11 @@ protected: //- Read tokens. Skip dummy tokens static void readToken(token& t, Istream& is); - //- Expand a variable (string/word/var starting with '$') + //- Expand variable static token expand ( const dictionary& dict, - const string& keyword, + const variable& var, const token& t ); diff --git a/src/OpenFOAM/db/dictionary/functionEntries/negEntry/negEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/negEntry/negEntry.C index 7607c7a66c..df1db047eb 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/negEntry/negEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/negEntry/negEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2018-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -62,21 +62,21 @@ Foam::string Foam::functionEntries::negEntry::negateVariable ) { // Read variable name as a word including the '$' - const word varWord(is); + const variable var(is); - if (varWord[0] != '$') + if (var[0] != '$') { FatalIOErrorInFunction ( parentDict ) << "Expected variable name beginning with a '$' but found '" - << varWord << "'" << exit(FatalIOError); + << var << "'" << exit(FatalIOError); return string::null; } // Strip the leading '$' from the variable name - const string varName = varWord(1, varWord.size()-1); + const string varName = var(1, var.size() - 1); // Lookup the variable name in the parent dictionary.... const entry* ePtr = parentDict.lookupScopedEntryPtr(varName, true, false); diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C index 154a94be8e..9646bcc4cf 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C @@ -49,6 +49,7 @@ bool Foam::primitiveEntry::expandVariable { // Recursive substitution mode. Replace between {} with expansion. string s(w(2, w.size()-3)); + // Substitute dictionary and environment variables. Do not allow // empty substitutions. stringOps::inplaceExpand(s, dict, true, false); diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C index fe4c16d021..3f980f7a5b 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntryIO.C @@ -46,10 +46,7 @@ void Foam::primitiveEntry::append ( disableFunctionEntries || w.size() == 1 - || ( - !(w[0] == '$' && expandVariable(w, dict)) - && !(w[0] == '#' && expandFunction(w, dict, is)) - ) + || !(w[0] == '#' && expandFunction(w, dict, is)) ) { newElmt(tokenIndex()++) = currToken; @@ -57,17 +54,13 @@ void Foam::primitiveEntry::append } else if (currToken.isVariable()) { - const string& w = currToken.stringToken(); + const variable& v = currToken.variableToken(); if ( disableFunctionEntries - || w.size() <= 3 - || !( - w[0] == '$' - && w[1] == token::BEGIN_BLOCK - && expandVariable(w, dict) - ) + || v.size() == 1 + || !(v[0] == '$' && expandVariable(v, dict)) ) { newElmt(tokenIndex()++) = currToken; diff --git a/src/OpenFOAM/primitives/strings/keyType/keyType.H b/src/OpenFOAM/primitives/strings/keyType/keyType.H index ec2a5d1aa9..cea290ac6e 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyType.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyType.H @@ -38,7 +38,7 @@ SourceFiles #ifndef keyType_H #define keyType_H -#include "word.H" +#include "variable.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -71,11 +71,13 @@ class keyType //- Is the keyType a pattern (regular expression) bool isPattern_; + // Private Member Functions //- Disallow assignments where we cannot determine string/word type void operator=(const std::string&); + public: // Static Data Members @@ -95,6 +97,9 @@ public: //- Copy constructor of word. Not treated as a regular expression inline keyType(const word&); + //- Copy constructor of variable. Not treated as a regular expression + inline keyType(const variable&); + //- Copy constructor of string. Treat as regular expression. inline keyType(const string&); @@ -130,6 +135,9 @@ public: //- Assign as word, not as non regular expression inline void operator=(const word&); + //- Assign as variable, not as non regular expression + inline void operator=(const variable&); + //- Assign as regular expression inline void operator=(const string&); diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H index 9a1becde0c..52f7c62b1c 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -46,6 +46,13 @@ inline Foam::keyType::keyType(const word& s) {} +inline Foam::keyType::keyType(const variable& v) +: + word(v, false), + isPattern_(false) +{} + + inline Foam::keyType::keyType(const string& s) : word(s, false), @@ -96,6 +103,14 @@ inline void Foam::keyType::operator=(const word& s) } +inline void Foam::keyType::operator=(const variable& v) +{ + // Bypass checking + string::operator=(v); + isPattern_ = false; +} + + inline void Foam::keyType::operator=(const string& s) { // Bypass checking diff --git a/src/OpenFOAM/primitives/strings/variable/variable.C b/src/OpenFOAM/primitives/strings/variable/variable.C new file mode 100644 index 0000000000..b3c8842140 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/variable/variable.C @@ -0,0 +1,44 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "variable.H" +#include "debug.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +const char* const Foam::variable::typeName = "variable"; +int Foam::variable::debug(Foam::debug::debugSwitch(variable::typeName, 0)); +const Foam::variable Foam::variable::null; + + +// * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * // + +void Foam::writeEntry(Ostream& os, const variable& value) +{ + os << value; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/variable/variable.H b/src/OpenFOAM/primitives/strings/variable/variable.H new file mode 100644 index 0000000000..fa908500e6 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/variable/variable.H @@ -0,0 +1,144 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::variable + +Description + A variable is a word with support for additional characters, in particular + '$' and '/'. + +SourceFiles + variable.C + +\*---------------------------------------------------------------------------*/ + +#ifndef variable_H +#define variable_H + +#include "word.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward declaration of friend functions and operators +class variable; +class Istream; +class Ostream; + +Istream& operator>>(Istream&, variable&); +Ostream& operator<<(Ostream&, const variable&); + + +/*---------------------------------------------------------------------------*\ + Class variable Declaration +\*---------------------------------------------------------------------------*/ + +class variable +: + public word +{ + // Private Member Functions + + //- Strip invalid characters from this variable + inline void stripInvalid(); + + +public: + + // Static Data Members + + static const char* const typeName; + static int debug; + + //- An empty variable + static const variable null; + + + // Constructors + + //- Construct null + inline variable(); + + //- Copy constructor + inline variable(const variable&); + + //- Copy constructor of word + inline variable(const word&); + + //- Copy constructor of string + inline variable(const string&, const bool doStripInvalid=true); + + //- Copy constructor of std::string + inline variable(const std::string&, const bool doStripInvalid=true); + + //- Copy constructor of character array + inline variable(const char*, const bool doStripInvalid=true); + + //- Construct from Istream + // Words are treated as literals, strings with an auto-test + variable(Istream&); + + + // Member Functions + + //- Is this character valid for a variable + inline static bool valid(char); + + + // Member Operators + + // Assignment + + inline void operator=(const variable&); + inline void operator=(const word&); + inline void operator=(const string&); + inline void operator=(const std::string&); + inline void operator=(const char*); + + + // IOstream Operators + + friend Istream& operator>>(Istream&, variable&); + friend Ostream& operator<<(Ostream&, const variable&); +}; + + +void writeEntry(Ostream& os, const variable& value); + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "variableI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/variable/variableI.H b/src/OpenFOAM/primitives/strings/variable/variableI.H new file mode 100644 index 0000000000..e893ea7d95 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/variable/variableI.H @@ -0,0 +1,155 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +inline void Foam::variable::stripInvalid() +{ + // skip stripping unless debug is active to avoid + // costly operations + if (debug && string::stripInvalid(*this)) + { + std::cerr + << "variable::stripInvalid() called for variable " + << this->c_str() << std::endl; + + if (debug > 1) + { + std::cerr + << " For debug level (= " << debug + << ") > 1 this is considered fatal" << std::endl; + std::abort(); + } + } +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +inline Foam::variable::variable() +: + word() +{} + + +inline Foam::variable::variable(const variable& v) +: + word(v) +{} + + +inline Foam::variable::variable(const word& w) +: + word(w) +{} + + +inline Foam::variable::variable(const string& s, const bool doStripInvalid) +: + word(s) +{ + if (doStripInvalid) + { + stripInvalid(); + } +} + + +inline Foam::variable::variable(const std::string& s, const bool doStripInvalid) +: + word(s) +{ + if (doStripInvalid) + { + stripInvalid(); + } +} + + +inline Foam::variable::variable(const char* s, const bool doStripInvalid) +: + word(s) +{ + if (doStripInvalid) + { + stripInvalid(); + } +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +inline bool Foam::variable::valid(char c) +{ + return + ( + !isspace(c) + && c != '"' // string quote + && c != '\'' // string quote + && c != ';' // end statement + && c != '{' // beg subdict + && c != '}' // end subdict + ); +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +inline void Foam::variable::operator=(const variable& s) +{ + string::operator=(s); +} + + +inline void Foam::variable::operator=(const word& s) +{ + string::operator=(s); +} + + +inline void Foam::variable::operator=(const string& s) +{ + string::operator=(s); + stripInvalid(); +} + + +inline void Foam::variable::operator=(const std::string& s) +{ + string::operator=(s); + stripInvalid(); +} + + +inline void Foam::variable::operator=(const char* s) +{ + string::operator=(s); + stripInvalid(); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/variable/variableIO.C b/src/OpenFOAM/primitives/strings/variable/variableIO.C new file mode 100644 index 0000000000..65521fc19d --- /dev/null +++ b/src/OpenFOAM/primitives/strings/variable/variableIO.C @@ -0,0 +1,101 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "variable.H" +#include "IOstreams.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +Foam::variable::variable(Istream& is) +: + word() +{ + is >> *this; +} + + +Foam::Istream& Foam::operator>>(Istream& is, variable& v) +{ + token t(is); + + if (!t.good()) + { + is.setBad(); + return is; + } + + if (t.isVariable()) + { + v = t.variableToken(); + } + else if (t.isWord()) + { + v = t.wordToken(); + } + else if (t.isString()) + { + // Convert string to word stripping invalid characters + v = t.stringToken(); + string::stripInvalid(v); + + // flag empty strings and bad chars as an error + if (v.empty() || v.size() != t.stringToken().size()) + { + is.setBad(); + FatalIOErrorInFunction(is) + << "wrong token type - expected word, found " + "non-word characters " + << t.info() + << exit(FatalIOError); + return is; + } + } + else + { + is.setBad(); + FatalIOErrorInFunction(is) + << "wrong token type - expected word, found " + << t.info() + << exit(FatalIOError); + + return is; + } + + // Check state of IOstream + is.check("Istream& operator>>(Istream&, word&)"); + + return is; +} + + +Foam::Ostream& Foam::operator<<(Ostream& os, const variable& w) +{ + os.write(w); + os.check("Ostream& operator<<(Ostream&, const variable&)"); + return os; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/word/wordI.H b/src/OpenFOAM/primitives/strings/word/wordI.H index 1330a16617..ba2097b5e9 100644 --- a/src/OpenFOAM/primitives/strings/word/wordI.H +++ b/src/OpenFOAM/primitives/strings/word/wordI.H @@ -119,6 +119,8 @@ inline bool Foam::word::valid(char c) !isspace(c) && c != '"' // string quote && c != '\'' // string quote + && c != '/' // path separator + && c != '$' // variable indicator && c != ';' // end statement && c != '{' // beg subdict && c != '}' // end subdict diff --git a/src/OpenFOAM/primitives/strings/word/wordIO.C b/src/OpenFOAM/primitives/strings/word/wordIO.C index f4cd9ff089..f947297a7a 100644 --- a/src/OpenFOAM/primitives/strings/word/wordIO.C +++ b/src/OpenFOAM/primitives/strings/word/wordIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -52,7 +52,7 @@ Foam::Istream& Foam::operator>>(Istream& is, word& w) } else if (t.isString()) { - // try a bit harder and convert string to word + // Convert string to word stripping invalid characters w = t.stringToken(); string::stripInvalid(w); @@ -62,7 +62,7 @@ Foam::Istream& Foam::operator>>(Istream& is, word& w) is.setBad(); FatalIOErrorInFunction(is) << "wrong token type - expected word, found " - "non-word characters " + "non-word characters " << t.info() << exit(FatalIOError); return is;