From 2720d9fd9f421d79e2657f07a0369d124b794ecb Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Thu, 28 Mar 2019 07:44:59 +0100 Subject: [PATCH] Minor improvements to loading of nav.dat files - simgear::strutils::readNonNegativeInt() is better than std::stoul() at detecting and reporting erroneous input (for one, it checks that the given string *represents* an integer that fits in the specified type, as opposed to just "starts with digits"). - The format version is now an unsigned int (just because it's largely enough and simgear::strutils::readNonNegativeInt() isn't instantiated for now). - The format version is now printed to the log (it was already for apt.dat files, but not for nav.dat files). --- src/Navaids/navdb.cxx | 22 ++++++++++++++-------- src/Navaids/navdb.hxx | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Navaids/navdb.cxx b/src/Navaids/navdb.cxx index 49f20f4b5..c089a03ac 100644 --- a/src/Navaids/navdb.cxx +++ b/src/Navaids/navdb.cxx @@ -61,6 +61,8 @@ using std::string; using std::vector; +namespace strutils = simgear::strutils; + // Duplicate navaids with the same ident will be removed if the disance // between them is less than this. static const double DUPLICATE_DETECTION_RADIUS_NM = 10; @@ -156,7 +158,7 @@ static bool canBeDuplicate(FGPositionedRef ref, FGPositioned::Type type, // corresponding data into the NavDataCache. PositionedID NavLoader::processNavLine( const string& line, const string& utf8Path, unsigned int lineNum, - FGPositioned::Type type, unsigned long version) + FGPositioned::Type type, unsigned int version) { NavDataCache* cache = NavDataCache::instance(); int rowCode, elev_ft, freq, range; @@ -446,29 +448,33 @@ void NavLoader::loadNav(const SGPath& path, std::size_t bytesReadSoFar, } unsigned int lineNumber; - unsigned long version; + unsigned int version; vector fields(simgear::strutils::split(line, 0, 1)); try { if (fields.empty()) { - throw std::logic_error(""); + throw sg_format_exception(); } - version = std::stoul(fields[0]); - } catch (const std::logic_error&) { + version = strutils::readNonNegativeInt(fields[0]); + } catch (const sg_exception& exc) { std::string strippedLine = simgear::strutils::stripTrailingNewlines(line); std::string errMsg = utf8Path + ": "; if (fields.empty()) { - errMsg += "unable to parse version: empty line"; + errMsg += "unable to parse format version: empty line"; SG_LOG(SG_NAVAID, SG_ALERT, errMsg); } else { - errMsg += "unable to parse version from header"; - SG_LOG(SG_NAVAID, SG_ALERT, errMsg << ": " << strippedLine); + errMsg += "unable to parse format version"; + SG_LOG(SG_NAVAID, SG_ALERT, + errMsg << " (" << exc.what() << "): " << strippedLine); } throw sg_format_exception(errMsg, strippedLine); } + SG_LOG(SG_NAVAID, SG_INFO, + "nav.dat format version (" << utf8Path << "): " << version); + for (lineNumber = 3; std::getline(in, line); lineNumber++) { processNavLine(line, utf8Path, lineNumber, FGPositioned::INVALID, version); diff --git a/src/Navaids/navdb.hxx b/src/Navaids/navdb.hxx index e3c168e68..cc9d4a029 100644 --- a/src/Navaids/navdb.hxx +++ b/src/Navaids/navdb.hxx @@ -58,9 +58,9 @@ class NavLoader { const std::string& utf8Path, unsigned int lineNum, FGPositioned::Type type = FGPositioned::INVALID, - unsigned long version = 810); + unsigned int version = 810); }; } // of namespace flightgear - + #endif // _FG_NAVDB_HXX