forked from subsurface/subsurface
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse-xml.c
2883 lines (2548 loc) · 74.6 KB
/
parse-xml.c
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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#define __USE_XOPEN
#include <time.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/tree.h>
#include <libxslt/transform.h>
#include <libdivecomputer/parser.h>
#include "gettext.h"
#include "dive.h"
#include "device.h"
#include "membuffer.h"
int verbose, quit;
int metric = 1;
static xmlDoc *test_xslt_transforms(xmlDoc *doc, const char **params);
/* the dive table holds the overall dive list; target table points at
* the table we are currently filling */
struct dive_table dive_table;
struct dive_table *target_table = NULL;
/* Trim a character string by removing leading and trailing white space characters.
* Parameter: a pointer to a null-terminated character string (buffer);
* Return value: length of the trimmed string, excluding the terminal 0x0 byte
* The original pointer (buffer) remains valid after this function has been called
* and points to the trimmed string */
int trimspace(char *buffer) {
int i, size, start, end;
size = strlen(buffer);
for(start = 0; isspace(buffer[start]); start++)
if (start >= size) return 0; // Find 1st character following leading whitespace
for(end = size - 1; isspace(buffer[end]); end--) // Find last character before trailing whitespace
if (end <= 0) return 0;
for(i = start; i <= end; i++) // Move the nonspace characters to the start of the string
buffer[i-start] = buffer[i];
size = end - start + 1;
buffer[size] = 0x0; // then terminate the string
return size; // return string length
}
/*
* Clear a dive_table
*/
void clear_table(struct dive_table *table)
{
for (int i = 0; i < table->nr; i++)
free(table->dives[i]);
table->nr = 0;
}
/*
* Add a dive into the dive_table array
*/
void record_dive_to_table(struct dive *dive, struct dive_table *table)
{
assert(table != NULL);
int nr = table->nr, allocated = table->allocated;
struct dive **dives = table->dives;
if (nr >= allocated) {
allocated = (nr + 32) * 3 / 2;
dives = realloc(dives, allocated * sizeof(struct dive *));
if (!dives)
exit(1);
table->dives = dives;
table->allocated = allocated;
}
dives[nr] = fixup_dive(dive);
table->nr = nr + 1;
}
void record_dive(struct dive *dive)
{
record_dive_to_table(dive, &dive_table);
}
static void start_match(const char *type, const char *name, char *buffer)
{
if (verbose > 2)
printf("Matching %s '%s' (%s)\n",
type, name, buffer);
}
static void nonmatch(const char *type, const char *name, char *buffer)
{
if (verbose > 1)
printf("Unable to match %s '%s' (%s)\n",
type, name, buffer);
}
typedef void (*matchfn_t)(char *buffer, void *);
static int match(const char *pattern, int plen,
const char *name,
matchfn_t fn, char *buf, void *data)
{
switch (name[plen]) {
case '\0':
case '.':
break;
default:
return 0;
}
if (memcmp(pattern, name, plen))
return 0;
fn(buf, data);
return 1;
}
struct units xml_parsing_units;
const struct units SI_units = SI_UNITS;
const struct units IMPERIAL_units = IMPERIAL_UNITS;
/*
* Dive info as it is being built up..
*/
#define MAX_EVENT_NAME 128
static struct divecomputer *cur_dc;
static struct dive *cur_dive;
static dive_trip_t *cur_trip = NULL;
static struct sample *cur_sample;
static struct picture *cur_picture;
static union {
struct event event;
char allocation[sizeof(struct event)+MAX_EVENT_NAME];
} event_allocation = { .event.deleted = 1 };
#define cur_event event_allocation.event
static struct {
struct {
const char *model;
uint32_t deviceid;
const char *nickname, *serial_nr, *firmware;
} dc;
} cur_settings;
static bool in_settings = false;
static bool in_userid = false;
static struct tm cur_tm;
static int cur_cylinder_index, cur_ws_index;
static int lastndl, laststoptime, laststopdepth, lastcns, lastpo2, lastindeco;
static int lastcylinderindex, lastsensor;
static struct extra_data cur_extra_data;
/*
* If we don't have an explicit dive computer,
* we use the implicit one that every dive has..
*/
static struct divecomputer *get_dc(void)
{
return cur_dc ?: &cur_dive->dc;
}
static enum import_source {
UNKNOWN,
LIBDIVECOMPUTER,
DIVINGLOG,
UDDF,
} import_source;
static void divedate(const char *buffer, timestamp_t *when)
{
int d, m, y;
int hh, mm, ss;
hh = 0;
mm = 0;
ss = 0;
if (sscanf(buffer, "%d.%d.%d %d:%d:%d", &d, &m, &y, &hh, &mm, &ss) >= 3) {
/* This is ok, and we got at least the date */
} else if (sscanf(buffer, "%d-%d-%d %d:%d:%d", &y, &m, &d, &hh, &mm, &ss) >= 3) {
/* This is also ok */
} else {
fprintf(stderr, "Unable to parse date '%s'\n", buffer);
return;
}
cur_tm.tm_year = y;
cur_tm.tm_mon = m - 1;
cur_tm.tm_mday = d;
cur_tm.tm_hour = hh;
cur_tm.tm_min = mm;
cur_tm.tm_sec = ss;
*when = utc_mktime(&cur_tm);
}
static void divetime(const char *buffer, timestamp_t *when)
{
int h, m, s = 0;
if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
cur_tm.tm_hour = h;
cur_tm.tm_min = m;
cur_tm.tm_sec = s;
*when = utc_mktime(&cur_tm);
}
}
/* Libdivecomputer: "2011-03-20 10:22:38" */
static void divedatetime(char *buffer, timestamp_t *when)
{
int y, m, d;
int hr, min, sec;
if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
&y, &m, &d, &hr, &min, &sec) == 6) {
cur_tm.tm_year = y;
cur_tm.tm_mon = m - 1;
cur_tm.tm_mday = d;
cur_tm.tm_hour = hr;
cur_tm.tm_min = min;
cur_tm.tm_sec = sec;
*when = utc_mktime(&cur_tm);
}
}
enum ParseState {
FINDSTART,
FINDEND
};
static void divetags(char *buffer, struct tag_entry **tags)
{
int i = 0, start = 0, end = 0;
enum ParseState state = FINDEND;
int len = buffer ? strlen(buffer) : 0;
while (i < len) {
if (buffer[i] == ',') {
if (state == FINDSTART) {
/* Detect empty tags */
} else if (state == FINDEND) {
/* Found end of tag */
if (i > 0 && buffer[i - 1] != '\\') {
buffer[i] = '\0';
state = FINDSTART;
taglist_add_tag(tags, buffer + start);
} else {
state = FINDSTART;
}
}
} else if (buffer[i] == ' ') {
/* Handled */
} else {
/* Found start of tag */
if (state == FINDSTART) {
state = FINDEND;
start = i;
} else if (state == FINDEND) {
end = i;
}
}
i++;
}
if (state == FINDEND) {
if (end < start)
end = len - 1;
if (len > 0) {
buffer[end + 1] = '\0';
taglist_add_tag(tags, buffer + start);
}
}
}
enum number_type {
NEITHER,
FLOAT
};
static enum number_type parse_float(const char *buffer, double *res, const char **endp)
{
double val;
static bool first_time = true;
errno = 0;
val = ascii_strtod(buffer, endp);
if (errno || *endp == buffer)
return NEITHER;
if (**endp == ',') {
if (IS_FP_SAME(val, rint(val))) {
/* we really want to send an error if this is a Subsurface native file
* as this is likely indication of a bug - but right now we don't have
* that information available */
if (first_time) {
fprintf(stderr, "Floating point value with decimal comma (%s)?\n", buffer);
first_time = false;
}
/* Try again in permissive mode*/
val = strtod_flags(buffer, endp, 0);
}
}
*res = val;
return FLOAT;
}
union int_or_float {
double fp;
};
static enum number_type integer_or_float(char *buffer, union int_or_float *res)
{
const char *end;
return parse_float(buffer, &res->fp, &end);
}
static void pressure(char *buffer, pressure_t *pressure)
{
double mbar = 0.0;
union int_or_float val;
switch (integer_or_float(buffer, &val)) {
case FLOAT:
/* Just ignore zero values */
if (!val.fp)
break;
switch (xml_parsing_units.pressure) {
case PASCAL:
mbar = val.fp / 100;
break;
case BAR:
/* Assume mbar, but if it's really small, it's bar */
mbar = val.fp;
if (fabs(mbar) < 5000)
mbar = mbar * 1000;
break;
case PSI:
mbar = psi_to_mbar(val.fp);
break;
}
if (fabs(mbar) > 5 && fabs(mbar) < 5000000) {
pressure->mbar = rint(mbar);
break;
}
/* fallthrough */
default:
printf("Strange pressure reading %s\n", buffer);
}
}
static void cylinder_use(char *buffer, enum cylinderuse *cyl_use)
{
if (trimspace(buffer))
*cyl_use = cylinderuse_from_text(buffer);
}
static void salinity(char *buffer, int *salinity)
{
union int_or_float val;
switch (integer_or_float(buffer, &val)) {
case FLOAT:
*salinity = rint(val.fp * 10.0);
break;
default:
printf("Strange salinity reading %s\n", buffer);
}
}
static void depth(char *buffer, depth_t *depth)
{
union int_or_float val;
switch (integer_or_float(buffer, &val)) {
case FLOAT:
switch (xml_parsing_units.length) {
case METERS:
depth->mm = rint(val.fp * 1000);
break;
case FEET:
depth->mm = feet_to_mm(val.fp);
break;
}
break;
default:
printf("Strange depth reading %s\n", buffer);
}
}
static void extra_data_start(void)
{
memset(&cur_extra_data, 0, sizeof(struct extra_data));
}
static void extra_data_end(void)
{
// don't save partial structures - we must have both key and value
if (cur_extra_data.key && cur_extra_data.value)
add_extra_data(cur_dc, cur_extra_data.key, cur_extra_data.value);
}
static void weight(char *buffer, weight_t *weight)
{
union int_or_float val;
switch (integer_or_float(buffer, &val)) {
case FLOAT:
switch (xml_parsing_units.weight) {
case KG:
weight->grams = rint(val.fp * 1000);
break;
case LBS:
weight->grams = lbs_to_grams(val.fp);
break;
}
break;
default:
printf("Strange weight reading %s\n", buffer);
}
}
static void temperature(char *buffer, temperature_t *temperature)
{
union int_or_float val;
switch (integer_or_float(buffer, &val)) {
case FLOAT:
switch (xml_parsing_units.temperature) {
case KELVIN:
temperature->mkelvin = val.fp * 1000;
break;
case CELSIUS:
temperature->mkelvin = C_to_mkelvin(val.fp);
break;
case FAHRENHEIT:
temperature->mkelvin = F_to_mkelvin(val.fp);
break;
}
break;
default:
printf("Strange temperature reading %s\n", buffer);
}
/* temperatures outside -40C .. +70C should be ignored */
if (temperature->mkelvin < ZERO_C_IN_MKELVIN - 40000 ||
temperature->mkelvin > ZERO_C_IN_MKELVIN + 70000)
temperature->mkelvin = 0;
}
static void sampletime(char *buffer, duration_t *time)
{
int i;
int min, sec;
i = sscanf(buffer, "%d:%d", &min, &sec);
switch (i) {
case 1:
sec = min;
min = 0;
/* fallthrough */
case 2:
time->seconds = sec + min * 60;
break;
default:
printf("Strange sample time reading %s\n", buffer);
}
}
static void offsettime(char *buffer, offset_t *time)
{
duration_t uoffset;
int sign = 1;
if (*buffer == '-') {
sign = -1;
buffer++;
}
/* yes, this could indeed fail if we have an offset > 34yrs
* - too bad */
sampletime(buffer, &uoffset);
time->seconds = sign * uoffset.seconds;
}
static void duration(char *buffer, duration_t *time)
{
/* DivingLog 5.08 (and maybe other versions) appear to sometimes
* store the dive time as 44.00 instead of 44:00;
* This attempts to parse this in a fairly robust way */
if (!strchr(buffer, ':') && strchr(buffer, '.')) {
char *mybuffer = strdup(buffer);
char *dot = strchr(mybuffer, '.');
*dot = ':';
sampletime(mybuffer, time);
free(mybuffer);
} else {
sampletime(buffer, time);
}
}
static void percent(char *buffer, fraction_t *fraction)
{
double val;
const char *end;
switch (parse_float(buffer, &val, &end)) {
case FLOAT:
/* Turn fractions into percent unless explicit.. */
if (val <= 1.0) {
while (isspace(*end))
end++;
if (*end != '%')
val *= 100;
}
/* Then turn percent into our integer permille format */
if (val >= 0 && val <= 100.0) {
fraction->permille = rint(val * 10);
break;
}
default:
printf(translate("gettextFromC", "Strange percentage reading %s\n"), buffer);
break;
}
}
static void gasmix(char *buffer, fraction_t *fraction)
{
/* libdivecomputer does negative percentages. */
if (*buffer == '-')
return;
if (cur_cylinder_index < MAX_CYLINDERS)
percent(buffer, fraction);
}
static void gasmix_nitrogen(char *buffer, struct gasmix *gasmix)
{
/* Ignore n2 percentages. There's no value in them. */
}
static void cylindersize(char *buffer, volume_t *volume)
{
union int_or_float val;
switch (integer_or_float(buffer, &val)) {
case FLOAT:
volume->mliter = rint(val.fp * 1000);
break;
default:
printf("Strange volume reading %s\n", buffer);
break;
}
}
static void utf8_string(char *buffer, void *_res)
{
char **res = _res;
int size;
size = trimspace(buffer);
if(size)
*res = strdup(buffer);
}
static void event_name(char *buffer, char *name)
{
int size = trimspace(buffer);
if (size >= MAX_EVENT_NAME)
size = MAX_EVENT_NAME-1;
memcpy(name, buffer, size);
name[size] = 0;
}
/* Extract the dive computer type from the xml text buffer */
static void get_dc_type(char *buffer, enum dive_comp_type *dct)
{
if (trimspace(buffer)) {
for (enum dive_comp_type i = 0; i < NUM_DC_TYPE; i++) {
if (strcmp(buffer, divemode_text[i]) == 0)
*dct = i;
}
}
}
#define MATCH(pattern, fn, dest) ({ \
/* Silly type compatibility test */ \
if (0) (fn)("test", dest); \
match(pattern, strlen(pattern), name, (matchfn_t) (fn), buf, dest); })
static void get_index(char *buffer, int *i)
{
*i = atoi(buffer);
}
static void get_uint8(char *buffer, uint8_t *i)
{
*i = atoi(buffer);
}
static void get_bearing(char *buffer, bearing_t *bearing)
{
bearing->degrees = atoi(buffer);
}
static void get_rating(char *buffer, int *i)
{
int j = atoi(buffer);
if (j >= 0 && j <= 5) {
*i = j;
}
}
static void double_to_o2pressure(char *buffer, o2pressure_t *i)
{
i->mbar = rint(ascii_strtod(buffer, NULL) * 1000.0);
}
static void hex_value(char *buffer, uint32_t *i)
{
*i = strtoul(buffer, NULL, 16);
}
static void get_tripflag(char *buffer, tripflag_t *tf)
{
*tf = strcmp(buffer, "NOTRIP") ? TF_NONE : NO_TRIP;
}
/*
* Divinglog is crazy. The temperatures are in celsius. EXCEPT
* for the sample temperatures, that are in Fahrenheit.
* WTF?
*
* Oh, and I think Diving Log *internally* probably kept them
* in celsius, because I'm seeing entries like
*
* <Temp>32.0</Temp>
*
* in there. Which is freezing, aka 0 degC. I bet the "0" is
* what Diving Log uses for "no temperature".
*
* So throw away crap like that.
*
* It gets worse. Sometimes the sample temperatures are in
* Celsius, which apparently happens if you are in a SI
* locale. So we now do:
*
* - temperatures < 32.0 == Celsius
* - temperature == 32.0 -> garbage, it's a missing temperature (zero converted from C to F)
* - temperatures > 32.0 == Fahrenheit
*/
static void fahrenheit(char *buffer, temperature_t *temperature)
{
union int_or_float val;
switch (integer_or_float(buffer, &val)) {
case FLOAT:
if (IS_FP_SAME(val.fp, 32.0))
break;
if (val.fp < 32.0)
temperature->mkelvin = C_to_mkelvin(val.fp);
else
temperature->mkelvin = F_to_mkelvin(val.fp);
break;
default:
fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
}
}
/*
* Did I mention how bat-shit crazy divinglog is? The sample
* pressures are in PSI. But the tank working pressure is in
* bar. WTF^2?
*
* Crazy stuff like this is why subsurface has everything in
* these inconvenient typed structures, and you have to say
* "pressure->mbar" to get the actual value. Exactly so that
* you can never have unit confusion.
*
* It gets worse: sometimes apparently the pressures are in
* bar, sometimes in psi. Dirk suspects that this may be a
* DivingLog Uemis importer bug, and that they are always
* supposed to be in bar, but that the importer got the
* sample importing wrong.
*
* Sadly, there's no way to really tell. So I think we just
* have to have some arbitrary cut-off point where we assume
* that smaller values mean bar.. Not good.
*/
static void psi_or_bar(char *buffer, pressure_t *pressure)
{
union int_or_float val;
switch (integer_or_float(buffer, &val)) {
case FLOAT:
if (val.fp > 400)
pressure->mbar = psi_to_mbar(val.fp);
else
pressure->mbar = rint(val.fp * 1000);
break;
default:
fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
}
}
static int divinglog_fill_sample(struct sample *sample, const char *name, char *buf)
{
return MATCH("time.p", sampletime, &sample->time) ||
MATCH("depth.p", depth, &sample->depth) ||
MATCH("temp.p", fahrenheit, &sample->temperature) ||
MATCH("press1.p", psi_or_bar, &sample->cylinderpressure) ||
0;
}
static void uddf_gasswitch(char *buffer, struct sample *sample)
{
int idx = atoi(buffer);
int seconds = sample->time.seconds;
struct dive *dive = cur_dive;
struct divecomputer *dc = get_dc();
add_gas_switch_event(dive, dc, seconds, idx);
}
static int uddf_fill_sample(struct sample *sample, const char *name, char *buf)
{
return MATCH("divetime", sampletime, &sample->time) ||
MATCH("depth", depth, &sample->depth) ||
MATCH("temperature", temperature, &sample->temperature) ||
MATCH("tankpressure", pressure, &sample->cylinderpressure) ||
MATCH("ref.switchmix", uddf_gasswitch, sample) ||
0;
}
static void eventtime(char *buffer, duration_t *duration)
{
sampletime(buffer, duration);
if (cur_sample)
duration->seconds += cur_sample->time.seconds;
}
static void try_to_match_autogroup(const char *name, char *buf)
{
int autogroupvalue;
start_match("autogroup", name, buf);
if (MATCH("state.autogroup", get_index, &autogroupvalue)) {
set_autogroup(autogroupvalue);
return;
}
nonmatch("autogroup", name, buf);
}
void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int seconds, int idx)
{
/* The gas switch event format is insane for historical reasons */
struct gasmix *mix = &dive->cylinder[idx].gasmix;
int o2 = get_o2(mix);
int he = get_he(mix);
struct event *ev;
int value;
o2 = (o2 + 5) / 10;
he = (he + 5) / 10;
value = o2 + (he << 16);
ev = add_event(dc, seconds, he ? SAMPLE_EVENT_GASCHANGE2 : SAMPLE_EVENT_GASCHANGE, 0, value, "gaschange");
if (ev) {
ev->gas.index = idx;
ev->gas.mix = *mix;
}
}
static void get_cylinderindex(char *buffer, uint8_t *i)
{
*i = atoi(buffer);
if (lastcylinderindex != *i) {
add_gas_switch_event(cur_dive, get_dc(), cur_sample->time.seconds, *i);
lastcylinderindex = *i;
}
}
static void get_sensor(char *buffer, uint8_t *i)
{
*i = atoi(buffer);
lastsensor = *i;
}
static void try_to_fill_dc_settings(const char *name, char *buf)
{
start_match("divecomputerid", name, buf);
if (MATCH("model.divecomputerid", utf8_string, &cur_settings.dc.model))
return;
if (MATCH("deviceid.divecomputerid", hex_value, &cur_settings.dc.deviceid))
return;
if (MATCH("nickname.divecomputerid", utf8_string, &cur_settings.dc.nickname))
return;
if (MATCH("serial.divecomputerid", utf8_string, &cur_settings.dc.serial_nr))
return;
if (MATCH("firmware.divecomputerid", utf8_string, &cur_settings.dc.firmware))
return;
nonmatch("divecomputerid", name, buf);
}
static void try_to_fill_event(const char *name, char *buf)
{
start_match("event", name, buf);
if (MATCH("event", event_name, cur_event.name))
return;
if (MATCH("name", event_name, cur_event.name))
return;
if (MATCH("time", eventtime, &cur_event.time))
return;
if (MATCH("type", get_index, &cur_event.type))
return;
if (MATCH("flags", get_index, &cur_event.flags))
return;
if (MATCH("value", get_index, &cur_event.value))
return;
if (MATCH("cylinder", get_index, &cur_event.gas.index)) {
/* We add one to indicate that we got an actual cylinder index value */
cur_event.gas.index++;
return;
}
if (MATCH("o2", percent, &cur_event.gas.mix.o2))
return;
if (MATCH("he", percent, &cur_event.gas.mix.he))
return;
nonmatch("event", name, buf);
}
static int match_dc_data_fields(struct divecomputer *dc, const char *name, char *buf)
{
if (MATCH("maxdepth", depth, &dc->maxdepth))
return 1;
if (MATCH("meandepth", depth, &dc->meandepth))
return 1;
if (MATCH("max.depth", depth, &dc->maxdepth))
return 1;
if (MATCH("mean.depth", depth, &dc->meandepth))
return 1;
if (MATCH("duration", duration, &dc->duration))
return 1;
if (MATCH("divetime", duration, &dc->duration))
return 1;
if (MATCH("divetimesec", duration, &dc->duration))
return 1;
if (MATCH("surfacetime", duration, &dc->surfacetime))
return 1;
if (MATCH("airtemp", temperature, &dc->airtemp))
return 1;
if (MATCH("watertemp", temperature, &dc->watertemp))
return 1;
if (MATCH("air.temperature", temperature, &dc->airtemp))
return 1;
if (MATCH("water.temperature", temperature, &dc->watertemp))
return 1;
if (MATCH("pressure.surface", pressure, &dc->surface_pressure))
return 1;
if (MATCH("salinity.water", salinity, &dc->salinity))
return 1;
if (MATCH("key.extradata", utf8_string, &cur_extra_data.key))
return 1;
if (MATCH("value.extradata", utf8_string, &cur_extra_data.value))
return 1;
return 0;
}
/* We're in the top-level dive xml. Try to convert whatever value to a dive value */
static void try_to_fill_dc(struct divecomputer *dc, const char *name, char *buf)
{
start_match("divecomputer", name, buf);
if (MATCH("date", divedate, &dc->when))
return;
if (MATCH("time", divetime, &dc->when))
return;
if (MATCH("model", utf8_string, &dc->model))
return;
if (MATCH("deviceid", hex_value, &dc->deviceid))
return;
if (MATCH("diveid", hex_value, &dc->diveid))
return;
if (MATCH("dctype", get_dc_type, &dc->divemode))
return;
if (MATCH("no_o2sensors", get_sensor, &dc->no_o2sensors))
return;
if (match_dc_data_fields(dc, name, buf))
return;
nonmatch("divecomputer", name, buf);
}
/* We're in samples - try to convert the random xml value to something useful */
static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
{
int in_deco;
start_match("sample", name, buf);
if (MATCH("pressure.sample", pressure, &sample->cylinderpressure))
return;
if (MATCH("cylpress.sample", pressure, &sample->cylinderpressure))
return;
if (MATCH("pdiluent.sample", pressure, &sample->cylinderpressure))
return;
if (MATCH("o2pressure.sample", pressure, &sample->o2cylinderpressure))
return;
if (MATCH("cylinderindex.sample", get_cylinderindex, &sample->sensor))
return;
if (MATCH("sensor.sample", get_sensor, &sample->sensor))
return;
if (MATCH("depth.sample", depth, &sample->depth))
return;
if (MATCH("temp.sample", temperature, &sample->temperature))
return;
if (MATCH("temperature.sample", temperature, &sample->temperature))
return;
if (MATCH("sampletime.sample", sampletime, &sample->time))
return;
if (MATCH("time.sample", sampletime, &sample->time))
return;
if (MATCH("ndl.sample", sampletime, &sample->ndl))
return;
if (MATCH("tts.sample", sampletime, &sample->tts))
return;
if (MATCH("in_deco.sample", get_index, &in_deco)) {
sample->in_deco = (in_deco == 1);
return;
}
if (MATCH("stoptime.sample", sampletime, &sample->stoptime))
return;
if (MATCH("stopdepth.sample", depth, &sample->stopdepth))
return;
if (MATCH("cns.sample", get_uint8, &sample->cns))
return;
if (MATCH("sensor1.sample", double_to_o2pressure, &sample->o2sensor[0])) // CCR O2 sensor data
return;
if (MATCH("sensor2.sample", double_to_o2pressure, &sample->o2sensor[1]))
return;
if (MATCH("sensor3.sample", double_to_o2pressure, &sample->o2sensor[2])) // up to 3 CCR sensors
return;
if (MATCH("po2.sample", double_to_o2pressure, &sample->setpoint)) {
cur_dive->dc.divemode = CCR;
return;
}
if (MATCH("heartbeat", get_uint8, &sample->heartbeat))
return;
if (MATCH("bearing", get_bearing, &sample->bearing))
return;
switch (import_source) {
case DIVINGLOG:
if (divinglog_fill_sample(sample, name, buf))
return;
break;
case UDDF:
if (uddf_fill_sample(sample, name, buf))
return;
break;
default:
break;
}
nonmatch("sample", name, buf);
}
void try_to_fill_userid(const char *name, char *buf)
{
if (prefs.save_userid_local)
set_userid(buf);
}
static const char *country, *city;
static void divinglog_place(char *place, char **location)
{
char buffer[1024], *p;
int len;
len = snprintf(buffer, sizeof(buffer),
"%s%s%s%s%s",
place,
city ? ", " : "",
city ? city : "",
country ? ", " : "",
country ? country : "");
p = malloc(len + 1);
memcpy(p, buffer, len + 1);
*location = p;
city = NULL;
country = NULL;
}
static int divinglog_dive_match(struct dive *dive, const char *name, char *buf)
{
return MATCH("divedate", divedate, &dive->when) ||
MATCH("entrytime", divetime, &dive->when) ||
MATCH("divetime", duration, &dive->dc.duration) ||
MATCH("depth", depth, &dive->dc.maxdepth) ||
MATCH("depthavg", depth, &dive->dc.meandepth) ||
MATCH("tanktype", utf8_string, &dive->cylinder[0].type.description) ||
MATCH("tanksize", cylindersize, &dive->cylinder[0].type.size) ||