stringOpsEvaluate.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 "stringOpsEvaluate.H"
29 #include "stringOps.H"
30 #include "StringStream.H"
31 #include "fieldExprDriver.H"
32 #include "error.H"
33 
34 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
35 
37 (
38  label fieldWidth,
39  const std::string& str,
40  size_t pos,
41  size_t len
42 )
43 {
44  if (fieldWidth < 1)
45  {
46  fieldWidth = 1;
47  }
48 
50 
51  const auto trimPoints = stringOps::findTrim(str, pos, len);
52 
53  pos = trimPoints.first;
54  len = (trimPoints.second - trimPoints.first);
55 
56  if (!len)
57  {
58  return string();
59  }
60 
62 
64  {
65  expressions::fieldExprDriver driver(fieldWidth);
66  driver.parse(str, pos, len);
67  result = std::move(driver.result());
68  }
69 
70  if (!result.hasValue() || !result.size())
71  {
72  InfoErr
73  << "Failed evaluation: "
74  << str.substr(pos, len) << nl;
75 
76  return string();
77  }
78 
80  if (result.size() <= 1)
81  {
82  result.writeValue(os);
83  }
84  else
85  {
86  result.writeField(os);
87  }
88 
89  return os.str();
90 }
91 
92 
94 (
95  const std::string& str,
96  size_t pos,
97  size_t len
98 )
99 {
100  return stringOps::evaluate(1, str, pos, len);
101 }
102 
103 
104 // ************************************************************************* //
stringOpsEvaluate.H
String expression evaluation.
Foam::expressions::exprResult::size
label size() const
The field or object size.
Definition: exprResultI.H:281
Foam::expressions::fieldExpr::parseDriver
Driver for generic primitive field expressions.
Definition: fieldExprDriver.H:82
StringStream.H
Input/output from string buffers.
Foam::expressions::exprDriver::result
const exprResult & result() const noexcept
Const access to expression result.
Definition: exprDriver.H:390
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::InfoErr
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
Foam::expressions::exprResult
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:124
error.H
Foam::expressions::exprResult::writeField
void writeField(Ostream &os, const word &keyword="") const
Write the field, optionally as an entry.
Definition: exprResult.C:629
Foam::expressions::exprResult::writeValue
void writeValue(Ostream &os) const
Write the single value, or the first value from field.
Definition: exprResult.C:661
Foam::stringOps::findTrim
std::pair< size_t, size_t > findTrim(const std::string &s, size_t pos=0, size_t len=std::string::npos)
Find (first, last) non-space locations in string or sub-string.
Foam::expressions::exprResult::hasValue
bool hasValue() const
Has a value?
Definition: exprResultI.H:228
os
OBJstream os(runTime.globalPath()/outputName)
fieldExprDriver.H
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::OStringStream
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:227
stringOps.H
Foam::expressions::fieldExpr::parseDriver::parse
virtual unsigned parse(const std::string &expr, size_t pos=0, size_t len=std::string::npos)
Execute the parser.
Definition: fieldExprDriver.C:91
Foam::stringOps::evaluate
string evaluate(label fieldWidth, const std::string &s, size_t pos=0, size_t len=std::string::npos)
String evaluation with specified (positive, non-zero) field width.
Definition: stringOpsEvaluate.C:37
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177