primitiveEntry.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "primitiveEntry.H"
27 #include "dictionary.H"
28 #include "OSspecific.H"
29 #include "stringOps.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 void Foam::primitiveEntry::append(const UList<token>& varTokens)
34 {
35  forAll(varTokens, i)
36  {
37  newElmt(tokenIndex()++) = varTokens[i];
38  }
39 }
40 
41 
42 bool Foam::primitiveEntry::expandVariable
43 (
44  const variable& w,
45  const dictionary& dict
46 )
47 {
48  if (w.size() > 2 && w[0] == '$' && w[1] == token::BEGIN_BLOCK)
49  {
50  // Recursive substitution mode. Replace between {} with expansion.
51  string s(w(2, w.size() - 3));
52 
53  // Substitute dictionary and environment variables. Do not allow
54  // empty substitutions.
55  stringOps::inplaceExpandEntry(s, dict, true, false);
56  variable newW(w);
57  newW.std::string::replace(1, newW.size() - 1, s);
58 
59  return expandVariable(newW, dict);
60  }
61  else
62  {
63  string varName = w(1, w.size() - 1);
64 
65  // Lookup the variable name in the given dictionary....
66  // Note: allow wildcards to match? For now disabled since following
67  // would expand internalField to wildcard match and not expected
68  // internalField:
69  // internalField XXX;
70  // boundaryField { ".*" {YYY;} movingWall {value $internalField;}
71  const entry* ePtr = dict.lookupScopedEntryPtr(varName, true, false);
72 
73  // ...if defined append its tokens into this
74  if (ePtr)
75  {
76  if (ePtr->isDict())
77  {
78  append(ePtr->dict().tokens());
79  }
80  else
81  {
82  append(ePtr->stream());
83  }
84  }
85  else
86  {
87  // Not in the dictionary - try an environment variable
88  string envStr = getEnv(varName);
89 
90  if (envStr.empty())
91  {
93  (
94  dict
95  ) << "Illegal dictionary entry or environment variable name "
96  << varName << endl << "Valid dictionary entries are "
97  << dict.toc() << exit(FatalIOError);
98 
99  return false;
100  }
101  append(tokenList(IStringStream('(' + envStr + ')')()));
102  }
103  }
104  return true;
105 }
106 
107 
108 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
109 
111 :
112  entry(key),
113  ITstream(is)
114 {
115  name() += '/' + keyword();
116 }
117 
118 
120 :
121  entry(key),
122  ITstream(key, tokenList(1, t))
123 {}
124 
125 
127 (
128  const keyType& key,
129  const UList<token>& tokens
130 )
131 :
132  entry(key),
133  ITstream(key, tokens)
134 {}
135 
136 
138 (
139  const keyType& key,
140  List<token>&& tokens
141 )
142 :
143  entry(key),
144  ITstream(key, move(tokens))
145 {}
146 
147 
148 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
149 
151 {
152  const tokenList& tokens = *this;
153 
154  if (tokens.empty())
155  {
156  return -1;
157  }
158  else
159  {
160  return tokens.first().lineNumber();
161  }
162 }
163 
164 
166 {
167  const tokenList& tokens = *this;
168 
169  if (tokens.empty())
170  {
171  return -1;
172  }
173  else
174  {
175  return tokens.last().lineNumber();
176  }
177 }
178 
179 
181 {
182  ITstream& is = const_cast<primitiveEntry&>(*this);
183  is.rewind();
184  return is;
185 }
186 
187 
189 {
191  << "Attempt to return primitive entry " << info()
192  << " as a sub-dictionary"
193  << abort(FatalError);
194 
195  return dictionary::null;
196 }
197 
198 
200 {
202  << "Attempt to return primitive entry " << info()
203  << " as a sub-dictionary"
204  << abort(FatalError);
205 
206  return const_cast<dictionary&>(dictionary::null);
207 }
208 
209 
210 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Input token stream.
Definition: ITstream.H:53
virtual Istream & rewind()
Rewind and return the stream so that it may be read again.
Definition: ITstream.C:185
label tokenIndex() const
Return the current token index.
Definition: ITstream.H:140
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
T & newElmt(const label)
Return subscript-checked element of UList.
Definition: ListI.H:152
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
T & first()
Return the first element of the list.
Definition: UListI.H:114
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
T & last()
Return the last element of the list.
Definition: UListI.H:128
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
const entry * lookupScopedEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present,.
Definition: dictionary.C:737
wordList toc() const
Return the table of contents.
Definition: dictionary.C:976
static const dictionary null
Null dictionary.
Definition: dictionary.H:258
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
const keyType & keyword() const
Return keyword.
Definition: entry.H:123
A class for handling keywords in dictionaries.
Definition: keyType.H:69
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
primitiveEntry(const keyType &, Istream &)
Construct from keyword and a Istream.
label endLineNumber() const
Return line number of last token in dictionary.
ITstream & stream() const
Return token stream if this entry is a primitive entry.
const dictionary & dict() const
This entry is not a dictionary,.
const fileName & name() const
Return the dictionary name.
label startLineNumber() const
Return line number of first token in dictionary.
A token holds items read from Istream.
Definition: token.H:73
@ BEGIN_BLOCK
Definition: token.H:113
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
string & inplaceExpandEntry(string &s, const dictionary &dict, const bool allowEnvVars, const bool allowEmpty, const char sigil='$')
Inplace expand occurrences of variables according to the dictionary.
Definition: stringOps.C:760
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
errorManip< error > abort(error &err)
Definition: errorManip.H:131
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
IOerror FatalIOError
error FatalError
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:42
dictionary dict