From: Florent Rougon Date: Sun, 24 Apr 2016 17:45:37 +0000 (+0200) Subject: apt.dat parser: various little improvements X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d85fc7b7e72406fb3385a69c8967ed462e625c9b;p=flightgear.git apt.dat parser: various little improvements - don't duplicate the line buffer (array of char as well as std::string), it is not useful here; - don't corrupt (truncate) input lines longer than 2048 bytes: there is no such limitation in the apt.dat v1000 format spec; - fix comments handling: the apt.dat v1000 format spec states they must start with *two* '#' characters. --- diff --git a/src/Airports/apt_loader.cxx b/src/Airports/apt_loader.cxx index 9545dddfa..9fe89a42a 100644 --- a/src/Airports/apt_loader.cxx +++ b/src/Airports/apt_loader.cxx @@ -98,9 +98,7 @@ public: } string line; - char tmp[2049]; - tmp[2048] = 0; - + unsigned int line_id = 0; unsigned int line_num = 0; @@ -130,13 +128,11 @@ public: throwExceptionIfStreamError(in, "apt.dat", apt_dat); - while ( in.getline(tmp, 2048) ) { - line = tmp; // string copy, ack + while ( std::getline(in, line) ) { line_num++; - if ( line.empty() || isspace(tmp[0]) || tmp[0] == '#' ) { + if ( isBlankOrCommentLine(line) ) continue; - } if ((line_num % 100) == 0) { // every 100 lines @@ -144,7 +140,8 @@ public: cache->setRebuildPhaseProgress(NavDataCache::REBUILD_AIRPORTS, percent); } - line_id = atoi(tmp); + // Extract the first field into 'line_id' + line_id = atoi(line.c_str()); if ( line_id == 1 /* Airport */ || line_id == 16 /* Seaplane base */ || @@ -228,6 +225,13 @@ private: NavDataCache* cache; PositionedID currentAirportID; + // Tell whether an apt.dat line is blank or a comment line + bool isBlankOrCommentLine(const std::string& line) + { + size_t pos = line.find_first_not_of(" \t"); + return ( pos == std::string::npos || line.find("##", pos) == pos ); + } + void throwExceptionIfStreamError(const sg_gzifstream& input_stream, const std::string& short_name, const std::string& full_path)