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 -------------------------------------------------------------------------------
11 License
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 
31 namespace Foam
32 {
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 const char* const pTraits<Scalar>::typeName = "scalar";
37 const char* const pTraits<Scalar>::componentNames[] = { "" };
38 
39 const Scalar pTraits<Scalar>::zero = 0.0;
40 const Scalar pTraits<Scalar>::one = 1.0;
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
51 {
52  is >> p_;
53 }
54 
55 
56 // * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
57 
58 word name(const Scalar val)
59 {
60  // Caution std::to_string(double) is locale sensitive!
61  std::ostringstream buf;
62  buf << val;
63 
64  return word(buf.str(), false); // Needs no stripping
65 }
66 
67 
68 Scalar ScalarRead(const char* buf)
69 {
70  char* endptr = nullptr;
71  errno = 0;
72  const auto parsed = ScalarConvert(buf, &endptr);
73 
74  const parsing::errorType err =
75  (
76  (parsed < -ScalarVGREAT || parsed > ScalarVGREAT)
78  : parsing::checkConversion(buf, endptr)
79  );
80 
81  if (err != parsing::errorType::NONE)
82  {
83  FatalIOErrorInFunction("unknown")
84  << parsing::errorNames[err] << " '" << buf << "'"
85  << exit(FatalIOError);
86  }
87 
88  // Round underflow to zero
89  return
90  (
91  (parsed > -ScalarVSMALL && parsed < ScalarVSMALL)
92  ? 0
93  : Scalar(parsed)
94  );
95 }
96 
97 
98 bool ScalarRead(const char* buf, Scalar& val)
99 {
100  char* endptr = nullptr;
101  errno = 0;
102  const auto parsed = ScalarConvert(buf, &endptr);
103 
104  // Round underflow to zero
105  val =
106  (
107  (parsed >= -ScalarVSMALL && parsed <= ScalarVSMALL)
108  ? 0
109  : Scalar(parsed)
110  );
111 
112  return
113  (
114  (parsed < -ScalarVGREAT || parsed > ScalarVGREAT)
115  ? false
117  );
118 }
119 
120 
121 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
122 
123 Scalar ScalarRead(Istream& is)
124 {
125  Scalar val(0);
126  is >> val;
127 
128  return val;
129 }
130 
131 
132 Istream& operator>>(Istream& is, Scalar& val)
133 {
134  token t(is);
135 
136  if (!t.good())
137  {
139  << "Bad token - could not get scalar value"
140  << exit(FatalIOError);
141  is.setBad();
142  return is;
143  }
144 
145  // Accept separated '-' (or '+') while expecting a number.
146  // This can arise during dictionary expansions (Eg, -$value)
147 
148  char prefix = 0;
149  if (t.isPunctuation())
150  {
151  prefix = t.pToken();
152  if (prefix == token::PLUS || prefix == token::MINUS)
153  {
154  is >> t;
155  }
156  }
157 
158  if (t.isNumber())
159  {
160  val =
161  (
162  (prefix == token::MINUS)
163  ? (0 - t.number())
164  : t.number()
165  );
166  }
167  else
168  {
170  << "Wrong token type - expected scalar value, found ";
171  if (prefix == token::PLUS || prefix == token::MINUS)
172  {
173  FatalIOError << '\'' << prefix << "' followed by ";
174  }
175  FatalIOError << t.info() << exit(FatalIOError);
176  is.setBad();
177  return is;
178  }
179 
180  is.check(FUNCTION_NAME);
181  return is;
182 }
183 
184 
185 Ostream& operator<<(Ostream& os, const Scalar val)
186 {
187  os.write(val);
189  return os;
190 }
191 
192 
193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
194 
195 } // End namespace Foam
196 
197 // ************************************************************************* //
Subtract or start of negative number.
Definition: token.H:145
static const Scalar zero
Definition: Scalar.H:109
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
virtual Ostream & write(const char c) override
Write character.
Definition: OBJstream.C:69
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
No error encountered.
#define ScalarVSMALL
Definition: doubleScalar.C:36
static const char *const typeName
Definition: Scalar.H:107
#define ScalarVGREAT
Definition: doubleScalar.C:35
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
#define ScalarConvert
Definition: doubleScalar.C:41
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
static const Scalar max
Definition: Scalar.H:111
errorType checkConversion(const char *buf, char *endptr)
Sanity check after strtof, strtod, etc.
Definition: parsingI.H:22
pTraits(const Base &obj)
Copy construct from base class.
Definition: pTraits.H:86
Addition [isseparator].
Definition: token.H:144
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
static const Scalar min
Definition: Scalar.H:112
static const char *const componentNames[]
Definition: Scalar.H:108
errorType
Enumeration for possible parsing error.
Definition: parsing.H:55
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
#define Scalar
Definition: doubleScalar.C:34
Scalar ScalarRead(const char *buf)
Parse entire buffer as a float/double, skipping leading/trailing whitespace.
Definition: Scalar.C:61
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
#define ScalarROOTVGREAT
Definition: doubleScalar.C:37
static const Scalar one
Definition: Scalar.H:110
Namespace for OpenFOAM.
const Foam::Enum< errorType > errorNames
Strings corresponding to the errorType.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...