Loading...
Searching...
No Matches
substitutionModel.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-------------------------------------------------------------------------------
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
29#include "stringOps.H"
30
31// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32
33namespace Foam
34{
37}
42
43// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
48}
49
52{
54};
55
56
58{
59 const label lBegin = KEY_BEGIN.length();
60 const label lEnd = KEY_END.length();
61
62 wordHashSet keys;
63
64 size_t pos0 = 0;
65 size_t pos = 0;
66 string cleanedBuffer = "";
67 while (((pos = buffer.find(KEY_BEGIN, pos)) != string::npos))
68 {
69 cleanedBuffer += buffer.substr(pos0, pos-pos0);
70
71 size_t posEnd = buffer.find(KEY_END, pos);
72
73 if (posEnd != string::npos)
74 {
75 const word k(cleanKey(buffer.substr(pos+lBegin, posEnd-pos-lEnd)));
76 keys.insert(k);
77 cleanedBuffer += keyify(k);
78 }
79
80 pos = posEnd + lEnd;
81 pos0 = pos;
82 }
83
84 cleanedBuffer += buffer.substr(pos0, buffer.length() - pos0);
85 buffer = cleanedBuffer;
86
87 return keys.toc();
88}
89
90
92(
93 const word& key,
94 const string& value
95)
96{
97 builtin_.insert(cleanKey(key), value.c_str());
98}
99
102{
103 return builtin_.contains(key);
104}
105
106
108(
109 const word& key,
110 const string& value
111)
112{
113 builtin_.set(cleanKey(key), value.c_str());
114}
115
116
117bool Foam::substitutionModel::replaceBuiltin(const word& key, string& str)
118{
119 if (builtin_.found(key))
120 {
121 str.replaceAll(keyify(key), builtin_[key].c_str());
122 return true;
123 }
124
125 return false;
126}
127
128
130{
131 const string str0 = str;
132
133 // Quick exit if there are no keys in the string
134 if (str.find(KEY_BEGIN) == string::npos) return false;
135
136 forAllConstIters(builtin_, iter)
137 {
138 str.replaceAll(keyify(iter.key()), iter.val().c_str());
139 }
140
141 return str != str0;
142}
143
144
146{
147 for (const auto& iter : builtin_.csorted())
148 {
149 os << keyify(iter.key()).c_str() << " : " << iter.val() << nl;
150 }
151}
152
153
154// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
155
156Foam::substitutionModel::substitutionModel
157(
158 const dictionary& dict,
159 const Time& time
160)
161:
162 dict_(dict),
163 time_(time)
164{}
165
166
167// ************************************************************************* //
label k
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
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
A class for handling character strings derived from std::string.
Definition string.H:76
static std::string::size_type length(const char *s)
Length of the character sequence (with nullptr protection).
Definition string.H:259
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 wordList getKeys(string &buffer)
Return all keys from a string buffer.
static bool containsBuiltin(const word &key)
Return true if key is builtin.
static const word KEY_END
Keyword ending characters.
static const word KEY_BEGIN
Keyword starting characters.
static void writeBuiltins(Ostream &os)
Write all builtins to stream.
virtual wordList keys() const =0
Return a word list of the keys.
const dictionary dict_
Construction dictionary.
static word cleanKey(const string &str)
Clean the key text.
static void setBuiltinStr(const word &key, const string &value)
Set a builtin to the hash table.
static string keyify(const word &w)
Return a key representation from a word.
static HashTable< string > builtin_
Built-in substitutions.
static bool replaceBuiltin(const word &key, string &str)
Replace key in string.
static void addBuiltinStr(const word &key, const string &value)
Add a builtin to the hash table - does not overwrite.
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
OBJstream os(runTime.globalPath()/outputName)
string trim(const std::string &s)
Return string trimmed of leading and trailing whitespace.
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
List< word > wordList
List of word.
Definition fileName.H:60
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition HashSet.H:80
dimensionedScalar pos0(const dimensionedScalar &ds)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
#define defineRunTimeSelectionTable(baseType, argNames)
Define run-time selection table.
dictionary dict
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition stdFoam.H:235