intIO.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) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2017 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 "int.H"
30 #include "error.H"
31 #include "parsing.H"
32 #include "IOstreams.H"
33 #include <cinttypes>
34 
35 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
36 
37 int Foam::readInt(const char* buf)
38 {
39  char *endptr = nullptr;
40  errno = 0;
41  const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
42 
43  const int val = int(parsed);
44 
45  const parsing::errorType err =
46  (
47  (parsed < INT_MIN || parsed > INT_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::readInt(const char* buf, int& val)
64 {
65  char *endptr = nullptr;
66  errno = 0;
67  const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
68 
69  val = int(parsed);
70 
71  return
72  (
73  (parsed < INT_MIN || parsed > INT_MAX)
74  ? false
75  : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
76  );
77 }
78 
79 
81 {
82  int val(0);
83  is >> val;
84 
85  return val;
86 }
87 
88 
89 // ************************************************************************* //
int.H
System signed integer.
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
error.H
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::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::readInt
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:80