wordDirective.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "wordDirective.H"
29 #include "dictionary.H"
30 #include "stringOps.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace functionEntries
38 {
40  (
41  functionEntry,
42  wordDirective,
43  execute,
44  dictionaryIstream,
45  word
46  );
47 
49  (
50  functionEntry,
51  wordDirective,
52  execute,
53  primitiveEntryIstream,
54  word
55  );
56 
57 } // End namespace functionEntries
58 } // End namespace Foam
59 
60 
61 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
62 
63 Foam::token Foam::functionEntries::wordDirective::evaluate
64 (
65  const dictionary& parentDict,
66  Istream& is
67 )
68 {
69  token tok(is);
70 
71  // The string to evaluate
72  string str;
73 
74  if (tok.isStringType()) // Also accepts a single bare word
75  {
76  // - #word expr
77  // - #word "expr"
78  // - #word #{ expr #}
79  str = tok.stringToken();
80  }
81  else if (tok.isPunctuation(token::BEGIN_BLOCK))
82  {
83  // - #word { expr }
84  // strip comments
85  if (!continueReadUntilRightBrace(is, str, true))
86  {
88  (
89  is,
90  "Premature end while reading #word - missing '}'?"
91  );
92  }
93  }
94  else
95  {
97  << "Invalid input for #word."
98  " Expecting a string or block to expand, but found" << nl
99  << tok.info() << endl
100  << exit(FatalIOError);
101  }
102 
103  stringOps::inplaceExpand(str, parentDict);
104 
105  word result(word::validate(str)); // Includes trimming etc.
106 
107  if (!result.empty())
108  {
109  tok = std::move(result);
110  return tok;
111  }
112 
113  // Expanded to nothing - treat as a no-op
114  return token::undefinedToken;
115 }
116 
117 
118 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
119 
121 (
122  const dictionary& parentDict,
124  Istream& is
125 )
126 {
127  token tok(evaluate(parentDict, is));
128 
129  if (tok.good())
130  {
131  // Can add evaluated value directly into primitiveEntry
132  entry.append(std::move(tok), true); // Lazy resizing
133  }
134 
135  return true;
136 }
137 
138 
140 (
141  dictionary& parentDict,
142  Istream& is
143 )
144 {
145  token tok(evaluate(parentDict, is));
146 
147  if (tok.good())
148  {
149  // Using putBack to insert evaluated value into stream
150  is.putBack(tok);
151  }
152 
153  return true;
154 }
155 
156 
157 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::primitiveEntry
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
Definition: primitiveEntry.H:63
Foam::word::validate
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:45
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
addToMemberFunctionSelectionTable.H
Macros for easy insertion into member function selection tables.
Foam::functionEntries::wordDirective::execute
static bool execute(const dictionary &parentDict, primitiveEntry &entry, Istream &is)
Execute in a primitiveEntry context.
Definition: wordDirective.C:121
wordDirective.H
Foam::entry::reportReadWarning
static void reportReadWarning(const IOstream &, const std::string &)
Report a read warning (on std::cerr)
Definition: entry.C:48
Foam::token::undefinedToken
static const token undefinedToken
An undefined token.
Definition: token.H:294
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::functionEntries::addNamedToMemberFunctionSelectionTable
addNamedToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream, calc)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:159
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::token::good
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:405
Foam::functionEntry::continueReadUntilRightBrace
static bool continueReadUntilRightBrace(Istream &is, std::string &str, const bool stripComments=true)
Slurp a string until a closing '}' is found.
Definition: functionEntry.C:66
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
dictionary.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
stringOps.H
Foam::stringOps::evaluate
string evaluate(label fieldWidth, const std::string &s, size_t pos=0, size_t len=std::string::npos)
String evaluation with specified (positive, non-zero) field width.
Definition: stringOpsEvaluate.C:37