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