]> git.tdb.fi Git - ext/subsurface.git/blob - parse.c
Start actually reporting the numbers we parsed
[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         return (temp.mkelvin + 272150) / 1000;
94 }
95
96 static int to_PSI(pressure_t pressure)
97 {
98         return pressure.mbar * 0.0145037738 + 0.5;
99 }
100
101 struct sample {
102         duration_t time;
103         depth_t depth;
104         temperature_t temperature;
105         pressure_t tankpressure;
106         int tankindex;
107 };
108
109 struct dive {
110         time_t when;
111         depth_t maxdepth, meandepth;
112         duration_t duration, surfacetime;
113         depth_t visibility;
114         temperature_t airtemp, watertemp;
115         pressure_t beginning_pressure, end_pressure;
116         int samples;
117         struct sample sample[];
118 };
119
120 static void record_dive(struct dive *dive)
121 {
122         int i;
123         static int nr;
124         struct tm *tm;
125
126         tm = gmtime(&dive->when);
127
128         printf("Dive %d with %d samples at %02d:%02d:%02d %04d-%02d-%02d\n",
129                 ++nr, dive->samples,
130                 tm->tm_hour, tm->tm_min, tm->tm_sec,
131                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
132         for (i = 0; i < dive->samples; i++) {
133                 struct sample *s = dive->sample + i;
134
135                 printf("%4d:%02d: %3d ft, %2d C, %4d PSI\n",
136                         s->time.seconds / 60,
137                         s->time.seconds % 60,
138                         to_feet(s->depth),
139                         to_C(s->temperature),
140                         to_PSI(s->tankpressure));
141         }
142 }
143
144 static void nonmatch(const char *type, const char *fullname, const char *name, char *buffer)
145 {
146         if (verbose)
147                 printf("Unable to match %s '(%.*s)%s' (%s)\n", type,
148                         (int) (name - fullname), fullname, name,
149                         buffer);
150         free(buffer);
151 }
152
153 static const char *last_part(const char *name)
154 {
155         const char *p = strrchr(name, '.');
156         return p ? p+1 : name;
157 }
158
159 typedef void (*matchfn_t)(char *buffer, void *);
160
161 static int match(const char *pattern, const char *name, matchfn_t fn, char *buf, void *data)
162 {
163         if (strcasecmp(pattern, name))
164                 return 0;
165         fn(buf, data);
166         return 1;
167 }
168
169 /*
170  * Dive info as it is being built up..
171  */
172 static int alloc_samples;
173 static struct dive *dive;
174 static struct sample *sample;
175 static struct tm tm;
176
177 static time_t utc_mktime(struct tm *tm)
178 {
179         static const int mdays[] = {
180             0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
181         };
182         int year = tm->tm_year;
183         int month = tm->tm_mon;
184         int day = tm->tm_mday;
185
186         if (year < 70)
187                 year += 100;
188         else if (year > 1900)
189                 year -= 1900;
190
191         /* Normalized to Jan 1, 1970: unix time */
192         year -= 70;
193
194         if (year < 0 || year > 129) /* algo only works for 1970-2099 */
195                 return -1;
196         if (month < 0 || month > 11) /* array bounds */
197                 return -1;
198         if (month < 2 || (year + 2) % 4)
199                 day--;
200         if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
201                 return -1;
202         return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
203                 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
204 }
205
206 static void divedate(char *buffer, void *_when)
207 {
208         int d,m,y;
209         time_t *when = _when;
210
211         if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
212                 tm.tm_year = y;
213                 tm.tm_mon = m-1;
214                 tm.tm_mday = d;
215                 if (tm.tm_sec | tm.tm_min | tm.tm_hour)
216                         *when = utc_mktime(&tm);
217         }
218         free(buffer);
219 }
220
221 static void divetime(char *buffer, void *_when)
222 {
223         int h,m,s = 0;
224         time_t *when = _when;
225
226         if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
227                 tm.tm_hour = h;
228                 tm.tm_min = m;
229                 tm.tm_sec = s;
230                 if (tm.tm_year)
231                         *when = utc_mktime(&tm);
232         }
233         free(buffer);
234 }
235
236 union int_or_float {
237         long i;
238         double fp;
239 };
240
241 enum number_type {
242         NEITHER,
243         INTEGER,
244         FLOAT
245 };
246
247 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
248 {
249         char *end;
250         long val;
251         double fp;
252
253         /* Integer or floating point? */
254         val = strtol(buffer, &end, 10);
255         if (val < 0 || end == buffer)
256                 return NEITHER;
257
258         /* Looks like it might be floating point? */
259         if (*end == '.') {
260                 errno = 0;
261                 fp = strtod(buffer, &end);
262                 if (!errno) {
263                         res->fp = fp;
264                         return FLOAT;
265                 }
266         }
267
268         res->i = val;
269         return INTEGER;
270 }
271
272 static void pressure(char *buffer, void *_press)
273 {
274         pressure_t *pressure = _press;
275         union int_or_float val;
276
277         switch (integer_or_float(buffer, &val)) {
278         case FLOAT:
279                 /* Maybe it's in Bar? */
280                 if (val.fp < 500.0) {
281                         pressure->mbar = val.fp * 1000;
282                         break;
283                 }
284                 printf("Unknown fractional pressure reading %s\n", buffer);
285                 break;
286
287         case INTEGER:
288                 /*
289                  * Random integer? Maybe in PSI? Or millibar already?
290                  *
291                  * We assume that 5 bar is a ridiculous tank pressure,
292                  * so if it's smaller than 5000, it's in PSI..
293                  */
294                 if (val.i < 5000) {
295                         pressure->mbar = val.i * 68.95;
296                         break;
297                 }
298                 pressure->mbar = val.i;
299                 break;
300         default:
301                 printf("Strange pressure reading %s\n", buffer);
302         }
303         free(buffer);
304 }
305
306 static void depth(char *buffer, void *_depth)
307 {
308         depth_t *depth = _depth;
309         union int_or_float val;
310
311         switch (integer_or_float(buffer, &val)) {
312         /* Integer values are probably in feet */
313         case INTEGER:
314                 depth->mm = 304.8 * val.i;
315                 break;
316         /* Float? Probably meters.. */
317         case FLOAT:
318                 depth->mm = val.fp * 1000;
319                 break;
320         default:
321                 printf("Strange depth reading %s\n", buffer);
322         }
323         free(buffer);
324 }
325
326 static void temperature(char *buffer, void *_temperature)
327 {
328         temperature_t *temperature = _temperature;
329         union int_or_float val;
330
331         switch (integer_or_float(buffer, &val)) {
332         /* C or F? Who knows? Let's default to Celsius */
333         case INTEGER:
334                 val.fp = val.i;
335                 /* Fallthrough */
336         case FLOAT:
337                 /* Ignore zero. It means "none" */
338                 if (!val.fp)
339                         break;
340                 /* Celsius */
341                 if (val.fp < 50.0) {
342                         temperature->mkelvin = (val.fp + 273.16) * 1000;
343                         break;
344                 }
345                 /* Fahrenheit */
346                 if (val.fp < 212.0) {
347                         temperature->mkelvin = (val.fp + 459.67) * 5000/9;
348                         break;
349                 }
350                 /* Kelvin or already millikelvin */
351                 if (val.fp < 1000.0)
352                         val.fp *= 1000;
353                 temperature->mkelvin = val.fp;
354                 break;
355         default:
356                 printf("Strange temperature reading %s\n", buffer);
357         }
358         free(buffer);
359 }
360
361 static void sampletime(char *buffer, void *_time)
362 {
363         duration_t *time = _time;
364         union int_or_float val;
365
366         switch (integer_or_float(buffer, &val)) {
367         case INTEGER:
368                 time->seconds = val.i;
369                 break;
370         default:
371                 printf("Strange sample time reading %s\n", buffer);
372         }
373         free(buffer);
374 }
375
376 /* We're in samples - try to convert the random xml value to something useful */
377 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
378 {
379         const char *last = last_part(name);
380
381         if (match("pressure", last, pressure, buf, &sample->tankpressure))
382                 return;
383         if (match("cylpress", last, pressure, buf, &sample->tankpressure))
384                 return;
385         if (match("depth", last, depth, buf, &sample->depth))
386                 return;
387         if (match("temperature", last, temperature, buf, &sample->temperature))
388                 return;
389         if (match("sampletime", last, sampletime, buf, &sample->time))
390                 return;
391         if (match("time", last, sampletime, buf, &sample->time))
392                 return;
393
394         nonmatch("sample", name, last, buf);
395 }
396
397 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
398 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
399 {
400         const char *last = last_part(name);
401
402         if (match("date", last, divedate, buf, &dive->when))
403                 return;
404         if (match("time", last, divetime, buf, &dive->when))
405                 return;
406         nonmatch("dive", name, last, buf);
407 }
408
409 static unsigned int dive_size(int samples)
410 {
411         return sizeof(struct dive) + samples*sizeof(struct sample);
412 }
413
414 /*
415  * File boundaries are dive boundaries. But sometimes there are
416  * multiple dives per file, so there can be other events too that
417  * trigger a "new dive" marker and you may get some nesting due
418  * to that. Just ignore nesting levels.
419  */
420 static void dive_start(void)
421 {
422         unsigned int size;
423
424         if (dive)
425                 return;
426
427         alloc_samples = 5;
428         size = dive_size(alloc_samples);
429         dive = malloc(size);
430         if (!dive)
431                 exit(1);
432         memset(dive, 0, size);
433         memset(&tm, 0, sizeof(tm));
434 }
435
436 static void dive_end(void)
437 {
438         if (!dive)
439                 return;
440         record_dive(dive);
441         dive = NULL;
442 }
443
444 static void sample_start(void)
445 {
446         int nr;
447
448         if (!dive)
449                 return;
450         nr = dive->samples;
451         if (nr >= alloc_samples) {
452                 unsigned int size;
453
454                 alloc_samples = (alloc_samples * 3)/2 + 10;
455                 size = dive_size(alloc_samples);
456                 dive = realloc(dive, size);
457                 if (!dive)
458                         return;
459         }
460         sample = dive->sample + nr;
461         memset(sample, 0, sizeof(*sample));
462 }
463
464 static void sample_end(void)
465 {
466         sample = NULL;
467         if (!dive)
468                 return;
469         dive->samples++;
470 }
471
472 static void entry(const char *name, int size, const char *raw)
473 {
474         char *buf = malloc(size+1);
475
476         if (!buf)
477                 return;
478         memcpy(buf, raw, size);
479         buf[size] = 0;
480         if (sample) {
481                 try_to_fill_sample(sample, name, buf);
482                 return;
483         }
484         if (dive) {
485                 try_to_fill_dive(dive, name, buf);
486                 return;
487         }
488 }
489
490 static const char *nodename(xmlNode *node, char *buf, int len)
491 {
492
493         if (!node || !node->name)
494                 return "root";
495
496         buf += len;
497         *--buf = 0;
498         len--;
499
500         for(;;) {
501                 const char *name = node->name;
502                 int i = strlen(name);
503                 while (--i >= 0) {
504                         unsigned char c = name[i];
505                         *--buf = tolower(c);
506                         if (!--len)
507                                 return buf;
508                 }
509                 node = node->parent;
510                 if (!node || !node->name)
511                         return buf;
512                 *--buf = '.';
513                 if (!--len)
514                         return buf;
515         }
516 }
517
518 #define MAXNAME 64
519
520 static void visit_one_node(xmlNode *node)
521 {
522         int len;
523         const unsigned char *content;
524         char buffer[MAXNAME];
525         const char *name;
526
527         content = node->content;
528         if (!content)
529                 return;
530
531         /* Trim whitespace at beginning */
532         while (isspace(*content))
533                 content++;
534
535         /* Trim whitespace at end */
536         len = strlen(content);
537         while (len && isspace(content[len-1]))
538                 len--;
539
540         if (!len)
541                 return;
542
543         /* Don't print out the node name if it is "text" */
544         if (!strcmp(node->name, "text"))
545                 node = node->parent;
546
547         name = nodename(node, buffer, sizeof(buffer));
548
549         entry(name, len, content);
550 }
551
552 static void traverse(xmlNode *node)
553 {
554         xmlNode *n;
555
556         for (n = node; n; n = n->next) {
557                 /* XML from libdivecomputer: 'dive' per new dive */
558                 if (!strcmp(n->name, "dive")) {
559                         dive_start();
560                         traverse(n->children);
561                         dive_end();
562                         continue;
563                 }
564
565                 /*
566                  * At least both libdivecomputer and Suunto
567                  * agree on "sample".
568                  *
569                  * Well - almost. Ignore case.
570                  */
571                 if (!strcasecmp(n->name, "sample")) {
572                         sample_start();
573                         traverse(n->children);
574                         sample_end();
575                         continue;
576                 }
577
578                 /* Anything else - just visit it and recurse */
579                 visit_one_node(n);
580                 traverse(n->children);
581         }
582 }
583
584 static void parse(const char *filename)
585 {
586         xmlDoc *doc;
587
588         doc = xmlReadFile(filename, NULL, 0);
589         if (!doc) {
590                 fprintf(stderr, "Failed to parse '%s'.\n", filename);
591                 return;
592         }
593
594         dive_start();
595         traverse(xmlDocGetRootElement(doc));
596         dive_end();
597         xmlFreeDoc(doc);
598         xmlCleanupParser();
599 }
600
601 static void parse_argument(const char *arg)
602 {
603         const char *p = arg+1;
604
605         do {
606                 switch (*p) {
607                 case 'v':
608                         verbose++;
609                         continue;
610                 default:
611                         fprintf(stderr, "Bad argument '%s'\n", arg);
612                         exit(1);
613                 }
614         } while (*++p);
615 }
616
617 int main(int argc, char **argv)
618 {
619         int i;
620
621         LIBXML_TEST_VERSION
622
623         for (i = 1; i < argc; i++) {
624                 const char *a = argv[i];
625
626                 if (a[0] == '-') {
627                         parse_argument(a);
628                         continue;
629                 }
630                 parse(a);
631         }
632         return 0;
633 }