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-2019 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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 
49 :
50  p_(val)
51 {}
52 
53 
55 {
56  is >> p_;
57 }
58 
59 
60 // * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
61 
62 word name(const Scalar val)
63 {
64  std::ostringstream buf;
65  buf << val;
66 
67  return word(buf.str(), false); // Needs no stripping
68 }
69 
70 
71 Scalar ScalarRead(const char* buf)
72 {
73  char* endptr = nullptr;
74  errno = 0;
75  const auto parsed = ScalarConvert(buf, &endptr);
76 
77  const parsing::errorType err =
78  (
79  (parsed < -ScalarVGREAT || parsed > ScalarVGREAT)
81  : parsing::checkConversion(buf, endptr)
82  );
83 
84  if (err != parsing::errorType::NONE)
85  {
86  FatalIOErrorInFunction("unknown")
87  << parsing::errorNames[err] << " '" << buf << "'"
88  << exit(FatalIOError);
89  }
90 
91  // Round underflow to zero
92  return
93  (
94  (parsed > -ScalarVSMALL && parsed < ScalarVSMALL)
95  ? 0
96  : Scalar(parsed)
97  );
98 }
99 
100 
101 bool ScalarRead(const char* buf, Scalar& val)
102 {
103  char* endptr = nullptr;
104  errno = 0;
105  const auto parsed = ScalarConvert(buf, &endptr);
106 
107  // Round underflow to zero
108  val =
109  (
110  (parsed >= -ScalarVSMALL && parsed <= ScalarVSMALL)
111  ? 0
112  : Scalar(parsed)
113  );
114 
115  return
116  (
117  (parsed < -ScalarVGREAT || parsed > ScalarVGREAT)
118  ? false
120  );
121 }
122 
123 
124 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
125 
127 {
128  Scalar val(0);
129  is >> val;
130 
131  return val;
132 }
133 
134 
136 {
137  token t(is);
138 
139  if (!t.good())
140  {
142  << "Bad token - could not get scalar value"
143  << exit(FatalIOError);
144  is.setBad();
145  return is;
146  }
147 
148  if (t.isNumber())
149  {
150  val = t.number();
151  }
152  else
153  {
155  << "Wrong token type - expected scalar value, found "
156  << t.info()
157  << exit(FatalIOError);
158  is.setBad();
159  return is;
160  }
161 
162  is.check(FUNCTION_NAME);
163  return is;
164 }
165 
166 
168 {
169  os.write(val);
170  os.check(FUNCTION_NAME);
171  return os;
172 }
173 
174 
175 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
176 
177 } // End namespace Foam
178 
179 // ************************************************************************* //
Foam::pTraits< Scalar >::vsmall
static const Scalar vsmall
Definition: Scalar.H:107
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::ScalarRead
Scalar ScalarRead(const char *buf)
Parse entire buffer as a float/double, skipping leading/trailing whitespace.
Definition: Scalar.C:71
Foam::parsing::checkConversion
errorType checkConversion(const char *buf, char *endptr)
Sanity check after strtof, strtod, etc.
Definition: parsingI.H:29
Foam::parsing::errorType::RANGE
Range error.
Foam::pTraits< Scalar >::min
static const Scalar min
Definition: Scalar.H:104
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Scalar
#define Scalar
Definition: doubleScalar.C:41
Foam::token::isNumber
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:561
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::parsing::errorType::NONE
No error encountered.
ScalarVSMALL
#define ScalarVSMALL
Definition: doubleScalar.C:43
Foam::token::number
scalar number() const
Return label, float or double value.
Definition: tokenI.H:567
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:551
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::pTraits< Scalar >::typeName
static const char *const typeName
Definition: Scalar.H:99
Foam::pTraits::pTraits
pTraits(const PrimitiveType &p)
Copy construct from primitive.
Definition: pTraits.H:63
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::pTraits< Scalar >::rootMax
static const Scalar rootMax
Definition: Scalar.H:105
Foam::parsing::errorNames
const Foam::Enum< errorType > errorNames
Strings corresponding to the errorType.
ScalarROOTVGREAT
#define ScalarROOTVGREAT
Definition: doubleScalar.C:44
Foam::parsing::errorType
errorType
Enumeration for possible parsing error.
Definition: parsing.H:57
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::token::good
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:399
Foam::pTraits< Scalar >::one
static const Scalar one
Definition: Scalar.H:102
Foam::pTraits< Scalar >::max
static const Scalar max
Definition: Scalar.H:103
Foam::pTraits< Scalar >::componentNames
static const char *const componentNames[]
Definition: Scalar.H:100
ScalarVGREAT
#define ScalarVGREAT
Definition: doubleScalar.C:42
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.H:360
ScalarConvert
#define ScalarConvert
Definition: doubleScalar.C:48
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::pTraits< Scalar >::rootMin
static const Scalar rootMin
Definition: Scalar.H:106
Foam::pTraits< Scalar >::zero
static const Scalar zero
Definition: Scalar.H:101