mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
- Any trailing whitespace when parsing from strings or character buffers
is ignored rather than being treated as an error. This is consistent
with behaviour when reading from an Istream and with leading whitespace
being ignored in the underlying atof/atod, strtof/strtod... functions.
- Allow parsing directly from a std::string instead of just from a 'char*'.
This reflects the C++11 addition of std::stod to complement the C
functions strtod. This also makes it easier to parse string directly
without using an IStringStream.
- Two-parameter parsing methods return success/failure.
Eg,
if (readInt32(str, &int32Val)) ...
- One-parameter parsing methods return the value on success or
emit a FatalIOError.
Eg,
const char* buf;
int32Val = readInt32(buf, &);
- Improved consistency when parsing unsigned ints.
Use strtoimax and strtoumax throughout.
- Rename readDoubleScalar -> readDouble, readFloatScalar -> readFloat.
Using the primitive name directly instead of the Foam typedef for
better consistency with readInt32 etc.
- Clean/improve parseNasScalar.
Handle normal numbers directly, reduce some operations.
99 lines
2.7 KiB
C
99 lines
2.7 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);
|
|
|
|
if (parsed < INT_MIN || parsed > INT_MAX)
|
|
{
|
|
// Range error
|
|
errno = ERANGE;
|
|
}
|
|
|
|
const parsing::errorType err = 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);
|
|
|
|
if (parsed < INT_MIN || parsed > INT_MAX)
|
|
{
|
|
// Range error
|
|
errno = ERANGE;
|
|
}
|
|
|
|
const parsing::errorType err = parsing::checkConversion(buf, endptr);
|
|
if (err != parsing::errorType::NONE)
|
|
{
|
|
#ifdef FULLDEBUG
|
|
IOWarningInFunction("unknown")
|
|
<< parsing::errorNames[err] << " '" << buf << "'"
|
|
<< endl;
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
int Foam::readInt(Istream& is)
|
|
{
|
|
int val;
|
|
is >> val;
|
|
|
|
return val;
|
|
}
|
|
|
|
|
|
// ************************************************************************* //
|