int64IO.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 "int64.H"
30 #include "error.H"
31 #include "parsing.H"
32 #include "IOstreams.H"
33 #include <cinttypes>
34 #include <cmath>
35 
36 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
37 
38 int64_t Foam::readInt64(const char* buf)
39 {
40  char *endptr = nullptr;
41  errno = 0;
42  const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
43 
44  const int64_t val = int64_t(parsed);
45 
46  const parsing::errorType err =
47  (
48  (parsed < INT64_MIN || parsed > INT64_MAX)
49  ? parsing::errorType::RANGE
50  : parsing::checkConversion(buf, endptr)
51  );
52 
53  if (err != parsing::errorType::NONE)
54  {
55  FatalIOErrorInFunction("unknown")
56  << parsing::errorNames[err] << " '" << buf << "'"
57  << exit(FatalIOError);
58  }
59 
60  return val;
61 }
62 
63 
64 bool Foam::readInt64(const char* buf, int64_t& val)
65 {
66  char *endptr = nullptr;
67  errno = 0;
68  const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
69 
70  val = int64_t(parsed);
71 
72  return
73  (
74  (parsed < INT64_MIN || parsed > INT64_MAX)
75  ? false
76  : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
77  );
78 }
79 
80 
82 {
83  int64_t val(0);
84  is >> val;
85 
86  return val;
87 }
88 
89 
91 {
92  token t(is);
93 
94  if (!t.good())
95  {
97  << "Bad token - could not get int64"
98  << exit(FatalIOError);
99  is.setBad();
100  return is;
101  }
102 
103  if (t.isLabel())
104  {
105  val = int64_t(t.labelToken());
106  }
107  else if (t.isScalar())
108  {
109  const scalar sval(t.scalarToken());
110  const intmax_t parsed = intmax_t(std::round(sval));
111  val = 0 + int64_t(parsed);
112 
113  // Accept integral floating-point values.
114  // Eg, from string expression evaluation (#1696)
115 
116  if (parsed < INT64_MIN || parsed > INT64_MAX)
117  {
119  << "Expected integral (int64), value out-of-range "
120  << t.info()
121  << exit(FatalIOError);
122  is.setBad();
123  return is;
124  }
125  else if (1e-4 < std::abs(sval - scalar(parsed)))
126  {
128  << "Expected integral (int64), found non-integral value "
129  << t.info()
130  << exit(FatalIOError);
131  is.setBad();
132  return is;
133  }
134  }
135  else
136  {
138  << "Wrong token type - expected label (int64), found "
139  << t.info()
140  << exit(FatalIOError);
141  is.setBad();
142  return is;
143  }
144 
145  is.check(FUNCTION_NAME);
146  return is;
147 }
148 
149 
150 Foam::Ostream& Foam::operator<<(Ostream& os, const int64_t val)
151 {
152  os.write(label(val));
153  os.check(FUNCTION_NAME);
154  return os;
155 }
156 
157 
158 #if defined(__APPLE__)
159 Foam::Istream& Foam::operator>>(Istream& is, long& val)
160 {
161  return operator>>(is, reinterpret_cast<int64_t&>(val));
162 }
163 
164 Foam::Ostream& Foam::operator<<(Ostream& os, const long val)
165 {
166  return (os << int64_t(val));
167 }
168 #endif
169 
170 
171 // ************************************************************************* //
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:487
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
int64.H
64bit signed integer
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:481
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:68
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
error.H
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::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:51
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::token::isScalar
bool isScalar() const noexcept
Token is FLOAT or DOUBLE.
Definition: tokenI.H:535
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::token::scalarToken
scalar scalarToken() const
Return float or double value.
Definition: tokenI.H:545
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
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:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::readInt64
int64_t readInt64(Istream &is)
Read int64_t from stream.
Definition: int64IO.C:81