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-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 #include "int64.H"
30 #include "error.H"
31 #include "parsing.H"
32 #include "IOstreams.H"
33 #include <cinttypes>
34 
35 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
36 
37 int64_t Foam::readInt64(const char* buf)
38 {
39  char *endptr = nullptr;
40  errno = 0;
41  const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
42 
43  const int64_t val = int64_t(parsed);
44 
45  const parsing::errorType err =
46  (
47  (parsed < INT64_MIN || parsed > INT64_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::readInt64(const char* buf, int64_t& val)
64 {
65  char *endptr = nullptr;
66  errno = 0;
67  const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
68 
69  val = int64_t(parsed);
70 
71  return
72  (
73  (parsed < INT64_MIN || parsed > INT64_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 int64"
88  << exit(FatalIOError);
89  is.setBad();
90  return is;
91  }
92 
93  if (t.isLabel())
94  {
95  val = int64_t(t.labelToken());
96  }
97  else
98  {
100  << "Wrong token type - expected label (int64), found "
101  << t.info()
102  << exit(FatalIOError);
103  is.setBad();
104  return is;
105  }
106 
107  is.check(FUNCTION_NAME);
108  return is;
109 }
110 
111 
113 {
114  int64_t val(0);
115  is >> val;
116 
117  return val;
118 }
119 
120 
122 {
123  os.write(label(val));
124  os.check(FUNCTION_NAME);
125  return os;
126 }
127 
128 
129 #if defined(__APPLE__)
130 Foam::Istream& Foam::operator>>(Istream& is, long& val)
131 {
132  return operator>>(is, reinterpret_cast<int64_t&>(val));
133 }
134 
135 Foam::Ostream& Foam::operator<<(Ostream& os, const long val)
136 {
137  return (os << int64_t(val));
138 }
139 #endif
140 
141 
142 // ************************************************************************* //
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:456
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
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::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:228
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:368
Foam::token::isLabel
bool isLabel() const
Token is LABEL.
Definition: tokenI.H:450
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
error.H
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:513
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
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.H:352
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::readInt64
int64_t readInt64(Istream &is)
Read int64_t from stream.
Definition: int64IO.C:112