dictionary: Rationalised the handling of dictionary variable

by introducing a new specialised type, variable, derived from word with
additional valid characters.  This avoids some complex type-juggling in the
parser and keyType in which string was used to represent either a string or a
variable.
This commit is contained in:
Henry Weller
2019-08-15 09:16:45 +01:00
parent 19999767f3
commit 3192d64875
20 changed files with 616 additions and 109 deletions

View File

@ -450,7 +450,7 @@ int main(int argc, char *argv[])
} }
else else
{ {
dictPtr = new dictionary; dictPtr = new dictionary(dictPath);
dictFormat = readDict(*dictPtr, dictPath); dictFormat = readDict(*dictPtr, dictPath);
} }

View File

@ -108,6 +108,8 @@ $(strings)/word/wordIO.C
$(strings)/word/wordIOList.C $(strings)/word/wordIOList.C
$(strings)/fileName/fileName.C $(strings)/fileName/fileName.C
$(strings)/fileName/fileNameIO.C $(strings)/fileName/fileNameIO.C
$(strings)/variable/variable.C
$(strings)/variable/variableIO.C
$(strings)/keyType/keyType.C $(strings)/keyType/keyType.C
$(strings)/wordRe/wordRe.C $(strings)/wordRe/wordRe.C
$(strings)/lists/hashedWordList.C $(strings)/lists/hashedWordList.C
@ -237,13 +239,13 @@ db/IOobjects/GlobalIOField/GlobalIOFields.C
IOobject = db/IOobject IOobject = db/IOobject
/* $(IOobject)/IOobject.C in global.Cver */ $(IOobject)/IOobject.C
$(IOobject)/IOobjectIO.C $(IOobject)/IOobjectIO.C
$(IOobject)/IOobjectReadHeader.C $(IOobject)/IOobjectReadHeader.C
$(IOobject)/IOobjectWriteHeader.C $(IOobject)/IOobjectWriteHeader.C
regIOobject = db/regIOobject regIOobject = db/regIOobject
/* $(regIOobject)/regIOobject.C in global.Cver */ $(regIOobject)/regIOobject.C
$(regIOobject)/regIOobjectRead.C $(regIOobject)/regIOobjectRead.C
$(regIOobject)/regIOobjectWrite.C $(regIOobject)/regIOobjectWrite.C

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -241,40 +241,32 @@ Foam::Istream& Foam::ISstream::read(token& t)
} }
} }
case '$': case '$' :
{ {
// Look ahead // Look ahead
char nextC; char nextC;
if (read(nextC).bad()) if (read(nextC).bad())
{ {
// Return $ as word // Return $ as a variable
t = token(word(c)); t = token(variable(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 *this; return *this;
} }
else else
{ {
putback(nextC); putback(nextC);
putback(c); putback(c);
readWordToken(t);
variable* vPtr = new variable;
if (readVariable(*vPtr).bad())
{
delete vPtr;
t.setBad();
}
else
{
t = vPtr;
}
return *this; return *this;
} }
} }
@ -610,7 +602,7 @@ Foam::Istream& Foam::ISstream::readVariable(string& str)
&& ( && (
c == token::BEGIN_BLOCK c == token::BEGIN_BLOCK
|| c == token::END_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'; buf[errLen] = '\0';
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "word '" << buf << "...'\n" << "variable '" << buf << "...'\n"
<< " is too long (max. " << maxLen << " characters)" << " is too long (max. " << maxLen << " characters)"
<< exit(FatalIOError); << exit(FatalIOError);
@ -647,16 +639,33 @@ Foam::Istream& Foam::ISstream::readVariable(string& str)
else else
{ {
buf[nChar++] = c; 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; buf[nChar++] = c;
if (nChar == maxLen) if (nChar == maxLen)
{ {
buf[errLen] = '\0'; buf[errLen] = '\0';
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "word '" << buf << "...'\n" << "variable '" << buf << "...'\n"
<< " is too long (max. " << maxLen << " characters)" << " is too long (max. " << maxLen << " characters)"
<< exit(FatalIOError); << exit(FatalIOError);
@ -689,7 +698,7 @@ Foam::Istream& Foam::ISstream::readVariable(string& str)
buf[nChar] = '\0'; buf[nChar] = '\0';
str = buf; 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) if (c != token::END_BLOCK)
{ {
putback(c); putback(c);

View File

@ -41,6 +41,7 @@ SourceFiles
#include "uLabel.H" #include "uLabel.H"
#include "scalar.H" #include "scalar.H"
#include "word.H" #include "word.H"
#include "variable.H"
#include "InfoProxy.H" #include "InfoProxy.H"
#include "refCount.H" #include "refCount.H"
#include "typeInfo.H" #include "typeInfo.H"
@ -246,6 +247,7 @@ private:
{ {
punctuationToken punctuationToken_; punctuationToken punctuationToken_;
word* wordTokenPtr_; word* wordTokenPtr_;
variable* variableTokenPtr_;
string* stringTokenPtr_; string* stringTokenPtr_;
label labelToken_; label labelToken_;
floatScalar floatScalarToken_; floatScalar floatScalarToken_;
@ -329,6 +331,7 @@ public:
inline const word& wordToken() const; inline const word& wordToken() const;
inline bool isVariable() const; inline bool isVariable() const;
inline const variable& variableToken() const;
inline bool isString() const; inline bool isString() const;
inline const string& stringToken() const; inline const string& stringToken() const;
@ -386,6 +389,9 @@ public:
inline void operator=(word*); inline void operator=(word*);
inline void operator=(const word&); inline void operator=(const word&);
inline void operator=(variable*);
inline void operator=(const variable&);
inline void operator=(string*); inline void operator=(string*);
inline void operator=(const string&); inline void operator=(const string&);
@ -402,6 +408,7 @@ public:
inline bool operator==(const token&) const; inline bool operator==(const token&) const;
inline bool operator==(const punctuationToken) const; inline bool operator==(const punctuationToken) const;
inline bool operator==(const word&) const; inline bool operator==(const word&) const;
inline bool operator==(const variable&) const;
inline bool operator==(const string&) const; inline bool operator==(const string&) const;
inline bool operator==(const label) const; inline bool operator==(const label) const;
inline bool operator==(const floatScalar) const; inline bool operator==(const floatScalar) const;
@ -414,6 +421,7 @@ public:
inline bool operator!=(const token&) const; inline bool operator!=(const token&) const;
inline bool operator!=(const punctuationToken) const; inline bool operator!=(const punctuationToken) const;
inline bool operator!=(const word&) const; inline bool operator!=(const word&) const;
inline bool operator!=(const variable&) const;
inline bool operator!=(const string&) const; inline bool operator!=(const string&) const;
inline bool operator!=(const label) const; inline bool operator!=(const label) const;
inline bool operator!=(const floatScalar) const; inline bool operator!=(const floatScalar) 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 | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -33,7 +33,11 @@ inline void Foam::token::clear()
{ {
delete wordTokenPtr_; delete wordTokenPtr_;
} }
else if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING) else if (type_ == VARIABLE)
{
delete variableTokenPtr_;
}
else if (type_ == STRING || type_ == VERBATIMSTRING)
{ {
delete stringTokenPtr_; delete stringTokenPtr_;
} }
@ -84,8 +88,11 @@ inline Foam::token::token(const token& t)
wordTokenPtr_ = new word(*t.wordTokenPtr_); wordTokenPtr_ = new word(*t.wordTokenPtr_);
break; break;
case STRING:
case VARIABLE: case VARIABLE:
variableTokenPtr_ = new variable(*t.variableTokenPtr_);
break;
case STRING:
case VERBATIMSTRING: case VERBATIMSTRING:
stringTokenPtr_ = new string(*t.stringTokenPtr_); stringTokenPtr_ = new string(*t.stringTokenPtr_);
break; break;
@ -250,14 +257,27 @@ inline bool Foam::token::isVariable() const
return (type_ == VARIABLE); 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 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 inline const Foam::string& Foam::token::stringToken() const
{ {
if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING) if (type_ == STRING || type_ == VERBATIMSTRING)
{ {
return *stringTokenPtr_; return *stringTokenPtr_;
} }
@ -453,8 +473,11 @@ inline void Foam::token::operator=(const token& t)
wordTokenPtr_ = new word(*t.wordTokenPtr_); wordTokenPtr_ = new word(*t.wordTokenPtr_);
break; break;
case STRING:
case VARIABLE: case VARIABLE:
variableTokenPtr_ = new variable(*t.variableTokenPtr_);
break;
case STRING:
case VERBATIMSTRING: case VERBATIMSTRING:
stringTokenPtr_ = new string(*t.stringTokenPtr_); stringTokenPtr_ = new string(*t.stringTokenPtr_);
break; break;
@ -507,6 +530,18 @@ inline void Foam::token::operator=(const word& w)
operator=(new 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) inline void Foam::token::operator=(string* sPtr)
{ {
clear(); clear();
@ -573,8 +608,10 @@ inline bool Foam::token::operator==(const token& t) const
case WORD: case WORD:
return *wordTokenPtr_ == *t.wordTokenPtr_; return *wordTokenPtr_ == *t.wordTokenPtr_;
case STRING:
case VARIABLE: case VARIABLE:
return *variableTokenPtr_ == *t.variableTokenPtr_;
case STRING:
case VERBATIMSTRING: case VERBATIMSTRING:
return *stringTokenPtr_ == *t.stringTokenPtr_; return *stringTokenPtr_ == *t.stringTokenPtr_;
@ -614,11 +651,16 @@ inline bool Foam::token::operator==(const word& w) const
return (type_ == WORD && wordToken() == w); 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 inline bool Foam::token::operator==(const string& s) const
{ {
return return
( (
(type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING) (type_ == STRING || type_ == VERBATIMSTRING)
&& stringToken() == s && stringToken() == s
); );
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -66,16 +66,15 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& t)
os << *t.wordTokenPtr_; os << *t.wordTokenPtr_;
break; break;
case token::VARIABLE:
os << *t.variableTokenPtr_;
break;
case token::STRING: case token::STRING:
case token::VERBATIMSTRING: case token::VERBATIMSTRING:
os << *t.stringTokenPtr_; os << *t.stringTokenPtr_;
break; break;
case token::VARIABLE:
// Behaviour differs according to stream type
os.write(t);
break;
case token::LABEL: case token::LABEL:
os << t.labelToken_; os << t.labelToken_;
break; break;
@ -164,7 +163,7 @@ ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
break; break;
case token::VARIABLE: case token::VARIABLE:
os << " the variable " << t.stringToken(); os << " the variable " << t.variableToken();
break; break;
case token::VERBATIMSTRING: case token::VERBATIMSTRING:
@ -240,7 +239,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<token>& ip)
break; break;
case token::VARIABLE: case token::VARIABLE:
os << " the variable " << t.stringToken(); os << " the variable " << t.variableToken();
break; break;
case token::VERBATIMSTRING: case token::VERBATIMSTRING:

View File

@ -59,8 +59,7 @@ bool Foam::entry::getKeyword(keyType& keyword, token& keywordToken, Istream& is)
} }
else if (keywordToken.isVariable()) else if (keywordToken.isVariable())
{ {
// Disable wildcards for variables keyword = keywordToken.variableToken();
keyword = keyType(keywordToken.stringToken(), false);
return true; return true;
} }
else if (keywordToken.isString()) else if (keywordToken.isString())

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2018-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -73,43 +73,31 @@ void Foam::functionEntries::ifeqEntry::readToken(token& t, Istream& is)
Foam::token Foam::functionEntries::ifeqEntry::expand Foam::token Foam::functionEntries::ifeqEntry::expand
( (
const dictionary& dict, const dictionary& dict,
const string& keyword, const variable& var,
const token& t const token& t
) )
{ {
if (keyword[0] == '$') word varName = var(1, var.size()-1);
{
word varName = keyword(1, keyword.size()-1);
// lookup the variable name in the given dictionary // lookup the variable name in the given dictionary
const entry* ePtr = dict.lookupScopedEntryPtr const entry* ePtr = dict.lookupScopedEntryPtr
( (
varName, varName,
true, true,
true true
); );
if (ePtr) 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())
{ {
// Re-form as a string token so we can compare to string return token(ePtr->stream());
return token(keyword, t.lineNumber());
} }
else 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()) 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()) else if (t.isVariable())
{ {
return expand(dict, t.stringToken(), t); return expand(dict, t.variableToken(), t);
}
else if (t.isString())
{
return expand(dict, t.stringToken(), t);
} }
else else
{ {

View File

@ -97,11 +97,11 @@ protected:
//- Read tokens. Skip dummy tokens //- Read tokens. Skip dummy tokens
static void readToken(token& t, Istream& is); static void readToken(token& t, Istream& is);
//- Expand a variable (string/word/var starting with '$') //- Expand variable
static token expand static token expand
( (
const dictionary& dict, const dictionary& dict,
const string& keyword, const variable& var,
const token& t const token& t
); );

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2018-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -62,21 +62,21 @@ Foam::string Foam::functionEntries::negEntry::negateVariable
) )
{ {
// Read variable name as a word including the '$' // Read variable name as a word including the '$'
const word varWord(is); const variable var(is);
if (varWord[0] != '$') if (var[0] != '$')
{ {
FatalIOErrorInFunction FatalIOErrorInFunction
( (
parentDict parentDict
) << "Expected variable name beginning with a '$' but found '" ) << "Expected variable name beginning with a '$' but found '"
<< varWord << "'" << exit(FatalIOError); << var << "'" << exit(FatalIOError);
return string::null; return string::null;
} }
// Strip the leading '$' from the variable name // 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.... // Lookup the variable name in the parent dictionary....
const entry* ePtr = parentDict.lookupScopedEntryPtr(varName, true, false); const entry* ePtr = parentDict.lookupScopedEntryPtr(varName, true, false);

View File

@ -49,6 +49,7 @@ bool Foam::primitiveEntry::expandVariable
{ {
// Recursive substitution mode. Replace between {} with expansion. // Recursive substitution mode. Replace between {} with expansion.
string s(w(2, w.size()-3)); string s(w(2, w.size()-3));
// Substitute dictionary and environment variables. Do not allow // Substitute dictionary and environment variables. Do not allow
// empty substitutions. // empty substitutions.
stringOps::inplaceExpand(s, dict, true, false); stringOps::inplaceExpand(s, dict, true, false);

View File

@ -46,10 +46,7 @@ void Foam::primitiveEntry::append
( (
disableFunctionEntries disableFunctionEntries
|| w.size() == 1 || w.size() == 1
|| ( || !(w[0] == '#' && expandFunction(w, dict, is))
!(w[0] == '$' && expandVariable(w, dict))
&& !(w[0] == '#' && expandFunction(w, dict, is))
)
) )
{ {
newElmt(tokenIndex()++) = currToken; newElmt(tokenIndex()++) = currToken;
@ -57,17 +54,13 @@ void Foam::primitiveEntry::append
} }
else if (currToken.isVariable()) else if (currToken.isVariable())
{ {
const string& w = currToken.stringToken(); const variable& v = currToken.variableToken();
if if
( (
disableFunctionEntries disableFunctionEntries
|| w.size() <= 3 || v.size() == 1
|| !( || !(v[0] == '$' && expandVariable(v, dict))
w[0] == '$'
&& w[1] == token::BEGIN_BLOCK
&& expandVariable(w, dict)
)
) )
{ {
newElmt(tokenIndex()++) = currToken; newElmt(tokenIndex()++) = currToken;

View File

@ -38,7 +38,7 @@ SourceFiles
#ifndef keyType_H #ifndef keyType_H
#define keyType_H #define keyType_H
#include "word.H" #include "variable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -71,11 +71,13 @@ class keyType
//- Is the keyType a pattern (regular expression) //- Is the keyType a pattern (regular expression)
bool isPattern_; bool isPattern_;
// Private Member Functions // Private Member Functions
//- Disallow assignments where we cannot determine string/word type //- Disallow assignments where we cannot determine string/word type
void operator=(const std::string&); void operator=(const std::string&);
public: public:
// Static Data Members // Static Data Members
@ -95,6 +97,9 @@ public:
//- Copy constructor of word. Not treated as a regular expression //- Copy constructor of word. Not treated as a regular expression
inline keyType(const word&); 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. //- Copy constructor of string. Treat as regular expression.
inline keyType(const string&); inline keyType(const string&);
@ -130,6 +135,9 @@ public:
//- Assign as word, not as non regular expression //- Assign as word, not as non regular expression
inline void operator=(const word&); inline void operator=(const word&);
//- Assign as variable, not as non regular expression
inline void operator=(const variable&);
//- Assign as regular expression //- Assign as regular expression
inline void operator=(const string&); inline void operator=(const string&);

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License 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) inline Foam::keyType::keyType(const string& s)
: :
word(s, false), 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) inline void Foam::keyType::operator=(const string& s)
{ {
// Bypass checking // Bypass checking

View File

@ -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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#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;
}
// ************************************************************************* //

View File

@ -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 <http://www.gnu.org/licenses/>.
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
// ************************************************************************* //

View File

@ -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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include <cctype>
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
inline void Foam::variable::stripInvalid()
{
// skip stripping unless debug is active to avoid
// costly operations
if (debug && string::stripInvalid<variable>(*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();
}
// ************************************************************************* //

View File

@ -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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#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<variable>(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;
}
// ************************************************************************* //

View File

@ -119,6 +119,8 @@ inline bool Foam::word::valid(char c)
!isspace(c) !isspace(c)
&& c != '"' // string quote && c != '"' // string quote
&& c != '\'' // string quote && c != '\'' // string quote
&& c != '/' // path separator
&& c != '$' // variable indicator
&& c != ';' // end statement && c != ';' // end statement
&& c != '{' // beg subdict && c != '{' // beg subdict
&& c != '}' // end subdict && c != '}' // end subdict

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -52,7 +52,7 @@ Foam::Istream& Foam::operator>>(Istream& is, word& w)
} }
else if (t.isString()) else if (t.isString())
{ {
// try a bit harder and convert string to word // Convert string to word stripping invalid characters
w = t.stringToken(); w = t.stringToken();
string::stripInvalid<word>(w); string::stripInvalid<word>(w);
@ -62,7 +62,7 @@ Foam::Istream& Foam::operator>>(Istream& is, word& w)
is.setBad(); is.setBad();
FatalIOErrorInFunction(is) FatalIOErrorInFunction(is)
<< "wrong token type - expected word, found " << "wrong token type - expected word, found "
"non-word characters " "non-word characters "
<< t.info() << t.info()
<< exit(FatalIOError); << exit(FatalIOError);
return is; return is;