environmentVariable.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 "environmentVariable.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace substitutionModels
36 {
37  defineTypeNameAndDebug(environmentVariable, 0);
39  (
40  substitutionModel,
41  environmentVariable,
42  dictionary
43  );
44 }
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
50 Foam::substitutionModels::environmentVariable::environmentVariable
51 (
52  const dictionary& dict,
53  const Time& time
54 )
55 :
56  substitutionModel(dict, time),
57  entries_()
58 {
59  // Populate entries
60  const dictionary& entriesDict = dict.subDict("entries");
61  for (const auto& e : entriesDict)
62  {
63  entries_.insert(cleanKey(e.keyword()), string(e.stream()));
64  }
65 }
66 
67 
68 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69 
71 (
72  const word& keyName
73 ) const
74 {
75  return entries_.found(keyName);
76 }
77 
78 
80 (
81  const word& key,
82  string& buffer
83 ) const
84 {
85  if (!valid(key)) return false;
86 
87  const string env(Foam::getEnv(entries_[key]));
88 
89  buffer.replaceAll(keyify(key), env);
90 
91  return true;
92 }
93 
94 
96 {
97  return entries_.sortedToc();
98 }
99 
100 
101 // ************************************************************************* //
dictionary dict
virtual wordList keys() const
Return a word list of the keys.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Base class for substitution models.
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: POSIX.C:341
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.
bool env(const std::string &envName)
Deprecated(2020-05) check for existence of environment variable.
Definition: OSspecific.H:96
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool valid(const word &keyName) const
Return true of model applies to this keyName.
auto key(const Type &t) -> std::enable_if_t< std::is_enum_v< Type >, std::underlying_type_t< Type > >
Definition: foamGltfBase.H:103
virtual bool apply(const word &key, string &buffer) const
Apply substitutions to this string buffer.
addToRunTimeSelectionTable(substitutionModel, dictionaryValue, dictionary)
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
defineTypeNameAndDebug(dictionaryValue, 0)
static word cleanKey(const string &str)
Clean the key text.
Namespace for OpenFOAM.