1
0
Fork 0

Minor improvements to loading of nav.dat files

- simgear::strutils::readNonNegativeInt<unsigned int>() 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<unsigned long, 10>()
  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).
This commit is contained in:
Florent Rougon 2019-03-28 07:44:59 +01:00
parent 378999dd80
commit 2720d9fd9f
2 changed files with 16 additions and 10 deletions

View file

@ -61,6 +61,8 @@
using std::string; using std::string;
using std::vector; using std::vector;
namespace strutils = simgear::strutils;
// Duplicate navaids with the same ident will be removed if the disance // Duplicate navaids with the same ident will be removed if the disance
// between them is less than this. // between them is less than this.
static const double DUPLICATE_DETECTION_RADIUS_NM = 10; 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. // corresponding data into the NavDataCache.
PositionedID NavLoader::processNavLine( PositionedID NavLoader::processNavLine(
const string& line, const string& utf8Path, unsigned int lineNum, 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(); NavDataCache* cache = NavDataCache::instance();
int rowCode, elev_ft, freq, range; int rowCode, elev_ft, freq, range;
@ -446,29 +448,33 @@ void NavLoader::loadNav(const SGPath& path, std::size_t bytesReadSoFar,
} }
unsigned int lineNumber; unsigned int lineNumber;
unsigned long version; unsigned int version;
vector<string> fields(simgear::strutils::split(line, 0, 1)); vector<string> fields(simgear::strutils::split(line, 0, 1));
try { try {
if (fields.empty()) { if (fields.empty()) {
throw std::logic_error(""); throw sg_format_exception();
} }
version = std::stoul(fields[0]); version = strutils::readNonNegativeInt<unsigned int>(fields[0]);
} catch (const std::logic_error&) { } catch (const sg_exception& exc) {
std::string strippedLine = simgear::strutils::stripTrailingNewlines(line); std::string strippedLine = simgear::strutils::stripTrailingNewlines(line);
std::string errMsg = utf8Path + ": "; std::string errMsg = utf8Path + ": ";
if (fields.empty()) { 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); SG_LOG(SG_NAVAID, SG_ALERT, errMsg);
} else { } else {
errMsg += "unable to parse version from header"; errMsg += "unable to parse format version";
SG_LOG(SG_NAVAID, SG_ALERT, errMsg << ": " << strippedLine); SG_LOG(SG_NAVAID, SG_ALERT,
errMsg << " (" << exc.what() << "): " << strippedLine);
} }
throw sg_format_exception(errMsg, 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++) { for (lineNumber = 3; std::getline(in, line); lineNumber++) {
processNavLine(line, utf8Path, lineNumber, FGPositioned::INVALID, version); processNavLine(line, utf8Path, lineNumber, FGPositioned::INVALID, version);

View file

@ -58,9 +58,9 @@ class NavLoader {
const std::string& utf8Path, const std::string& utf8Path,
unsigned int lineNum, unsigned int lineNum,
FGPositioned::Type type = FGPositioned::INVALID, FGPositioned::Type type = FGPositioned::INVALID,
unsigned long version = 810); unsigned int version = 810);
}; };
} // of namespace flightgear } // of namespace flightgear
#endif // _FG_NAVDB_HXX #endif // _FG_NAVDB_HXX