ENH: primitives: abstract out number parsing

This commit is contained in:
mattijs
2013-08-13 17:23:17 +01:00
parent 401b2624f9
commit 36a3d00766

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -296,7 +296,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
buf[nChar++] = c; buf[nChar++] = c;
// get everything that could resemble a number and let // get everything that could resemble a number and let
// strtod() determine the validity // readScalar determine the validity
while while
( (
is_.get(c) is_.get(c)
@ -348,24 +348,25 @@ Foam::Istream& Foam::ISstream::read(token& t)
} }
else else
{ {
char *endptr = NULL;
if (asLabel) if (asLabel)
{ {
long longVal(strtol(buf, &endptr, 10)); label labelVal;
t = label(longVal); if (readLabel(buf, labelVal))
// return as a scalar if doesn't fit in a label
if (*endptr || t.labelToken() != longVal)
{ {
t = scalar(strtod(buf, &endptr)); t = labelVal;
} }
} else
else {
{ // Maybe too big? Try as scalar
scalar scalarVal(strtod(buf, &endptr)); scalar scalarVal;
t = scalarVal; if (readScalar(buf, scalarVal))
{
t = scalarVal;
}
else
{
t.setBad();
}
// --------------------------------------- // ---------------------------------------
// this would also be possible if desired: // this would also be possible if desired:
// --------------------------------------- // ---------------------------------------
@ -380,12 +381,20 @@ Foam::Istream& Foam::ISstream::read(token& t)
// t = labelVal; // t = labelVal;
// } // }
// } // }
}
// not everything converted: bad format or trailing junk }
if (*endptr) }
else
{ {
t.setBad(); scalar scalarVal;
if (readScalar(buf, scalarVal))
{
t = scalarVal;
}
else
{
t.setBad();
}
} }
} }
} }