1
0
Fork 0

Maintenance: apt_loader

SPDX tags.
parameter to loadAirportFormFile() changed to const&.
parameter to Line() ctor changed to const&.
parameter to loadAirport() changed to const&.
member variable initialization.
++prefix for complex types.
virtual dtor.
This commit is contained in:
scttgs0 2023-05-23 00:56:19 -05:00
parent 2c40595d3c
commit cda2337041
2 changed files with 691 additions and 718 deletions

View file

@ -1,65 +1,46 @@
// apt_loader.cxx -- a front end loader of the apt.dat file. This loader /*
// populates the runway and basic classes. * SPDX-FileName: apt_loader.cxx
// * SPDX-FileComment: a front end loader of the apt.dat file. This loader populates the runway and basic classes.
// Written by Curtis Olson, started August 2000. * SPDX-FileCopyrightText: Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
// * SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt */
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
#include "apt_loader.hxx"
#include <simgear/compiler.h>
#include <algorithm> #include <algorithm>
#include <cerrno>
#include <cstddef> // std::size_t
#include <ctype.h> // isspace()
#include <iostream>
#include <sstream> // std::istringstream
#include <stdlib.h> // atof(), atoi() #include <stdlib.h> // atof(), atoi()
#include <string.h> // memchr() #include <string.h> // memchr()
#include <ctype.h> // isspace() #include <string>
#include <cerrno> #include <utility> // std::pair, std::move()
#include <vector>
#include <simgear/compiler.h>
#include <simgear/constants.h> #include <simgear/constants.h>
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
#include <simgear/io/iostreams/sgstream.hxx> #include <simgear/io/iostreams/sgstream.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
#include <simgear/structure/exception.hxx> #include <simgear/structure/exception.hxx>
#include <simgear/misc/sg_path.hxx>
#include <cstddef> // std::size_t #include <ATC/CommStation.hxx>
#include <string>
#include <vector>
#include <utility> // std::pair, std::move()
#include "airport.hxx"
#include "runways.hxx"
#include "pavement.hxx"
#include <Navaids/NavDataCache.hxx> #include <Navaids/NavDataCache.hxx>
#include <Navaids/positioned.hxx> #include <Navaids/positioned.hxx>
#include <ATC/CommStation.hxx>
#include <iostream> #include "airport.hxx"
#include <sstream> // std::istringstream #include "apt_loader.hxx"
#include "pavement.hxx"
#include "runways.hxx"
using std::vector;
using std::string; using std::string;
using std::vector;
namespace strutils = simgear::strutils; namespace strutils = simgear::strutils;
@ -76,14 +57,14 @@ static FGPositioned::Type fptypeFromRobinType(unsigned int aType)
} }
} }
namespace flightgear namespace flightgear {
{
APTLoader::APTLoader() APTLoader::APTLoader()
: last_apt_id(""), : last_apt_id(""),
last_apt_elev(0.0), last_apt_elev(0.0),
currentAirportPosID(0), currentAirportPosID(0),
cache(NavDataCache::instance()) cache(NavDataCache::instance())
{ } {
}
APTLoader::~APTLoader() {} APTLoader::~APTLoader() {}
@ -143,8 +124,7 @@ void APTLoader::readAptDatFile(const SGPath &aptdb_file,
unsigned int aptDatFormatVersion = unsigned int aptDatFormatVersion =
strutils::readNonNegativeInt<unsigned int>(fields[0]); strutils::readNonNegativeInt<unsigned int>(fields[0]);
SG_LOG(SG_GENERAL, SG_INFO, SG_LOG(SG_GENERAL, SG_INFO,
"apt.dat format version (" << apt_dat << "): " << "apt.dat format version (" << apt_dat << "): " << aptDatFormatVersion);
aptDatFormatVersion);
} }
} }
} // end of the apt.dat header } // end of the apt.dat header
@ -160,8 +140,7 @@ void APTLoader::readAptDatFile(const SGPath &aptdb_file,
if ((line_num % 100) == 0) { if ((line_num % 100) == 0) {
// every 100 lines // every 100 lines
unsigned int percent = ((bytesReadSoFar + in.approxOffset()) * 100) unsigned int percent = ((bytesReadSoFar + in.approxOffset()) * 100) / totalSizeOfAllAptDatFiles;
/ totalSizeOfAllAptDatFiles;
cache->setRebuildPhaseProgress( cache->setRebuildPhaseProgress(
NavDataCache::REBUILD_READING_APT_DAT_FILES, percent); NavDataCache::REBUILD_READING_APT_DAT_FILES, percent);
} }
@ -191,8 +170,7 @@ void APTLoader::readAptDatFile(const SGPath &aptdb_file,
if (skipAirport) { if (skipAirport) {
SG_LOG(SG_GENERAL, SG_INFO, SG_LOG(SG_GENERAL, SG_INFO,
apt_dat << ":" << line_num << ": skipping airport " << apt_dat << ":" << line_num << ": skipping airport " << currentAirportId << " (already defined earlier)");
currentAirportId << " (already defined earlier)" );
} else { } else {
// We haven't seen this airport yet in any apt.dat file // We haven't seen this airport yet in any apt.dat file
RawAirportInfo& airportInfo = insertRetval.first->second; RawAirportInfo& airportInfo = insertRetval.first->second;
@ -223,7 +201,7 @@ void APTLoader::loadAirports()
// Loop over all airports found in all apt.dat files // Loop over all airports found in all apt.dat files
for (AirportInfoMapType::const_iterator it = airportInfoMap.begin(); for (AirportInfoMapType::const_iterator it = airportInfoMap.begin();
it != airportInfoMap.end(); it++) { it != airportInfoMap.end(); ++it) {
// Full path to the apt.dat file this airport info comes from // Full path to the apt.dat file this airport info comes from
const string aptDat = it->second.file.utf8Str(); const string aptDat = it->second.file.utf8Str();
@ -247,7 +225,7 @@ void APTLoader::loadAirports()
} }
// Parse and return specific apt.dat file containing a single airport. // Parse and return specific apt.dat file containing a single airport.
const FGAirport* APTLoader::loadAirportFromFile(std::string id, const SGPath& aptdb_file) const FGAirport* APTLoader::loadAirportFromFile(const std::string& id, const SGPath& aptdb_file)
{ {
std::size_t bytesReadSoFar = 10; std::size_t bytesReadSoFar = 10;
std::size_t totalSizeOfAllAptDatFiles = 100; std::size_t totalSizeOfAllAptDatFiles = 100;
@ -263,7 +241,7 @@ static bool isCommLine(const int code)
return ((code >= 50) && (code <= 56)) || ((code >= 1050) && (code <= 1056)); return ((code >= 50) && (code <= 56)) || ((code >= 1050) && (code <= 1056));
} }
const FGAirport* APTLoader::loadAirport(const string aptDat, const std::string airportID, RawAirportInfo* airport_info, bool createFGAirport) const FGAirport* APTLoader::loadAirport(const string& aptDat, const std::string& airportID, RawAirportInfo* airport_info, bool createFGAirport)
{ {
// The first line for this airport was already split over whitespace, but // The first line for this airport was already split over whitespace, but
// remains to be parsed for the most part. // remains to be parsed for the most part.
@ -274,7 +252,7 @@ const FGAirport* APTLoader::loadAirport(const string aptDat, const std::string a
// Loop over the second and subsequent lines // Loop over the second and subsequent lines
for (LinesList::const_iterator linesIt = lines.begin(); for (LinesList::const_iterator linesIt = lines.begin();
linesIt != lines.end(); linesIt++) { linesIt != lines.end(); ++linesIt) {
// Beware that linesIt->str may end with an '\r' character, see above! // Beware that linesIt->str may end with an '\r' character, see above!
unsigned int rowCode = linesIt->rowCode; unsigned int rowCode = linesIt->rowCode;
@ -329,8 +307,7 @@ const FGAirport* APTLoader::loadAirport(const string aptDat, const std::string a
case None: case None:
std::ostringstream oss; std::ostringstream oss;
string cleanedLine = cleanLine(linesIt->str); string cleanedLine = cleanLine(linesIt->str);
oss << aptDat << ":" << linesIt->number << ": unexpected row code " << oss << aptDat << ":" << linesIt->number << ": unexpected row code " << rowCode;
rowCode;
SG_LOG(SG_GENERAL, SG_ALERT, oss.str() << " (" << cleanedLine << ")"); SG_LOG(SG_GENERAL, SG_ALERT, oss.str() << " (" << cleanedLine << ")");
throw sg_format_exception(oss.str(), cleanedLine); throw sg_format_exception(oss.str(), cleanedLine);
break; break;
@ -400,8 +377,8 @@ std::string APTLoader::cleanLine(const std::string& line)
// Lines obtained from readAptDatFile() may end with \r, which can be quite // Lines obtained from readAptDatFile() may end with \r, which can be quite
// confusing when printed to the terminal. // confusing when printed to the terminal.
for (std::string::reverse_iterator it = res.rbegin(); for (std::string::reverse_iterator it = res.rbegin();
it != res.rend() && *it == '\r'; /* empty */) it != res.rend() && *it == '\r';
{ // The beauty of C++ iterators... /* empty */) { // The beauty of C++ iterators...
it = std::string::reverse_iterator(res.erase((it + 1).base())); it = std::string::reverse_iterator(res.erase((it + 1).base()));
} }
@ -429,8 +406,7 @@ void APTLoader::finishAirport(const string& aptDat)
if (!rwy_count) { if (!rwy_count) {
currentAirportPosID = 0; currentAirportPosID = 0;
SG_LOG( SG_GENERAL, SG_ALERT, "Error in '" << aptDat << SG_LOG(SG_GENERAL, SG_ALERT, "Error in '" << aptDat << "': no runways for " << last_apt_id << ", skipping.");
"': no runways for " << last_apt_id << ", skipping." );
return; return;
} }
@ -475,8 +451,8 @@ void APTLoader::parseRunwayLine810(const string& aptDat, unsigned int lineNum,
{ {
if (token.size() < 11) { if (token.size() < 11) {
SG_LOG(SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v810 runway line " << aptDat << ":" << lineNum << ": invalid v810 runway line "
"(row code 10): at least 11 fields are required" ); << "(row code 10): at least 11 fields are required");
return; return;
} }
@ -513,16 +489,14 @@ void APTLoader::parseRunwayLine810(const string& aptDat, unsigned int lineNum,
} else { } else {
// (pair of) runways // (pair of) runways
string rwy_displ_threshold = token[6]; string rwy_displ_threshold = token[6];
vector<string> displ vector<string> displ = simgear::strutils::split(rwy_displ_threshold, ".");
= simgear::strutils::split( rwy_displ_threshold, "." );
double displ_thresh1 = atof(displ[0].c_str()); double displ_thresh1 = atof(displ[0].c_str());
double displ_thresh2 = atof(displ[1].c_str()); double displ_thresh2 = atof(displ[1].c_str());
displ_thresh1 *= SG_FEET_TO_METER; displ_thresh1 *= SG_FEET_TO_METER;
displ_thresh2 *= SG_FEET_TO_METER; displ_thresh2 *= SG_FEET_TO_METER;
string rwy_stopway = token[7]; string rwy_stopway = token[7];
vector<string> stop vector<string> stop = simgear::strutils::split(rwy_stopway, ".");
= simgear::strutils::split( rwy_stopway, "." );
double stopway1 = atof(stop[0].c_str()); double stopway1 = atof(stop[0].c_str());
double stopway2 = atof(stop[1].c_str()); double stopway2 = atof(stop[1].c_str());
stopway1 *= SG_FEET_TO_METER; stopway1 *= SG_FEET_TO_METER;
@ -552,8 +526,8 @@ void APTLoader::parseRunwayLine850(const string& aptDat, unsigned int lineNum,
{ {
if (token.size() < 26) { if (token.size() < 26) {
SG_LOG(SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v850 runway line " << aptDat << ":" << lineNum << ": invalid v850 runway line "
"(row code 100): at least 26 fields are required" ); << "(row code 100): at least 26 fields are required");
return; return;
} }
@ -632,8 +606,8 @@ void APTLoader::parseWaterRunwayLine850(const string& aptDat,
{ {
if (token.size() < 9) { if (token.size() < 9) {
SG_LOG(SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v850 water runway line " << aptDat << ":" << lineNum << ": invalid v850 water runway line "
"(row code 101): at least 9 fields are required" ); << "(row code 101): at least 9 fields are required");
return; return;
} }
@ -681,8 +655,8 @@ void APTLoader::parseHelipadLine850(const string& aptDat, unsigned int lineNum,
{ {
if (token.size() < 12) { if (token.size() < 12) {
SG_LOG(SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v850 helipad line " << aptDat << ":" << lineNum << ": invalid v850 helipad line "
"(row code 102): at least 12 fields are required" ); << "(row code 102): at least 12 fields are required");
return; return;
} }
@ -751,9 +725,8 @@ void APTLoader::parseNodeLine850(NodeList *nodelist,
if (token.size() < minNbTokens[rowCode - 111]) { if (token.size() < minNbTokens[rowCode - 111]) {
SG_LOG(SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid v850 node line " << aptDat << ":" << lineNum << ": invalid v850 node line "
"(row code " << rowCode << "): at least " << << "(row code " << rowCode << "): at least " << minNbTokens[rowCode - 111] << " fields are required");
minNbTokens[rowCode-111] << " fields are required" );
return; return;
} }
@ -802,13 +775,13 @@ void APTLoader::parseCommLine(const string& aptDat,
{ {
if (token.size() < 3) { if (token.size() < 3) {
SG_LOG(SG_GENERAL, SG_WARN, SG_LOG(SG_GENERAL, SG_WARN,
aptDat << ":" << lineNum << ": invalid Comm Frequency line " << aptDat << ":" << lineNum << ": invalid Comm Frequency line "
"(row code " << rowCode << "): at least 3 fields are required" ); << "(row code " << rowCode << "): at least 3 fields are required");
return; return;
} else if (rwy_count <= 0) { } else if (rwy_count <= 0) {
SG_LOG(SG_GENERAL, SG_ALERT, SG_LOG(SG_GENERAL, SG_ALERT,
aptDat << ":" << lineNum << ": no runways, skipping Comm " << aptDat << ":" << lineNum << ": no runways, skipping Comm "
"Frequency line for " << last_apt_id ); << "Frequency line for " << last_apt_id);
// There used to be no 'return' here. Not sure how useful this is, but // There used to be no 'return' here. Not sure how useful this is, but
// clearly this code block doesn't make sense without it, so here it is. // clearly this code block doesn't make sense without it, so here it is.
return; return;
@ -859,10 +832,8 @@ void APTLoader::parseCommLine(const string& aptDat,
switch (rowCode) { switch (rowCode) {
case 50: case 50:
ty = FGPositioned::FREQ_AWOS; ty = FGPositioned::FREQ_AWOS;
for (size_t i = 2; i < token.size(); ++i) for (size_t i = 2; i < token.size(); ++i) {
{ if (token[i] == "ATIS") {
if (token[i] == "ATIS")
{
ty = FGPositioned::FREQ_ATIS; ty = FGPositioned::FREQ_ATIS;
break; break;
} }
@ -913,4 +884,4 @@ bool metarDataLoad(const SGPath& metar_file)
return true; return true;
} }
} // of namespace flightgear } // namespace flightgear

View file

@ -7,7 +7,6 @@
#pragma once #pragma once
#include "airport.hxx"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -18,18 +17,21 @@
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#include <simgear/structure/SGSharedPtr.hxx> #include <simgear/structure/SGSharedPtr.hxx>
#include "airport.hxx"
class NavDataCache; class NavDataCache;
class sg_gzifstream; class sg_gzifstream;
class FGPavement; class FGPavement;
namespace flightgear { namespace flightgear {
class APTLoader class APTLoader
{ {
public: public:
APTLoader(); APTLoader();
~APTLoader(); virtual ~APTLoader();
// Read the specified apt.dat file into 'airportInfoMap'. // Read the specified apt.dat file into 'airportInfoMap'.
// 'bytesReadSoFar' and 'totalSizeOfAllAptDatFiles' are used for progress // 'bytesReadSoFar' and 'totalSizeOfAllAptDatFiles' are used for progress
@ -43,11 +45,11 @@ public:
// Load a specific airport defined in aptdb_file, and return a "rich" view // Load a specific airport defined in aptdb_file, and return a "rich" view
// of the airport including taxiways, pavement and line features. // of the airport including taxiways, pavement and line features.
const FGAirport* loadAirportFromFile(std::string id, const SGPath& aptdb_file); const FGAirport* loadAirportFromFile(const std::string& id, const SGPath& aptdb_file);
private: private:
struct Line { struct Line {
Line(unsigned int number_, unsigned int rowCode_, std::string str_) Line(unsigned int number_, unsigned int rowCode_, const std::string& str_)
: number(number_), rowCode(rowCode_), str(str_) {} : number(number_), rowCode(rowCode_), str(str_) {}
unsigned int number; unsigned int number;
@ -78,7 +80,7 @@ private:
APTLoader(const APTLoader&); // disable copy constructor APTLoader(const APTLoader&); // disable copy constructor
APTLoader& operator=(const APTLoader&); // disable copy-assignment operator APTLoader& operator=(const APTLoader&); // disable copy-assignment operator
const FGAirport* loadAirport(const std::string aptDat, const std::string airportID, RawAirportInfo* airport_info, bool createFGAirport = false); const FGAirport* loadAirport(const std::string& aptDat, const std::string& airportID, RawAirportInfo* airport_info, bool createFGAirport = false);
// Tell whether an apt.dat line is blank or a comment line // Tell whether an apt.dat line is blank or a comment line
bool isBlankOrCommentLine(const std::string& line); bool isBlankOrCommentLine(const std::string& line);
@ -111,10 +113,10 @@ private:
std::vector<std::string> token; std::vector<std::string> token;
AirportInfoMapType airportInfoMap; AirportInfoMapType airportInfoMap;
double rwy_lat_accum; double rwy_lat_accum{0.0};
double rwy_lon_accum; double rwy_lon_accum{0.0};
double last_rwy_heading; double last_rwy_heading{0.0};
int rwy_count; int rwy_count{0};
std::string last_apt_id; std::string last_apt_id;
double last_apt_elev; double last_apt_elev;
SGGeod tower; SGGeod tower;