exprString.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) 2019-2021 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 "exprString.H"
29#include "stringOps.H"
30#include "expressionEntry.H"
31
32// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33
35(
36 std::string& str,
37 const dictionary& dict,
38 const bool stripComments
39)
40{
41 if (stripComments)
42 {
44 }
45
47}
48
49
52(
53 const word& key,
54 const dictionary& dict,
55 const bool stripComments
56)
57{
58 exprString expr;
59 expr.readEntry(key, dict, true, stripComments); // mandatory
60
61 return expr;
62}
63
64
67(
68 const word& key,
69 const dictionary& dict,
70 const bool stripComments
71)
72{
73 exprString expr;
74 expr.readEntry(key, dict, false, stripComments); // optional
75
76 return expr;
77}
78
79
80// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81
83(
84 const dictionary& dict,
85 const bool stripComments
86)
87{
88 inplaceExpand(*this, dict, stripComments);
89
90 #ifdef FULLDEBUG
91 (void)valid();
92 #endif
93}
94
95
97{
99}
100
101
103(
104 const word& keyword,
105 const dictionary& dict,
106 bool mandatory,
107 const bool stripComments
108)
109{
110 const bool ok = dict.readEntry(keyword, *this, keyType::LITERAL, mandatory);
111
112 if (ok && !empty())
113 {
114 this->expand(dict, stripComments); // strip comments
115 }
116 else
117 {
118 clear();
119 }
120
121 return ok;
122}
123
124
126(
127 const word& keyword,
128 Ostream& os,
129 bool writeEmpty
130) const
131{
132 const bool ok = (writeEmpty || !empty());
133
134 if (ok)
135 {
136 if (!keyword.empty())
137 {
138 os.writeKeyword(keyword);
139 }
140
141 // Write as regular or verbatim string
142
143 token tok(*this);
144 if (!empty())
145 {
147 }
148
149 os.write(tok);
150 os.endEntry();
151 }
152
153 return ok;
154}
155
156
157// ************************************************************************* //
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
static void inplaceExpand(std::string &s, const dictionary &dict)
Inplace expand expression with dictionary variables/entries.
void trim()
Inplace trim leading and trailing whitespace.
Definition: exprString.C:96
static exprString getOptional(const word &keyword, const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:67
static void inplaceExpand(std::string &str, const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:35
void expand(const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:83
static exprString getEntry(const word &keyword, const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:52
bool readEntry(const word &keyword, const dictionary &dict, bool mandatory=true, const bool stripComments=true)
Definition: exprString.C:103
bool writeEntry(const word &keyword, Ostream &os, bool writeEmpty=false) const
Definition: exprString.C:126
@ LITERAL
String literal.
Definition: keyType.H:81
A token holds an item read from Istream.
Definition: token.H:69
@ VERBATIM
Definition: token.H:100
bool setType(const tokenType tokType) noexcept
Change the token type, for similar types.
Definition: tokenI.H:317
A class for handling words, derived from Foam::string.
Definition: word.H:68
patchWriters clear()
OBJstream os(runTime.globalPath()/outputName)
A variant of Foam::string with expansion of dictionary variables into a comma-separated form.
void inplaceRemoveComments(std::string &s)
Remove C/C++ comments inplace.
Definition: stringOps.C:1088
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1067
dictionary dict