Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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 "int.H"
30#include "error.H"
31#include "parsing.H"
32#include "IOstreams.H"
33#include <cinttypes>
34
35// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
36
37int 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)
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::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
76 );
77}
78
79
80int Foam::readInt(Istream& is)
81{
82 int val(0);
83 is >> val;
84
85 return val;
86}
87
88
89// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
System signed integer.
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
int readInt(Istream &is)
Read int from stream.
Definition intIO.C:73
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