forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
garmin_gpi.cc
1629 lines (1390 loc) · 39.8 KB
/
garmin_gpi.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Support for Garmin Points of Interest (.gpi files)
Copyright (C) 2007 Olaf Klein, [email protected]
Copyright (C) 2007-2012 Robert Lipe, [email protected]
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:
* 2007/05/18: initial release (only a reader)
* 2007/05/20: added writer code with embedded bitmap
* 2007/05/22: add support for multiple bounding boxes
(useful / required!) for large waypoints lists
* 2007/05/23: add optional user bitmap
* 2007/06/02: new method to compute center (mean) of bounds
avoid endless loop in group splitting
* 2007/07/10: put address fields (i.e. city) into GMSD
* 2007/07/12: add write support for new address fields
* 2007/10/20: add option unique
* 2007/12/02: support speed and proximity distance (+ alerts)
* 2008/01/14: fix structure error after adding speed/proximity
* 2008/03/22: add options "speed" and "proximity" (default values) and "sleep"
ToDo:
* Display mode ("Symbol & Name") ??? not in gpi ???
* support category from GMSD "Garmin Special Data"
*/
#include "defs.h"
#include "cet_util.h"
#include "jeeps/gpsmath.h"
#include "garmin_fs.h"
#include "garmin_gpi.h"
#include <stdlib.h>
#include <QtCore/QTextCodec>
#define MYNAME "garmin_gpi"
#define GPI_DBG 1
#undef GPI_DBG
#define DEFAULT_ICON "Waypoint"
#define WAYPOINTS_PER_BLOCK 128
/* flags used in the gpi address mask */
#define GPI_ADDR_CITY 1
#define GPI_ADDR_COUNTRY 2
#define GPI_ADDR_STATE 4
#define GPI_ADDR_POSTAL_CODE 8
#define GPI_ADDR_ADDR 16
static char* opt_cat, *opt_pos, *opt_notes, *opt_hide_bitmap, *opt_descr, *opt_bitmap;
static char* opt_unique, *opt_alerts, *opt_units, *opt_speed, *opt_proximity, *opt_sleep;
static char* opt_writecodec;
static double defspeed, defproximity;
static int alerts;
static arglist_t garmin_gpi_args[] = {
{
"alerts", &opt_alerts, "Enable alerts on speed or proximity distance",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"bitmap", &opt_bitmap, "Use specified bitmap on output",
NULL, ARGTYPE_FILE, ARG_NOMINMAX
},
{
"category", &opt_cat, "Default category on output",
"My points", ARGTYPE_STRING, ARG_NOMINMAX
},
{
"hide", &opt_hide_bitmap, "Don't show gpi bitmap on device",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"descr", &opt_descr, "Write description to address field",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"notes", &opt_notes, "Write notes to address field",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"position", &opt_pos, "Write position to address field",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"proximity", &opt_proximity, "Default proximity",
NULL, ARGTYPE_STRING, ARG_NOMINMAX
},
{
"sleep", &opt_sleep, "After output job done sleep n second(s)",
NULL, ARGTYPE_INT, "1", NULL
},
{
"speed", &opt_speed, "Default speed",
NULL, ARGTYPE_STRING, ARG_NOMINMAX
},
{
"unique", &opt_unique, "Create unique waypoint names (default = yes)",
"Y", ARGTYPE_BOOL, ARG_NOMINMAX
},
{
"units", &opt_units, "Units used for names with @speed ('s'tatute or 'm'etric)",
"m", ARGTYPE_STRING, ARG_NOMINMAX
},
{
"writecodec", &opt_writecodec, "codec to use for writing strings",
"windows-1252", ARGTYPE_STRING, ARG_NOMINMAX
},
ARG_TERMINATOR
};
typedef struct {
public:
int D2;
char S3[9]; /* "GRMRECnn" */
time_t crdate; /* creation date and time */
char POI[4]; /* "POI" */
char S8[3];
QString group;
QString category;
} reader_data_t;
typedef struct writer_data_s {
queue Q;
int ct;
int sz;
int alert;
bounds bds;
struct writer_data_s* top_left;
struct writer_data_s* top_right;
struct writer_data_s* buttom_left;
struct writer_data_s* buttom_right;
} writer_data_t;
typedef struct gpi_waypt_data_s {
int sz;
char* addr;
char* postal_code;
} gpi_waypt_data_t;
typedef struct {
int32_t size;
int16_t res1;
int16_t res2;
int32_t image_offset;
int32_t header_size;
int32_t width;
int32_t height;
int16_t planes;
int16_t bpp;
int32_t compression_type;
int32_t image_data_size;
int32_t resolution_h;
int32_t resolution_v;
int32_t used_colors;
int32_t important_colors;
} bmp_header_t;
typedef struct {
int16_t index;
int16_t height;
int16_t width;
int16_t line_sz;
int16_t bpp;
int16_t fixed_0;
int32_t image_size;
int32_t fixed_2c;
int32_t flag1;
int32_t tr_color;
int32_t flag2;
int32_t size_2c;
} gpi_bitmap_header_t;
typedef struct {
int sz;
int alerts;
short mask;
char addr_is_dynamic;
char* addr;
char* city;
char* country;
char* phone_nr;
char* postal_code;
char* state;
} gpi_waypt_t;
static gbfile* fin, *fout;
static int16_t codepage; /* code-page, i.e. 1252 */
static reader_data_t* rdata;
static writer_data_t* wdata;
static short_handle short_h;
static char units;
static time_t gpi_timestamp = 0;
#ifdef GPI_DBG
# define PP warning("@%1$6x (%1$8d): ", gbftell(fin))
# define dbginfo warning
#else
# define PP
#endif
/*******************************************************************************
* %%% gpi reader %%% *
*******************************************************************************/
/* look for or initialize GMSD */
static garmin_fs_t*
gpi_gmsd_init(Waypoint* wpt)
{
garmin_fs_t* gmsd = GMSD_FIND(wpt);
if (wpt == NULL) {
fatal(MYNAME ": Error in file structure.\n");
}
if (gmsd == NULL) {
gmsd = garmin_fs_alloc(-1);
fs_chain_add(&wpt->fs, (format_specific_data*) gmsd);
}
return gmsd;
}
/* read a standard string with or without 'EN' (or whatever) header */
static char*
gpi_read_string_old(const char* field)
{
int l1;
char* res = NULL;
l1 = gbfgetint16(fin);
if (l1 > 0) {
short l2;
char first;
first = gbfgetc(fin);
if (first == 0) {
char en[2];
is_fatal((gbfgetc(fin) != 0),
MYNAME ": Error reading field '%s'!", field);
gbfread(en, 1, sizeof(en), fin);
l2 = gbfgetint16(fin);
is_fatal((l2 + 4 != l1),
MYNAME ": Error out of sync (wrong size %d/%d) on field '%s'!", l1, l2, field);
if ((en[0] < 'A') || (en[0] > 'Z') || (en[1] < 'A') || (en[1] > 'Z')) {
fatal(MYNAME ": Invalid country code!\n");
}
res = (char*) xmalloc(l2 + 1);
res[l2] = '\0';
PP;
if (l2 > 0) {
gbfread(res, 1, l2, fin);
}
} else {
res = (char*) xmalloc(l1 + 1);
*res = first;
*(res + l1) = '\0';
PP;
l1--;
if (l1 > 0) {
gbfread(res + 1, 1, l1, fin);
}
}
}
#ifdef GPI_DBG
dbginfo("%s: %s\n", field, (res == NULL) ? "<NULL>" : res);
#endif
return res;
}
static QString
gpi_read_string(const char* field)
{
char*s = gpi_read_string_old(field);
QString rv = STRTOUNICODE(s);
xfree(s);
return rv;
}
static void
read_header(void)
{
int len, i;
#ifdef GPI_DBG
struct tm tm;
char stime[32];
#endif
i = gbfgetint32(fin);
if (i != 0) {
i = gbfgetint32(fin);
}
rdata->D2 = gbfgetint32(fin);
gbfread(&rdata->S3, 1, sizeof(rdata->S3) - 1, fin); /* GRMRECnn */
if (strncmp(rdata->S3, "GRMREC", 6) != 0) {
fatal(MYNAME ": No GPI file!\n");
}
PP;
rdata->crdate = gbfgetint32(fin);
#ifdef GPI_DBG
tm = *localtime(&rdata->crdate);
tm.tm_year += 20; /* !!! */
tm.tm_mday -= 1; /* !!! */
strftime(stime, sizeof(stime), "%Y/%m/%d %H:%M:%S", &tm);
dbginfo("crdate = %lu (%s)\n", rdata->crdate, stime);
#endif
(void) gbfgetint16(fin); /* 0 */
len = gbfgetint16(fin);
gbfseek(fin, len, SEEK_CUR); /* "my.gpi" */
i = gbfgetint32(fin); /* 1 */
(void) gbfgetint32(fin); /* 12 */
/* There are two dwords next. On most typical files, they're
* "1" and "12". On files from garminoneline.de/extras/poi, the
* next two words are "15" and "5" and there's 17 additional bytes
* that I can't identify. So hardcode a seek here for now.
*/
if (i == 15) {
gbfseek(fin, 17, SEEK_CUR);
}
gbfread(&rdata->POI, 1, sizeof(rdata->POI) - 1, fin);
if (strncmp(rdata->POI, "POI", 3) != 0) {
fatal(MYNAME ": Wrong or unsupported GPI file!\n");
}
for (i = 0; i < 3; i++) {
(void)gbfgetc(fin);
}
gbfread(&rdata->S8, 1, sizeof(rdata->S8) - 1, fin);
codepage = gbfgetint16(fin);
(void) gbfgetint16(fin); /* typically 0, but 0x11 in
Garminonline.de files. */
#ifdef GPI_DBG
PP;
dbginfo("< leaving header\n");
#endif
}
/* gpi tag handler */
static int read_tag(const char* caller, const int tag, Waypoint* wpt);
/* read a single poi with all options */
static void
read_poi(const int sz, const int tag)
{
int pos, len;
Waypoint* wpt;
#ifdef GPI_DBG
PP;
dbginfo("> reading poi (size %d)\n", sz);
#endif
PP;
len = 0;
if (tag == 0x80002) {
len = gbfgetint32(fin); /* sub-header size */
}
#ifdef GPI_DBG
dbginfo("poi sublen = %1$d (0x%1$x)\n", len);
#endif
(void) len;
pos = gbftell(fin);
wpt = new Waypoint;
wpt->icon_descr = DEFAULT_ICON;
wpt->latitude = GPS_Math_Semi_To_Deg(gbfgetint32(fin));
wpt->longitude = GPS_Math_Semi_To_Deg(gbfgetint32(fin));
(void) gbfgetint16(fin); /* ? always 1 ? */
(void) gbfgetc(fin); /* seems to 1 when extra options present */
wpt->shortname = gpi_read_string("Shortname");
while (gbftell(fin) < (gbsize_t)(pos + sz - 4)) {
int tag = gbfgetint32(fin);
if (! read_tag("read_poi", tag, wpt)) {
break;
}
}
if (wpt->description.isEmpty() && !wpt->notes.isEmpty()) {
wpt->description = wpt->notes;
}
if (wpt->notes.isEmpty() && !wpt->description.isEmpty()) {
wpt->notes = wpt->description;
}
waypt_add(wpt);
#ifdef GPI_DBG
PP;
dbginfo("< leaving poi\n");
#endif
}
/* read poi's following a group header */
static void
read_poi_list(const int sz)
{
int pos, i;
pos = gbftell(fin);
#ifdef GPI_DBG
PP;
dbginfo("> reading poi list (-> %1$x / %1$d )\n", pos + sz);
#endif
PP;
i = gbfgetint32(fin); /* mostly 23 (0x17) */
#ifdef GPI_DBG
dbginfo("list sublen = %1$d (0x%1$x)\n", i);
#else
(void) i;
#endif
(void) gbfgetint32(fin); /* max-lat */
(void) gbfgetint32(fin); /* max-lon */
(void) gbfgetint32(fin); /* min-lat */
(void) gbfgetint32(fin); /* min-lon */
(void) gbfgetc(fin); /* three unknown bytes */
(void) gbfgetc(fin); /* ? should be zero ? */
(void) gbfgetc(fin);
(void) gbfgetint32(fin); /* ? const 0x1000100 ? */
while (gbftell(fin) < (gbsize_t)(pos + sz - 4)) {
int tag = gbfgetint32(fin);
if (! read_tag("read_poi_list", tag, NULL)) {
return;
}
}
#ifdef GPI_DBG
PP;
dbginfo("< leaving poi list\n");
#endif
}
static void
read_poi_group(const int sz, const int tag)
{
int pos;
pos = gbftell(fin);
#ifdef GPI_DBG
PP;
dbginfo("> reading poi group (-> %1$x / %1$d)\n", pos + sz);
#endif
if (tag == 0x80009) {
int subsz;
PP;
subsz = gbfgetint32(fin); /* ? offset to category data ? */
#ifdef GPI_DBG
dbginfo("group sublen = %d (-> %x / %d)\n", subsz, pos + subsz + 4, pos + subsz + 4);
#else
(void)subsz;
#endif
}
rdata->group = gpi_read_string("Group");
while (gbftell(fin) < (gbsize_t)(pos + sz)) {
int subtag = gbfgetint32(fin);
if (! read_tag("read_poi_group", subtag, NULL)) {
break;
}
}
#ifdef GPI_DBG
PP;
dbginfo("< leaving poi group\n");
#endif
}
// TODO: 'tag' is probably not a 32 bit value.
// most likely it's a pair of 16's: the first pair is the tag number.
// if the second 16 is "eight", then it's an
// extended thingy and it has a 4-byte extended record length (total number
// of bytes for all record fields and all nested records, starting after the
// length field)
/* gpi tag handler */
static int
read_tag(const char* caller, const int tag, Waypoint* wpt)
{
int pos, sz, dist;
double speed;
short mask;
QString str;
char* cstr;
garmin_fs_t* gmsd;
sz = gbfgetint32(fin);
pos = gbftell(fin);
#ifdef GPI_DBG
PP;
dbginfo("%s: tag = 0x%x (size %d)\n", caller, tag, sz);
#endif
if ((tag >= 0x80000) && (tag <= 0x800ff)) {
sz += 4;
}
switch (tag) {
case 0x3: /* size = 12 */
case 0x80003: /* size = 12 */
dist = gbfgetint16(fin); /* proximity distance in meters */
speed = (double)gbfgetint16(fin) / 100; /* speed in meters per second */
if (dist > 0) {
WAYPT_SET(wpt, proximity, dist);
}
if (speed > 0) {
/* speed isn't part of a normal waypoint
WAYPT_SET(wpt, speed, speed);
*/
if ((wpt->shortname.isEmpty() || ((wpt->shortname).indexOf('@'))==-1)) {
if (units == 's') {
speed = MPS_TO_MPH(speed);
} else {
speed = MPS_TO_KPH(speed);
}
QString base = wpt->shortname.isEmpty() ? "WPT" : wpt->shortname;
wpt->shortname = base + QString("@%1").arg(speed,0,'f',0);
}
}
(void) gbfgetint32(fin);
(void) gbfgetint32(fin);
break;
case 0x4: /* size = 2 ? */
case 0x6: /* size = 2 ? */
break;
case 0x5: /* group bitmap */
break;
case 0x7:
(void) gbfgetint16(fin); /* category number */
rdata->category = gpi_read_string("Category");
break;
case 0xa:
wpt->description = gpi_read_string("Description");
break;
case 0xe: /* ? notes or description / or both ? */
mask = gbfgetc(fin);
// Olaf's code called this a mask, but the bits below have nothing
// in common. I'm wondering if that first byte is something else and
// a type e is always a note.
switch (mask) {
case 0x01:
case 0x05:
case 0x32:
str = gpi_read_string("Notes");
default:
break;
}
if (!wpt->description.isEmpty()) {
wpt->notes = str;
} else {
wpt->description = str;
}
break;
case 0x2:
case 0x80002:
read_poi(sz, tag);
break;
case 0x80008:
read_poi_list(sz);
break;
case 0x9: /* ? older versions / no category data ? */
case 0x80009: /* current POI loader */
read_poi_group(sz, tag);
break;
case 0x8000b: /* address (street/city...) */
(void) gbfgetint32(fin);
// FALLTHROUGH
case 0xb: /* as seen in German POI files. */
PP;
mask = gbfgetint16(fin); /* address fields mask */
#ifdef GPI_DBG
dbginfo("GPI Address field mask: %d (0x%02x)\n", mask, mask);
#endif
if ((mask & GPI_ADDR_CITY) && (cstr = gpi_read_string_old("City"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(city, cstr);
}
if ((mask & GPI_ADDR_COUNTRY) && (cstr = gpi_read_string_old("Country"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(country, cstr);
}
if ((mask & GPI_ADDR_STATE) && (cstr = gpi_read_string_old("State"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(state, cstr);
}
if ((mask & GPI_ADDR_POSTAL_CODE) && (cstr = gpi_read_string_old("Postal code"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(postal_code, cstr);
}
if ((mask & GPI_ADDR_ADDR) && (cstr = gpi_read_string_old("Street address"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(addr, cstr);
}
break;
case 0xc:
mask = gbfgetint16(fin);
if ((mask & 1) && (cstr = gpi_read_string_old("Phone"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(phone_nr, cstr);
}
if ((mask & 2) && (cstr = gpi_read_string_old("Phone2"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(phone_nr2, cstr);
}
if ((mask & 4) && (cstr = gpi_read_string_old("Fax"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(fax_nr, cstr);
}
if ((mask & 8) && (cstr = gpi_read_string_old("Email"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(email, cstr);
}
if ((mask & 0x10) && (str = gpi_read_string("Link"), !str.isEmpty())) {
waypt_add_url(wpt, str, str);
}
break;
case 0x8000c: /* phone-number */
(void) gbfgetint32(fin);
PP;
mask = gbfgetint16(fin); /* phone fields mask */
#ifdef GPI_DBG
dbginfo("GPI Phone field mask: %d (0x%02x)\n", mask, mask);
#endif
if ((mask & 1) && (cstr = gpi_read_string_old("Phone"))) {
gmsd = gpi_gmsd_init(wpt);
GMSD_SET(phone_nr, cstr);
}
break;
case 0x80012: /* ? sounds / images ? */
break;
/* Images? Seen in http://geepeeex.com/Stonepages.gpi */
case 0xd:
break;
case 0x11:
case 0x80007:
/* Looks like some kind of calendar information. */
#ifdef GPI_DBG
{
int x;
unsigned char* b = (unsigned char*) xmalloc(sz);
fprintf(stderr, "Tag: %x\n", tag);
gbfread(b, 1, sz, fin);
fprintf(stderr, "\n");
for (x = 0; x < sz; x++) {
fprintf(stderr, "%02x ", b[x]);
}
fprintf(stderr, "\n");
for (x = 0; x < sz; x++) {
fprintf(stderr, "%c", isalnum(b[x]) ? b[x] : '.');
}
fprintf(stderr, "\n");
}
#endif // GPI_DBG
break;
default:
warning(MYNAME ": Unknown tag (0x%x). Please report!\n", tag);
return 0;
}
gbfseek(fin, pos + sz, SEEK_SET);
return 1;
}
/*******************************************************************************
* %%% gpi writer %%% *
*******************************************************************************/
static void
write_string(const char* str, const char long_format)
{
int len;
len = strlen(str);
if (long_format) {
gbfputint32(len + 4, fout);
gbfwrite("EN", 1, 2, fout);
}
gbfputint16(len, fout);
gbfwrite(str, 1, len, fout);
}
static void
write_string(const QString& str, const char long_format)
{
write_string(STRFROMUNICODE(str), long_format);
}
static int
compare_wpt_cb(const queue* a, const queue* b)
{
const Waypoint* wa = (Waypoint*) a;
const Waypoint* wb = (Waypoint*) b;
return wa->shortname.compare(wb->shortname);
}
static char
compare_strings(const QString& s1, const QString& s2)
{
return s1.compare(s2);
}
static writer_data_t*
wdata_alloc()
{
writer_data_t* res;
res = (writer_data_t*) xcalloc(1, sizeof(*res));
QUEUE_INIT(&res->Q);
waypt_init_bounds(&res->bds);
return res;
}
static void
wdata_free(writer_data_t* data)
{
queue* elem, *tmp;
QUEUE_FOR_EACH(&data->Q, elem, tmp) {
Waypoint* wpt = (Waypoint*)elem;
if (wpt->extra_data) {
gpi_waypt_t* dt = (gpi_waypt_t*) wpt->extra_data;
if (dt->addr_is_dynamic) {
xfree(dt->addr);
}
xfree(dt);
}
delete wpt;
}
if (data->top_left) {
wdata_free(data->top_left);
}
if (data->top_right) {
wdata_free(data->top_right);
}
if (data->buttom_left) {
wdata_free(data->buttom_left);
}
if (data->buttom_right) {
wdata_free(data->buttom_right);
}
xfree(data);
}
static void
wdata_add_wpt(writer_data_t* data, Waypoint* wpt)
{
data->ct++;
ENQUEUE_TAIL(&data->Q, &wpt->Q);
waypt_add_to_bounds(&data->bds, wpt);
}
static void
wdata_check(writer_data_t* data)
{
queue* elem, *tmp;
double center_lat, center_lon;
if ((data->ct <= WAYPOINTS_PER_BLOCK) ||
/* avoid endless loop for points (more than WAYPOINTS_PER_BLOCK)
at same coordinates */
((data->bds.min_lat >= data->bds.max_lat) && (data->bds.min_lon >= data->bds.max_lon))) {
if (data->ct > 1) {
sortqueue(&data->Q, compare_wpt_cb);
}
return;
}
/* compute the (mean) center of current bounds */
center_lat = center_lon = 0;
QUEUE_FOR_EACH(&data->Q, elem, tmp) {
Waypoint* wpt = (Waypoint*) elem;
center_lat += wpt->latitude;
center_lon += wpt->longitude;
}
center_lat /= data->ct;
center_lon /= data->ct;
QUEUE_FOR_EACH(&data->Q, elem, tmp) {
Waypoint* wpt = (Waypoint*) elem;
writer_data_t** ref;
if (wpt->latitude < center_lat) {
if (wpt->longitude < center_lon) {
ref = &data->buttom_left;
} else {
ref = &data->buttom_right;
}
} else {
if (wpt->longitude < center_lon) {
ref = &data->top_left;
} else {
ref = &data->top_right;
}
}
if (*ref == NULL) {
*ref = wdata_alloc();
}
data->ct--;
dequeue(&wpt->Q);
wdata_add_wpt(*ref, wpt);
}
if (data->top_left) {
wdata_check(data->top_left);
}
if (data->top_right) {
wdata_check(data->top_right);
}
if (data->buttom_left) {
wdata_check(data->buttom_left);
}
if (data->buttom_right) {
wdata_check(data->buttom_right);
}
}
static int
wdata_compute_size(writer_data_t* data)
{
queue* elem, *tmp;
int res = 0;
if (QUEUE_EMPTY(&data->Q))
goto skip_empty_block; /* do not issue an empty block */
res = 23; /* bounds, ... of tag 0x80008 */
QUEUE_FOR_EACH(&data->Q, elem, tmp) {
Waypoint* wpt = (Waypoint*) elem;
gpi_waypt_t* dt;
garmin_fs_t* gmsd;
QString str;
res += 12; /* tag/sz/sub-sz */
res += 19; /* poi fixed size */
res += wpt->shortname.length();
if (! opt_hide_bitmap) {
res += 10; /* tag(4) */
}
dt = (gpi_waypt_t*) xcalloc(1, sizeof(*dt));
wpt->extra_data = dt;
if (alerts) {
#if NEW_STRINGS
// examine closely.
const char* pos;
int pidx;
if ((pidx = wpt->shortname.indexOf('@')) != -1) {
pos = CSTR(wpt->shortname.mid(pidx));
#else
char* pos;
if ((pos = strchr(wpt->shortname, '@'))) {
#endif
double speed, scale;
if (units == 's') {
scale = MPH_TO_MPS(1);
} else {
scale = KPH_TO_MPS(1);
}
parse_speed(pos + 1, &speed, scale, MYNAME);
if (speed > 0) {
WAYPT_SET(wpt, speed, speed);
}
#if 0
if (pos > wpt->shortname) {
wpt->shortname[pos - wpt->shortname] = '\0';
}
#endif
} else if ((opt_speed) && (! WAYPT_HAS(wpt, speed))) {
WAYPT_SET(wpt, speed, defspeed);
}
if ((opt_proximity) && (! WAYPT_HAS(wpt, proximity))) {
WAYPT_SET(wpt, proximity, defproximity);
}
if ((WAYPT_HAS(wpt, speed) && (wpt->speed > 0)) ||
(WAYPT_HAS(wpt, proximity) && (wpt->proximity > 0))) {
data->alert = 1;
dt->alerts++;
res += 20; /* tag(3) */
}
}
str = QString();
if (opt_descr) {
if (!wpt->description.isEmpty()) {
str = xstrdup(wpt->description);
}
} else if (opt_notes) {
if (!wpt->notes.isEmpty()) {
str = xstrdup(wpt->notes);
}
} else if (opt_pos) {
str = pretty_deg_format(wpt->latitude, wpt->longitude, 's', " ", 0);
}
if (!str.isEmpty()) {
dt->addr_is_dynamic = 1;
dt->addr = xstrdup(str);
dt->mask |= GPI_ADDR_ADDR;
dt->sz += (8 + strlen(dt->addr));
}
if ((gmsd = GMSD_FIND(wpt))) {
if ((dt->mask == 0) && ((dt->addr = GMSD_GET(addr, NULL)))) {
dt->mask |= GPI_ADDR_ADDR;
dt->sz += (8 + strlen(dt->addr));
}
if ((dt->city = GMSD_GET(city, NULL))) {
dt->mask |= GPI_ADDR_CITY;
dt->sz += (8 + strlen(dt->city));
}
if ((dt->country = GMSD_GET(country, NULL))) {
dt->mask |= GPI_ADDR_COUNTRY;
dt->sz += (8 + strlen(dt->country));
}
if ((dt->state = GMSD_GET(state, NULL))) {
dt->mask |= GPI_ADDR_STATE;
dt->sz += (8 + strlen(dt->state));
}
if ((dt->postal_code = GMSD_GET(postal_code, NULL))) {
dt->mask |= GPI_ADDR_POSTAL_CODE;
dt->sz += (2 + strlen(dt->postal_code)); /* short form */
}
if ((dt->phone_nr = GMSD_GET(phone_nr, NULL))) {
res += (12 + 4 + strlen(dt->phone_nr));
}
}
if (dt->mask) {
dt->sz += 2; /* + mask (two bytes) */
}