forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g7towin.cc
592 lines (493 loc) · 12.4 KB
/
g7towin.cc
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
Support for G7ToWin data files (.g7t),
Copyright (C) 2007 Olaf Klein, o.b.klein@gpsbabel.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
*/
/*
History:
04/07/2007: start programming
04/15/2007: added to gpsbabel
*/
#include "defs.h"
#include "cet_util.h"
#include "csv_util.h"
#include "garmin_fs.h"
#include "garmin_tables.h"
#include "jeeps/gpsmath.h"
#include "strptime.h"
#include <cstdio>
#include <cstdlib>
#include <ctime>
#if CSVFMTS_ENABLED
#define MYNAME "g7towin"
#define G7T_HEADER "Version 2:G7T"
static gbfile* fin;
static grid_type grid;
static int datum;
static gpsdata_type mode;
static double altf;
static int gardown;
static int event_ct;
static
arglist_t g7towin_args[] = {
ARG_TERMINATOR
};
#define WAYPT__OFS 0x00000
#define TRKPT__OFS 0x01000
#define WPT_c0_OFS 0x0c000
#define WPT_c1_OFS 0x0c100
#define WPT_c2_OFS 0x0c200
#define WPT_c3_OFS 0x0c300
#define WPT_c4_OFS 0x0c400
#define WPT_c5_OFS 0x0c500
#define WPT_c6_OFS 0x0c600
#define WPT_c7_OFS 0x0c700
#define WPT_c8_OFS 0x0c800
#define WPT_cA_OFS 0x0cA00
#define WPT_cB_OFS 0x0cB00
#define WPT_cC_OFS 0x0cC00
#define WPT_cD_OFS 0x0cD00
static void
parse_line(char* buff, int index, const char* delimiter, Waypoint* wpt)
{
char* cin;
garmin_fs_p gmsd = GMSD_FIND(wpt);
while ((cin = csv_lineparse(buff, delimiter, "", index++))) {
buff = nullptr;
cin = lrtrim(cin);
if ((*cin == '\0') ||
(strcmp(cin, "INF") == 0) ||
(strcmp(cin, "1e25") == 0) ||
(strcmp(cin, "1.0e25") == 0)) {
continue;
}
switch (index) {
int categories;
struct tm tm;
char* cerr;
case TRKPT__OFS + 1:
cin += parse_coordinates(cin, datum, grid,
&wpt->latitude, &wpt->longitude, MYNAME);
while (isspace(*cin)) {
cin++;
}
memset(&tm, 0, sizeof(tm));
cerr = strptime(cin, "%a %b %d %H:%M:%S %Y", &tm);
if (cerr == nullptr) {
fatal(MYNAME ": Unable to convert date (%s)!\n", cin);
}
wpt->SetCreationTime(mkgmtime(&tm));
break;
case WAYPT__OFS + 1:
wpt->description = (cin);
break;
case WAYPT__OFS + 2:
wpt->icon_descr = gt_find_desc_from_icon_number(
atoi(cin), PCX);
break;
case WAYPT__OFS + 4:
if (strcmp(cin, "S+C") == 0) {
GMSD_SET(display, gt_display_mode_symbol_and_comment);
} else if (strcmp(cin, "S") == 0) {
GMSD_SET(display, gt_display_mode_symbol);
} else if (strcmp(cin, "S+N") == 0) {
GMSD_SET(display, gt_display_mode_symbol_and_name);
}
break;
case WPT_cA_OFS + 1:
case WPT_c1_OFS + 1:
wpt->shortname = cin;
break;
case WPT_cA_OFS + 4:
case WPT_c4_OFS + 2:
GMSD_SETSTR(city, cin);
break;
case WPT_cA_OFS + 5:
case WPT_c4_OFS + 3:
GMSD_SETSTR(state, cin);
break;
case WPT_cA_OFS + 6:
case WPT_c4_OFS + 4:
GMSD_SETSTR(cc, cin);
break;
case WPT_cB_OFS + 1:
case WPT_c6_OFS + 2:
GMSD_SETSTR(facility, cin);
break;
case WPT_cB_OFS + 2:
case WPT_c6_OFS + 3:
GMSD_SETSTR(addr, cin);
break;
case WPT_cB_OFS + 3: /*cross road */
case WPT_c6_OFS + 4:
GMSD_SETSTR(cross_road, cin);
break;
case TRKPT__OFS + 2: /* altitude */
case WPT_cC_OFS + 1:
case WPT_c5_OFS + 1:
case WPT_c8_OFS + 1:
wpt->altitude = altf * atof(cin);
break;
case TRKPT__OFS + 3: /* depth */
case WPT_cC_OFS + 2:
case WPT_c5_OFS + 2:
case WPT_c8_OFS + 2:
WAYPT_SET(wpt, depth, altf * atof(cin));
break;
case TRKPT__OFS + 10: /* temperature */
if (*cin == '|') {
cin++; /* in track points */
}
if (strcmp(cin, "1e25") == 0) {
break;
}
if (strcmp(cin, "1.0e25") == 0) {
break;
}
/* !!! NO BREAK !!! */
case WPT_cD_OFS + 1:
case WPT_cB_OFS + 6:
WAYPT_SET(wpt, temperature, atof(cin));
break;
case WAYPT__OFS + 6: /* proximity */
case WPT_cD_OFS + 2:
WAYPT_SET(wpt, proximity, atof(cin));
break;
case WPT_cB_OFS + 5:
case WPT_cD_OFS + 3:
categories = atoi(cin);
if (categories != 0) {
GMSD_SET(category, atoi(cin));
}
break;
#if 0
/* currently unused */
case TRKPT__OFS + 5: /* distance from previous point */
case TRKPT__OFS + 6: /* distance from segment start */
case TRKPT__OFS + 7: /* distance from start */
case TRKPT__OFS + 8: /* velocity from previous point */
case TRKPT__OFS + 9: /* time (in seconds) from previous point */
break;
case WAYPT__OFS + 3: /* ignore color */
break;
case WAYPT__OFS + 5: /* always '0' */
break;
case TRKPT__OFS + 4:
if (case_ignore_strcmp(cin, "FT") == 0) ;
else if (case_ignore_strcmp(cin, "M") == 0) ;
else if (case_ignore_strcmp(cin, "SM") == 0) ;
else if (case_ignore_strcmp(cin, "NM") == 0) ;
else if (case_ignore_strcmp(cin, "KM") == 0) ;
break;
case WPT_cB_OFS + 4: /* unknown (datatype) */
break;
case WPT_cC_OFS + 3: /* waypt_class (always FF) */
break;
case WPT_cC_OFS + 4: /* class & subclass */
case WPT_cC_OFS + 5:
case WPT_cC_OFS + 6:
case WPT_cC_OFS + 7:
case WPT_cC_OFS + 8:
case WPT_cC_OFS + 9:
case WPT_cC_OFS + 10:
case WPT_cC_OFS + 11:
case WPT_cC_OFS + 12:
case WPT_cC_OFS + 13:
case WPT_cC_OFS + 14:
case WPT_cC_OFS + 15:
case WPT_cC_OFS + 16:
case WPT_cC_OFS + 17:
case WPT_cC_OFS + 18:
case WPT_cC_OFS + 19:
case WPT_cC_OFS + 20:
case WPT_cC_OFS + 21:
break;
case WPT_cC_OFS + 22:
/* distance */
break;
#endif
}
}
}
static Waypoint*
parse_waypt(char* buff)
{
char* cin;
struct tm tm;
Waypoint* wpt = new Waypoint;
garmin_fs_p gmsd = garmin_fs_alloc(-1);
fs_chain_add(&wpt->fs, (format_specific_data*) gmsd);
if (gardown) {
cin = buff + 6;
} else {
/* We've seen waypoints with length of 14 and 15 !!! */
cin = buff + 15;
while ((cin > buff) && (! isspace(*cin))) {
cin--;
}
}
while (isspace(*cin)) {
cin--;
}
if (cin >= buff) {
char*s = xstrndup(buff, cin - buff + 1);
wpt->shortname = s;
xfree(s);
}
if (gardown) {
buff += 6;
} else {
buff += 15;
}
while (isspace(*buff)) {
buff++;
}
buff += parse_coordinates(buff, datum, grid,
&wpt->latitude, &wpt->longitude, MYNAME);
while (isspace(*buff)) {
buff++;
}
memset(&tm, 0, sizeof(tm));
char* cerr = strptime(buff, "%a %b %d %H:%M:%S %Y", &tm);
if (cerr == nullptr) {
fatal(MYNAME ": Unable to convert date (%s)!\n", buff);
}
wpt->SetCreationTime(mkgmtime(&tm));
/* go over time stamp */
int i = 5;
while (buff && i) {
i--;
buff = strchr(buff, ' ');
if (buff) {
buff++;
}
}
if (gardown && (buff == nullptr)) {
return wpt;
}
is_fatal((buff == nullptr), MYNAME ": Incomplete waypoint line!");
while (isspace(*buff)) {
buff++;
}
parse_line(buff, WAYPT__OFS, "^", wpt);
return wpt;
}
static Waypoint*
parse_trkpt(char* buff)
{
Waypoint* wpt = new Waypoint;
garmin_fs_p gmsd = garmin_fs_alloc(-1);
fs_chain_add(&wpt->fs, (format_specific_data*) gmsd);
parse_line(buff, TRKPT__OFS, ";", wpt);
return wpt;
}
/*
* parse_categories is currently only a dummy procedure.
* w'll need a central storage with binding to the module
* which has established a list of category names.
*/
static void
parse_categories(char* buff)
{
char* cin;
int cat = 0;
while ((cin = csv_lineparse(buff, ",", "", cat++))) {
uint16_t cx;
buff = nullptr;
cin = lrtrim(cin);
if (*cin == 0) {
continue;
}
garmin_fs_convert_category(cin, &cx);
}
}
/* main functions */
static void
rd_init(const QString& fname)
{
fin = gbfopen(fname, "rb", MYNAME);
gardown = 1;
mode = wptdata;
grid = grid_lat_lon_dmm;
datum = DATUM_WGS84;
altf = 1;
event_ct = 0;
}
static void
rd_deinit()
{
gbfclose(fin);
}
static void
data_read()
{
char* buff;
int line = 0;
Waypoint* wpt = nullptr;
route_head* head = nullptr;
while ((buff = gbfgetstr(fin))) {
if ((line++ == 0) && fin->unicode) {
cet_convert_init(CET_CHARSET_UTF8, 1);
}
char* cin = lrtrim(buff);
if (!*cin) {
continue;
}
char* cdata = cin+1;
while (! isspace(*cdata)) {
cdata++;
}
while (isspace(*cdata)) {
cdata++;
}
if (! *cdata) {
continue;
}
switch (*cin) {
case '#': /* comment */
break;
case 'A':
if (case_ignore_strncmp(cdata, "Meter", 5) == 0) {
altf = 1.0;
} else if (case_ignore_strncmp(cdata, "Feet", 4) == 0) {
altf = FEET_TO_METERS(1.0);
}
break;
case 'C': /* categories */
parse_categories(cdata);
break;
case 'D':
datum = gt_lookup_datum_index(cdata, MYNAME);
break;
case 'I': /* event point */
wpt = new Waypoint;
cdata += parse_coordinates(cdata, datum, grid,
&wpt->latitude, &wpt->longitude, MYNAME);
wpt->shortname = QString("Event%1").arg(++event_ct);
while (isspace(*cdata)) {
cdata++;
}
if (*cdata == ';') {
cdata++;
wpt->icon_descr = gt_find_desc_from_icon_number(
atoi(cdata), PCX);
}
waypt_add(wpt);
break;
case 'M':
grid = gt_lookup_grid_type(cdata, MYNAME);
break;
case 'P': /* proximity waypoint */
case 'W': /* normal waypoint */
wpt = parse_waypt(cin + 3);
if (wpt) {
if (mode == rtedata) {
route_add_wpt(head, wpt);
} else {
waypt_add(wpt);
}
}
break;
case 'c': /* additional lines */
switch (*(cin+1)) {
int index;
case 'A':
case 'B':
case 'C':
case 'D':
index = WPT_cA_OFS + ((*(cin+1) - 'A') * 256);
parse_line(cdata, index, "|", wpt);
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
index = WPT_c0_OFS + ((*(cin+1) - '0') * 256);
parse_line(cdata, index, ";", wpt);
break;
case 'L':
waypt_add_url(wpt, xstrdup(cdata), nullptr);
break;
default:
break;
}
break;
case 'N': /* track log header */
mode = trkdata;
head = route_head_alloc();
cdata = strchr(cdata, '-');
if (cdata) {
while (isspace(*cdata)) {
cdata++;
}
if (*cdata) {
char* s = strrchr(cdata, ',');
if (s) {
*s = '\0';
s = strrchr(cdata, ',');
if (s) {
*s = '\0';
head->rte_name = cdata;
}
}
}
}
track_add_head(head);
break;
case 'R': /* route header */
mode = rtedata;
head = route_head_alloc();
cdata += 3; /*skip route number */
if (*cdata) {
head->rte_name = cdata;
}
route_add_head(head);
break;
case 'T':
wpt = parse_trkpt(cdata);
if (wpt) {
track_add_wpt(head, wpt);
}
break;
case 'V':
if (strcmp(cin, G7T_HEADER) != 0) {
fatal(MYNAME ": Invalid version or invalid file!\n");
}
gardown = 0;
break;
default:
break;
}
}
}
/* --------------------------------------------------------------------------- */
ff_vecs_t g7towin_vecs = {
ff_type_file,
{ ff_cap_read, ff_cap_read, ff_cap_read },
rd_init,
nullptr,
rd_deinit,
nullptr,
data_read,
nullptr,
nullptr,
g7towin_args,
CET_CHARSET_MS_ANSI, 0
, NULL_POS_OPS,
nullptr
};
#endif /* CSVFMTS_ENABLED */