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-2018 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 
34 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
35 
36 uint32_t Foam::readUint32(const char* buf)
37 {
38  char *endptr = nullptr;
39  errno = 0;
40  const uintmax_t parsed = ::strtoumax(buf, &endptr, 10);
41 
42  const uint32_t val = uint32_t(parsed);
43 
44  const parsing::errorType err =
45  (
46  (parsed > UINT32_MAX)
47  ? parsing::errorType::RANGE
48  : parsing::checkConversion(buf, endptr)
49  );
50 
51  if (err != parsing::errorType::NONE)
52  {
53  FatalIOErrorInFunction("unknown")
54  << parsing::errorNames[err] << " '" << buf << "'"
55  << exit(FatalIOError);
56  }
57 
58  return val;
59 }
60 
61 
62 bool Foam::readUint32(const char* buf, uint32_t& val)
63 {
64  char *endptr = nullptr;
65  errno = 0;
66  const uintmax_t parsed = ::strtoumax(buf, &endptr, 10);
67 
68  val = uint32_t(parsed);
69 
70  return
71  (
72  (parsed > UINT32_MAX)
73  ? false
74  : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
75  );
76 }
77 
78 
80 {
81  token t(is);
82 
83  if (!t.good())
84  {
86  << "Bad token - could not get uint32"
87  << exit(FatalIOError);
88  is.setBad();
89  return is;
90  }
91 
92  if (t.isLabel())
93  {
94  val = uint32_t(t.labelToken());
95  }
96  else
97  {
99  << "Wrong token type - expected label (uint32), found "
100  << t.info()
101  << exit(FatalIOError);
102  is.setBad();
103  return is;
104  }
105 
106  is.check(FUNCTION_NAME);
107  return is;
108 }
109 
110 
112 {
113  uint32_t val(0);
114  is >> val;
115 
116  return val;
117 }
118 
119 
121 {
122  os.write(label(val));
123  os.check(FUNCTION_NAME);
124  return os;
125 }
126 
127 
128 // ************************************************************************* //
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...
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::readUint32
uint32_t readUint32(Istream &is)
Read uint32_t from stream.
Definition: uint32IO.C:111
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
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
uint32.H
32bit unsigned integer
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