Simplify read error detection.

This commit is contained in:
Daniel Patterson 2016-11-14 18:43:30 -08:00
parent 608044305d
commit d8b016b92a

View File

@ -56,20 +56,16 @@ class FileReader
"bytewise reading requires trivially copyable type"); "bytewise reading requires trivially copyable type");
if (count == 0) if (count == 0)
return; return;
input_stream.read(reinterpret_cast<char *>(dest), count * sizeof(T));
// safe to cast here, according to CPP docs, negative values for gcount const auto &result = input_stream.read(reinterpret_cast<char *>(dest), count * sizeof(T));
// are never used. if (!result)
const unsigned long bytes_read = static_cast<unsigned long>(input_stream.gcount());
const auto expected_bytes = count * sizeof(T);
if (bytes_read == 0)
{ {
throw util::exception("Error reading from " + filename + ": " + std::strerror(errno)); if (result.eof())
{
throw util::exception("Error reading from " + filename +
": Unexpected end of file");
} }
else if (bytes_read < expected_bytes) throw util::exception("Error reading from " + filename + ": " + std::strerror(errno));
{
throw util::exception("Error reading from " + filename + ": Unexpected end of file");
} }
} }