uint32IO.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) 2014-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2020 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 #include "uint32.H"
30 #include "parsing.H"
31 #include "IOstreams.H"
32 #include <cinttypes>
33 #include <cmath>
34 
35 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
36 
37 uint32_t Foam::readUint32(const char* buf)
38 {
39  char *endptr = nullptr;
40  errno = 0;
41  const uintmax_t parsed = ::strtoumax(buf, &endptr, 10);
42 
43  const uint32_t val = uint32_t(parsed);
44 
45  const parsing::errorType err =
46  (
47  (parsed > UINT32_MAX)
48  ? parsing::errorType::RANGE
49  : parsing::checkConversion(buf, endptr)
50  );
51 
52  if (err != parsing::errorType::NONE)
53  {
54  FatalIOErrorInFunction("unknown")
55  << parsing::errorNames[err] << " '" << buf << "'"
56  << exit(FatalIOError);
57  }
58 
59  return val;
60 }
61 
62 
63 bool Foam::readUint32(const char* buf, uint32_t& val)
64 {
65  char *endptr = nullptr;
66  errno = 0;
67  const uintmax_t parsed = ::strtoumax(buf, &endptr, 10);
68 
69  val = uint32_t(parsed);
70 
71  return
72  (
73  (parsed > UINT32_MAX)
74  ? false
75  : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
76  );
77 }
78 
79 
81 {
82  uint32_t val(0);
83  is >> val;
84 
85  return val;
86 }
87 
88 
90 {
91  token t(is);
92 
93  if (!t.good())
94  {
96  << "Bad token - could not get uint32"
97  << exit(FatalIOError);
98  is.setBad();
99  return is;
100  }
101 
102  if (t.isLabel())
103  {
104  val = uint32_t(t.labelToken());
105  }
106  else if (t.isScalar())
107  {
108  const scalar sval(t.scalarToken());
109  const uintmax_t parsed = uintmax_t(std::round(sval));
110  val = 0 + uint32_t(parsed);
111 
112  // Accept integral floating-point values.
113  // Eg, from string expression evaluation (#1696)
114 
115  if ((sval < -1e-4) || parsed > UINT32_MAX)
116  {
118  << "Expected label (uint32), value out-of-range "
119  << t.info()
120  << exit(FatalIOError);
121  is.setBad();
122  return is;
123  }
124  else if (1e-4 < std::abs(sval - scalar(parsed)))
125  {
127  << "Expected label (uint32), found non-integral value "
128  << t.info()
129  << exit(FatalIOError);
130  is.setBad();
131  return is;
132  }
133  }
134  else
135  {
137  << "Wrong token type - expected label (uint32), found "
138  << t.info()
139  << exit(FatalIOError);
140  is.setBad();
141  return is;
142  }
143 
144  is.check(FUNCTION_NAME);
145  return is;
146 }
147 
148 
149 Foam::Ostream& Foam::operator<<(Ostream& os, const uint32_t val)
150 {
151  os.write(label(val));
152  os.check(FUNCTION_NAME);
153  return os;
154 }
155 
156 
157 // ************************************************************************* //
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::parsing::checkConversion
errorType checkConversion(const char *buf, char *endptr)
Sanity check after strtof, strtod, etc.
Definition: parsingI.H:29
parsing.H
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::readUint32
uint32_t readUint32(Istream &is)
Read uint32_t from stream.
Definition: uint32IO.C:80
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::parsing::errorNames
const Foam::Enum< errorType > errorNames
Strings corresponding to the errorType.
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:58
os
OBJstream os(runTime.globalPath()/outputName)
Foam::token::isScalar
bool isScalar() const noexcept
Token is FLOAT or DOUBLE.
Definition: tokenI.H:561
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:405
Foam::token::scalarToken
scalar scalarToken() const
Return float or double value.
Definition: tokenI.H:571
uint32.H
32bit unsigned integer
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::IOstream::setBad
void setBad()
Set stream state to be 'bad'.
Definition: IOstream.H:369
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56