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 -------------------------------------------------------------------------------
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 "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  {
146  tok.setType(token::tokenType::VERBATIM);
147  }
148 
149  os.write(tok);
150  os.endEntry();
151  }
152 
153  return ok;
154 }
155 
156 
157 // ************************************************************************* //
Foam::expressions::exprString::getEntry
static exprString getEntry(const word &keyword, const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:52
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::stringOps::inplaceTrim
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1067
exprString.H
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::expressions::exprString::getOptional
static exprString getOptional(const word &keyword, const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:67
Foam::stringOps::inplaceRemoveComments
void inplaceRemoveComments(std::string &s)
Remove C/C++ comments inplace.
Definition: stringOps.C:1088
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
expressionEntry.H
Foam::expressions::exprString::expand
void expand(const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:83
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::expressions::exprString::readEntry
bool readEntry(const word &keyword, const dictionary &dict, bool mandatory=true, const bool stripComments=true)
Definition: exprString.C:103
Foam::expressions::exprString::writeEntry
bool writeEntry(const word &keyword, Ostream &os, bool writeEmpty=false) const
Definition: exprString.C:126
clear
patchWriters clear()
Foam::expressions::exprString
Definition: exprString.H:60
Foam::expressions::exprString
A variant of Foam::string with expansion of dictionary variables into a comma-separated form.
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:81
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::expressions::exprString::inplaceExpand
static void inplaceExpand(std::string &str, const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:35
Foam::stringOps::expand
string expand(const std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:718
stringOps.H
Foam::token::setType
bool setType(const tokenType tokType) noexcept
Change the token type, for similar types.
Definition: tokenI.H:317
Foam::expressions::exprString::trim
void trim()
Inplace trim leading and trailing whitespace.
Definition: exprString.C:96