Commit 0ce7e364 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: improve read handling of bad streams #1033

- a failed attempt to read a value (eg, word, label, scalar) from a
  stream now always provokes a FatalIOError.
  This helps avoid some difficult to trace input errors.
parent 3f017a01
......@@ -208,12 +208,9 @@ Foam::Istream& Foam::ITstream::read(token& tok)
{
if (eof())
{
FatalIOErrorInFunction
(
*this
) << "attempt to read beyond EOF"
FatalIOErrorInFunction(*this)
<< "attempt to read beyond EOF"
<< exit(FatalIOError);
setBad();
}
else
......
......@@ -54,13 +54,11 @@ void Foam::dimensioned<Type>::initialize(Istream& is)
if (dims != dimensions_)
{
FatalIOErrorInFunction
(
is
) << "The dimensions " << dims
<< " provided do not match the required dimensions "
<< dimensions_
<< abort(FatalIOError);
FatalIOErrorInFunction(is)
<< "The dimensions " << dims
<< " provided do not match the required dimensions "
<< dimensions_
<< abort(FatalIOError);
}
}
......
......@@ -40,6 +40,7 @@ const Scalar pTraits<Scalar>::rootMax = ScalarROOTVGREAT;
const char* const pTraits<Scalar>::componentNames[] = { "" };
pTraits<Scalar>::pTraits(const Scalar& val)
:
p_(val)
......@@ -132,6 +133,9 @@ Istream& operator>>(Istream& is, Scalar& val)
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get scalar value"
<< exit(FatalIOError);
is.setBad();
return is;
}
......@@ -139,16 +143,18 @@ Istream& operator>>(Istream& is, Scalar& val)
if (t.isNumber())
{
val = t.number();
is.check(FUNCTION_NAME);
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected Scalar, found " << t.info()
<< "Wrong token type - expected scalar value, found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
is.check(FUNCTION_NAME);
return is;
}
......
......@@ -161,6 +161,9 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& sw)
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get bool"
<< exit(FatalIOError);
is.setBad();
return is;
}
......@@ -171,26 +174,26 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& sw)
}
else if (t.isWord())
{
// Allow reading invalid value, but report immediately
// Permit invalid value, but catch immediately for better messages
sw = Switch(t.wordToken(), true);
if (!sw.valid())
{
is.setBad();
FatalIOErrorInFunction(is)
<< "expected 'true/false', 'on/off' ... found " << t.wordToken()
<< "Expected 'true/false', 'on/off' ... found "
<< t.wordToken()
<< exit(FatalIOError);
is.setBad();
return is;
}
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected bool, found " << t
<< "Wrong token type - expected bool, found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -43,6 +43,9 @@ Foam::Istream& Foam::operator>>(Istream& is, direction& d)
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get direction"
<< exit(FatalIOError);
is.setBad();
return is;
}
......@@ -53,11 +56,11 @@ Foam::Istream& Foam::operator>>(Istream& is, direction& d)
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected direction, found " << t.info()
<< "Wrong token type - expected label (direction), found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -80,6 +80,9 @@ Foam::Istream& Foam::operator>>(Istream& is, int32_t& val)
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get int32"
<< exit(FatalIOError);
is.setBad();
return is;
}
......@@ -90,11 +93,11 @@ Foam::Istream& Foam::operator>>(Istream& is, int32_t& val)
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected int32_t, found " << t.info()
<< "Wrong token type - expected label (int32), found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -80,6 +80,9 @@ Foam::Istream& Foam::operator>>(Istream& is, int64_t& val)
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get int64"
<< exit(FatalIOError);
is.setBad();
return is;
}
......@@ -90,11 +93,11 @@ Foam::Istream& Foam::operator>>(Istream& is, int64_t& val)
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected int64_t, found " << t.info()
<< "Wrong token type - expected label (int64), found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -79,6 +79,9 @@ Foam::Istream& Foam::operator>>(Istream& is, uint32_t& val)
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get uint32"
<< exit(FatalIOError);
is.setBad();
return is;
}
......@@ -89,11 +92,11 @@ Foam::Istream& Foam::operator>>(Istream& is, uint32_t& val)
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected uint32_t, found " << t.info()
<< "Wrong token type - expected label (uint32), found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -79,6 +79,9 @@ Foam::Istream& Foam::operator>>(Istream& is, uint64_t& val)
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get uint64"
<< exit(FatalIOError);
is.setBad();
return is;
}
......@@ -89,11 +92,11 @@ Foam::Istream& Foam::operator>>(Istream& is, uint64_t& val)
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected uint64_t, found " << t.info()
<< "Wrong token type - expected label (uint64), found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -36,31 +36,34 @@ Foam::fileName::fileName(Istream& is)
}
Foam::Istream& Foam::operator>>(Istream& is, fileName& fn)
Foam::Istream& Foam::operator>>(Istream& is, fileName& val)
{
token t(is);
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get string"
<< exit(FatalIOError);
is.setBad();
return is;
}
if (t.isString())
{
fn = t.stringToken();
val = t.stringToken();
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected string, found " << t.info()
<< "Wrong token type - expected string, found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
fn.stripInvalid();
val.stripInvalid();
is.check(FUNCTION_NAME);
return is;
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -58,44 +58,46 @@ bool Foam::keyType::match(const std::string& text, bool literal) const
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, keyType& kw)
Foam::Istream& Foam::operator>>(Istream& is, keyType& val)
{
token t(is);
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get a word/regex"
<< exit(FatalIOError);
is.setBad();
return is;
}
if (t.isWord())
{
kw = t.wordToken();
val = t.wordToken();
}
else if (t.isString())
{
// Assign from string. Set as regular expression.
kw = t.stringToken();
kw.isPattern_ = true;
// Assign from string, treat as regular expression
val = t.stringToken();
val.isPattern_ = true;
// flag empty strings as an error
if (kw.empty())
// Flag empty strings as an error
if (val.empty())
{
is.setBad();
FatalIOErrorInFunction(is)
<< "empty word/expression "
<< "Empty word/expression"
<< exit(FatalIOError);
is.setBad();
return is;
}
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected word or string, found "
<< "Wrong token type - expected word or string, found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -36,27 +36,30 @@ Foam::string::string(Istream& is)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, string& s)
Foam::Istream& Foam::operator>>(Istream& is, string& val)
{
token t(is);
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get string"
<< exit(FatalIOError);
is.setBad();
return is;
}
if (t.isString())
{
s = t.stringToken();
val = t.stringToken();
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected string, found " << t.info()
<< "Wrong token type - expected string, found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -36,46 +36,47 @@ Foam::word::word(Istream& is)
}
Foam::Istream& Foam::operator>>(Istream& is, word& w)
Foam::Istream& Foam::operator>>(Istream& is, word& val)
{
token t(is);
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get word"
<< exit(FatalIOError);
is.setBad();
return is;
}
if (t.isWord())
{
w = t.wordToken();
val = t.wordToken();
}
else if (t.isString())
{
// Try a bit harder and convert string to word
w = t.stringToken();
string::stripInvalid<word>(w);
val = t.stringToken();
string::stripInvalid<word>(val);
// Flag empty strings and bad chars as an error
if (w.empty() || w.size() != t.stringToken().size())
if (val.empty() || val.size() != t.stringToken().size())
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected word,"
" found non-word characters "
<< "Empty word or non-word characters "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected word, found "
<< "Wrong token type - expected word, found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -45,43 +45,45 @@ Foam::wordRe::wordRe(Istream& is)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, wordRe& w)
Foam::Istream& Foam::operator>>(Istream& is, wordRe& val)
{
token t(is);
if (!t.good())
{
FatalIOErrorInFunction(is)
<< "Bad token - could not get wordRe"
<< exit(FatalIOError);
is.setBad();
return is;
}
if (t.isWord())
{
w = t.wordToken();
val = t.wordToken();
}
else if (t.isString())
{
// Auto-tests for regular expression
w = t.stringToken();
// Auto-detects regex
val = t.stringToken();
// flag empty strings as an error
if (w.empty())
// Flag empty strings as an error
if (val.empty())
{
is.setBad();
FatalIOErrorInFunction(is)
<< "empty word/expression "
<< "Empty word/expression"
<< exit(FatalIOError);
is.setBad();
return is;
}
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected word or string, found "
<< "Wrong token type - expected word or string, found "
<< t.info()
<< exit(FatalIOError);
is.setBad();
return is;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment