Loading...
Searching...
No Matches
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,2024 OpenFOAM Foundation
9 Copyright (C) 2017-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
37uint64_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)
49 : parsing::checkConversion(buf, endptr)
50 );
51
52 if (err != parsing::errorType::NONE)
53 {
54 FatalIOErrorInFunction("unknown")
55 << parsing::errorNames[err] << " '" << buf << "'"
57 }
58
59 return val;
60}
61
62
63bool 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
76 );
77}
78
79
80uint64_t Foam::readUint64(Istream& is)
81{
82 uint64_t val(0);
83 is >> val;
84
85 return val;
86}
87
88
89Foam::Istream& Foam::operator>>(Istream& is, uint64_t& val)
90{
91 token t(is);
92
93 if (!t.good())
94 {
96 << "Bad token - could not get uint64"
97 << exit(FatalIOError);
98 is.setBad();
99 return is;
100 }
101
102 if (t.is_uint64())
103 {
104 val = t.uint64Token();
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 + uint64_t(parsed);
111
112 // Accept integral floating-point values.
113 // Eg, from string expression evaluation (#1696)
114
115 if ((sval < -1e-4) || parsed > UINT64_MAX)
116 {
118 << "Expected label (uint64), 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 (uint64), 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 (uint64), found "
138 << t.info()
139 << exit(FatalIOError);
140 is.setBad();
141 return is;
143
144 is.check(FUNCTION_NAME);
145 return is;
146}
147
148
149Foam::Ostream& Foam::operator<<(Ostream& os, const uint64_t val)
150{
151 os.write(val);
152 os.check(FUNCTION_NAME);
153 return os;
154}
155
156
157#ifdef __APPLE__
158Foam::Ostream& Foam::operator<<(Ostream& os, const unsigned long val)
159{
160 os << uint64_t(val);
161 return os;
162}
163#endif
164
165
166// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void setBad() noexcept
Set stream state to be 'bad'.
Definition IOstream.H:488
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A token holds an item read from Istream.
Definition token.H:70
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition tokenI.H:584
bool isScalar() const noexcept
Token is FLOAT or DOUBLE.
Definition tokenI.H:971
bool is_uint64() const noexcept
Token is UNSIGNED_INTEGER_64 or is convertible to one.
Definition tokenI.H:812
scalar scalarToken() const
Return float or double value.
Definition tokenI.H:981
uint64_t uint64Token() const
Return int64 value, convert from other integer type or Error.
Definition tokenI.H:829
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition token.H:1253
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
errorType
Enumeration for possible parsing error.
Definition parsing.H:56
@ RANGE
Range error.
Definition parsing.H:59
@ NONE
No error encountered.
Definition parsing.H:57
const Foam::Enum< errorType > errorNames
Strings corresponding to the errorType.
errorType checkConversion(const char *buf, char *endptr)
Sanity check after strtof, strtod, etc.
Definition parsingI.H:22
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
uint64_t readUint64(Istream &is)
Read uint64_t from stream.
Definition uint64IO.C:73
volScalarField & e
64bit unsigned integer