uint64IO.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 "uint64.H"
30 #include "parsing.H"
31 #include "IOstreams.H"
32 #include <cinttypes>
33 #include <cmath>
34 
35 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
36 
37 uint64_t Foam::readUint64(const char* buf)
38 {
39  char *endptr = nullptr;
40  errno = 0;
41  const uintmax_t parsed = ::strtoumax(buf, &endptr, 10);
42 
43  const uint64_t val = uint64_t(parsed);
44 
45  const parsing::errorType err =
46  (
47  (parsed > UINT64_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::readUint64(const char* buf, uint64_t& val)
64 {
65  char *endptr = nullptr;
66  errno = 0;
67  const uintmax_t parsed = ::strtoumax(buf, &endptr, 10);
68 
69  val = uint64_t(parsed);
70 
71  return
72  (
73  (parsed > UINT64_MAX)
74  ? false
75  : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
76  );
77 }
78 
79 
81 {
82  token t(is);
83 
84  if (!t.good())
85  {
87  << "Bad token - could not get uint64"
88  << exit(FatalIOError);
89  is.setBad();
90  return is;
91  }
92 
93  if (t.isLabel())
94  {
95  val = uint64_t(t.labelToken());
96  }
97  else if (t.isScalar())
98  {
99  const scalar sval(t.scalarToken());
100  const uintmax_t parsed = uintmax_t(std::round(sval));
101  val = 0 + uint64_t(parsed);
102 
103  // Accept integral floating-point values.
104  // Eg, from string expression evaluation (#1696)
105 
106  if ((sval < -1e-4) || parsed > UINT64_MAX)
107  {
109  << "Expected label (uint64), value out-of-range "
110  << t.info()
111  << exit(FatalIOError);
112  is.setBad();
113  return is;
114  }
115  else if (1e-4 < std::abs(sval - scalar(parsed)))
116  {
118  << "Expected label (uint64), found non-integral value "
119  << t.info()
120  << exit(FatalIOError);
121  is.setBad();
122  return is;
123  }
124  }
125  else
126  {
128  << "Wrong token type - expected label (uint64), found "
129  << t.info()
130  << exit(FatalIOError);
131  is.setBad();
132  return is;
133  }
134 
135  is.check(FUNCTION_NAME);
136  return is;
137 }
138 
139 
141 {
142  uint64_t val(0);
143  is >> val;
144 
145  return val;
146 }
147 
148 
149 Foam::Ostream& Foam::operator<<(Ostream& os, const uint64_t val)
150 {
151  os.write(label(val));
152  os.check(FUNCTION_NAME);
153  return os;
154 }
155 
156 
157 #ifdef __APPLE__
158 Foam::Ostream& Foam::operator<<(Ostream& os, const unsigned long val)
159 {
160  os << uint64_t(val);
161  return os;
162 }
163 #endif
164 
165 
166 // ************************************************************************* //
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:477
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::token::isScalar
bool isScalar() const
Token is FLOAT or DOUBLE.
Definition: tokenI.H:525
Foam::readUint64
uint64_t readUint64(Istream &is)
Read uint64_t from stream.
Definition: uint64IO.C:140
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::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::token::good
bool good() const
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:389
Foam::token::isLabel
bool isLabel() const
Token is LABEL.
Definition: tokenI.H:471
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:523
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:56
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::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::token::scalarToken
scalar scalarToken() const
Return float or double value.
Definition: tokenI.H:535
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:265
Foam::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.H:360
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:392
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
uint64.H
64bit unsigned integer