115 lines
2.9 KiB
C++
115 lines
2.9 KiB
C++
#ifndef GERMANAIRLINESVA_FILE_SIMULATORDATABASE_H
|
|
#define GERMANAIRLINESVA_FILE_SIMULATORDATABASE_H
|
|
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "simdata/gate.h"
|
|
#include "simdata/runway.h"
|
|
#include "simdata/simDatabase.h"
|
|
|
|
/*
|
|
* Header
|
|
*
|
|
* CHAR[5] | UINT8
|
|
* --------+--------
|
|
* VGAS | VERSION
|
|
*/
|
|
/*
|
|
* Airport
|
|
*
|
|
* UINT8 | CHAR[] | UINT16 | GATE[] | UINT8 | RUNWAY[]
|
|
* --------+--------+----------+--------+---------+---------
|
|
* STRLEN | ICAO | NUMGATES | GATES | NUMRWYS | RUNWAYS
|
|
*/
|
|
|
|
namespace germanairlinesva
|
|
{
|
|
namespace file
|
|
{
|
|
namespace simdata
|
|
{
|
|
static inline void toFile(const SimDatabase &database,
|
|
const std::string &file)
|
|
{
|
|
std::ofstream out(file, std::fstream::binary);
|
|
|
|
// File Header, Last member is version
|
|
std::uint8_t header[] = {'V', 'G', 'A', 'S', '\0', 1};
|
|
out.write(reinterpret_cast<const char *>(header), 6);
|
|
|
|
database.toFile(out);
|
|
|
|
out.close();
|
|
}
|
|
|
|
static inline const SimDatabase readVersion1(std::ifstream &in)
|
|
{
|
|
SimDatabase database;
|
|
|
|
std::uint16_t numAirports = read<std::uint16_t>(in);
|
|
|
|
for (int i = 0; i < numAirports; i++) {
|
|
// ICAO
|
|
std::string icao = readString(in);
|
|
// Gates
|
|
std::uint16_t numGates = read<std::uint16_t>(in);
|
|
for (int j = 0; j < numGates; j++) {
|
|
std::string designator = readString(in);
|
|
struct germanairlinesva::geodata::point center =
|
|
read<struct germanairlinesva::geodata::point>(in);
|
|
std::uint8_t radius = read<std::uint8_t>(in);
|
|
|
|
database.addGate(icao, designator, center, radius);
|
|
}
|
|
// Runways
|
|
std::uint8_t numRunways = read<std::uint8_t>(in);
|
|
for (int j = 0; j < numRunways; j++) {
|
|
std::string designator = readString(in);
|
|
// Bounds
|
|
struct germanairlinesva::geodata::box bounds =
|
|
read<struct germanairlinesva::geodata::box>(in);
|
|
std::uint8_t width = read<std::uint8_t>(in);
|
|
std::uint16_t length = read<std::uint16_t>(in);
|
|
std::uint16_t trueHeading = read<std::uint16_t>(in);
|
|
|
|
database
|
|
.addRunway(icao, designator, bounds, width, length, trueHeading);
|
|
}
|
|
}
|
|
|
|
in.close();
|
|
|
|
return database;
|
|
}
|
|
|
|
static inline const SimDatabase fromFile(const std::string &file)
|
|
{
|
|
std::ifstream in(file, std::ifstream::binary);
|
|
|
|
// File Header
|
|
char ident[5];
|
|
in.read(ident, 5);
|
|
if (strcmp(ident, "VGAS") != 0) {
|
|
throw std::invalid_argument("Wrong file");
|
|
}
|
|
std::uint8_t version = read<std::uint8_t>(in);
|
|
|
|
if (version == 1) {
|
|
return readVersion1(in);
|
|
}
|
|
|
|
in.close();
|
|
return SimDatabase();
|
|
}
|
|
} // namespace simdata
|
|
} // namespace file
|
|
} // namespace germanairlinesva
|
|
|
|
#endif
|