]> git.tdb.fi Git - ext/subsurface.git/blob - parse.c
5c991fa8af8c9403c50a7725cbe7990a04b75c4b
[ext/subsurface.git] / parse.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
9
10 static int verbose;
11
12 /*
13  * Some silly typedefs to make our units very explicit.
14  *
15  * Also, the units are chosen so that values can be expressible as
16  * integers, so that we never have FP rounding issues. And they
17  * are small enough that converting to/from imperial units doesn't
18  * really matter.
19  *
20  * We also strive to make '0' a meaningless number saying "not
21  * initialized", since many values are things that may not have
22  * been reported (eg tank pressure or temperature from dive
23  * computers that don't support them). But sometimes -1 is an even
24  * more explicit way of saying "not there".
25  *
26  * Thus "millibar" for pressure, for example, or "millikelvin" for
27  * temperatures. Doing temperatures in celsius or fahrenheit would
28  * make for loss of precision when converting from one to the other,
29  * and using millikelvin is SI-like but also means that a temperature
30  * of '0' is clearly just a missing temperature or tank pressure.
31  *
32  * Also strive to use units that can not possibly be mistaken for a
33  * valid value in a "normal" system without conversion. If the max
34  * depth of a dive is '20000', you probably didn't convert from mm on
35  * output, or if the max depth gets reported as "0.2ft" it was either
36  * a really boring dive, or there was some missing input conversion,
37  * and a 60-ft dive got recorded as 60mm.
38  *
39  * Doing these as "structs containing value" means that we always
40  * have to explicitly write out those units in order to get at the
41  * actual value. So there is hopefully little fear of using a value
42  * in millikelvin as Fahrenheit by mistake.
43  *
44  * We don't actually use these all yet, so maybe they'll change, but
45  * I made a number of types as guidelines.
46  */
47 typedef struct {
48         int seconds;
49 } duration_t;
50
51 typedef struct {
52         int mm;
53 } depth_t;
54
55 typedef struct {
56         int mbar;
57 } pressure_t;
58
59 typedef struct {
60         int mkelvin;
61 } temperature_t;
62
63 typedef struct {
64         int mliter;
65 } volume_t;
66
67 typedef struct {
68         int permille;
69 } fraction_t;
70
71 typedef struct {
72         int grams;
73 } weight_t;
74
75 typedef struct {
76         fraction_t o2;
77         fraction_t n2;
78         fraction_t he2;
79 } gasmix_t;
80
81 typedef struct {
82         volume_t size;
83         pressure_t pressure;
84 } tank_type_t;
85
86 static int to_feet(depth_t depth)
87 {
88         return depth.mm * 0.00328084 + 0.5;
89 }
90
91 static int to_C(temperature_t temp)
92 {
93         if (!temp.mkelvin)
94                 return 0;
95         return (temp.mkelvin - 273150) / 1000;
96 }
97
98 static int to_PSI(pressure_t pressure)
99 {
100         return pressure.mbar * 0.0145037738 + 0.5;
101 }
102
103 struct sample {
104         duration_t time;
105         depth_t depth;
106         temperature_t temperature;
107         pressure_t tankpressure;
108         int tankindex;
109 };
110
111 struct dive {
112         time_t when;
113         depth_t maxdepth, meandepth;
114         duration_t duration, surfacetime;
115         depth_t visibility;
116         temperature_t airtemp, watertemp;
117         pressure_t beginning_pressure, end_pressure;
118         int samples;
119         struct sample sample[];
120 };
121
122 static struct dive **dive_table;
123 static int nr_dives, nr_allocated;
124
125 static void record_dive(struct dive *dive)
126 {
127         if (nr_dives >= nr_allocated) {
128                 nr_allocated = (nr_dives + 32) * 3 / 2;
129                 dive_table = realloc(dive_table, nr_allocated * sizeof(struct dive *));
130                 if (!dive_table)
131                         exit(1);
132         }
133         dive_table[nr_dives++] = dive;
134 }
135
136 static void show_dive(int nr, struct dive *dive)
137 {
138         int i;
139         struct tm *tm;
140
141         tm = gmtime(&dive->when);
142
143         printf("Dive %d with %d samples at %02d:%02d:%02d %04d-%02d-%02d\n",
144                 nr, dive->samples,
145                 tm->tm_hour, tm->tm_min, tm->tm_sec,
146                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
147
148         if (!verbose)
149                 return;
150
151         for (i = 0; i < dive->samples; i++) {
152                 struct sample *s = dive->sample + i;
153
154                 printf("%4d:%02d: %3d ft, %2d C, %4d PSI\n",
155                         s->time.seconds / 60,
156                         s->time.seconds % 60,
157                         to_feet(s->depth),
158                         to_C(s->temperature),
159                         to_PSI(s->tankpressure));
160         }
161 }
162
163 static int sortfn(const void *_a, const void *_b)
164 {
165         const struct dive *a = *(void **)_a;
166         const struct dive *b = *(void **)_b;
167
168         if (a->when < b->when)
169                 return -1;
170         if (a->when > b->when)
171                 return 1;
172         return 0;
173 }
174
175 static void report_dives(void)
176 {
177         int i;
178         qsort(dive_table, nr_dives, sizeof(struct dive *), sortfn);
179
180         for (i = 0; i < nr_dives; i++)
181                 show_dive(i+1, dive_table[i]);
182 }
183
184 static void nonmatch(const char *type, const char *fullname, const char *name, char *buffer)
185 {
186         if (verbose > 1)
187                 printf("Unable to match %s '(%.*s)%s' (%s)\n", type,
188                         (int) (name - fullname), fullname, name,
189                         buffer);
190         free(buffer);
191 }
192
193 static const char *last_part(const char *name)
194 {
195         const char *p = strrchr(name, '.');
196         return p ? p+1 : name;
197 }
198
199 typedef void (*matchfn_t)(char *buffer, void *);
200
201 static int match(const char *pattern, const char *name, matchfn_t fn, char *buf, void *data)
202 {
203         if (strcasecmp(pattern, name))
204                 return 0;
205         fn(buf, data);
206         return 1;
207 }
208
209 /*
210  * Dive info as it is being built up..
211  */
212 static int alloc_samples;
213 static struct dive *dive;
214 static struct sample *sample;
215 static struct tm tm;
216
217 static time_t utc_mktime(struct tm *tm)
218 {
219         static const int mdays[] = {
220             0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
221         };
222         int year = tm->tm_year;
223         int month = tm->tm_mon;
224         int day = tm->tm_mday;
225
226         /* First normalize relative to 1900 */
227         if (year < 70)
228                 year += 100;
229         else if (year > 1900)
230                 year -= 1900;
231
232         /* Normalized to Jan 1, 1970: unix time */
233         year -= 70;
234
235         if (year < 0 || year > 129) /* algo only works for 1970-2099 */
236                 return -1;
237         if (month < 0 || month > 11) /* array bounds */
238                 return -1;
239         if (month < 2 || (year + 2) % 4)
240                 day--;
241         if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
242                 return -1;
243         return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
244                 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
245 }
246
247 static void divedate(char *buffer, void *_when)
248 {
249         int d,m,y;
250         time_t *when = _when;
251
252         if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
253                 tm.tm_year = y;
254                 tm.tm_mon = m-1;
255                 tm.tm_mday = d;
256                 if (tm.tm_sec | tm.tm_min | tm.tm_hour)
257                         *when = utc_mktime(&tm);
258         }
259         free(buffer);
260 }
261
262 static void divetime(char *buffer, void *_when)
263 {
264         int h,m,s = 0;
265         time_t *when = _when;
266
267         if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
268                 tm.tm_hour = h;
269                 tm.tm_min = m;
270                 tm.tm_sec = s;
271                 if (tm.tm_year)
272                         *when = utc_mktime(&tm);
273         }
274         free(buffer);
275 }
276
277 /* Libdivecomputer: "2011-03-20 10:22:38" */
278 static void divedatetime(char *buffer, void *_when)
279 {
280         int y,m,d;
281         int hr,min,sec;
282         time_t *when = _when;
283
284         if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
285                 &y, &m, &d, &hr, &min, &sec) == 6) {
286                 tm.tm_year = y;
287                 tm.tm_mon = m-1;
288                 tm.tm_mday = d;
289                 tm.tm_hour = hr;
290                 tm.tm_min = min;
291                 tm.tm_sec = sec;
292                 *when = utc_mktime(&tm);
293         }
294         free(buffer);
295 }
296
297 union int_or_float {
298         long i;
299         double fp;
300 };
301
302 enum number_type {
303         NEITHER,
304         INTEGER,
305         FLOAT
306 };
307
308 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
309 {
310         char *end;
311         long val;
312         double fp;
313
314         /* Integer or floating point? */
315         val = strtol(buffer, &end, 10);
316         if (val < 0 || end == buffer)
317                 return NEITHER;
318
319         /* Looks like it might be floating point? */
320         if (*end == '.') {
321                 errno = 0;
322                 fp = strtod(buffer, &end);
323                 if (!errno) {
324                         res->fp = fp;
325                         return FLOAT;
326                 }
327         }
328
329         res->i = val;
330         return INTEGER;
331 }
332
333 static void pressure(char *buffer, void *_press)
334 {
335         pressure_t *pressure = _press;
336         union int_or_float val;
337
338         switch (integer_or_float(buffer, &val)) {
339         case FLOAT:
340                 /* Maybe it's in Bar? */
341                 if (val.fp < 500.0) {
342                         pressure->mbar = val.fp * 1000;
343                         break;
344                 }
345                 printf("Unknown fractional pressure reading %s\n", buffer);
346                 break;
347
348         case INTEGER:
349                 /*
350                  * Random integer? Maybe in PSI? Or millibar already?
351                  *
352                  * We assume that 5 bar is a ridiculous tank pressure,
353                  * so if it's smaller than 5000, it's in PSI..
354                  */
355                 if (val.i < 5000) {
356                         pressure->mbar = val.i * 68.95;
357                         break;
358                 }
359                 pressure->mbar = val.i;
360                 break;
361         default:
362                 printf("Strange pressure reading %s\n", buffer);
363         }
364         free(buffer);
365 }
366
367 static void depth(char *buffer, void *_depth)
368 {
369         depth_t *depth = _depth;
370         union int_or_float val;
371
372         switch (integer_or_float(buffer, &val)) {
373         /* Integer values are probably in feet */
374         case INTEGER:
375                 depth->mm = 304.8 * val.i;
376                 break;
377         /* Float? Probably meters.. */
378         case FLOAT:
379                 depth->mm = val.fp * 1000;
380                 break;
381         default:
382                 printf("Strange depth reading %s\n", buffer);
383         }
384         free(buffer);
385 }
386
387 static void temperature(char *buffer, void *_temperature)
388 {
389         temperature_t *temperature = _temperature;
390         union int_or_float val;
391
392         switch (integer_or_float(buffer, &val)) {
393         /* C or F? Who knows? Let's default to Celsius */
394         case INTEGER:
395                 val.fp = val.i;
396                 /* Fallthrough */
397         case FLOAT:
398                 /* Ignore zero. It means "none" */
399                 if (!val.fp)
400                         break;
401                 /* Celsius */
402                 if (val.fp < 50.0) {
403                         temperature->mkelvin = (val.fp + 273.16) * 1000;
404                         break;
405                 }
406                 /* Fahrenheit */
407                 if (val.fp < 212.0) {
408                         temperature->mkelvin = (val.fp + 459.67) * 5000/9;
409                         break;
410                 }
411                 /* Kelvin or already millikelvin */
412                 if (val.fp < 1000.0)
413                         val.fp *= 1000;
414                 temperature->mkelvin = val.fp;
415                 break;
416         default:
417                 printf("Strange temperature reading %s\n", buffer);
418         }
419         free(buffer);
420 }
421
422 static void sampletime(char *buffer, void *_time)
423 {
424         int i;
425         int min, sec;
426         duration_t *time = _time;
427
428         i = sscanf(buffer, "%d:%d", &min, &sec);
429         switch (i) {
430         case 1:
431                 sec = min;
432                 min = 0;
433         /* fallthrough */
434         case 2:
435                 time->seconds = sec + min*60;
436                 break;
437         default:
438                 printf("Strange sample time reading %s\n", buffer);
439         }
440         free(buffer);
441 }
442
443 static void duration(char *buffer, void *_time)
444 {
445         sampletime(buffer, _time);
446 }
447
448 static void ignore(char *buffer, void *_time)
449 {
450 }
451
452 /* We're in samples - try to convert the random xml value to something useful */
453 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
454 {
455         const char *last = last_part(name);
456
457         if (match("pressure", last, pressure, buf, &sample->tankpressure))
458                 return;
459         if (match("cylpress", last, pressure, buf, &sample->tankpressure))
460                 return;
461         if (match("depth", last, depth, buf, &sample->depth))
462                 return;
463         if (match("temperature", last, temperature, buf, &sample->temperature))
464                 return;
465         if (match("sampletime", last, sampletime, buf, &sample->time))
466                 return;
467         if (match("time", last, sampletime, buf, &sample->time))
468                 return;
469
470         nonmatch("sample", name, last, buf);
471 }
472
473 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
474 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
475 {
476         const char *last = last_part(name);
477
478         if (match("date", last, divedate, buf, &dive->when))
479                 return;
480         if (match("time", last, divetime, buf, &dive->when))
481                 return;
482         if (match("datetime", last, divedatetime, buf, &dive->when))
483                 return;
484         if (match("maxdepth", last, depth, buf, &dive->maxdepth))
485                 return;
486         if (match("meandepth", last, depth, buf, &dive->meandepth))
487                 return;
488         if (match("divetime", last, duration, buf, &dive->duration))
489                 return;
490         if (match("divetimesec", last, duration, buf, &dive->duration))
491                 return;
492         if (match("surfacetime", last, duration, buf, &dive->surfacetime))
493                 return;
494         if (match("airtemp", last, temperature, buf, &dive->airtemp))
495                 return;
496         if (match("watertemp", last, temperature, buf, &dive->watertemp))
497                 return;
498         if (match("cylinderstartpressure", last, pressure, buf, &dive->beginning_pressure))
499                 return;
500         if (match("cylinderendpressure", last, pressure, buf, &dive->end_pressure))
501                 return;
502         if (match("divenumber", last, ignore, buf, NULL))
503                 return;
504         if (match("diveseries", last, ignore, buf, NULL))
505                 return;
506         if (match("number", last, ignore, buf, NULL))
507                 return;
508         if (match("size", last, ignore, buf, NULL))
509                 return;
510         if (match("fingerprint", last, ignore, buf, NULL))
511                 return;
512         nonmatch("dive", name, last, buf);
513 }
514
515 static unsigned int dive_size(int samples)
516 {
517         return sizeof(struct dive) + samples*sizeof(struct sample);
518 }
519
520 /*
521  * File boundaries are dive boundaries. But sometimes there are
522  * multiple dives per file, so there can be other events too that
523  * trigger a "new dive" marker and you may get some nesting due
524  * to that. Just ignore nesting levels.
525  */
526 static void dive_start(void)
527 {
528         unsigned int size;
529
530         if (dive)
531                 return;
532
533         alloc_samples = 5;
534         size = dive_size(alloc_samples);
535         dive = malloc(size);
536         if (!dive)
537                 exit(1);
538         memset(dive, 0, size);
539         memset(&tm, 0, sizeof(tm));
540 }
541
542 static void dive_end(void)
543 {
544         if (!dive)
545                 return;
546         record_dive(dive);
547         dive = NULL;
548 }
549
550 static void sample_start(void)
551 {
552         int nr;
553
554         if (!dive)
555                 return;
556         nr = dive->samples;
557         if (nr >= alloc_samples) {
558                 unsigned int size;
559
560                 alloc_samples = (alloc_samples * 3)/2 + 10;
561                 size = dive_size(alloc_samples);
562                 dive = realloc(dive, size);
563                 if (!dive)
564                         return;
565         }
566         sample = dive->sample + nr;
567         memset(sample, 0, sizeof(*sample));
568 }
569
570 static void sample_end(void)
571 {
572         sample = NULL;
573         if (!dive)
574                 return;
575         dive->samples++;
576 }
577
578 static void entry(const char *name, int size, const char *raw)
579 {
580         char *buf = malloc(size+1);
581
582         if (!buf)
583                 return;
584         memcpy(buf, raw, size);
585         buf[size] = 0;
586         if (sample) {
587                 try_to_fill_sample(sample, name, buf);
588                 return;
589         }
590         if (dive) {
591                 try_to_fill_dive(dive, name, buf);
592                 return;
593         }
594 }
595
596 static const char *nodename(xmlNode *node, char *buf, int len)
597 {
598
599         if (!node || !node->name)
600                 return "root";
601
602         buf += len;
603         *--buf = 0;
604         len--;
605
606         for(;;) {
607                 const char *name = node->name;
608                 int i = strlen(name);
609                 while (--i >= 0) {
610                         unsigned char c = name[i];
611                         *--buf = tolower(c);
612                         if (!--len)
613                                 return buf;
614                 }
615                 node = node->parent;
616                 if (!node || !node->name)
617                         return buf;
618                 *--buf = '.';
619                 if (!--len)
620                         return buf;
621         }
622 }
623
624 #define MAXNAME 64
625
626 static void visit_one_node(xmlNode *node)
627 {
628         int len;
629         const unsigned char *content;
630         char buffer[MAXNAME];
631         const char *name;
632
633         content = node->content;
634         if (!content)
635                 return;
636
637         /* Trim whitespace at beginning */
638         while (isspace(*content))
639                 content++;
640
641         /* Trim whitespace at end */
642         len = strlen(content);
643         while (len && isspace(content[len-1]))
644                 len--;
645
646         if (!len)
647                 return;
648
649         /* Don't print out the node name if it is "text" */
650         if (!strcmp(node->name, "text"))
651                 node = node->parent;
652
653         name = nodename(node, buffer, sizeof(buffer));
654
655         entry(name, len, content);
656 }
657
658 static void traverse(xmlNode *node)
659 {
660         xmlNode *n;
661
662         for (n = node; n; n = n->next) {
663                 /* XML from libdivecomputer: 'dive' per new dive */
664                 if (!strcmp(n->name, "dive")) {
665                         dive_start();
666                         traverse(n->children);
667                         dive_end();
668                         continue;
669                 }
670
671                 /*
672                  * At least both libdivecomputer and Suunto
673                  * agree on "sample".
674                  *
675                  * Well - almost. Ignore case.
676                  */
677                 if (!strcasecmp(n->name, "sample")) {
678                         sample_start();
679                         traverse(n->children);
680                         sample_end();
681                         continue;
682                 }
683
684                 /* Anything else - just visit it and recurse */
685                 visit_one_node(n);
686                 traverse(n->children);
687         }
688 }
689
690 static void parse(const char *filename)
691 {
692         xmlDoc *doc;
693
694         doc = xmlReadFile(filename, NULL, 0);
695         if (!doc) {
696                 fprintf(stderr, "Failed to parse '%s'.\n", filename);
697                 return;
698         }
699
700         dive_start();
701         traverse(xmlDocGetRootElement(doc));
702         dive_end();
703         xmlFreeDoc(doc);
704         xmlCleanupParser();
705 }
706
707 static void parse_argument(const char *arg)
708 {
709         const char *p = arg+1;
710
711         do {
712                 switch (*p) {
713                 case 'v':
714                         verbose++;
715                         continue;
716                 default:
717                         fprintf(stderr, "Bad argument '%s'\n", arg);
718                         exit(1);
719                 }
720         } while (*++p);
721 }
722
723 int main(int argc, char **argv)
724 {
725         int i;
726
727         LIBXML_TEST_VERSION
728
729         for (i = 1; i < argc; i++) {
730                 const char *a = argv[i];
731
732                 if (a[0] == '-') {
733                         parse_argument(a);
734                         continue;
735                 }
736                 parse(a);
737         }
738         report_dives();
739         return 0;
740 }