scalarImpl.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 // Could also check if 'Scalar' is defined...
30 #ifndef Foam_use_scalarImpl_code
31 #error "scalarImpl.C" is only to be included internally
32 #endif
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
41 const char* const pTraits<Scalar>::typeName = "scalar";
42 const char* const pTraits<Scalar>::componentNames[] = { "" };
43 
44 const Scalar pTraits<Scalar>::zero = 0.0;
45 const Scalar pTraits<Scalar>::one = 1.0;
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
56 {
57  is >> p_;
58 }
59 
60 
61 // * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
62 
63 word name(const Scalar val)
64 {
65  // Caution std::to_string(double) is locale sensitive!
66  std::ostringstream buf;
67  buf << val;
68 
69  return word(buf.str(), false); // Needs no stripping
70 }
71 
72 
73 Scalar ScalarRead(const char* buf)
74 {
75  char* endptr = nullptr;
76  errno = 0;
77  const auto parsed = ScalarConvert(buf, &endptr);
78 
79  const parsing::errorType err =
80  (
81  (parsed < -ScalarVGREAT || parsed > ScalarVGREAT)
83  : parsing::checkConversion(buf, endptr)
84  );
85 
86  if (err != parsing::errorType::NONE)
87  {
88  FatalIOErrorInFunction("unknown")
89  << parsing::errorNames[err] << " '" << buf << "'"
90  << exit(FatalIOError);
91  }
92 
93  // Round underflow to zero
94  return
95  (
96  (parsed > -ScalarVSMALL && parsed < ScalarVSMALL)
97  ? 0
98  : Scalar(parsed)
99  );
100 }
101 
102 
103 bool ScalarRead(const char* buf, Scalar& val)
104 {
105  char* endptr = nullptr;
106  errno = 0;
107  const auto parsed = ScalarConvert(buf, &endptr);
108 
109  // Round underflow to zero
110  val =
111  (
112  (parsed >= -ScalarVSMALL && parsed <= ScalarVSMALL)
113  ? 0
114  : Scalar(parsed)
115  );
116 
117  return
118  (
119  (parsed < -ScalarVGREAT || parsed > ScalarVGREAT)
120  ? false
122  );
123 }
124 
125 
126 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
127 
128 Scalar ScalarRead(Istream& is)
129 {
130  Scalar val(0);
131  is >> val;
132 
133  return val;
134 }
135 
136 
137 Istream& operator>>(Istream& is, Scalar& val)
138 {
139  token t(is);
140 
141  if (!t.good())
142  {
144  << "Bad token - could not get scalar value"
145  << exit(FatalIOError);
146  is.setBad();
147  return is;
148  }
149 
150  // Accept separated '-' (or '+') while expecting a number.
151  // This can arise during dictionary expansions (Eg, -$value)
152 
153  char prefix = 0;
154  if (t.isPunctuation())
155  {
156  prefix = t.pToken();
157  if (prefix == token::PLUS || prefix == token::MINUS)
158  {
159  is >> t;
160  }
161  }
162 
163  if (t.isNumber())
164  {
165  val =
166  (
167  (prefix == token::MINUS)
168  ? (0 - t.number())
169  : t.number()
170  );
171  }
172  else
173  {
175  << "Wrong token type - expected scalar value, found ";
176  if (prefix == token::PLUS || prefix == token::MINUS)
177  {
178  FatalIOError << '\'' << prefix << "' followed by ";
179  }
180  FatalIOError << t.info() << exit(FatalIOError);
181  is.setBad();
182  return is;
183  }
184 
185  is.check(FUNCTION_NAME);
186  return is;
187 }
188 
189 
190 Ostream& operator<<(Ostream& os, const Scalar val)
191 {
192  os.write(val);
194  return os;
195 }
196 
197 
198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199 
200 } // End namespace Foam
201 
202 // ************************************************************************* //
Subtract or start of negative number.
Definition: token.H:145
static const Scalar zero
Definition: scalarImpl.H:115
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: scalarImpl.H:113
#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:61
static const Scalar max
Definition: scalarImpl.H:117
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:72
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: scalarImpl.H:118
static const char *const componentNames[]
Definition: scalarImpl.H:114
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: scalarImpl.C:66
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:629
#define ScalarROOTVGREAT
Definition: doubleScalar.C:37
static const Scalar one
Definition: scalarImpl.H:116
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 ...