dictionaryValue.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) 2024 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 "dictionaryValue.H"
30 #include "IFstream.H"
31 #include "polyMesh.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace substitutionModels
38 {
41 }
42 }
43 
44 
45 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
46 
48 (
49  const dictionary& dict,
50  const word& key,
51  string& buffer
52 ) const
53 {
54  const string& lookup = entries_[key];
55 
56  OStringStream oss;
57  if (lookup.empty())
58  {
59  // Add complete dictionary
60  oss << dict;
61  }
62  else
63  {
64  const entry* ePtr = dict.findScoped(lookup);
65 
66  if (!ePtr)
67  {
69  << "Unable to find entry " << lookup
70  << endl;
71  return false;
72  }
73 
74  if (ePtr->isDict())
75  {
76  const dictionary& de = ePtr->dict();
77 
78  // Write dictionary contents
79  oss << de.dictName() << de;
80  }
81  else
82  {
83  for (const auto& t : ePtr->stream())
84  {
85  if (oss.count()) oss << separator_;
86  oss << t;
87  }
88  }
89  }
90 
91  buffer.replaceAll(keyify(key), oss.str());
92 
93  return true;
94 }
95 
96 
97 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98 
99 Foam::substitutionModels::dictionaryValue::dictionaryValue
100 (
101  const dictionary& dict,
102  const Time& time
103 )
104 :
105  substitutionModel(dict, time),
106  object_(),
107  region_(polyMesh::defaultRegion),
108  path_(),
109  separator_(dict.getOrDefault<word>("separator", " ")),
110  entries_()
111 {
112  const auto* oPtr = dict.findEntry("object");
113  const auto* pPtr = dict.findEntry("path");
114 
115  if (oPtr && pPtr)
116  {
118  << "Specify either 'object' or 'path' but not both"
119  << exit(FatalIOError);
120  }
121 
122  if (oPtr)
123  {
124  // Optionally read the region
125  dict.readIfPresent<word>("region", region_);
126 
127  // Must read the object name to look up
128  object_ = dict.get<word>("object");
129  }
130 
131  if (pPtr)
132  {
133  path_ = dict.get<fileName>("path").expand();
134  }
135 
136  // Populate entries
137  const dictionary& entriesDict = dict.subDict("entries");
138  for (const auto& e : entriesDict)
139  {
140  entries_.insert(cleanKey(e.keyword()), string(e.stream()));
141  }
142 }
143 
144 
145 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
146 
148 {
149  return entries_.found(keyName);
150 }
151 
152 
154 (
155  const word& key,
156  string& buffer
157 ) const
158 {
159  if (!valid(key)) return false;
160 
161  if (path_.size())
162  {
163  fileName path(path_);
164  if (replaceBuiltin(path))
165  {
166  path.clean();
167  }
168 
169  IFstream is(path);
170 
171  if (!is.good())
172  {
174  << "Unable to find dictionary at " << path
175  << ". Deactivating." << endl;
176 
177  return false;
178  }
179 
180  return processDict(dictionary(is), key, buffer);
181  }
182  else
183  {
184  const auto* obrPtr = time_.cfindObject<objectRegistry>(region_);
185 
186  if (!obrPtr)
187  {
189  << "Unable to find region " << region_
190  << ". Deactivating." << endl;
191 
192  return false;
193  }
194 
195  // Find object; recursive lookup into parent
196  const auto* dictPtr = obrPtr->cfindObject<IOdictionary>(object_, true);
197 
198  if (!dictPtr)
199  {
201  << "Unable find dictionary " << object_
202  << " on region " << region_
203  << ". Deactivating." << endl;
204 
205  return false;
206  }
207 
208  return processDict(*dictPtr, key, buffer);
209  }
210 }
211 
212 
214 {
215  return entries_.sortedToc();
216 }
217 
218 
219 // ************************************************************************* //
dictionary dict
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
bool processDict(const dictionary &dict, const word &key, string &buffer) const
Main function to process the dictionary.
Base class for substitution models.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Lookup type of boundary radiation properties.
Definition: lookup.H:57
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
Foam::string str() const
Get the string. As Foam::string instead of std::string (may change in future)
Definition: StringStream.H:256
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
A class for handling words, derived from Foam::string.
Definition: word.H:63
std::streamsize count()
The number of bytes outputted.
Definition: StringStream.H:250
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Input from file stream as an ISstream, normally using std::ifstream for the actual input...
Definition: IFstream.H:51
virtual bool valid(const word &keyName) const
Return true of model applies to this keyName.
addToRunTimeSelectionTable(substitutionModel, dictionaryValue, dictionary)
virtual wordList keys() const
Return a word list of the keys.
string & replaceAll(const std::string &s1, const std::string &s2, size_type pos=0)
Replace all occurrences of sub-string s1 with s2, beginning at pos in the string. ...
Definition: string.C:117
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:637
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
defineTypeNameAndDebug(dictionaryValue, 0)
The dictionaryValue substitution model. Dictionaries can be retrieved from an object registry...
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
virtual bool apply(const word &key, string &buffer) const
Apply substitutions to this string buffer.
static word cleanKey(const string &str)
Clean the key text.
static string keyify(const word &w)
Return a key representation from a word.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:208
Namespace for OpenFOAM.
string expand(const std::string &s, const HashTable< string > &mapping, const char sigil='$')
Expand occurrences of variables according to the mapping and return the expanded string.
Definition: stringOps.C:705
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...