-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPConv.cpp
219 lines (173 loc) · 8.56 KB
/
FPConv.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// FPConv.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <direct.h>
#include <list>
#include <Windows.h>
#define MAKE_WP_TYPE(ch1, ch2) (((ch1) << 8) | (ch2))
#define NRESULT_OK 0
#define countof(x) (sizeof(x) / sizeof(x[0]))
typedef int NRESULT;
struct SFPLWaypoint
{
BYTE byType;
char szName[6];
float fLat, fLon;
INT nHeight;
};
typedef std::list<SFPLWaypoint> FLIGHTPLAN;
static FLIGHTPLAN s_fpl;
void printUsage()
{
printf("USAGE: FPConv.exe <infile> <cycle> [<out path>]\n\n");
printf("<infile> - Jeppesen FliteStar/FliteMap flight plan in generic text format.\n");
printf("<cycle> - 4-symbol cycle number, i.e. '1711', '1802' and so on\n");
printf("<out path> - Path to output .fms files.\n");
}
NRESULT readPlan(FILE* hFile, FLIGHTPLAN* pFpl)
{
SFPLWaypoint wp;
NRESULT nRes = NRESULT_OK;
INT n;
float f;
char sz[128], *psz;
pFpl->clear();
while (fgets(sz, countof(sz), hFile))
{
// Если встретился FPL - выходим с мыслью вернуться и продолжать
if (!strncmp(sz, "FPL", 3))
{
nRes = 1;
break;
}
else if (!strncmp(sz, "FWP", 3))
{
n = MAKE_WP_TYPE(sz[4], sz[6]);
switch (n)
{
case MAKE_WP_TYPE('p', 'a'): // Airport
wp.byType = 1;
break;
case MAKE_WP_TYPE('p', 'n'): // NDB
wp.byType = 2;
break;
case MAKE_WP_TYPE('p', 'v'): // VOR
wp.byType = 3;
break;
case MAKE_WP_TYPE('p', 'i'): // Intersect
wp.byType = 11;
break;
case MAKE_WP_TYPE('u', 's'): // Airport
wp.byType = 28;
break;
default:
printf("Wrong waypoint type: %s", sz);
nRes = -1;
}
if (nRes) break;
strncpy(wp.szName, &sz[8], countof(wp.szName));
psz = strchr(wp.szName, ' ');
if (psz) *psz = 0;
sscanf(&sz[16], "%d %f", &n, &f);
f = n + f / 60;
if (sz[14] == 'S') f *= -1;
else if (sz[14] != 'N')
{
printf("Wrong symbol in 14-th column (latitude): %s", sz);
nRes = -2;
break;
}
wp.fLat = f;
sscanf(&sz[27], "%d %f", &n, &f);
f = n + f / 60;
if (sz[25] == 'W') f *= -1;
else if (sz[25] != 'E')
{
printf("Wrong symbol in 25-th column (longitude): %s", sz);
nRes = -3;
break;
}
wp.fLon = f;
sscanf(&sz[37], "%d", &wp.nHeight);
pFpl->push_back(wp);
}
}
return nRes;
}
NRESULT writePlan(FLIGHTPLAN* pFpl, char* szCycle)
{
char sz[32];
FLIGHTPLAN::iterator wp, wplast;
//SFPLWaypoint wp;
FILE* hFile;
INT n;
if (pFpl->empty()) return NRESULT_OK;
sprintf(sz, "%s-%s.fms", pFpl->front().szName, pFpl->back().szName);
hFile = fopen(sz, "w+");
if (!hFile)
{
printf("Output file open error\n");
return -5;
}
fprintf(hFile, "I\n1100 Version\nCYCLE %s\n", szCycle);
// Departure
wp = pFpl->begin();
fprintf(hFile, "%sDEP %s\n", wp->byType == 1 ? "A" : "", wp->szName);
// Destination
wp = pFpl->end(); wplast = --wp;
fprintf(hFile, "%sDES %s\n", wp->byType == 1 ? "A" : "", wp->szName);
// Waypoints
n = pFpl->size();
if (n > 1)
{
fprintf(hFile, "NUMENR %d\n", n);
for (wp = pFpl->begin(); wp != pFpl->end(); ++wp)
{
fprintf(hFile, "%d %s", wp->byType, wp->szName);
if (wp == pFpl->begin()) fprintf(hFile, " ADEP");
else if (wp == wplast) fprintf(hFile, " ADES");
else fprintf(hFile, " DRCT");
fprintf(hFile, " %.6f %.6f %.6f\n", (float)wp->nHeight, wp->fLat, wp->fLon);
}
}
fclose(hFile);
printf("%s written\n", sz);
return NRESULT_OK;
}
int _tmain(int argc, _TCHAR* argv[])
{
char szCD[256];
FILE* hFile;
NRESULT nRes;
INT n;
printf("Jeppesen FliteStar/FliteMap to X-plane FMS flight plan converter\n\n");
if (argc < 3 || argc > 4)
{
printUsage();
return -1;
}
if (!_getcwd(szCD, countof(szCD))) return -5;
hFile = fopen(argv[1], "r");
if (!hFile)
{
printf("ERROR: couldn't open file \"%s\"\n\n", argv[1]);
return -2;
}
do
{
nRes = readPlan(hFile, &s_fpl);
if (argc == 4)
{
n = _chdir(argv[3]);
if (n < 0)
{
printf("Output dir open error\n");
return -4;
}
}
if (writePlan(&s_fpl, argv[2])) break;
if (argc == 4) _chdir(szCD);
} while (nRes > 0);
fclose(hFile);
return 0;
}