primitiveEntry.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "primitiveEntry.H"
30 #include "dictionary.H"
31 #include "OSspecific.H"
32 #include "stringOps.H"
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
36 bool Foam::primitiveEntry::expandVariable
37 (
38  const string& varName,
39  const dictionary& dict
40 )
41 {
42  if (varName.size() > 1 && varName[0] == token::BEGIN_BLOCK)
43  {
44  // Recursive substitution mode.
45  // Content between {} is replaced with expansion.
46  string expanded(varName.substr(1, varName.size()-2));
47 
48  // Substitute dictionary and environment variables.
49  // Do not allow empty substitutions.
50  stringOps::inplaceExpand(expanded, dict, true, false);
51 
52  return expandVariable(expanded, dict);
53  }
54 
55  // Lookup variable name in the given dictionary WITHOUT pattern matching.
56  // Having a pattern match means that in this example:
57  // {
58  // internalField XXX;
59  // boundaryField { ".*" {YYY;} movingWall {value $internalField;}
60  // }
61  // The $internalField would be matched by the ".*" !!!
62 
63  // Recursive, non-patterns
64  const entry* eptr = dict.findScoped(varName, keyType::LITERAL_RECURSIVE);
65 
66  if (!eptr)
67  {
68  // Not found - revert to environment variable
69  const string str(Foam::getEnv(varName));
70 
71  if (str.empty())
72  {
74  << "Illegal dictionary entry or environment variable name "
75  << varName << nl
76  << "Known dictionary entries: " << dict.toc() << nl
77  << exit(FatalIOError);
78 
79  return false;
80  }
81 
82  // Parse string into a series of tokens
83 
85 
86  ITstream::append(std::move(toks), true); // Lazy resizing
87  }
88  else if (eptr->isDict())
89  {
90  // Found dictionary entry
91 
92  tokenList toks(eptr->dict().tokens());
93 
94  ITstream::append(std::move(toks), true); // Lazy resizing
95  }
96  else
97  {
98  // Found primitive entry - copy tokens
99  ITstream::append(eptr->stream(), true); // Lazy resizing
100  }
101 
102  return true;
103 }
104 
105 
106 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
107 
109 :
110  entry(key),
111  ITstream(is)
112 {
113  name() += '.' + keyword();
114 }
115 
116 
118 :
119  entry(key),
120  ITstream(key, tokenList(1, tok))
121 {}
122 
123 
125 (
126  const keyType& key,
127  const UList<token>& tokens
128 )
129 :
130  entry(key),
131  ITstream(key, tokens)
132 {}
133 
134 
136 (
137  const keyType& key,
138  List<token>&& tokens
139 )
140 :
141  entry(key),
142  ITstream(key, std::move(tokens))
143 {}
144 
145 
146 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
147 
149 {
150  const tokenList& tokens = *this;
151 
152  if (tokens.size())
153  {
154  tokens.first().lineNumber();
155  }
156 
157  return -1;
158 }
159 
160 
162 {
163  const tokenList& tokens = *this;
164 
165  if (tokens.size())
166  {
167  return tokens.last().lineNumber();
168  }
169 
170  return -1;
171 }
172 
173 
175 {
176  ITstream& is = const_cast<primitiveEntry&>(*this);
177  is.rewind();
178  return is;
179 }
180 
181 
183 {
185  << "Attempt to return primitive entry " << info()
186  << " as a sub-dictionary"
187  << abort(FatalError);
188 
189  return dictionary::null;
190 }
191 
192 
194 {
196  << "Attempt to return primitive entry " << info()
197  << " as a sub-dictionary"
198  << abort(FatalError);
199 
200  return const_cast<dictionary&>(dictionary::null);
201 }
202 
203 
204 // ************************************************************************* //
Foam::entry::entry
entry(const keyType &keyword)
Construct from keyword.
Definition: entry.C:55
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::ITstream::append
void append(const token &t, const bool lazy)
Definition: ITstream.C:408
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::ITstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:356
Foam::keyType::LITERAL_RECURSIVE
Definition: keyType.H:78
Foam::primitiveEntry
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
Definition: primitiveEntry.H:62
primitiveEntry.H
Foam::entry::keyword
const keyType & keyword() const
Return keyword.
Definition: entry.H:187
Foam::primitiveEntry::primitiveEntry
primitiveEntry(const keyType &key, Istream &is)
Construct from keyword and a Istream.
Definition: primitiveEntryIO.C:264
Foam::primitiveEntry::stream
virtual ITstream & stream() const
Return token stream for this primitive entry.
Definition: primitiveEntry.C:174
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::primitiveEntry::dict
virtual const dictionary & dict() const
This entry is not a dictionary,.
Definition: primitiveEntry.C:182
Foam::getEnv
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
Foam::dictionary::null
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:385
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
Foam::label
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:62
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:55
Foam::primitiveEntry::endLineNumber
virtual label endLineNumber() const
Return line number of last token in dictionary.
Definition: primitiveEntry.C:161
Foam::primitiveEntry::startLineNumber
virtual label startLineNumber() const
Return line number of first token in dictionary.
Definition: primitiveEntry.C:148
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Definition: stringOps.C:752
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::ITstream::parse
static tokenList parse(const UList< char > &input, streamFormat format=ASCII)
Definition: ITstream.C:56
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:121
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::tokenList
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:44
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::dictionary::findScoped
const entry * findScoped(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for a scoped entry (const access) with the given keyword.
Definition: dictionary.C:389
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
dictionary.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:665
stringOps.H
Foam::primitiveEntry::name
virtual const fileName & name() const
Return the dictionary name.
Definition: primitiveEntry.H:140