#ifndef GERMANAIRLINESVA_FILE_SIMDATA_SIMDATAXP_H #define GERMANAIRLINESVA_FILE_SIMDATA_SIMDATAXP_H #include #include #include #include "simdata/gate.hpp" #include "simdata/runway.hpp" #include "util.hpp" namespace germanairlinesva { namespace file { namespace simdata { inline void makeGate15(std::vector &gates, const std::vector &fields) { std::string gateName; for (size_t j = 4; j < fields.size() - 1; j++) { gateName += fields[j] + " "; } gateName += fields.back(); gateName = std::regex_replace(gateName, std::regex{","}, "0"); gates.emplace_back(gateName, std::stod(fields[1]), std::stod(fields[2]), 40); } inline void makeRunway(std::vector &runways, const std::vector &fields) { runways.emplace_back(fields[8], std::stod(fields[9]), std::stod(fields[10]), std::stod(fields[18]), std::stod(fields[19]), std::stod(fields[1])); runways.emplace_back(fields[17], std::stod(fields[18]), std::stod(fields[19]), std::stod(fields[9]), std::stod(fields[10]), std::stod(fields[1])); } inline void makeGate1300(std::vector &gates, const std::vector &fields) { std::string gateName; for (size_t j = 6; j < fields.size() - 1; j++) { gateName += fields[j] + " "; } gateName += fields.back(); gateName = std::regex_replace(gateName, std::regex{","}, "0"); gates.emplace_back(gateName, std::stod(fields[1]), std::stod(fields[2]), 20); } inline void makeAirport( const std::string &kind, std::ifstream &infile, std::map, std::vector>> &airports, std::ofstream &logfile) { std::string line; std::string *currentIcao = nullptr; std::vector tmpGates; std::vector tmpRunways; int apCount = 0; int validCount = 0; while (std::getline(infile, line)) { std::vector fields = utilities::split(line, ' '); fields = utilities::select_T(fields, [](const std::string &s) { return s.length() > 0; }); if (fields.empty()) continue; if (fields[0] == "1") { // Write to file if ICAO is valid, and we have gates and runways if (currentIcao != nullptr && !tmpRunways.empty() && !tmpGates.empty()) { airports[*currentIcao] = {tmpGates, tmpRunways}; validCount += 1; logfile << "\t " << *currentIcao << " committed" << std::endl; } else if (currentIcao != nullptr) { logfile << "\t " << *currentIcao << " had no gates or runways" << std::endl; } tmpGates = std::vector(); tmpRunways = std::vector(); currentIcao = new std::string(fields[4]); apCount += 1; logfile << "\t<" << kind << "> " << line << std::endl; } else if (currentIcao != nullptr && fields[0] == "15") { makeGate15(tmpGates, fields); logfile << "\t\t " << line << std::endl; } else if (fields[0] == "16" || fields[0] == "17") { // Write to file if ICAO is valid, and we have gates and runways if (currentIcao != nullptr && !tmpRunways.empty() && !tmpGates.empty()) { airports[*currentIcao] = {tmpGates, tmpRunways}; validCount += 1; logfile << "\t " << *currentIcao << " committed" << std::endl; } else if (currentIcao != nullptr) { logfile << "\t " << *currentIcao << " had no gates or runways" << std::endl; } tmpGates = std::vector(); tmpRunways = std::vector(); currentIcao = nullptr; logfile << "\t<" << kind << " SKIPPED> " << line << std::endl; } else if (currentIcao != nullptr && fields[0] == "100") { makeRunway(tmpRunways, fields); logfile << "\t\t " << line << std::endl; } else if (currentIcao != nullptr && fields[0] == "1300") { makeGate1300(tmpGates, fields); logfile << "\t\t " << line << std::endl; } } if (currentIcao != nullptr && !tmpRunways.empty() && !tmpGates.empty()) { airports[*currentIcao] = {tmpGates, tmpRunways}; validCount += 1; logfile << "\t " << *currentIcao << " committed" << std::endl; } logfile << " " << apCount << " airports found, of which " << validCount << " are valid" << std::endl; } inline int scan( const std::string defaultFile, const std::string sceneryPack, const std::string logFile, std::map, std::vector>> &airports) { std::ifstream base(defaultFile); if (!base.good()) { return 1; } std::ifstream custom(sceneryPack); if (!custom.good()) { base.close(); return 2; } std::ofstream logfile(logFile, std::fstream::trunc); if (!logfile.good()) { base.close(); custom.close(); return 3; } // Default logfile << " " << defaultFile << std::endl; makeAirport("DEFAULT", base, airports, logfile); base.close(); std::string line; size_t pos; std::vector packs; while (std::getline(custom, line)) { if ((pos = line.find("SCENERY_PACK")) != std::string::npos) { std::string path = utilities::rtrim_copy(line.substr(pos + 13)) + "Earth nav data/apt.dat"; packs.emplace_back(path); } } std::reverse(packs.begin(), packs.end()); for (std::string const &path : packs) { std::ifstream pack(path); if (pack.good()) { logfile << " " << path << std::endl; makeAirport("CUSTOM", pack, airports, logfile); pack.close(); } else { pack.close(); logfile << "" << "Could not find " << path << std::endl; } } logfile << std::endl << " Total airports: " << airports.size() << std::endl; custom.close(); logfile.close(); return 0; } } // namespace simdata } // namespace file } // namespace germanairlinesva #endif