-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsfsformat.cpp
166 lines (150 loc) · 4.76 KB
/
msfsformat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Created by Ian Parker on 23/10/2024.
//
#include "msfsformat.h"
#include "flightplan.h"
#include "utils.h"
using namespace std;
using namespace UFC;
float dmmToDegrees(string dmm)
{
char d = dmm.at(0);
dmm = dmm.substr(1);
auto degrees = (float)atof(dmm.c_str());
auto idx = dmm.find(' ');
dmm = dmm.substr(idx + 1);
auto dm = (float)atof(dmm.c_str());
degrees += (dm / 60.0f);
if (d == 'S' || d == 'W')
{
degrees = -degrees;
}
return degrees;
}
std::shared_ptr<FlightPlan> MSFSFormat::load(std::shared_ptr<FlightConverter> flightConverter, std::string filename)
{
auto text = readTextFile(filename, false);
if (text.at(0).at(0) != "[flightplan]")
{
printf("MSFSFormat::load: Not a MSFS Flight Plan\n");
}
auto flightPlan = make_shared<FlightPlan>();
for (auto lineVec : text)
{
string line = lineVec.at(0);
string key;
string value;
auto idx = line.find('=');
if (idx != string::npos)
{
key = line.substr(0, idx);
value = line.substr(idx + 1);
}
trim(key);
trim(value);
if (key == "type")
{
printf("MSFSFormat::load: Type=%s\n", value.c_str());
if (value == "VFR")
{
flightPlan->m_type = FlightPlanType::VFR;
}
else if (value == "IFR")
{
flightPlan->m_type = FlightPlanType::IFR;
}
}
else if (key == "departure_id")
{
idx = value.find(',');
if (idx != string::npos)
{
value = value.substr(0, idx);
}
trim(value);
printf("MSFSFormat::load: Departure Airport=%s\n", value.c_str());
flightPlan->m_departureAirport = value;
}
else if (key == "departure_position")
{
printf("MSFSFormat::load: Departure Runway=%s\n", value.c_str());
flightPlan->m_departureRunway = value.c_str();
}
else if (key == "destination_id")
{
idx = value.find(',');
if (idx != string::npos)
{
value = value.substr(0, idx);
}
trim(value);
printf("MSFSFormat::load: Destination Airport=%s\n", value.c_str());
flightPlan->m_destinationAirport = value;
}
else if (key == "destination_position")
{
printf("MSFSFormat::load: Destination Runway=%s\n", value.c_str());
flightPlan->m_destinationRunway = value.c_str();
}
else if (key == "cruising_altitude")
{
flightPlan->m_cruisingAltitude = atof(value.c_str());
printf("MSFSFormat::load: Cruising Altitude=%d\n", flightPlan->m_cruisingAltitude);
}
else if (key.starts_with("waypoint."))
{
printf("MSFSFormat::load: Waypoint: %s\n", value.c_str());
auto waypointLine = splitString(value, ',');
string name;
string typeStr;
string latStr;
string lonStr;
// There doesn't seem to be any specification for this file, but I've seen
// two different waypoint types!
if (waypointLine.size() == 5)
{
name = waypointLine.at(0);
typeStr = waypointLine.at(1);
latStr = waypointLine.at(2);
lonStr = waypointLine.at(3);
}
else
{
name = waypointLine.at(0);
typeStr = waypointLine.at(3);
latStr = waypointLine.at(4);
lonStr = waypointLine.at(5);
}
printf("MSFSFormat::load: Waypoint: -> name: %s\n", name.c_str());
trim(typeStr);
WayPoint wayPoint;
if (typeStr == "A")
{
// Airport, skip it
printf("MSFSFormat::load: Waypoint: -> Skipping airport\n");
continue;
}
if (typeStr == "V")
{
wayPoint.type = NavAidType::VOR;
}
else if (typeStr == "I")
{
wayPoint.type = NavAidType::FIX;
}
trim(latStr);
float lat = dmmToDegrees(latStr);
trim(lonStr);
float lon = dmmToDegrees(lonStr);
printf("MSFSFormat::load: Waypoint: -> %0.5f, %0.5f\n", lat, lon);
wayPoint.id = name;
wayPoint.coordinate = Coordinate(lat, lon);
flightPlan->m_waypoints.push_back(wayPoint);
}
else
{
printf("MSFSFormat::load: Unhandled line: %s\n", line.c_str());
}
}
return flightPlan;
}