/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2025 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 "primitiveEntry.H"
#include "dictionary.H"
#include "OSspecific.H"
#include "stringOps.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::primitiveEntry::append(const UList& varTokens)
{
forAll(varTokens, i)
{
newElmt(tokenIndex()++) = varTokens[i];
}
}
bool Foam::primitiveEntry::expandVariable
(
const variable& w,
const dictionary& dict
)
{
if (w.size() > 2 && w[0] == '$' && w[1] == 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::inplaceExpandEntry(s, dict, true, false);
variable newW(w);
newW.std::string::replace(1, newW.size() - 1, s);
return expandVariable(newW, 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
// internalField:
// internalField XXX;
// boundaryField { ".*" {YYY;} movingWall {value $internalField;}
const entry* ePtr = dict.lookupScopedEntryPtr(varName, true, false);
// ...if defined append its tokens into this
if (ePtr)
{
if (ePtr->isDict())
{
append(ePtr->dict().tokens());
}
else
{
append(ePtr->stream());
}
}
else
{
// Not in the dictionary - try an environment variable
string envStr = getEnv(varName);
if (envStr.empty())
{
FatalIOErrorInFunction
(
dict
) << "Unknown dictionary entry or environment variable name "
<< varName << endl << "Valid dictionary entries are "
<< dict.toc() << exit(FatalIOError);
return false;
}
append(tokenList(IStringStream('(' + envStr + ')')()));
}
}
return true;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::primitiveEntry::primitiveEntry
(
const label keyLineNumber,
const keyType& key
)
:
entry(key, keyLineNumber),
ITstream(key)
{}
Foam::primitiveEntry::primitiveEntry(const keyType& key, const ITstream& is)
:
entry(key, is.lineNumber()),
ITstream(is)
{
name() += '/' + keyword();
}
Foam::primitiveEntry::primitiveEntry
(
const label keyLineNumber,
const keyType& key,
const ITstream& is
)
:
entry(key, keyLineNumber),
ITstream(is)
{
name() += '/' + keyword();
}
Foam::primitiveEntry::primitiveEntry(const keyType& key, const token& t)
:
entry(key, t.lineNumber()),
ITstream(key, t)
{}
Foam::primitiveEntry::primitiveEntry
(
const keyType& key,
const UList& tokens
)
:
entry(key, tokens.size() ? tokens[0].lineNumber() : -1),
ITstream(key, tokens)
{}
Foam::primitiveEntry::primitiveEntry
(
const keyType& key,
const List& tokens
)
:
entry(key, tokens.size() ? tokens[0].lineNumber() : -1),
ITstream(key, tokens)
{}
Foam::primitiveEntry::primitiveEntry
(
const keyType& key,
List&& tokens
)
:
entry(key, tokens.size() ? tokens[0].lineNumber() : -1),
ITstream(key, move(tokens))
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::label Foam::primitiveEntry::endLineNumber() const
{
const tokenList& tokens = *this;
if (tokens.empty())
{
return startLineNumber();
}
else
{
return tokens.last().lineNumber();
}
}
Foam::ITstream& Foam::primitiveEntry::stream() const
{
ITstream& is = const_cast(*this);
is.rewind();
return is;
}
const Foam::dictionary& Foam::primitiveEntry::dict() const
{
FatalErrorInFunction
<< "Attempt to return primitive entry " << info()
<< " as a sub-dictionary"
<< abort(FatalError);
return dictionary::null;
}
Foam::dictionary& Foam::primitiveEntry::dict()
{
FatalErrorInFunction
<< "Attempt to return primitive entry " << info()
<< " as a sub-dictionary"
<< abort(FatalError);
return const_cast(dictionary::null);
}
// ************************************************************************* //