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 -------------------------------------------------------------------------------
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 "substitutionModel.H"
29 #include "stringOps.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(substitutionModel, 0);
36  defineRunTimeSelectionTable(substitutionModel, dictionary);
37 }
38 
42 
43 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
44 
46 {
47  return KEY_BEGIN + w + KEY_END;
48 }
49 
50 
52 {
53  return stringOps::upper(stringOps::trim(str));
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 
100 
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 
117 bool 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 
156 Foam::substitutionModel::substitutionModel
157 (
158  const dictionary& dict,
159  const Time& time
160 )
161 :
162  dict_(dict),
163  time_(time)
164 {}
165 
166 
167 // ************************************************************************* //
static bool replaceBuiltin(const word &key, string &str)
Replace key in string.
dictionary dict
static std::string::size_type length(const char *s)
Length of the character sequence (with nullptr protection)
Definition: string.H:259
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
label k
Boltzmann constant.
static void setBuiltinStr(const word &key, const string &value)
Set a builtin to the hash table.
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:229
static void addBuiltinStr(const word &key, const string &value)
Add a builtin to the hash table - does not overwrite.
dimensionedScalar pos(const dimensionedScalar &ds)
A class for handling words, derived from Foam::string.
Definition: word.H:63
string trim(const std::string &s)
Return string trimmed of leading and trailing whitespace.
static const word KEY_BEGIN
Keyword starting characters.
dimensionedScalar pos0(const dimensionedScalar &ds)
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
defineTypeNameAndDebug(combustionModel, 0)
static HashTable< string > builtin_
Built-in substitutions.
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
static bool containsBuiltin(const word &key)
Return true if key is builtin.
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
Definition: foamGltfBase.H:105
static void writeBuiltins(Ostream &os)
Write all builtins to stream.
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
static wordList getKeys(string &buffer)
Return all keys from a string buffer.
static const word KEY_END
Keyword ending characters.
List< Key > toc() const
The table of contents (the keys) in unsorted order.
Definition: HashTable.C:141
static word cleanKey(const string &str)
Clean the key text.
static string keyify(const word &w)
Return a key representation from a word.
A class for handling character strings derived from std::string.
Definition: string.H:73
Namespace for OpenFOAM.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28