-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathssrf2divecmd.c
1262 lines (1083 loc) · 27.6 KB
/
ssrf2divecmd.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
/* $Id$ */
/*
* Copyright (c) 2018 Kristaps Dzonsons <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/queue.h>
#include <assert.h>
#if HAVE_ERR
# include <err.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <float.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <libdivecomputer/descriptor.h>
#include <expat.h>
#include "parser.h"
int verbose = 0;
struct parse {
XML_Parser p; /* parser routine */
const char *file; /* parsed filename */
struct dlog *curlog; /* current divelog */
struct dive *curdive; /* current dive */
struct diveq *dives; /* all dives */
struct divestat *stat; /* statistics */
char *ign; /* token we're ignoring (or NULL) */
size_t igndepth; /* nested ignore scopes */
size_t pid; /* current dive no. */
int in_deco; /* are we in deco? */
};
static void
logdbg(const struct parse *p, const char *fmt, ...)
__attribute__((format (printf, 2, 3)));
static void
logwarnx(const struct parse *p, const char *fmt, ...)
__attribute__((format (printf, 2, 3)));
static void
logerrx(struct parse *p, const char *fmt, ...)
__attribute__((format (printf, 2, 3)));
static __dead void
logfatal(const struct parse *p, const char *fmt, ...)
__attribute__((format (printf, 2, 3)));
static void
parse_open(void *, const XML_Char *, const XML_Char **);
static void
parse_close(void *, const XML_Char *);
static void
logerrx(struct parse *p, const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s:%zu:%zu: error: ", p->file,
XML_GetCurrentLineNumber(p->p),
XML_GetCurrentColumnNumber(p->p));
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
XML_StopParser(p->p, 0);
}
static void
logattr(const struct parse *p, const char *tag, const char *attr)
{
logwarnx(p, "%s: unknown <%s> attribute", attr, tag);
}
static void
lognattr(struct parse *p, const char *tag, const char *attr)
{
logerrx(p, "missing <%s> attribute: %s", attr, tag);
}
static void
logwarnx(const struct parse *p, const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s:%zu:%zu: warning: ", p->file,
XML_GetCurrentLineNumber(p->p),
XML_GetCurrentColumnNumber(p->p));
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
static void
logdbg(const struct parse *p, const char *fmt, ...)
{
va_list ap;
if ( ! verbose)
return;
fprintf(stderr, "%s:%zu:%zu: ", p->file,
XML_GetCurrentLineNumber(p->p),
XML_GetCurrentColumnNumber(p->p));
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
static void
logfatal(const struct parse *p, const char *fmt, ...)
{
va_list ap;
int er = errno;
fprintf(stderr, "%s:%zu:%zu: fatal: ", p->file,
XML_GetCurrentLineNumber(p->p),
XML_GetCurrentColumnNumber(p->p));
if (NULL != fmt) {
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, ": ");
}
fprintf(stderr, "%s\n", strerror(er));
exit(EXIT_FAILURE);
}
static char *
xstrndup(const struct parse *p, const char *cp, size_t sz)
{
char *pp;
if (NULL == (pp = strndup(cp, sz)))
logfatal(p, "strndup");
return(pp);
}
static void *
xreallocarray(const struct parse *p, void *ptr, size_t nm, size_t sz)
{
if (NULL == (ptr = reallocarray(ptr, nm, sz)))
logfatal(p, "reallocarray");
return(ptr);
}
static void *
xcalloc(const struct parse *p, size_t nm, size_t sz)
{
char *pp;
if (NULL == (pp = calloc(nm, sz)))
logfatal(p, "calloc");
return(pp);
}
static char *
xstrdup(const struct parse *p, const char *cp)
{
char *pp;
if (NULL == (pp = strdup(cp)))
logfatal(p, "strdup");
return(pp);
}
static void
logerrp(struct parse *p)
{
logerrx(p, "%s", XML_ErrorString(XML_GetErrorCode(p->p)));
}
/*
* Allocate a sample and insert it into the correct order.
*/
static struct samp *
samp_alloc(struct parse *p, size_t tm)
{
struct samp *samp, *ss;
assert(NULL != p->curdive);
/* Short-circuit: test if we're at the end. */
ss = TAILQ_LAST(&p->curdive->samps, sampq);
if (NULL != ss && ss->time < tm) {
samp = xcalloc(p, 1, sizeof(struct samp));
samp->time = tm;
TAILQ_INSERT_TAIL(&p->curdive->samps, samp, entries);
p->curdive->nsamps++;
return samp;
} else if (NULL != ss && ss->time == tm)
return ss;
TAILQ_FOREACH(ss, &p->curdive->samps, entries)
if (ss->time == tm)
return ss;
else if (ss->time > tm)
break;
samp = xcalloc(p, 1, sizeof(struct samp));
samp->time = tm;
if (NULL != ss)
TAILQ_INSERT_BEFORE(ss, samp, entries);
else
TAILQ_INSERT_TAIL(&p->curdive->samps, samp, entries);
p->curdive->nsamps++;
return samp;
}
static struct dgroup *
group_add(struct parse *p, size_t i, struct dive *d)
{
struct dive *dp = NULL;
struct dgroup *dg = p->stat->groups[i];
d->group = dg;
dg->ndives++;
if (0 == d->datetime) {
TAILQ_INSERT_TAIL(&dg->dives, d, gentries);
return dg;
}
TAILQ_FOREACH(dp, &dg->dives, gentries)
if (dp->datetime &&
dp->datetime > d->datetime)
break;
if (NULL == dp)
TAILQ_INSERT_TAIL(&dg->dives, d, gentries);
else
TAILQ_INSERT_BEFORE(dp, d, gentries);
return dg;
}
static struct dgroup *
group_alloc(struct parse *p, struct dive *d, const char *date)
{
size_t i = p->stat->groupsz;
p->stat->groupsz++;
p->stat->groups = xreallocarray
(p, p->stat->groups,
p->stat->groupsz,
sizeof(struct dgroup *));
p->stat->groups[i] = xcalloc
(p, 1, sizeof(struct dgroup));
/* Name our group for lookup, if applicable. */
if (NULL != date)
p->stat->groups[i]->name = xstrdup(p, date);
TAILQ_INIT(&p->stat->groups[i]->dives);
p->stat->groups[i]->id = i;
return group_add(p, i, d);
}
/*
* Parse depth in degrees celsius, "xx C".
* Returns <0 on failure.
*/
static double
parse_temp(struct parse *p, const char *v)
{
size_t sz;
char *cp;
int rc;
double val;
if ((sz = strlen(v)) < 2)
return 0;
if (strcmp(" C", &v[sz - 2]))
return 0;
cp = xstrndup(p, v, sz - 2);
rc = sscanf(cp, "%lg", &val);
free(cp);
return 1 != rc ? -1.0 : val;
}
/*
* Parse pressure in bar, "xx bar".
* Sets the result to be <0 on failure (parse error, negative value).
* Rounds value to zero if <= FLT_EPSILON.
* Returns zero on failure, non-zero on success.
*/
static int
parse_pressure(struct parse *p, const char *v, double *res)
{
size_t sz;
char *cp;
int rc;
double val;
*res = -1.0;
if ((sz = strlen(v)) < 4)
return 0;
if (strcmp(" bar", &v[sz - 4]))
return 0;
cp = xstrndup(p, v, sz - 4);
rc = sscanf(cp, "%lg", &val);
free(cp);
if (1 != rc || val < 0.0)
return 0;
if (val <= FLT_EPSILON)
val = 0.0;
*res = val;
return 1;
}
/*
* Parse volume in litres, "xx.x l".
* Returns <0 on failure.
*/
static double
parse_volume(struct parse *p, const char *v)
{
size_t sz;
char *cp;
int rc;
double val;
if ((sz = strlen(v)) < 2)
return 0;
if (strcmp(" l", &v[sz - 2]))
return 0;
cp = xstrndup(p, v, sz - 2);
rc = sscanf(cp, "%lg", &val);
free(cp);
return 1 != rc ? -1.0 : val;
}
/*
* Parse depth in metres, "xx m".
* Returns <0 on failure.
*/
static double
parse_depth(struct parse *p, const char *v)
{
size_t sz;
char *cp;
int rc;
double val;
if ((sz = strlen(v)) < 2)
return 0;
if (strcmp(" m", &v[sz - 2]))
return 0;
cp = xstrndup(p, v, sz - 2);
rc = sscanf(cp, "%lg", &val);
free(cp);
return 1 != rc ? -1.0 : val;
}
/*
* Parse a time formatted as "mm:ss min", with any number of digits in
* the minute component.
* Returns the time or 0 on failure.
*/
static int
parse_time(struct parse *p, const char *v, size_t *res)
{
size_t sz;
char *cp;
int rc, min, sec;
*res = 0;
if ((sz = strlen(v)) < 4)
return 0;
if (strcmp(" min", &v[sz - 4]))
return 0;
cp = xstrndup(p, v, sz - 4);
rc = sscanf(cp, "%d:%d", &min, &sec);
free(cp);
if (2 != rc)
return 0;
*res = min * 60 + sec;
return 1;
}
/*
* Parse a date-time from the date and time component.
* These must be in yyyy-mm-dd and hh:mm:ss formats.
* Returns the epoch stamp or <0 on failure.
*/
static int
parse_percent(const char *val, double *res)
{
size_t sz;
long long v;
char buf[32];
const char *er;
if ((sz = strlen(val)) < 2 || sz > sizeof(buf) - 1) {
warnx("1: %s", val);
return 0;
}
if ('%' != val[sz - 1]) {
warnx("2: %s", val);
return 0;
}
strlcpy(buf, val, sizeof(buf));
buf[sz - 1] = '\0';
v = strtonum(buf, 0, 100, &er);
if (NULL != er) {
warnx("3: %s, %s", buf, er);
return 0;
}
*res = v / 100.0;
return 1;
}
/*
* Parse a date-time from the date and time component.
* These must be in yyyy-mm-dd and hh:mm:ss formats.
* Returns the epoch stamp or <0 on failure.
*/
static time_t
parse_date(const char *date, const char *time)
{
struct tm tm;
int rc;
memset(&tm, 0, sizeof(struct tm));
rc = sscanf(date, "%d-%d-%d",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday);
if (3 != rc)
return -1;
tm.tm_year -= 1900;
tm.tm_mon -= 1;
rc = sscanf(time, "%d:%d:%d",
&tm.tm_hour, &tm.tm_min, &tm.tm_sec);
if (3 != rc)
return -1;
tm.tm_isdst = -1;
return mktime(&tm);
}
/*
* Subsurface's format, very annoyingly, puts the vendor and product
* into the same string.
* So we need to break it down.
* We don't get the model number, unfortunately.
*/
static void
parse_divecomputerid(struct parse *p, const XML_Char **atts)
{
const char *vendor, *product, *pmodel = NULL;
char *buf;
const XML_Char **ap;
dc_iterator_t *it;
dc_descriptor_t *d;
if (NULL != p->curlog->vendor) {
logerrx(p, "only one <divecomputerid> allowed");
return;
}
for (ap = atts; NULL != ap[0]; ap += 2)
if (0 == strcmp(ap[0], "model")) {
pmodel = ap[1];
break;
}
if (NULL == pmodel) {
lognattr(p, "divecomputerid", "model");
return;
}
if (DC_STATUS_SUCCESS != dc_descriptor_iterator(&it)) {
logerrx(p, "dc_descriptor_iterator");
return;
}
while (DC_STATUS_SUCCESS == dc_iterator_next(it, &d)) {
vendor = dc_descriptor_get_vendor(d);
product = dc_descriptor_get_product(d);
asprintf(&buf, "%s %s", vendor, product);
if (0 == strcmp(buf, pmodel)) {
p->curlog->vendor = xstrdup(p, vendor);
p->curlog->product = xstrdup(p, product);
free(buf);
dc_descriptor_free(d);
break;
}
free(buf);
dc_descriptor_free(d);
}
dc_iterator_free(it);
}
static void
parse_event(struct parse *p, const XML_Char **atts)
{
struct samp *samp = NULL;
int type = 0;
size_t tm;
const XML_Char **ap;
const char *typep = NULL, *tmp = NULL, *flagp = NULL;
/* All events must have a time and type. */
for (ap = atts; NULL != ap[0]; ap += 2)
if (0 == strcmp(*ap, "type"))
typep = ap[1];
else if (0 == strcmp(*ap, "time"))
tmp = ap[1];
else if (0 == strcmp(*ap, "flags"))
flagp = ap[1];
if (NULL == typep) {
lognattr(p, "event", "type");
return;
} else if (NULL == tmp) {
lognattr(p, "event", "time");
return;
}
type = atoi(typep);
if (type < 0 || type > EVENT_gaschange2) {
logerrx(p, "bad <event> type");
return;
} else if ( ! parse_time(p, tmp, &tm)) {
logerrx(p, "bad <event> time");
return;
}
/* Each is going to be a singleton. */
samp = samp_alloc(p, tm);
samp->events = xreallocarray
(p, samp->events, samp->eventsz + 1,
sizeof(struct sampevent));
memset(&samp->events[samp->eventsz],
0, sizeof(struct sampevent));
samp->events[samp->eventsz].type = type;
if (NULL != flagp)
samp->events[samp->eventsz].flags = atoi(flagp);
samp->eventsz++;
}
static void
parse_sample(struct parse *p, const XML_Char **atts)
{
const XML_Char **ap;
struct samp *samp;
struct dive *d = p->curdive;
const char *v;
size_t i, sz, tm, tank;
for (v = NULL, ap = atts; NULL != ap[0]; ap += 2)
if (0 == strcmp(*ap, "time"))
v = ap[1];
if (NULL == v) {
lognattr(p, "sample", "time");
return;
}
if ( ! parse_time(p, v, &tm)) {
logerrx(p, "bad <sample> time");
return;
}
logdbg(p, "new sample at %zu", tm);
samp = samp_alloc(p, tm);
if (samp->time > d->maxtime)
d->maxtime = samp->time;
if (d->datetime &&
d->datetime + (time_t)samp->time >
p->stat->timestamp_max)
p->stat->timestamp_max =
d->datetime + (time_t)samp->time;
for (ap = atts; NULL != ap[0]; ap += 2)
if (0 == strcmp(*ap, "depth")) {
samp->depth = parse_depth(p, ap[1]);
if (samp->depth < 0.0) {
logerrx(p, "bad <sample> depth");
return;
}
samp->flags |= SAMP_DEPTH;
} else if (0 == strcmp(*ap, "rbt")) {
if ( ! parse_time(p, ap[1], &samp->rbt)) {
logerrx(p, "bad <sample> depth");
return;
}
samp->flags |= SAMP_RBT;
} else if (0 == strcmp(*ap, "temp")) {
samp->temp = parse_temp(p, ap[1]);
if (samp->temp < 0.0) {
logerrx(p, "bad <sample> temp");
return;
}
samp->flags |= SAMP_TEMP;
} else if (0 == strncmp(*ap, "pressure", 8)) {
samp->pressure = xreallocarray
(p, samp->pressure,
samp->pressuresz + 1,
sizeof(struct samppres));
samp->pressuresz++;
if ( ! parse_pressure(p, ap[1],
&samp->pressure[samp->pressuresz - 1].pressure)) {
logerrx(p, "bad <sample> pressure");
return;
}
sz = strlen(*ap);
tank = sz > 8 ? atoi(*ap + 8) : 0;
samp->pressure[samp->pressuresz - 1].tank = tank + 1;
for (i = 0; i < d->cylsz; i++)
if (tank + 1 == d->cyls[i].num)
break;
if (i == d->cylsz) {
logerrx(p, "<sample> tank not found");
return;
}
} else if (0 == strcmp(*ap, "cns")) {
if ( ! parse_percent(ap[1], &samp->cns)) {
logerrx(p, "bad <sample> cns");
return;
}
samp->flags |= SAMP_CNS;
} else if (0 == strcmp(*ap, "ndl")) {
memset(&samp->deco, 0, sizeof(struct sampdeco));
if ( ! parse_time(p, ap[1], &samp->deco.duration)) {
logerrx(p, "bad <sample> ndl");
return;
}
samp->deco.type = DECO_ndl;
samp->flags |= SAMP_DECO;
} else if (0 == strcmp(*ap, "stopdepth")) {
/*
* We'll only use this if in_deco is set, later.
*/
samp->deco.depth = parse_depth(p, ap[1]);
if (samp->deco.depth < 0.0) {
logerrx(p, "bad <sample> stopdepth");
return;
}
} else if (0 == strcmp(*ap, "stoptime")) {
/*
* We'll only use this if in_deco is set, later.
*/
if ( ! parse_time(p, ap[1], &samp->deco.duration)) {
logerrx(p, "bad <sample> stoptime");
return;
}
} else if (0 == strcmp(*ap, "in_deco")) {
if (0 == strcmp(ap[1], "1")) {
if (p->in_deco) {
logerrx(p, "<sample> starting deco "
"when already in deco");
return;
}
p->in_deco = 1;
} else if (0 == strcmp(ap[1], "0")) {
if ( ! p->in_deco) {
logerrx(p, "<sample> ending deco "
"when not in deco");
return;
}
p->in_deco = 0;
} else {
logerrx(p, "bad <sample> in_deco");
return;
}
} else if (strcmp(*ap, "time"))
logwarnx(p, "unknown <sample> attribute: %s", ap[0]);
if (SAMP_DEPTH & samp->flags)
if (samp->depth > p->curdive->maxdepth)
p->curdive->maxdepth = samp->depth;
if (SAMP_TEMP & samp->flags) {
if (0 == p->curdive->hastemp) {
p->curdive->maxtemp = samp->temp;
p->curdive->mintemp = samp->temp;
p->curdive->hastemp = 1;
} else {
if (samp->temp > p->curdive->maxtemp)
p->curdive->maxtemp = samp->temp;
if (samp->temp < p->curdive->mintemp)
p->curdive->mintemp = samp->temp;
}
}
if (p->in_deco) {
if (SAMP_DECO & samp->flags) {
logerrx(p, "<sample> with overlapping ndl "
"and deco");
return;
}
/*
* ssrf doesn't differentiate between a deco and a deep
* stop: we assume a regular DECO_decostop.
*/
samp->deco.type = DECO_decostop;
samp->flags |= SAMP_DECO;
} else if ( ! (SAMP_DECO & samp->flags)) {
if (samp->deco.depth > FLT_EPSILON)
logwarnx(p, "<sample> has stopdepth "
"when not in deco mode");
if (samp->deco.duration > 0)
logwarnx(p, "<sample> has stoptime "
"when not in deco mode");
}
}
static void
parse_cylinder(struct parse *p, const XML_Char **atts)
{
const XML_Char **ap;
struct dive *d = p->curdive;
const char *size = NULL, *wp = NULL;
char *ep, *mixes[3];
mixes[0] = mixes[1] = mixes[2] = NULL;
for (ap = atts; NULL != ap[0]; ap += 2)
if (0 == strcmp(*ap, "o2")) {
free(mixes[0]);
mixes[0] = xstrdup(p, ap[1]);
} else if (0 == strcmp(*ap, "n2")) {
free(mixes[1]);
mixes[1] = xstrdup(p, ap[1]);
} else if (0 == strcmp(*ap, "he")) {
free(mixes[2]);
mixes[2] = xstrdup(p, ap[1]);
} else if (0 == strcmp(*ap, "description")) {
/* Do nothing. */ ;
} else if (0 == strcmp(*ap, "size")) {
size = ap[1];
} else if (0 == strcmp(*ap, "workpressure")) {
wp = ap[1];
} else
logattr(p, "cylinder", *ap);
d->gas = xreallocarray(p, d->gas,
d->gassz + 1, sizeof(struct divegas));
memset(&d->gas[d->gassz], 0, sizeof(struct divegas));
d->gas[d->gassz].num = d->gassz + 1;
if (NULL != mixes[0]) {
if ('\0' != mixes[0][0] &&
'%' == mixes[0][strlen(mixes[0]) - 1])
mixes[0][strlen(mixes[0]) - 1] = '\0';
d->gas[d->gassz].o2 = strtod(mixes[0], &ep);
if (ep == mixes[0] || ERANGE == errno) {
logerrx(p, "bad \"o2\" attribute");
return;
}
}
if (NULL != mixes[1]) {
if ('\0' != mixes[1][0] &&
'%' == mixes[1][strlen(mixes[1]) - 1])
mixes[1][strlen(mixes[1]) - 1] = '\0';
d->gas[d->gassz].n2 = strtod(mixes[1], &ep);
if (ep == mixes[1] || ERANGE == errno) {
logerrx(p, "bad \"n2\" attribute");
return;
}
}
if (NULL != mixes[2]) {
if ('\0' != mixes[2][0] &&
'%' == mixes[2][strlen(mixes[2]) - 1])
mixes[2][strlen(mixes[2]) - 1] = '\0';
d->gas[d->gassz].he = strtod(mixes[2], &ep);
if (ep == mixes[2] || ERANGE == errno) {
logerrx(p, "bad \"he\" attribute");
return;
}
}
free(mixes[0]);
free(mixes[1]);
free(mixes[2]);
d->cyls = xreallocarray(p, d->cyls,
d->cylsz + 1, sizeof(struct cylinder));
memset(&d->cyls[d->cylsz], 0, sizeof(struct cylinder));
d->cyls[d->cylsz].mix = d->gas[d->gassz].num;
d->cyls[d->cylsz].num = d->cylsz + 1;
if (NULL != size) {
d->cyls[d->cylsz].size = parse_volume(p, size);
if (d->cyls[d->cylsz].size < 0.0) {
logerrx(p, "bad \"size\" attribute");
return;
}
}
if (NULL != wp &&
! parse_pressure(p, wp, &d->cyls[d->cylsz].workpressure)) {
logerrx(p, "bad \"workpressure\" value");
return;
}
d->cylsz++;
d->gassz++;
}
static void
parse_dive(struct parse *p, const XML_Char **atts)
{
const XML_Char **ap;
struct dive *d, *dp;
const char *num = NULL, *dur = NULL, *date = NULL,
*time = NULL, *mode = NULL, *id = NULL, *er;
struct dgroup *grp;
p->curdive = d = xcalloc(p, 1, sizeof(struct dive));
TAILQ_INIT(&d->samps);
d->pid = ++p->pid;
d->line = XML_GetCurrentLineNumber(p->p);
d->log = p->curlog;
d->mode = MODE_OC;
for (ap = atts; NULL != ap[0]; ap += 2)
if (0 == strcmp(*ap, "number"))
num = ap[1];
else if (0 == strcmp(*ap, "diveid"))
id = ap[1];
else if (0 == strcmp(*ap, "duration"))
dur = ap[1];
else if (0 == strcmp(*ap, "date"))
date = ap[1];
else if (0 == strcmp(*ap, "time"))
time = ap[1];
else
logattr(p, "dive", *ap);
if (NULL != id && '\0' != *id)
d->fprint = xstrdup(p, id);
if (NULL != mode) {
if (0 == strcmp(mode, "freedive"))
d->mode = MODE_FREEDIVE;
else if (0 == strcmp(mode, "opencircuit"))
d->mode = MODE_OC;
else if (0 == strcmp(mode, "closedcircuit"))
d->mode = MODE_CC;
else if (0 == strcmp(mode, "gauge"))
d->mode = MODE_GAUGE;
else
logwarnx(p, "bad <dive> mode");
}
if (NULL != num) {
d->num = strtonum(num, 0, LONG_MAX, &er);
if (NULL != er) {
logerrx(p, "bad <dive> number");
return;
} else
logdbg(p, "new dive: %zu", d->num);
}
if (NULL != dur &&
! parse_time(p, dur, &d->duration)) {
logerrx(p, "bad <dive> duration");
return;
}
if (NULL != date && NULL != time) {
d->datetime = parse_date(date, time);
if (d->datetime < 0) {
logerrx(p, "bad <dive> date/time");
return;
}
/* Check against our global extrema. */
if (0 == p->stat->timestamp_min ||
d->datetime < p->stat->timestamp_min)
p->stat->timestamp_min = d->datetime;
if (0 == p->stat->timestamp_max ||
d->datetime > p->stat->timestamp_max)
p->stat->timestamp_max = d->datetime;
}
/*
* Now assign to our group.
* Our group assignment might require data we don't
* have, so we might not be grouping the dive.
*/
if (0 == p->stat->groupsz && verbose)
logdbg(p, "new default group");
grp = (0 == p->stat->groupsz) ?
group_alloc(p, d, NULL) :
group_add(p, 0, d);
if (NULL != date &&
(0 == grp->mintime || d->datetime < grp->mintime))
grp->mintime = d->datetime;
/*
* Now register the dive with the dive queue.
* If the dive has a date and time, we order everything
* by time; and furthermore, we order it by offsetting
* from our group's start time.
* If there's no date/time, we just append to the queue.
*/
/* Insert is in reverse order. */
TAILQ_FOREACH(dp, p->dives, entries)
if (dp->datetime - dp->group->mintime &&
dp->datetime - dp->group->mintime >
d->datetime - d->group->mintime)
break;
if (NULL == dp)
TAILQ_INSERT_TAIL(p->dives, d, entries);
else
TAILQ_INSERT_BEFORE(dp, d, entries);
}
static void
parse_divecomputer(struct parse *p, const XML_Char **atts)
{
const XML_Char **ap;
const char *diveid = NULL, *dctype = NULL;
p->curdive->mode = MODE_OC;
for (ap = atts; NULL != ap[0]; ap += 2)
if (0 == strcmp(*ap, "diveid"))
diveid = ap[1];
else if (0 == strcmp(*ap, "dctype"))
dctype = ap[1];
if (NULL != diveid)
p->curdive->fprint = xstrdup(p, diveid);
/* These are the only ones we care about. */
if (NULL != dctype) {
if (0 == strcasecmp(dctype, "freedive"))
p->curdive->mode = MODE_FREEDIVE;
else if (0 == strcasecmp(dctype, "ccr"))
p->curdive->mode = MODE_CC;
}