forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_util.cc
2250 lines (2062 loc) · 62.1 KB
/
csv_util.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
/*
Utilities for parsing Character Separated Value files (CSV)
Copyright (C) 2002 Alex Mottram (geo_alexm at cox-internet.com)
Copyright (C) 2002-2014 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
*/
#include <QtCore/QRegExp>
#include "defs.h"
#include "cet_util.h"
#include "csv_util.h"
#include "garmin_fs.h"
#include "grtcirc.h"
#include "jeeps/gpsmath.h"
#include "src/core/logging.h"
#include "strptime.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define MYNAME "CSV_UTIL"
/* macros */
#define LAT_DIR(a) a < 0.0 ? 'S' : 'N'
#define LON_DIR(a) a < 0.0 ? 'W' : 'E'
#define NONULL(a) a.isNull() ? "" : CSTRc(a)
#define ISWHITESPACE(a) ((a == ' ') || (a == '\t'))
/* convert excel time (days since 1900) to time_t and back again */
#define EXCEL_TO_TIMET(a) ((a - 25569.0) * 86400.0)
#define TIMET_TO_EXCEL(a) ((a / 86400.0) + 25569.0)
#define GPS_DATUM_WGS84 118
/*
* Internal numeric value to associate with each keyword in a style file.
* To add new keywords, just add an entry here, handle it in the switch
* statements below, add it to xcsv_tokens.in, and rebuild on a system
* that has GNU gperf on it.
*/
typedef enum {
XT_unused = 0,
XT_ALT_FEET,
XT_ALT_METERS,
XT_ANYNAME,
XT_CADENCE,
XT_CITY,
XT_CONSTANT,
XT_COUNTRY,
XT_DESCRIPTION,
XT_EXCEL_TIME,
XT_FACILITY,
XT_FILENAME,
XT_FORMAT,
XT_GEOCACHE_CONTAINER,
XT_GEOCACHE_DIFF,
XT_GEOCACHE_HINT,
XT_GEOCACHE_LAST_FOUND,
XT_GEOCACHE_PLACER,
XT_GEOCACHE_TERR,
XT_GEOCACHE_TYPE,
XT_GEOCACHE_ISAVAILABLE,
XT_GEOCACHE_ISARCHIVED,
XT_GMT_TIME,
XT_GPS_FIX,
XT_GPS_HDOP,
XT_GPS_PDOP,
XT_GPS_SAT,
XT_GPS_VDOP,
XT_HEART_RATE,
XT_HMSG_TIME,
XT_HMSL_TIME,
XT_ICON_DESCR,
XT_IGNORE,
XT_INDEX,
XT_ISO_TIME,
XT_ISO_TIME_MS,
XT_LATLON_HUMAN_READABLE,
XT_LAT_DECIMAL,
XT_LAT_DECIMALDIR,
XT_LAT_DIR,
XT_LAT_DIRDECIMAL,
XT_LAT_HUMAN_READABLE,
XT_LAT_INT32DEG,
XT_LAT_DDMMDIR,
XT_LAT_NMEA,
XT_LOCAL_TIME,
XT_LON_DECIMAL,
XT_LON_DECIMALDIR,
XT_LON_DIR,
XT_LON_DIRDECIMAL,
XT_LON_HUMAN_READABLE,
XT_LON_INT32DEG,
XT_LON_DDMMDIR,
XT_LON_NMEA,
XT_MAP_EN_BNG,
XT_NOTES,
XT_NET_TIME,
XT_PATH_COURSE,
XT_PATH_DISTANCE_KM,
XT_PATH_DISTANCE_METERS,
XT_PATH_DISTANCE_MILES,
XT_PATH_SPEED,
XT_PATH_SPEED_KNOTS,
XT_PATH_SPEED_KPH,
XT_PATH_SPEED_MPH,
XT_PHONE_NR,
XT_POSTAL_CODE,
XT_POWER,
XT_ROUTE_NAME,
XT_SHORTNAME,
XT_STATE,
XT_STREET_ADDR,
XT_TEMPERATURE,
XT_TEMPERATURE_F,
XT_TIMET_TIME,
XT_TIMET_TIME_MS,
XT_TRACK_NAME,
XT_TRACK_NEW,
XT_URL,
XT_UTM,
XT_UTM_ZONE,
XT_UTM_ZONEC,
XT_UTM_ZONEF,
XT_UTM_EASTING,
XT_UTM_NORTHING,
XT_URL_LINK_TEXT,
XT_YYYYMMDD_TIME
} xcsv_token;
// Static definition of in_word_set to meet C99 rules as used by Clang.
static struct xt_mapping*
in_word_set(register const char* str, register unsigned int len);
#include "xcsv_tokens.gperf"
/****************************************************************************/
/* obligatory global struct */
/****************************************************************************/
XcsvFile xcsv_file;
extern char* xcsv_urlbase;
extern char* prefer_shortnames;
#if CSVFMTS_ENABLED
static double pathdist = 0;
static double oldlon = 999;
static double oldlat = 999;
static int waypt_out_count;
static route_head* csv_track, *csv_route;
static double utm_northing, utm_easting, utm_zone = 0;
static char utm_zonec;
static UrlLink* link_;
#endif // CSVFMTS_ENABLED
/*********************************************************************/
/* csv_stringclean() - remove any unwanted characters from string. */
/* returns copy of string. */
/* usage: p = csv_stringclean(stringtoclean, "&,\"") */
/* (strip out ampersands, commas, and quotes. */
/*********************************************************************/
QString
csv_stringclean(const QString& source, const QString& to_nuke)
{
QString r = source;
QString regex = QString("[%1]").arg(to_nuke);
return r.remove(QRegExp(regex));
}
// csv_stringtrim() - trim whitespace and leading and trailing
// enclosures (quotes)
// returns a copy of the modified string
// usage: p = csv_stringtrim(string, "\"", 0)
char*
csv_stringtrim(const char* string, const char* enclosure, int strip_max)
{
static const char* p1 = NULL;
char* p2 = NULL;
char* tmp = xxstrdup(string,file,line);
size_t elen;
int stripped = 0;
if (!strlen(string)) {
return (tmp);
}
if (!enclosure) {
elen = 0;
} else {
elen = strlen(enclosure);
}
p2 = tmp + strlen(tmp) - 1;
p1 = tmp;
/* trim off trailing whitespace */
while ((p2 > p1) && isspace(*p2)) {
p2--;
}
/* advance p1 past any leading whitespace */
while ((p1 < p2) && (isspace(*p1))) {
p1++;
}
/* if no maximum strippage, assign a reasonable value to max */
strip_max = strip_max ? strip_max : 9999;
/* if we have enclosures, skip past them in pairs */
if (elen) {
while (
(stripped < strip_max) &&
((size_t)(p2 - p1 + 1) >= (elen * 2)) &&
(strncmp(p1, enclosure, elen) == 0) &&
(strncmp((p2 - elen + 1), enclosure, elen) == 0)) {
p2 -= elen;
p1 += elen;
stripped++;
}
}
/* copy what's left over back into tmp. */
memmove(tmp, p1, (p2 - p1) + 1);
tmp[(p2 - p1) + 1] = '\0';
return (tmp);
}
// Is this really the replacement for the above?
QString
csv_stringtrim(const QString& source, const QString& enclosure)
{
QString r = source;
r.replace(enclosure, "");
return r.trimmed();
}
/*****************************************************************************/
/* csv_lineparse() - extract data fields from a delimited string. designed */
/* to handle quoted and delimited data within quotes. */
/* returns temporary COPY of delimited data field (use it */
/* or lose it on the next call). */
/* usage: p = csv_lineparse(string, ",", "\"", line) [initial call] */
/* p = csv_lineparse(NULL, ",", "\"", line) [subsequent calls] */
/*****************************************************************************/
char*
csv_lineparse(const char* stringstart, const char* delimited_by,
const char* enclosed_in, const int line_no)
{
const char* sp;
static const char* p = NULL;
static char* tmp = NULL;
size_t dlen = 0, elen = 0, efound = 0;
int enclosedepth = 0;
short int dfound;
short int hyper_whitespace_delimiter = 0;
if (tmp) {
xfree(tmp);
tmp = NULL;
}
if (strcmp(delimited_by, "\\w") == 0) {
hyper_whitespace_delimiter = 1;
}
/*
* This is tacky. Our "csv" format is actually "commaspace" format.
* Changing that causes unwanted churn, but it also makes "real"
* comma separated data (such as likely to be produced by Excel, etc.)
* unreadable. So we silently change it here on a read and let the
* whitespace eater consume the space.
*/
if (strcmp(delimited_by, ", ") == 0) {
delimited_by = ",";
}
if (!p) {
/* first pass thru */
p = stringstart;
if (!p) {
/* last pass out */
return (NULL);
}
}
/* the beginning of the string we start with (this pass) */
sp = p;
/* length of delimiters and enclosures */
if ((delimited_by) && (!hyper_whitespace_delimiter)) {
dlen = strlen(delimited_by);
}
if (enclosed_in) {
elen = strlen(enclosed_in);
}
dfound = 0;
while ((*p) && (!dfound)) {
if ((elen) && (strncmp(p, enclosed_in, elen) == 0)) {
efound = 1;
p+=elen;
if (enclosedepth) {
enclosedepth--;
} else {
enclosedepth++;
}
continue;
}
if (!enclosedepth) {
if ((dlen) && (strncmp(p, delimited_by, dlen) == 0)) {
dfound = 1;
} else if ((hyper_whitespace_delimiter) && (ISWHITESPACE(*p))) {
dfound = 1;
while (ISWHITESPACE(*p)) {
p++;
}
} else {
p++;
}
} else {
p++;
}
}
/* allocate enough space for this data field */
tmp = (char*) xcalloc((p - sp) + 1, sizeof(char));
strncpy(tmp, sp, (p - sp));
tmp[p - sp] = '\0';
if (elen && efound) {
char* c = csv_stringtrim(tmp, enclosed_in, 0);
xfree(tmp);
tmp = c;
}
if (dfound) {
/* skip over the delimited_by */
p += dlen;
} else {
/* end of the line */
p = NULL;
}
if (enclosedepth != 0) {
warning(MYNAME
": Warning- Unbalanced Field Enclosures (%s) on line %d\n",
enclosed_in, line_no);
}
return (tmp);
}
#if CSVFMTS_ENABLED
/*****************************************************************************/
/* dec_to_intdeg() - convert decimal degrees to integer degreees */
/* usage: i = dec_to_intdeg(31.1234); */
/*****************************************************************************/
static int
dec_to_intdeg(const double d)
{
int ideg = 0;
if (d >= 0) {
ideg = (2147483647) - (d * 8388608);
} else {
ideg = (2147483647) - (fabs(d) * 8388608) + 1;
}
return(ideg);
}
/*****************************************************************************/
/* intdeg_to_dec() - convert integer degrees to decimal degreees */
/* usage: lat = dec_to_intdeg(ilat); */
/*****************************************************************************/
static double
intdeg_to_dec(const int ideg)
{
double d;
if (ideg >= 0) {
d = ((2147483647) - ideg) / (double)8388608;
} else {
d = ((-2147483647-1) + ideg) / (double)8388608;
}
return(d);
}
/*****************************************************************************/
/* decdir_to_dec() - convert a decimal/direction value into pure decimal. */
/* usage: lat = decdir_to_dec("W90.1234"); */
/* lat = decdir_to_dec("30.1234N"); */
/*****************************************************************************/
static double
decdir_to_dec(const char* decdir)
{
char* p;
const char* cp;
double rval;
int sign = 0;
cp = &decdir[0];
if ((*cp == 'W') || (*cp == 'S')) {
sign = -1;
} else if ((*cp == 'N') || (*cp == 'E')) {
sign = 1;
}
rval = sign ? strtod(&decdir[1], &p) : strtod(&decdir[0], &p);
if (sign == 0) {
if ((*p == 'W') || (*p == 'S')) {
sign = -1;
} else if ((*p == 'N') || (*p == 'E')) {
sign = 1;
}
}
return(rval * sign);
}
/*****************************************************************************/
/* ddmmdir_to_degrees() - convert ddmm/direction value into degrees */
/* usage: lat = ddmmdir_to_degrees("W90.1234"); */
/* lat = ddmmdir_to_degrees("30.1234N"); */
/*****************************************************************************/
static double
ddmmdir_to_degrees(const char* ddmmdir)
{
// if not N or E, prepend a '-' to ddmm2degrees input
// see XT_LAT_NMEA which handles ddmm directly
if (strchr(ddmmdir, 'W') || strchr(ddmmdir, 'S')) {
return ddmm2degrees(- atof(ddmmdir));
}
return ddmm2degrees(atof(ddmmdir));
}
#endif
/*****************************************************************************
* human_to_dec() - convert a "human-readable" lat and/or lon to decimal
* usage: human_to_dec( "N 41� 09.12' W 085� 09.36'", &lat, &lon );
* human_to_dec( "41 9 5.652 N", &lat, &lon );
*
* which: 0-no preference 1-prefer lat 2-prefer lon
*****************************************************************************/
void
human_to_dec(const char* instr, double* outlat, double* outlon, int which)
{
double unk[3] = {999,999,999};
double lat[3] = {999,999,999};
double lon[3] = {999,999,999};
int latsign = 0;
int lonsign = 0;
int unksign = 1;
const char* cur;
double* numres = unk;
int numind = 0;
char* buff;
if (strchr(instr, ',') != NULL) {
char* c;
buff = xstrdup(instr);
while ((c = strchr(buff, ','))) {
*c = '.';
}
} else {
buff = (char*)instr;
}
cur = buff;
while (cur && *cur) {
switch (*cur) {
case 'n':
case 's':
case 'N':
case 'S':
if (unk[0] != 999) {
numind = 0;
numres = unk;
lat[0] = unk[0];
lat[1] = unk[1];
lat[2] = unk[2];
unk[0] = unk[1] = unk[2] = 999;
} else {
numres = lat;
numind = 0;
lat[0] = lat[1] = lat[2] = 999;
}
if (*cur == 'n' || *cur == 'N') {
latsign = 1;
} else {
latsign = -1;
}
cur++;
break;
case 'w':
case 'e':
case 'W':
case 'E':
if (unk[0] != 999) {
numind = 0;
numres = unk;
lon[0] = unk[0];
lon[1] = unk[1];
lon[2] = unk[2];
unk[0] = unk[1] = unk[2] = 999;
} else {
numres = lon;
numind = 0;
lon[0] = lon[1] = lon[2] = 999;
}
if (*cur == 'e' || *cur == 'E') {
lonsign = 1;
} else {
lonsign = -1;
}
cur++;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
case '.':
case ',':
numres[numind] = atof(cur);
while (cur && *cur && strchr("1234567890.,",*cur)) {
cur++;
}
break;
case '-':
unksign = -1;
cur++;
break;
default:
if (numres[numind] != 999) {
numind++;
if (numind > 2) {
numres = unk;
numind = 0;
}
}
cur++;
break;
}
}
if (lat[0] == 999 && lon[0] == 999) {
if (which == 1) {
lat[0] = unk[0];
lat[1] = unk[1];
lat[2] = unk[2];
latsign = unksign;
} else if (which == 2) {
lon[0] = unk[0];
lon[1] = unk[1];
lon[2] = unk[2];
lonsign = unksign;
}
}
if (outlat) {
if (lat[0] != 999) {
*outlat = lat[0];
}
if (lat[1] != 999) {
*outlat += lat[1]/60.0;
}
if (lat[2] != 999) {
*outlat += lat[2]/3600.0;
}
if (*outlat > 360) {
*outlat = ddmm2degrees(*outlat); /* NMEA style */
}
if (latsign) {
*outlat *= latsign;
}
}
if (outlon) {
if (lon[0] != 999) {
*outlon = lon[0];
}
if (lon[1] != 999) {
*outlon += lon[1]/60.0;
}
if (lon[2] != 999) {
*outlon += lon[2]/3600.0;
}
if (*outlon > 360) {
*outlon = ddmm2degrees(*outlon); /* NMEA style */
}
if (lonsign) {
*outlon *= lonsign;
}
}
if (buff != instr) {
xfree(buff);
}
}
#if CSVFMTS_ENABLED
/*
* dec_to_human - convert decimal degrees to human readable
*/
QString
dec_to_human(const char* format, const char* dirs, double val)
{
char* subformat = NULL;
const char* formatptr = NULL;
char* percent = NULL;
char* type = NULL;
int index = 0;
int intvals[3] = {0,0,0};
double dblvals[3] = {0,0,0};
int sign = 0;
sign = (val < 0) ? 0 : 1;
dblvals[0] = fabs(val);
intvals[0] = (int)dblvals[0];
dblvals[1] = 60*(dblvals[0]-intvals[0]);
intvals[1] = (int)dblvals[1];
dblvals[2] = 60*(dblvals[1]-intvals[1]);
intvals[2] = (int)dblvals[2];
subformat = (char*) xmalloc(strlen(format)+2);
formatptr = format;
QString buff;
while (formatptr && *formatptr) {
strcpy(subformat, formatptr);
percent = strchr(subformat, '%');
if (percent) {
type = percent+1+strcspn(percent+1, "cdiouxXeEfgG%");
*(type+1) = '\0';
switch (*type) {
case 'c':
buff += QString().sprintf(subformat, dirs[sign]);
break;
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
if (index>2) {
fatal(MYNAME ": too many format specifiers\n");
}
buff += QString().sprintf(subformat, intvals[index]);
index++;
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
if (index>2) {
fatal(MYNAME ": too many format specifiers\n");
}
buff += QString().sprintf(subformat, dblvals[index]);
index++;
break;
case '%':
buff += subformat;
break;
default:
fatal(MYNAME ": invalid format specifier\n");
break;
}
} else {
buff += subformat;
}
formatptr += strlen(subformat);
}
xfree(subformat);
return buff;
}
/*****************************************************************************/
/* xcsv_file_init() - prepare xcsv_file for first use. */
/*****************************************************************************/
void xcsv_file_init(void)
{
xcsv_file.is_internal = false;
xcsv_file.field_delimiter = QString();
xcsv_file.field_encloser = QString();
xcsv_file.record_delimiter = QString();
xcsv_file.badchars = QString();
xcsv_file.ifield_ct = 0;
xcsv_file.ofield_ct = 0;
xcsv_file.xcsvfp = NULL;
xcsv_file.fname = QString();
xcsv_file.description = NULL;
xcsv_file.extension = NULL;
xcsv_file.prologue.clear();
xcsv_file.epilogue.clear();
QUEUE_INIT(&xcsv_file.ifield);
/* ofield is alloced to allow pointing back at ifields
* where applicable.
*/
xcsv_file.ofield = (queue*) xcalloc(sizeof(queue), 1);
QUEUE_INIT(xcsv_file.ofield);
/*
* Provide a sane default for CSV _files_.
*/
xcsv_file.type = ff_type_file;
xcsv_file.mkshort_handle = mkshort_new_handle();
xcsv_file.gps_datum = GPS_DATUM_WGS84;
}
XcsvFile::XcsvFile() {
is_internal = false;
ifield_ct = ofield_ct = 0;
extension = description = NULL;
// xcsv_file_init();
}
void validate_fieldmap(field_map_t* fmp, bool is_output) {
QString qkey = fmp->key;
QString qval = fmp->val;
QString qprintfc = fmp->printfc;
if (qkey.isEmpty()) {
Fatal() << MYNAME << ": xcsv style is missing" <<
(is_output ? "output" : "input") << "field type.";
}
if (!fmp->val) {
Fatal() << MYNAME << ": xcsv style" << qkey << "is missing default.";
}
if (is_output && !fmp->printfc) {
Fatal() << MYNAME << ": xcsv style" << qkey << "output is missing format specifier.";
}
}
/*****************************************************************************/
/* xcsv_ifield_add() - add input field to ifield queue. */
/* usage: xcsv_ifield_add("DESCRIPTION", "", "%s") */
/*****************************************************************************/
void
xcsv_ifield_add(char* key, char* val, char* pfc)
{
field_map_t* fmp = (field_map_t*) xcalloc(sizeof(*fmp), 1);
struct xt_mapping* xm = in_word_set(key, strlen(key));
fmp->key = key;
fmp->hashed_key = xm ? xm->xt_token : -1;
fmp->val = val;
fmp->printfc = pfc;
validate_fieldmap(fmp, false);
ENQUEUE_TAIL(&xcsv_file.ifield, &fmp->Q);
xcsv_file.ifield_ct++;
}
/*****************************************************************************/
/* xcsv_ofield_add() - add output field to ofield queue. */
/* usage: xcsv_ofield_add("LAT_DECIMAL", "", "%08.5lf") */
/*****************************************************************************/
void
xcsv_ofield_add(char* key, char* val, char* pfc, int options)
{
field_map_t* fmp = (field_map_t*) xcalloc(sizeof(*fmp), 1);
struct xt_mapping* xm = in_word_set(key, strlen(key));
fmp->key = key;
fmp->hashed_key = xm ? xm->xt_token : -1;
fmp->val = val;
fmp->printfc = pfc;
fmp->options = options;
validate_fieldmap(fmp, true);
ENQUEUE_TAIL(xcsv_file.ofield, &fmp->Q);
xcsv_file.ofield_ct++;
}
/*****************************************************************************/
/* xcsv_prologue_add() - add prologue line to prologue queue */
/* usage: xcsv_prologue_add("Four score and seven years ago today,") */
/*****************************************************************************/
void
xcsv_prologue_add(char* prologue)
{
xcsv_file.prologue.append(prologue);
}
/*****************************************************************************/
/* xcsv_epilogue_add() - add epilogue line to epilogue queue */
/* usage: xcsv_epilogue_add("shall not perish from the earth.") */
/*****************************************************************************/
void
xcsv_epilogue_add(char* epilogue)
{
xcsv_file.epilogue.append(epilogue);
}
static
QDateTime
yyyymmdd_to_time(const char* s)
{
QDate d = QDate::fromString(s, "yyyyMMdd");
return QDateTime(d);
}
/*
* sscanftime - Parse a date buffer using strftime format
*/
static
time_t
sscanftime(const char* s, const char* format, const int gmt)
{
struct tm stm;
memset(&stm, 0, sizeof(stm));
if (strptime(s, format, &stm)) {
if ((stm.tm_mday == 0) && (stm.tm_mon == 0) && (stm.tm_year == 0)) {
stm.tm_mday = 1;
stm.tm_mon = 0;
stm.tm_year = 70;
}
stm.tm_isdst = -1;
if (gmt) {
return mkgmtime(&stm);
} else {
return mktime(&stm);
}
}
// Don't fuss for empty strings.
if (*s) {
warning("date parse of string '%s' with format '%s' failed.\n",
s, format);
}
return 0;
}
static
time_t
addhms(const char* s, const char* format)
{
time_t tt =0;
int hour =0;
int min =0;
int sec =0;
int ac;
char* ampm = (char*) xmalloc(strlen(s) + 1);
ac = sscanf(s, format, &hour, &min, &sec, ampm);
/* If no time format in arg string, assume AM */
if (ac < 4) {
ampm[0] = 0;
}
if (ac) {
tt = ((tolower(ampm[0])=='p')?43200:0)+3600*hour+60*min+sec;
}
xfree(ampm);
return tt;
}
static
QString
writetime(const char* format, time_t t, bool gmt)
{
static struct tm* stmp;
if (gmt) {
stmp = gmtime(&t);
} else {
stmp = localtime(&t);
}
// It's unfortunate that we publish the definition of "strftime specifiers"
// in the style definitions. For this reason, we have to bust everything
// down to a time_t and then let strftime handle them.
char tbuff[1024];
strftime(tbuff, sizeof tbuff, format, stmp);
QDateTime dt = QDateTime::fromTime_t(t);
return QString(tbuff);
}
static
QString
writetime(const char* format, const gpsbabel::DateTime& t, bool gmt)
{
return writetime(format, t.toTime_t(), gmt);
}
QString
writehms(const char* format, time_t t, int gmt)
{
static struct tm no_time = tm();
static struct tm* stmp = &no_time;
if (gmt) {
stmp = gmtime(&t);
} else {
stmp = localtime(&t);
}
if (stmp == NULL) {
stmp = &no_time;
}
return QString().sprintf(format,
stmp->tm_hour, stmp->tm_min, stmp->tm_sec,
(stmp->tm_hour >= 12 ? "PM" : "AM"));
}
QString
writehms(const char* format, const gpsbabel::DateTime& t, int gmt)
{
return writehms(format, t.toTime_t(), gmt);
}
static
long
time_to_yyyymmdd(QDateTime t)
{
QDate d = t.date();
return d.year() * 10000 + d.month() * 100 + d.day();
}
static garmin_fs_t*
gmsd_init(Waypoint* wpt)
{
garmin_fs_t* gmsd = GMSD_FIND(wpt);
if (gmsd == NULL) {
gmsd = garmin_fs_alloc(-1);
fs_chain_add(&wpt->fs, (format_specific_data*) gmsd);
}
return gmsd;
}
/*****************************************************************************/
/* xcsv_parse_val() - parse incoming data into the waypt structure. */
/* usage: xcsv_parse_val("-123.34", *waypt, *field_map) */
/*****************************************************************************/
static void
xcsv_parse_val(const char* s, Waypoint* wpt, const field_map_t* fmp,
route_head** trk)
{
const char* enclosure = "";
geocache_data* gc_data = NULL;
if (!fmp->printfc) {
fatal(MYNAME ": xcsv style '%s' is missing format specifier", fmp->key);
}
if (0 == strcmp(fmp->printfc, "\"%s\"")) {
enclosure = "\"";
}
switch (fmp->hashed_key) {
case XT_IGNORE:
/* IGNORE -- Categorically ignore this... */