Commit f304b701 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

STYLE: simplify string handling for expansions in primitiveEntry

parent bc1f2fa9
......@@ -55,11 +55,11 @@ namespace Foam
class dictionaryEntry;
Ostream& operator<<(Ostream&, const dictionaryEntry&);
Ostream& operator<<(Ostream& os, const dictionaryEntry& de);
/*---------------------------------------------------------------------------*\
Class dictionaryEntry Declaration
Class dictionaryEntry Declaration
\*---------------------------------------------------------------------------*/
class dictionaryEntry
......@@ -69,10 +69,8 @@ class dictionaryEntry
{
// Private Member Functions
void readData(Istream&);
//- Dissallow bitwise copy
dictionaryEntry(const dictionary&);
//- Disallow bitwise copy
dictionaryEntry(const dictionaryEntry&) = delete;
public:
......@@ -80,20 +78,20 @@ public:
// Constructors
//- Construct from the parent dictionary and Istream
dictionaryEntry(const dictionary& parentDict, Istream&);
dictionaryEntry(const dictionary& parentDict, Istream& is);
//- Construct from the keyword, parent dictionary and a Istream
dictionaryEntry
(
const keyType&,
const keyType& key,
const dictionary& parentDict,
Istream&
Istream& is
);
//- Construct from the keyword, parent dictionary and a dictionary
dictionaryEntry
(
const keyType&,
const keyType& key,
const dictionary& parentDict,
const dictionary& dict
);
......@@ -102,7 +100,7 @@ public:
dictionaryEntry
(
const dictionary& parentDict,
const dictionaryEntry&
const dictionaryEntry& dictEnt
);
autoPtr<entry> clone(const dictionary& parentDict) const
......@@ -113,12 +111,6 @@ public:
// Member functions
//- Return the local dictionary name (final part of scoped name)
const word dictName() const
{
return dictionary::dictName();
}
//- Return the dictionary name (scoped, e.g. dictA::dictB::dictC)
const fileName& name() const
{
......@@ -153,8 +145,9 @@ public:
//- Return non-const access to dictionary
dictionary& dict();
//- Write
void write(Ostream&) const;
void write(Ostream& os) const;
//- Return info proxy.
// Used to print token information to a stream
......@@ -166,12 +159,12 @@ public:
// Ostream operator
friend Ostream& operator<<(Ostream&, const dictionaryEntry&);
friend Ostream& operator<<(Ostream& os, const dictionaryEntry& de);
};
template<>
Ostream& operator<<(Ostream&, const InfoProxy<dictionaryEntry>&);
Ostream& operator<<(Ostream& os, const InfoProxy<dictionaryEntry>& ip);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -69,8 +69,8 @@ class dictionaryListEntry
//- Returns size of dictionary without FoamFile
static label realSize(const dictionary&);
//- Dissallow bitwise copy
dictionaryListEntry(const dictionary&);
//- Disallow bitwise copy
dictionaryListEntry(const dictionaryListEntry&) = delete;
public:
......
......@@ -75,15 +75,18 @@ class entry
// Private Member Functions
//- Get the next valid keyword. Return true if a valid keyType.
//- Get the next valid keyword.
// \return True if it is a valid keyType.
static bool getKeyword
(
keyType& keyword,
token& keywordToken,
token& keyToken,
Istream& is
);
//- Get the next valid keyword otherwise return false
//- Get the next valid keyword.
// Warn when an invalid token is encountered that is not an end-of-block or eof
// \return True if it is a valid keyType.
static bool getKeyword(keyType& keyword, Istream& is);
......
......@@ -34,33 +34,33 @@ License
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::entry::getKeyword(keyType& keyword, token& keywordToken, Istream& is)
bool Foam::entry::getKeyword(keyType& keyword, token& keyToken, Istream& is)
{
// Read the next valid token discarding spurious ';'s
do
{
if
(
is.read(keywordToken).bad()
is.read(keyToken).bad()
|| is.eof()
|| !keywordToken.good()
|| !keyToken.good()
)
{
return false;
}
}
while (keywordToken == token::END_STATEMENT);
while (keyToken == token::END_STATEMENT);
// If the token is a valid keyword set 'keyword' return true...
if (keywordToken.isWord())
if (keyToken.isWord())
{
keyword = keywordToken.wordToken();
keyword = keyToken.wordToken();
return true;
}
else if (keywordToken.isString())
else if (keyToken.isString())
{
// Enable wildcards
keyword = keywordToken.stringToken();
keyword = keyToken.stringToken();
return true;
}
else
......@@ -72,34 +72,32 @@ bool Foam::entry::getKeyword(keyType& keyword, token& keywordToken, Istream& is)
bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
{
token keywordToken;
const bool ok = getKeyword(keyword, keywordToken, is);
token keyToken;
const bool valid = getKeyword(keyword, keyToken, is);
if (ok)
if (valid)
{
return true;
}
// Do some more checking
if (keyToken == token::END_BLOCK || is.eof())
{
return false;
}
else
{
// Do some more checking
if (keywordToken == token::END_BLOCK || is.eof())
{
return false;
}
else
{
// Otherwise the token is invalid
cerr<< "--> FOAM Warning : " << std::endl
<< " From function "
<< "entry::getKeyword(keyType&, Istream&)" << std::endl
<< " in file " << __FILE__
<< " at line " << __LINE__ << std::endl
<< " Reading " << is.name().c_str() << std::endl
<< " found " << keywordToken << std::endl
<< " expected either " << token::END_BLOCK << " or EOF"
<< std::endl;
return false;
}
// Otherwise the token is invalid
cerr<< "--> FOAM Warning : " << std::endl
<< " From function "
<< FUNCTION_NAME << std::endl
<< " in file " << __FILE__
<< " at line " << __LINE__ << std::endl
<< " Reading " << is.name().c_str() << std::endl
<< " found " << keyToken << std::endl
<< " expected either " << token::END_BLOCK << " or EOF"
<< std::endl;
return false;
}
}
......@@ -139,7 +137,7 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
// Otherwise the token is invalid
cerr<< "--> FOAM Warning : " << std::endl
<< " From function "
<< "entry::getKeyword(keyType&, Istream&)" << std::endl
<< FUNCTION_NAME << std::endl
<< " in file " << __FILE__
<< " at line " << __LINE__ << std::endl
<< " Reading " << is.name().c_str() << std::endl
......@@ -153,7 +151,6 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
{
if (keyword[0] == '#') // ... Function entry
{
const word functionName = keyword(1, keyword.size()-1);
if (disableFunctionEntries)
{
return parentDict.add
......@@ -169,6 +166,7 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
}
else
{
const word functionName(keyword.substr(1), false);
return functionEntry::execute(functionName, parentDict, is);
}
}
......@@ -183,19 +181,23 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
if (keyword.size() > 2 && keyword[1] == token::BEGIN_BLOCK)
{
// Recursive substitution mode. Replace between {} with
// expansion and then let standard variable expansion deal
// with rest.
string s(keyword(2, keyword.size()-3));
// Substitute dictionary and environment variables. Do not allow
// empty substitutions.
stringOps::inplaceExpand(s, parentDict, true, false);
keyword.std::string::replace(1, keyword.size()-1, s);
// Recursive substitution mode.
// Content between {} is replaced with expansion.
// Then let standard variable expansion deal with rest.
string expanded = keyword.substr(2, keyword.size()-3);
// Substitute dictionary and environment variables.
// Do not allow empty substitutions.
stringOps::inplaceExpand(expanded, parentDict, true, false);
// Restore the '$' prefix. Use replace since operator= is private
keyword.std::string::replace(1, keyword.size()-1, expanded);
}
if (nextToken == token::BEGIN_BLOCK)
{
const word varName = keyword(1, keyword.size()-1);
const word varName = keyword.substr(1);
// lookup the variable name in the given dictionary
const entry* ePtr = parentDict.lookupScopedEntryPtr
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -41,26 +41,24 @@ void Foam::primitiveEntry::append(const UList<token>& varTokens)
bool Foam::primitiveEntry::expandVariable
(
const string& w,
const string& varName,
const dictionary& dict
)
{
if (w.size() > 2 && w[0] == '$' && w[1] == token::BEGIN_BLOCK)
if (varName.size() > 1 && varName[0] == token::BEGIN_BLOCK)
{
// 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);
string newW(w);
newW.std::string::replace(1, newW.size()-1, s);
return expandVariable(newW, dict);
// Recursive substitution mode.
// Content between {} is replaced with expansion.
string expanded = varName.substr(1, varName.size()-2);
// Substitute dictionary and environment variables.
// Do not allow empty substitutions.
stringOps::inplaceExpand(expanded, dict, true, false);
return expandVariable(expanded, dict);
}
else
{
string varName = w(1, w.size()-1);
// lookup the variable name in the given dictionary....
// Note: allow wildcards to match? For now disabled since following
// would expand internalField to wildcard match and not expected
......@@ -83,8 +81,8 @@ bool Foam::primitiveEntry::expandVariable
}
else
{
// not in the dictionary - try an environment variable
string envStr = getEnv(varName);
// Not in the dictionary - try an environment variable
const string envStr = getEnv(varName);
if (envStr.empty())
{
......@@ -100,6 +98,7 @@ bool Foam::primitiveEntry::expandVariable
append(tokenList(IStringStream('(' + envStr + ')')()));
}
}
return true;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -25,13 +25,13 @@ Class
Foam::primitiveEntry
Description
A keyword and a list of tokens is a 'primitiveEntry'.
An primitiveEntry can be read, written and printed, and the types and
A keyword and a list of tokens comprise a primitiveEntry.
A primitiveEntry can be read, written and printed, and the types and
values of its tokens analysed.
A primitiveEntry is a high-level building block for data description. It
is a front-end for the token parser. A list of entries can be used as a
set of keyword syntax elements, for example.
A primitiveEntry is a high-level building block for data description.
It is a front-end for the token parser. A list of entries can be used
as a set of keyword syntax elements, for example.
SourceFiles
primitiveEntry.C
......@@ -42,9 +42,6 @@ SourceFiles
#ifndef primitiveEntry_H
#define primitiveEntry_H
#include "IStringStream.H"
#include "OStringStream.H"
#include "entry.H"
#include "ITstream.H"
#include "InfoProxy.H"
......@@ -68,29 +65,38 @@ class primitiveEntry
// Private Member Functions
//- Append the given tokens starting at the current tokenIndex
void append(const UList<token>&);
void append(const UList<token>& varTokens);
//- Append the given token to this entry
void append
(
const token& currToken,
const dictionary&,
Istream&
const dictionary& dict,
Istream& is
);
//- Expand the given variable (keyword starts with $)
bool expandVariable(const string&, const dictionary&);
//- Expand the given variable.
// The keyword starts with '$', but has been removed by the caller
// and thus passed as a varName.
// Keywords with '${}' are expanded recursively.
bool expandVariable
(
const string& varName,
const dictionary& dict
);
//- Expand the given function (keyword starts with #)
//- Expand the given function.
// The keyword starts with '#', but has been removed by the caller.
// and thus passed as a functionName.
bool expandFunction
(
const word&,
const dictionary&,
Istream&
const word& functionName,
const dictionary& dict,
Istream& is
);
//- Read the complete entry from the given stream
void readEntry(const dictionary&, Istream&);
void readEntry(const dictionary& dict, Istream& is);
public:
......@@ -98,26 +104,26 @@ public:
// Constructors
//- Construct from keyword and a Istream
primitiveEntry(const keyType&, Istream&);
primitiveEntry(const keyType& key, Istream& is);
//- Construct from keyword, parent dictionary and Istream
primitiveEntry(const keyType&, const dictionary& parentDict, Istream&);
primitiveEntry(const keyType& key, const dictionary& dict, Istream& is);
//- Construct from keyword and a ITstream
primitiveEntry(const keyType&, const ITstream&);
primitiveEntry(const keyType& key, const ITstream& is);
//- Construct from keyword and a single token
primitiveEntry(const keyType&, const token&);
primitiveEntry(const keyType& key, const token& t);
//- Construct from keyword and a list of tokens
primitiveEntry(const keyType&, const UList<token>&);
primitiveEntry(const keyType& key, const UList<token>& tokens);
//- Construct from keyword and by transferring a list of tokens
primitiveEntry(const keyType&, const Xfer<List<token>>&);
primitiveEntry(const keyType& key, const Xfer<List<token>>& tokens);
//- Construct from keyword and a T
template<class T>
primitiveEntry(const keyType&, const T&);
primitiveEntry(const keyType& key, const T& t);
autoPtr<entry> clone(const dictionary&) const
{
......@@ -166,13 +172,13 @@ public:
dictionary& dict();
//- Read tokens from the given stream
virtual bool read(const dictionary&, Istream&);
virtual bool read(const dictionary& dict, Istream& is);
//- Write
void write(Ostream&) const;
void write(Ostream& os) const;
//- Write, optionally with contents only (no keyword, etc)
void write(Ostream&, const bool contentsOnly) const;
void write(Ostream& os, const bool contentsOnly) const;
//- Return info proxy.
// Used to print token information to a stream
......@@ -184,7 +190,7 @@ public:
template<>
Ostream& operator<<(Ostream&, const InfoProxy<primitiveEntry>&);
Ostream& operator<<(Ostream& os, const InfoProxy<primitiveEntry>& ip);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -37,15 +37,15 @@ void Foam::primitiveEntry::append
{
if (currToken.isWord())
{
const word& w = currToken.wordToken();
const word& key = currToken.wordToken();
if
(
disableFunctionEntries
|| w.size() == 1
|| key.size() == 1
|| (
!(w[0] == '$' && expandVariable(w, dict))
&& !(w[0] == '#' && expandFunction(w, dict, is))
!(key[0] == '$' && expandVariable(key.substr(1), dict))
&& !(key[0] == '#' && expandFunction(key.substr(1), dict, is))
)
)
{
......@@ -54,16 +54,16 @@ void Foam::primitiveEntry::append
}
else if (currToken.isVariable())
{
const string& w = currToken.stringToken();
const string& key = currToken.stringToken();
if
(
disableFunctionEntries
|| w.size() <= 3
|| key.size() <= 3
|| !(
w[0] == '$'
&& w[1] == token::BEGIN_BLOCK
&& expandVariable(w, dict)
key[0] == '$'
&& key[1] == token::BEGIN_BLOCK
&& expandVariable(key.substr(1), dict)
)
)
{
......@@ -79,13 +79,12 @@ void Foam::primitiveEntry::append
bool Foam::primitiveEntry::expandFunction
(
const word& keyword,
const dictionary& parentDict,
const word& functionName,
const dictionary& dict,
Istream& is
)
{
word functionName = keyword(1, keyword.size()-1);
return functionEntry::execute(functionName, parentDict, *this, is);
return functionEntry::execute(functionName, dict, *this, is);
}
......
......@@ -25,6 +25,8 @@ License
#include "primitiveEntry.H"
#include "dictionary.H"
#include "IStringStream.H"
#include "OStringStream.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment