Scalar.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30
31namespace Foam
32{
33
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
36const char* const pTraits<Scalar>::typeName = "scalar";
37const char* const pTraits<Scalar>::componentNames[] = { "" };
38
39const Scalar pTraits<Scalar>::zero = 0.0;
40const Scalar pTraits<Scalar>::one = 1.0;
46
47
48// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49
51{
52 is >> p_;
53}
54
55
56// * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
57
58word name(const Scalar val)
59{
60 std::ostringstream buf;
61 buf << val;
62
63 return word(buf.str(), false); // Needs no stripping
64}
65
66
67Scalar ScalarRead(const char* buf)
68{
69 char* endptr = nullptr;
70 errno = 0;
71 const auto parsed = ScalarConvert(buf, &endptr);
72
73 const parsing::errorType err =
74 (
75 (parsed < -ScalarVGREAT || parsed > ScalarVGREAT)
77 : parsing::checkConversion(buf, endptr)
78 );
79
80 if (err != parsing::errorType::NONE)
81 {
82 FatalIOErrorInFunction("unknown")
83 << parsing::errorNames[err] << " '" << buf << "'"
85 }
86
87 // Round underflow to zero
88 return
89 (
90 (parsed > -ScalarVSMALL && parsed < ScalarVSMALL)
91 ? 0
92 : Scalar(parsed)
93 );
94}
95
96
97bool ScalarRead(const char* buf, Scalar& val)
98{
99 char* endptr = nullptr;
100 errno = 0;
101 const auto parsed = ScalarConvert(buf, &endptr);
102
103 // Round underflow to zero
104 val =
105 (
106 (parsed >= -ScalarVSMALL && parsed <= ScalarVSMALL)
107 ? 0
108 : Scalar(parsed)
109 );
110
111 return
112 (
113 (parsed < -ScalarVGREAT || parsed > ScalarVGREAT)
114 ? false
116 );
117}
118
119
120// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
121
123{
124 Scalar val(0);
125 is >> val;
126
127 return val;
128}
129
130
132{
133 token t(is);
134
135 if (!t.good())
136 {
138 << "Bad token - could not get scalar value"
139 << exit(FatalIOError);
140 is.setBad();
141 return is;
142 }
143
144 // Accept separated '-' (or '+') while expecting a number.
145 // This can arise during dictionary expansions (Eg, -$value)
146
147 char prefix = 0;
148 if (t.isPunctuation())
149 {
150 prefix = t.pToken();
151 if (prefix == token::PLUS || prefix == token::MINUS)
152 {
153 is >> t;
154 }
155 }
156
157 if (t.isNumber())
158 {
159 val =
160 (
161 (prefix == token::MINUS)
162 ? (0 - t.number())
163 : t.number()
164 );
165 }
166 else
167 {
169 << "Wrong token type - expected scalar value, found ";
170 if (prefix == token::PLUS || prefix == token::MINUS)
171 {
172 FatalIOError << '\'' << prefix << "' followed by ";
173 }
175 is.setBad();
176 return is;
177 }
178
180 return is;
181}
182
183
185{
186 os.write(val);
188 return os;
189}
190
191
192// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193
194} // End namespace Foam
195
196// ************************************************************************* //
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
void setBad()
Set stream state to be 'bad'.
Definition: IOstream.H:369
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
static const Scalar vsmall
Definition: Scalar.H:107
static const char *const componentNames[]
Definition: bool.H:104
static const complex rootMax
complex (ROOTVGREAT, ROOTVGREAT)
Definition: complex.H:295
static const complex min
complex (-VGREAT,-VGREAT)
Definition: complex.H:292
static const complex max
complex (VGREAT,VGREAT)
Definition: complex.H:293
static const complex rootMin
complex (-ROOTVGREAT, -ROOTVGREAT)
Definition: complex.H:294
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A token holds an item read from Istream.
Definition: token.H:69
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:587
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
@ PLUS
Addition [isseparator].
Definition: token.H:138
@ MINUS
Subtract or start of negative number.
Definition: token.H:139
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:485
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:405
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
scalar number() const
Return label, float or double value.
Definition: tokenI.H:593
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define ScalarROOTVGREAT
Definition: doubleScalar.C:44
#define ScalarVSMALL
Definition: doubleScalar.C:43
#define ScalarVGREAT
Definition: doubleScalar.C:42
#define ScalarConvert
Definition: doubleScalar.C:48
#define Scalar
Definition: doubleScalar.C:41
#define ScalarRead
Definition: doubleScalar.C:46
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
errorType
Enumeration for possible parsing error.
Definition: parsing.H:58
@ NONE
No error encountered.
const Foam::Enum< errorType > errorNames
Strings corresponding to the errorType.
errorType checkConversion(const char *buf, char *endptr)
Sanity check after strtof, strtod, etc.
Definition: parsingI.H:29
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
static const char *const typeName
The type name used in ensight case files.