1
0
Fork 0

apt.dat parser: better input stream handling

- use ( in.getline(...) ) as the main loop condition instead of
  ( ! in.eof() ). This should behave better (see
  <https://gehrcke.de/2011/06/reading-files-in-c-using-ifstream-dealing-correctly-with-badbit-failbit-eofbit-and-perror/>);
- check in.bad() after exiting from each reading loop; if the exit was
  caused by an error, log an appropriate message and throw an
  exception.
This commit is contained in:
Florent Rougon 2016-04-24 19:33:41 +02:00
parent d6f0bc6fb5
commit b0b81bcd3e

View file

@ -128,8 +128,9 @@ public:
} }
} // end of the apt.dat header } // end of the apt.dat header
while ( ! in.eof() ) { throwExceptionIfStreamError(in, "apt.dat", apt_dat);
in.getline(tmp, 2048);
while ( in.getline(tmp, 2048) ) {
line = tmp; // string copy, ack line = tmp; // string copy, ack
line_num++; line_num++;
@ -203,6 +204,7 @@ public:
} }
} }
throwExceptionIfStreamError(in, "apt.dat", apt_dat);
finishAirport(); finishAirport();
} }
@ -226,6 +228,18 @@ private:
NavDataCache* cache; NavDataCache* cache;
PositionedID currentAirportID; PositionedID currentAirportID;
void throwExceptionIfStreamError(const sg_gzifstream& input_stream,
const std::string& short_name,
const std::string& full_path)
{
if ( input_stream.bad() ) {
// strerror() isn't thread-safe, unfortunately...
SG_LOG( SG_GENERAL, SG_ALERT, "error while reading " << full_path );
throw sg_io_exception("error while reading " + short_name,
full_path.c_str());
}
}
void finishAirport() void finishAirport()
{ {
if (currentAirportID == 0) { if (currentAirportID == 0) {