Header only File Lib

This commit is contained in:
2022-09-24 13:39:26 +02:00
parent 9deae93fdd
commit 46214e9dc3
20 changed files with 396 additions and 590 deletions
-50
View File
@@ -1,50 +0,0 @@
#ifndef GERMANAIRLINESVA_FILE_LOGBOOK_H
#define GERMANAIRLINESVA_FILE_LOGBOOK_H
#include <cstdint>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include "constants.h"
#include "helpers.hpp"
#include "logbookEntry.h"
#include "util.hpp"
namespace germanairlinesva
{
namespace file
{
namespace logbook
{
/*
* Logbook Header (6)
* CHAR[5] | UINT8
* --------+--------
* VGAL | VERSION
*
* Logbook Entries (n)
* LOGBOOKENTRY[]
*/
class Logbook
{
private:
std::vector<LogbookEntry> entries;
void fromFile();
void readVersion1(std::ifstream &in);
public:
Logbook();
template <class... Args> inline void addEntry(Args &&...args)
{
this->entries.emplace_back(std::forward<Args>(args)...);
}
void toFile() const;
};
} // namespace logbook
} // namespace file
} // namespace germanairlinesva
#endif
+143
View File
@@ -0,0 +1,143 @@
#ifndef GERMANAIRLINESVA_FILE_LOGBOOK_LOGBOOK_H
#define GERMANAIRLINESVA_FILE_LOGBOOK_LOGBOOK_H
#include <cstdint>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include "constants.h"
#include "helpers.hpp"
#include "logbookEntry.hpp"
#include "util.hpp"
namespace germanairlinesva
{
namespace file
{
namespace logbook
{
/*
* Logbook Header (6)
* CHAR[5] | UINT8
* --------+--------
* VGAL | VERSION
*
* Logbook Entries (n)
* LOGBOOKENTRY[]
*/
class Logbook
{
private:
std::vector<LogbookEntry> entries;
inline void readVersion1(std::ifstream &in)
{
std::string date = readString(in, 10);
std::string flightNumber = readString(in, 4);
std::string aircraftType = readString(in, 4);
std::string aircraftRegistration = readString(in, 6);
std::string departureAirport = readString(in);
std::string departureGate = readString(in);
std::string departureRunway = readString(in);
std::string arrivalAirport = readString(in);
std::string arrivalGate = readString(in);
std::string arrivalRunway = readString(in);
std::string offBlockTime = readString(in, 5);
std::string outTime = readString(in, 5);
std::string inTime = readString(in, 5);
std::string onBlockTime = readString(in, 5);
float totalFlightTime = read<float>(in);
float taxiOutFuel = read<float>(in);
float inFlightFuel = read<float>(in);
float taxiInFuel = read<float>(in);
float totalFuel = read<float>(in);
float taxiOutDistance = read<float>(in);
float inFlightDistance = read<float>(in);
float taxiInDistance = read<float>(in);
float totalDistance = read<float>(in);
float maxLandingRate = read<float>(in);
std::uint8_t touchdowns = read<std::uint8_t>(in);
float maxLandingGees = read<float>(in);
std::string recordingFilename = readString(in);
float points = read<float>(in);
std::uint8_t flags = read<std::uint8_t>(in);
this->addEntry(date,
flightNumber,
aircraftType,
aircraftRegistration,
departureAirport,
departureGate,
departureRunway,
arrivalAirport,
arrivalGate,
arrivalRunway,
offBlockTime,
outTime,
inTime,
onBlockTime,
totalFlightTime,
taxiOutFuel,
inFlightFuel,
taxiInFuel,
totalFuel,
taxiOutDistance,
inFlightDistance,
taxiInDistance,
totalDistance,
maxLandingRate,
touchdowns,
maxLandingGees,
recordingFilename,
points,
flags);
}
inline void fromFile()
{
std::ifstream in(BASE_DIRECTORY LOGBOOK, std::ifstream::binary);
// File Header
std::string ident = readString(in, 5);
if (ident.compare("VGAL") != 0) {
throw std::invalid_argument("Wrong file");
}
std::uint8_t version = read<std::uint8_t>(in);
if (version == 1) {
while (in.peek() != EOF) {
this->readVersion1(in);
}
}
}
public:
inline Logbook()
{
if (util::fileExists(BASE_DIRECTORY LOGBOOK)) {
this->fromFile();
}
}
template <class... Args> inline void addEntry(Args &&...args)
{
this->entries.emplace_back(std::forward<Args>(args)...);
}
inline void toFile() const
{
std::ofstream out(BASE_DIRECTORY LOGBOOK, std::fstream::binary);
char header[] = {'V', 'G', 'A', 'L', '\0', 1};
out.write(header, 6);
for (const LogbookEntry &entry : this->entries) {
entry.toFile(out);
}
out.close();
}
};
} // namespace logbook
} // namespace file
} // namespace germanairlinesva
#endif
-158
View File
@@ -1,158 +0,0 @@
#ifndef GERMANAIRLINESVA_FILE_LOGBOOKENTRY_H
#define GERMANAIRLINESVA_FILE_LOGBOOKENTRY_H
#include <cstdint>
#include <fstream>
#include <string>
#include <vector>
#include "helpers.hpp"
namespace germanairlinesva
{
namespace file
{
namespace logbook
{
/*
* Preamble (24)
* CHAR[10] | CHAR[4] | CHAR[4] | CHAR[6]
* ---------+---------------+----------+-------------
* DATE | FLIGHT NUMBER | AIRCRAFT | REGISTRATION
* Departure Airport Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Departure Gate Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Departure Runway Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Arrival Airport Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Arrival Gate Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Arrival Runway Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Times (24)
* CHAR[5] | CHAR[5] | CHAR[5] | CHAR[4] | FLOAT
* ----------+---------------+--------------+---------------+------
* OFF BLOCK | TAKEOFF (OUT) | LANDING (ON) | ON BLOCK (IN) | TOTAL
* Fuels (16)
* FLOAT | FLOAT | FLOAT | FLOAT
* ---------+-----------+---------+------
* TAXI OUT | IN FLIGHT | TAXI IN | TOTAL
* Distances (16)
* FLOAT | FLOAT | FLOAT |FLOAT
* ---------+-----------+---------+-----
* TAXI OUT | IN FLIGHT | TAXI IN |TOTAL
* Landing (9)
* FLOAT | CHAR | FLOAT
* ---------+---------- +------------
* MAX RATE | TOUCHDOWNS | MAX G-FORCE
* Recording Filename (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Postamble (5)
* FLOAT | BITFIELD
* -------+---------
* POINTS | FLAGS
* Flags Bitfield
* 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
* ----+-----+-----+-----+-----+-----+-----+------
* NIL | NIL | NIL | NIL | NIL | NIL | NIL | FILED
*/
class LogbookEntry
{
private:
std::string date;
std::string flightNumber;
std::string aircraftType;
std::string aircraftRegistration;
std::string departureAirport;
std::string departureGate;
std::string departureRunway;
std::string arrivalAirport;
std::string arrivalGate;
std::string arrivalRunway;
std::string offBlockTime;
std::string outTime;
std::string onTime;
std::string onBlockTime;
float totalFlightTime;
float taxiOutFuel;
float inFlightFuel;
float taxiInFuel;
float totalFuel;
float taxiOutDistance;
float inFlightDistance;
float taxiInDistance;
float totalDistance;
float maxLandingRate;
std::uint8_t touchdowns;
float maxLandingGees;
std::string recordingFilename;
float points;
std::uint8_t flags;
public:
LogbookEntry(std::string date,
std::string flightNumber,
std::string aircraftType,
std::string aircraftRegistration,
std::string departureAirport,
std::string departureGate,
std::string departureRunway,
std::string arrivalAirport,
std::string arrivalGate,
std::string arrivalRunway,
std::string offBlockTime,
std::string outTime,
std::string onTime,
std::string onBlockTime,
float totalFlightTime,
float taxiOutFuel,
float inFlightFuel,
float taxiInFuel,
float totalFuel,
float taxiOutDistance,
float inFlightDistance,
float taxiInDistance,
float totalDistance,
float maxLandingRate,
std::uint8_t touchdowns,
float maxLandingGees,
std::string recordingFilename,
float points,
std::uint8_t flags);
void toFile(std::ofstream &out) const;
};
} // namespace logbook
} // namespace file
} // namespace germanairlinesva
#endif
+207
View File
@@ -0,0 +1,207 @@
#ifndef GERMANAIRLINESVA_FILE_LOGBOOK_LOGBOOKENTRY_H
#define GERMANAIRLINESVA_FILE_LOGBOOK_LOGBOOKENTRY_H
#include <cstdint>
#include <fstream>
#include <string>
#include <vector>
#include "helpers.hpp"
namespace germanairlinesva
{
namespace file
{
namespace logbook
{
/*
* Preamble (24)
* CHAR[10] | CHAR[4] | CHAR[4] | CHAR[6]
* ---------+---------------+----------+-------------
* DATE | FLIGHT NUMBER | AIRCRAFT | REGISTRATION
* Departure Airport Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Departure Gate Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Departure Runway Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Arrival Airport Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Arrival Gate Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Arrival Runway Name (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Times (24)
* CHAR[5] | CHAR[5] | CHAR[5] | CHAR[4] | FLOAT
* ----------+---------------+--------------+---------------+------
* OFF BLOCK | TAKEOFF (OUT) | LANDING (ON) | ON BLOCK (IN) | TOTAL
* Fuels (16)
* FLOAT | FLOAT | FLOAT | FLOAT
* ---------+-----------+---------+------
* TAXI OUT | IN FLIGHT | TAXI IN | TOTAL
* Distances (16)
* FLOAT | FLOAT | FLOAT |FLOAT
* ---------+-----------+---------+-----
* TAXI OUT | IN FLIGHT | TAXI IN |TOTAL
* Landing (9)
* FLOAT | CHAR | FLOAT
* ---------+---------- +------------
* MAX RATE | TOUCHDOWNS | MAX G-FORCE
* Recording Filename (2...256)
* UINT8 | CHAR[]
* -------+-------
* STRLEN | STRING
* Postamble (5)
* FLOAT | BITFIELD
* -------+---------
* POINTS | FLAGS
* Flags Bitfield
* 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
* ----+-----+-----+-----+-----+-----+-----+------
* NIL | NIL | NIL | NIL | NIL | NIL | NIL | FILED
*/
class LogbookEntry
{
private:
std::string date;
std::string flightNumber;
std::string aircraftType;
std::string aircraftRegistration;
std::string departureAirport;
std::string departureGate;
std::string departureRunway;
std::string arrivalAirport;
std::string arrivalGate;
std::string arrivalRunway;
std::string offBlockTime;
std::string outTime;
std::string onTime;
std::string onBlockTime;
float totalFlightTime;
float taxiOutFuel;
float inFlightFuel;
float taxiInFuel;
float totalFuel;
float taxiOutDistance;
float inFlightDistance;
float taxiInDistance;
float totalDistance;
float maxLandingRate;
std::uint8_t touchdowns;
float maxLandingGees;
std::string recordingFilename;
float points;
std::uint8_t flags;
public:
inline LogbookEntry(std::string date,
std::string flightNumber,
std::string aircraftType,
std::string aircraftRegistration,
std::string departureAirport,
std::string departureGate,
std::string departureRunway,
std::string arrivalAirport,
std::string arrivalGate,
std::string arrivalRunway,
std::string offBlockTime,
std::string outTime,
std::string onTime,
std::string onBlockTime,
float totalFlightTime,
float taxiOutFuel,
float inFlightFuel,
float taxiInFuel,
float totalFuel,
float taxiOutDistance,
float inFlightDistance,
float taxiInDistance,
float totalDistance,
float maxLandingRate,
std::uint8_t touchdowns,
float maxLandingGees,
std::string recordingFilename,
float points,
std::uint8_t flags)
: date(date), flightNumber(flightNumber),
aircraftType(aircraftType),
aircraftRegistration(aircraftRegistration),
departureAirport(departureAirport), departureGate(departureGate),
departureRunway(departureRunway), arrivalAirport(arrivalAirport),
arrivalGate(arrivalGate), arrivalRunway(arrivalRunway),
offBlockTime(offBlockTime), outTime(outTime), onTime(onTime),
onBlockTime(onBlockTime), totalFlightTime(totalFlightTime),
taxiOutFuel(taxiOutFuel), inFlightFuel(inFlightFuel),
taxiInFuel(taxiInFuel), totalFuel(totalFuel),
taxiOutDistance(taxiOutDistance),
inFlightDistance(inFlightDistance),
taxiInDistance(taxiInDistance), totalDistance(totalDistance),
maxLandingRate(maxLandingRate), touchdowns(touchdowns),
maxLandingGees(maxLandingGees),
recordingFilename(recordingFilename), points(points), flags(flags)
{
}
inline void toFile(std::ofstream &out) const
{
writeString(out, this->date, 10);
writeString(out, this->flightNumber, 4);
writeString(out, this->aircraftType, 4);
writeString(out, this->aircraftRegistration, 6);
writeString(out, this->departureAirport);
writeString(out, this->departureGate);
writeString(out, this->departureRunway);
writeString(out, this->arrivalAirport);
writeString(out, this->arrivalGate);
writeString(out, this->arrivalRunway);
writeString(out, this->offBlockTime, 5);
writeString(out, this->outTime, 5);
writeString(out, this->onTime, 5);
writeString(out, this->onBlockTime, 5);
write<decltype(this->totalFlightTime)>(out, this->totalFlightTime);
write<decltype(this->taxiOutFuel)>(out, this->taxiOutFuel);
write<decltype(this->inFlightFuel)>(out, this->inFlightFuel);
write<decltype(this->taxiInFuel)>(out, this->taxiInFuel);
write<decltype(this->totalFuel)>(out, this->totalFuel);
write<decltype(this->taxiOutDistance)>(out, this->taxiOutDistance);
write<decltype(this->inFlightDistance)>(out, this->inFlightDistance);
write<decltype(this->taxiInDistance)>(out, this->taxiInDistance);
write<decltype(this->totalDistance)>(out, this->totalDistance);
write<decltype(this->maxLandingRate)>(out, this->maxLandingRate);
write<decltype(this->touchdowns)>(out, this->touchdowns);
write<decltype(this->maxLandingGees)>(out, this->maxLandingGees);
writeString(out, this->recordingFilename);
write<decltype(this->points)>(out, this->points);
write<decltype(this->flags)>(out, this->flags);
}
};
} // namespace logbook
} // namespace file
} // namespace germanairlinesva
#endif