Files
openfoam/src/OpenFOAM/primitives/ints/int/intIO.C
2019-01-02 13:49:39 +01:00

87 lines
2.4 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "int.H"
#include "error.H"
#include "parsing.H"
#include "IOstreams.H"
#include <cinttypes>
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
int Foam::readInt(const char* buf)
{
char *endptr = nullptr;
errno = 0;
const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
const int val = int(parsed);
const parsing::errorType err =
(
(parsed < INT_MIN || parsed > INT_MAX)
? parsing::errorType::RANGE
: parsing::checkConversion(buf, endptr)
);
if (err != parsing::errorType::NONE)
{
FatalIOErrorInFunction("unknown")
<< parsing::errorNames[err] << " '" << buf << "'"
<< exit(FatalIOError);
}
return val;
}
bool Foam::readInt(const char* buf, int& val)
{
char *endptr = nullptr;
errno = 0;
const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
val = int(parsed);
return
(
(parsed < INT_MIN || parsed > INT_MAX)
? false
: (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
);
}
int Foam::readInt(Istream& is)
{
int val(0);
is >> val;
return val;
}
// ************************************************************************* //