Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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"
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35namespace Foam
36{
37namespace substitutionModels
38{
40 addToRunTimeSelectionTable(substitutionModel, dictionaryValue, dictionary);
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 OCharStream 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());
93 return true;
94}
95
96
97// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98
99Foam::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 * * * * * * * * * * * * * //
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;
207
208 return processDict(*dictPtr, key, buffer);
209 }
210}
211
212
214{
215 return entries_.sortedToc();
216}
217
218
219// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Input from file stream as an ISstream, normally using std::ifstream for the actual input.
Definition IFstream.H:55
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
An OSstream with internal List storage.
auto str() const
For OStringStream compatibility, return the buffer as string copy.
std::streamsize count() const
The number of bytes outputted.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
word dictName() const
The local dictionary name (final part of scoped name).
Definition dictionaryI.H:53
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
virtual bool isDict() const noexcept
True if this entry is a dictionary.
Definition entry.H:284
virtual const dictionary & dict() const =0
Return dictionary, if entry is a dictionary, otherwise Fatal.
A class for handling file names.
Definition fileName.H:75
Registry of regIOobjects.
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Mesh consisting of general polyhedral cells.
Definition polyMesh.H:79
Lookup type of boundary radiation properties.
Definition lookup.H:60
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
Base class for substitution models.
const Time & time_
Reference to the time database.
static word cleanKey(const string &str)
Clean the key text.
static string keyify(const word &w)
Return a key representation from a word.
static bool replaceBuiltin(const word &key, string &str)
Replace key in string.
The dictionaryValue substitution model. Dictionaries can be retrieved from an object registry,...
virtual bool apply(const word &key, string &buffer) const
Apply substitutions to this string buffer.
virtual bool valid(const word &keyName) const
Return true of model applies to this keyName.
bool processDict(const dictionary &dict, const word &key, string &buffer) const
Main function to process the dictionary.
virtual wordList keys() const
Return a word list of the keys.
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
dictionary dict
volScalarField & e