]> git.tdb.fi Git - ext/subsurface.git/blob - parse-xml.c
Use 'units' value instead of guessing based on integer/FP
[ext/subsurface.git] / parse-xml.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 #include "dive.h"
11
12 int verbose;
13
14 struct dive_table dive_table;
15
16 /*
17  * Add a dive into the dive_table array
18  */
19 static void record_dive(struct dive *dive)
20 {
21         int nr = dive_table.nr, allocated = dive_table.allocated;
22         struct dive **dives = dive_table.dives;
23
24         if (nr >= allocated) {
25                 allocated = (nr + 32) * 3 / 2;
26                 dives = realloc(dives, allocated * sizeof(struct dive *));
27                 if (!dives)
28                         exit(1);
29                 dive_table.dives = dives;
30                 dive_table.allocated = allocated;
31         }
32         dives[nr] = dive;
33         dive_table.nr = nr+1;
34 }
35
36 static void start_match(const char *type, const char *name, char *buffer)
37 {
38         if (verbose > 2)
39                 printf("Matching %s '%s' (%s)\n",
40                         type, name, buffer);
41 }
42
43 static void nonmatch(const char *type, const char *name, char *buffer)
44 {
45         if (verbose > 1)
46                 printf("Unable to match %s '%s' (%s)\n",
47                         type, name, buffer);
48         free(buffer);
49 }
50
51 typedef void (*matchfn_t)(char *buffer, void *);
52
53 static int match(const char *pattern, int plen,
54                  const char *name, int nlen,
55                  matchfn_t fn, char *buf, void *data)
56 {
57         if (plen > nlen)
58                 return 0;
59         if (memcmp(pattern, name + nlen - plen, plen))
60                 return 0;
61         fn(buf, data);
62         return 1;
63 }
64
65 /*
66  * We keep our internal data in well-specified units, but
67  * the input may come in some random format. This keeps track
68  * of the incoming units.
69  */
70 static struct units {
71         enum { METERS, FEET } length;
72         enum { LITER, CUFT } volume;
73         enum { BAR, PSI } pressure;
74         enum { CELSIUS, FAHRENHEIT } temperature;
75         enum { KG, LBS } weight;
76 } units;
77
78 /* We're going to default to SI units for input */
79 static const struct units SI_units = {
80         .length = METERS,
81         .volume = LITER,
82         .pressure = BAR,
83         .temperature = CELSIUS,
84         .weight = KG
85 };
86
87 /*
88  * Dive info as it is being built up..
89  */
90 static int alloc_samples;
91 static struct dive *dive;
92 static struct sample *sample;
93 static struct tm tm;
94 static int suunto, uemis;
95 static int event_index, gasmix_index;
96
97 static time_t utc_mktime(struct tm *tm)
98 {
99         static const int mdays[] = {
100             0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
101         };
102         int year = tm->tm_year;
103         int month = tm->tm_mon;
104         int day = tm->tm_mday;
105
106         /* First normalize relative to 1900 */
107         if (year < 70)
108                 year += 100;
109         else if (year > 1900)
110                 year -= 1900;
111
112         /* Normalized to Jan 1, 1970: unix time */
113         year -= 70;
114
115         if (year < 0 || year > 129) /* algo only works for 1970-2099 */
116                 return -1;
117         if (month < 0 || month > 11) /* array bounds */
118                 return -1;
119         if (month < 2 || (year + 2) % 4)
120                 day--;
121         if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
122                 return -1;
123         return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
124                 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
125 }
126
127 static void divedate(char *buffer, void *_when)
128 {
129         int d,m,y;
130         time_t *when = _when;
131         int success = 0;
132
133         success = tm.tm_sec | tm.tm_min | tm.tm_hour;
134         if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
135                 tm.tm_year = y;
136                 tm.tm_mon = m-1;
137                 tm.tm_mday = d;
138         } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
139                 tm.tm_year = y;
140                 tm.tm_mon = m-1;
141                 tm.tm_mday = d;
142         } else {
143                 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
144                 success = 0;
145         }
146
147         if (success)
148                 *when = utc_mktime(&tm);
149
150         free(buffer);
151 }
152
153 static void divetime(char *buffer, void *_when)
154 {
155         int h,m,s = 0;
156         time_t *when = _when;
157
158         if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
159                 tm.tm_hour = h;
160                 tm.tm_min = m;
161                 tm.tm_sec = s;
162                 if (tm.tm_year)
163                         *when = utc_mktime(&tm);
164         }
165         free(buffer);
166 }
167
168 /* Libdivecomputer: "2011-03-20 10:22:38" */
169 static void divedatetime(char *buffer, void *_when)
170 {
171         int y,m,d;
172         int hr,min,sec;
173         time_t *when = _when;
174
175         if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
176                 &y, &m, &d, &hr, &min, &sec) == 6) {
177                 tm.tm_year = y;
178                 tm.tm_mon = m-1;
179                 tm.tm_mday = d;
180                 tm.tm_hour = hr;
181                 tm.tm_min = min;
182                 tm.tm_sec = sec;
183                 *when = utc_mktime(&tm);
184         }
185         free(buffer);
186 }
187
188 union int_or_float {
189         double fp;
190 };
191
192 enum number_type {
193         NEITHER,
194         FLOAT
195 };
196
197 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
198 {
199         char *end;
200         long val;
201         double fp;
202
203         /* Integer or floating point? */
204         val = strtol(buffer, &end, 10);
205         if (val < 0 || end == buffer)
206                 return NEITHER;
207
208         /* Looks like it might be floating point? */
209         if (*end == '.') {
210                 errno = 0;
211                 fp = strtod(buffer, &end);
212                 if (!errno) {
213                         res->fp = fp;
214                         return FLOAT;
215                 }
216         }
217
218         res->fp = val;
219         return FLOAT;
220 }
221
222 static void pressure(char *buffer, void *_press)
223 {
224         double mbar;
225         pressure_t *pressure = _press;
226         union int_or_float val;
227
228         switch (integer_or_float(buffer, &val)) {
229         case FLOAT:
230                 switch (units.pressure) {
231                 case BAR:
232                         /* Assume mbar, but if it's really small, it's bar */
233                         mbar = val.fp;
234                         if (mbar < 5000)
235                                 mbar = mbar * 1000;
236                         break;
237                 case PSI:
238                         mbar = val.fp * 68.95;
239                         break;
240                 }
241                 if (mbar > 5 && mbar < 500000) {
242                         pressure->mbar = mbar + 0.5;
243                         break;
244                 }
245         /* fallthrough */
246         default:
247                 printf("Strange pressure reading %s\n", buffer);
248         }
249         free(buffer);
250 }
251
252 static void depth(char *buffer, void *_depth)
253 {
254         depth_t *depth = _depth;
255         union int_or_float val;
256
257         switch (integer_or_float(buffer, &val)) {
258         case FLOAT:
259                 switch (units.length) {
260                 case METERS:
261                         depth->mm = val.fp * 1000 + 0.5;
262                         break;
263                 case FEET:
264                         depth->mm = val.fp * 304.8 + 0.5;
265                         break;
266                 }
267                 break;
268         default:
269                 printf("Strange depth reading %s\n", buffer);
270         }
271         free(buffer);
272 }
273
274 static void temperature(char *buffer, void *_temperature)
275 {
276         temperature_t *temperature = _temperature;
277         union int_or_float val;
278
279         switch (integer_or_float(buffer, &val)) {
280         case FLOAT:
281                 /* Ignore zero. It means "none" */
282                 if (!val.fp)
283                         break;
284                 /* Celsius */
285                 switch (units.temperature) {
286                 case CELSIUS:
287                         temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
288                         break;
289                 case FAHRENHEIT:
290                         temperature->mkelvin = (val.fp + 459.67) * 5000/9;
291                         break;
292                 }
293                 break;
294         default:
295                 printf("Strange temperature reading %s\n", buffer);
296         }
297         free(buffer);
298 }
299
300 static void sampletime(char *buffer, void *_time)
301 {
302         int i;
303         int min, sec;
304         duration_t *time = _time;
305
306         i = sscanf(buffer, "%d:%d", &min, &sec);
307         switch (i) {
308         case 1:
309                 sec = min;
310                 min = 0;
311         /* fallthrough */
312         case 2:
313                 time->seconds = sec + min*60;
314                 break;
315         default:
316                 printf("Strange sample time reading %s\n", buffer);
317         }
318         free(buffer);
319 }
320
321 static void duration(char *buffer, void *_time)
322 {
323         sampletime(buffer, _time);
324 }
325
326 static void percent(char *buffer, void *_fraction)
327 {
328         fraction_t *fraction = _fraction;
329         union int_or_float val;
330
331         switch (integer_or_float(buffer, &val)) {
332         case FLOAT:
333                 if (val.fp <= 100.0)
334                         fraction->permille = val.fp * 10 + 0.5;
335                 break;
336
337         default:
338                 printf("Strange percentage reading %s\n", buffer);
339                 break;
340         }
341         free(buffer);
342 }
343
344 static void gasmix(char *buffer, void *_fraction)
345 {
346         /* libdivecomputer does negative percentages. */
347         if (*buffer == '-')
348                 return;
349         if (gasmix_index < MAX_MIXES)
350                 percent(buffer, _fraction);
351 }
352
353 static void gasmix_nitrogen(char *buffer, void *_gasmix)
354 {
355         /* Ignore n2 percentages. There's no value in them. */
356 }
357
358 static void utf8_string(char *buffer, void *_res)
359 {
360         *(char **)_res = buffer;
361 }
362
363 /*
364  * Uemis water_pressure. In centibar. And when converting to
365  * depth, I'm just going to always use saltwater, because I
366  * think "true depth" is just stupid. From a diving standpoint,
367  * "true depth" is pretty much completely pointless, unless
368  * you're doing some kind of underwater surveying work.
369  *
370  * So I give water depths in "pressure depth", always assuming
371  * salt water. So one atmosphere per 10m.
372  */
373 static void water_pressure(char *buffer, void *_depth)
374 {
375         depth_t *depth = _depth;
376         union int_or_float val;
377         float atm;
378
379         switch (integer_or_float(buffer, &val)) {
380         case FLOAT:
381                 switch (units.pressure) {
382                 case BAR:
383                         /* It's actually centibar! */
384                         atm = (val.fp / 100) / 1.01325;
385                         break;
386                 case PSI:
387                         /* I think it's centiPSI too. Crazy. */
388                         atm = (val.fp / 100) * 0.0680459639;
389                         break;
390                 }
391                 /* 10 m per atm */
392                 depth->mm = 10000 * atm;
393                 break;
394         default:
395                 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
396         }
397         free(buffer);
398 }
399
400 #define MATCH(pattern, fn, dest) \
401         match(pattern, strlen(pattern), name, len, fn, buf, dest)
402
403 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
404 {
405         return  MATCH(".reading.dive_time", sampletime, &sample->time) ||
406                 MATCH(".reading.water_pressure", water_pressure, &sample->depth);
407 }
408
409 /* We're in samples - try to convert the random xml value to something useful */
410 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
411 {
412         int len = strlen(name);
413
414         start_match("sample", name, buf);
415         if (MATCH(".sample.pressure", pressure, &sample->tankpressure))
416                 return;
417         if (MATCH(".sample.cylpress", pressure, &sample->tankpressure))
418                 return;
419         if (MATCH(".sample.depth", depth, &sample->depth))
420                 return;
421         if (MATCH(".sample.temp", temperature, &sample->temperature))
422                 return;
423         if (MATCH(".sample.temperature", temperature, &sample->temperature))
424                 return;
425         if (MATCH(".sample.sampletime", sampletime, &sample->time))
426                 return;
427         if (MATCH(".sample.time", sampletime, &sample->time))
428                 return;
429
430         if (uemis) {
431                 if (uemis_fill_sample(sample, name, len, buf))
432                         return;
433         }
434
435         nonmatch("sample", name, buf);
436 }
437
438 /*
439  * Crazy suunto xml. Look at how those o2/he things match up.
440  */
441 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
442 {
443         return  MATCH(".o2pct", percent, &dive->gasmix[0].o2) ||
444                 MATCH(".hepct_0", percent, &dive->gasmix[0].he) ||
445                 MATCH(".o2pct_2", percent, &dive->gasmix[1].o2) ||
446                 MATCH(".hepct_1", percent, &dive->gasmix[1].he) ||
447                 MATCH(".o2pct_3", percent, &dive->gasmix[2].o2) ||
448                 MATCH(".hepct_2", percent, &dive->gasmix[2].he) ||
449                 MATCH(".o2pct_4", percent, &dive->gasmix[3].o2) ||
450                 MATCH(".hepct_3", percent, &dive->gasmix[3].he);
451 }
452
453 static int buffer_value(char *buffer)
454 {
455         int val = atoi(buffer);
456         free(buffer);
457         return val;
458 }
459
460 static void uemis_length_unit(char *buffer, void *_unused)
461 {
462         units.length = buffer_value(buffer) ? FEET : METERS;
463 }
464
465 static void uemis_volume_unit(char *buffer, void *_unused)
466 {
467         units.volume = buffer_value(buffer) ? CUFT : LITER;
468 }
469
470 static void uemis_pressure_unit(char *buffer, void *_unused)
471 {
472         units.pressure = buffer_value(buffer) ? PSI : BAR;
473 }
474
475 static void uemis_temperature_unit(char *buffer, void *_unused)
476 {
477         units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
478 }
479
480 static void uemis_weight_unit(char *buffer, void *_unused)
481 {
482         units.weight = buffer_value(buffer) ? LBS : KG;
483 }
484
485 static void uemis_time_unit(char *buffer, void *_unused)
486 {
487 }
488
489 static void uemis_date_unit(char *buffer, void *_unused)
490 {
491 }
492
493 /* Modified julian day, yay! */
494 static void uemis_date_time(char *buffer, void *_when)
495 {
496         time_t *when = _when;
497         union int_or_float val;
498
499         switch (integer_or_float(buffer, &val)) {
500         case FLOAT:
501                 *when = (val.fp - 40587.5) * 86400;
502                 break;
503         default:
504                 fprintf(stderr, "Strange julian date: %s", buffer);
505         }
506         free(buffer);
507 }
508
509 /*
510  * Uemis doesn't know time zones. You need to do them as
511  * minutes, not hours.
512  *
513  * But that's ok, we don't track timezones yet either. We
514  * just turn everything into "localtime expressed as UTC".
515  */
516 static void uemis_time_zone(char *buffer, void *_when)
517 {
518         time_t *when = _when;
519         signed char tz = atoi(buffer);
520
521         *when += tz * 3600;
522 }
523
524 static int uemis_dive_match(struct dive *dive, const char *name, int len, char *buf)
525 {
526         return  MATCH(".units.length", uemis_length_unit, &units) ||
527                 MATCH(".units.volume", uemis_volume_unit, &units) ||
528                 MATCH(".units.pressure", uemis_pressure_unit, &units) ||
529                 MATCH(".units.temperature", uemis_temperature_unit, &units) ||
530                 MATCH(".units.weight", uemis_weight_unit, &units) ||
531                 MATCH(".units.time", uemis_time_unit, &units) ||
532                 MATCH(".units.date", uemis_date_unit, &units) ||
533                 MATCH(".date_time", uemis_date_time, &dive->when) ||
534                 MATCH(".time_zone", uemis_time_zone, &dive->when);
535 }
536
537 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
538 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
539 {
540         int len = strlen(name);
541
542         start_match("dive", name, buf);
543         if (MATCH(".date", divedate, &dive->when))
544                 return;
545         if (MATCH(".time", divetime, &dive->when))
546                 return;
547         if (MATCH(".datetime", divedatetime, &dive->when))
548                 return;
549         if (MATCH(".maxdepth", depth, &dive->maxdepth))
550                 return;
551         if (MATCH(".meandepth", depth, &dive->meandepth))
552                 return;
553         if (MATCH(".duration", duration, &dive->duration))
554                 return;
555         if (MATCH(".divetime", duration, &dive->duration))
556                 return;
557         if (MATCH(".divetimesec", duration, &dive->duration))
558                 return;
559         if (MATCH(".surfacetime", duration, &dive->surfacetime))
560                 return;
561         if (MATCH(".airtemp", temperature, &dive->airtemp))
562                 return;
563         if (MATCH(".watertemp", temperature, &dive->watertemp))
564                 return;
565         if (MATCH(".cylinderstartpressure", pressure, &dive->beginning_pressure))
566                 return;
567         if (MATCH(".cylinderendpressure", pressure, &dive->end_pressure))
568                 return;
569         if (MATCH(".location", utf8_string, &dive->location))
570                 return;
571         if (MATCH(".notes", utf8_string, &dive->notes))
572                 return;
573
574         if (MATCH(".o2", gasmix, &dive->gasmix[gasmix_index].o2))
575                 return;
576         if (MATCH(".n2", gasmix_nitrogen, &dive->gasmix[gasmix_index]))
577                 return;
578         if (MATCH(".he", gasmix, &dive->gasmix[gasmix_index].he))
579                 return;
580
581         /* Suunto XML files are some crazy sh*t. */
582         if (suunto && suunto_dive_match(dive, name, len, buf))
583                 return;
584
585         if (uemis && uemis_dive_match(dive, name, len, buf))
586                 return;
587
588         nonmatch("dive", name, buf);
589 }
590
591 static unsigned int dive_size(int samples)
592 {
593         return sizeof(struct dive) + samples*sizeof(struct sample);
594 }
595
596 /*
597  * File boundaries are dive boundaries. But sometimes there are
598  * multiple dives per file, so there can be other events too that
599  * trigger a "new dive" marker and you may get some nesting due
600  * to that. Just ignore nesting levels.
601  */
602 static void dive_start(void)
603 {
604         unsigned int size;
605
606         if (dive)
607                 return;
608
609         alloc_samples = 5;
610         size = dive_size(alloc_samples);
611         dive = malloc(size);
612         if (!dive)
613                 exit(1);
614         memset(dive, 0, size);
615         memset(&tm, 0, sizeof(tm));
616 }
617
618 static char *generate_name(struct dive *dive)
619 {
620         int len;
621         struct tm *tm;
622         char buffer[256], *p;
623
624         tm = gmtime(&dive->when);
625
626         len = snprintf(buffer, sizeof(buffer),
627                 "%04d-%02d-%02d "
628                 "%02d:%02d:%02d "
629                 "(%d ft, %d min)",
630                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
631                 tm->tm_hour, tm->tm_min, tm->tm_sec,
632                 to_feet(dive->maxdepth), dive->duration.seconds / 60);
633         p = malloc(len+1);
634         if (!p)
635                 exit(1);
636         memcpy(p, buffer, len+1);
637         return p;
638 }
639
640 static void sanitize_gasmix(struct dive *dive)
641 {
642         int i;
643
644         for (i = 0; i < MAX_MIXES; i++) {
645                 gasmix_t *mix = dive->gasmix+i;
646                 unsigned int o2, he;
647
648                 o2 = mix->o2.permille;
649                 he = mix->he.permille;
650
651                 /* Regular air: leave empty */
652                 if (!he) {
653                         if (!o2)
654                                 continue;
655                         /* 20.9% or 21% O2 is just air */
656                         if (o2 >= 209 && o2 <= 210) {
657                                 mix->o2.permille = 0;
658                                 continue;
659                         }
660                 }
661
662                 /* Sane mix? */
663                 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
664                         continue;
665                 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
666                 memset(mix, 0, sizeof(*mix));
667         }
668 }
669
670 static void dive_end(void)
671 {
672         if (!dive)
673                 return;
674         if (!dive->name)
675                 dive->name = generate_name(dive);
676         sanitize_gasmix(dive);
677         record_dive(dive);
678         dive = NULL;
679         gasmix_index = 0;
680 }
681
682 static void suunto_start(void)
683 {
684         suunto++;
685         units = SI_units;
686 }
687
688 static void suunto_end(void)
689 {
690         suunto--;
691 }
692
693 static void uemis_start(void)
694 {
695         uemis++;
696         units = SI_units;
697 }
698
699 static void uemis_end(void)
700 {
701 }
702
703 static void event_start(void)
704 {
705 }
706
707 static void event_end(void)
708 {
709         event_index++;
710 }
711
712 static void gasmix_start(void)
713 {
714 }
715
716 static void gasmix_end(void)
717 {
718         gasmix_index++;
719 }
720
721 static void sample_start(void)
722 {
723         int nr;
724
725         if (!dive)
726                 return;
727         nr = dive->samples;
728         if (nr >= alloc_samples) {
729                 unsigned int size;
730
731                 alloc_samples = (alloc_samples * 3)/2 + 10;
732                 size = dive_size(alloc_samples);
733                 dive = realloc(dive, size);
734                 if (!dive)
735                         return;
736         }
737         sample = dive->sample + nr;
738         memset(sample, 0, sizeof(*sample));
739         event_index = 0;
740 }
741
742 static void sample_end(void)
743 {
744         if (!dive)
745                 return;
746
747         if (sample->time.seconds > dive->duration.seconds) {
748                 if (sample->depth.mm)
749                         dive->duration = sample->time;
750         }
751
752         if (sample->depth.mm > dive->maxdepth.mm)
753                 dive->maxdepth.mm = sample->depth.mm;
754
755         if (sample->temperature.mkelvin) {
756                 if (!dive->watertemp.mkelvin || dive->watertemp.mkelvin > sample->temperature.mkelvin)
757                         dive->watertemp = sample->temperature;
758         }
759
760         sample = NULL;
761         dive->samples++;
762 }
763
764 static void entry(const char *name, int size, const char *raw)
765 {
766         char *buf = malloc(size+1);
767
768         if (!buf)
769                 return;
770         memcpy(buf, raw, size);
771         buf[size] = 0;
772         if (sample) {
773                 try_to_fill_sample(sample, name, buf);
774                 return;
775         }
776         if (dive) {
777                 try_to_fill_dive(dive, name, buf);
778                 return;
779         }
780 }
781
782 static const char *nodename(xmlNode *node, char *buf, int len)
783 {
784         if (!node || !node->name)
785                 return "root";
786
787         buf += len;
788         *--buf = 0;
789         len--;
790
791         for(;;) {
792                 const char *name = node->name;
793                 int i = strlen(name);
794                 while (--i >= 0) {
795                         unsigned char c = name[i];
796                         *--buf = tolower(c);
797                         if (!--len)
798                                 return buf;
799                 }
800                 node = node->parent;
801                 if (!node || !node->name)
802                         return buf;
803                 *--buf = '.';
804                 if (!--len)
805                         return buf;
806         }
807 }
808
809 #define MAXNAME 64
810
811 static void visit_one_node(xmlNode *node)
812 {
813         int len;
814         const unsigned char *content;
815         char buffer[MAXNAME];
816         const char *name;
817
818         content = node->content;
819         if (!content)
820                 return;
821
822         /* Trim whitespace at beginning */
823         while (isspace(*content))
824                 content++;
825
826         /* Trim whitespace at end */
827         len = strlen(content);
828         while (len && isspace(content[len-1]))
829                 len--;
830
831         if (!len)
832                 return;
833
834         /* Don't print out the node name if it is "text" */
835         if (!strcmp(node->name, "text"))
836                 node = node->parent;
837
838         name = nodename(node, buffer, sizeof(buffer));
839
840         entry(name, len, content);
841 }
842
843 static void traverse(xmlNode *root);
844
845 static void traverse_properties(xmlNode *node)
846 {
847         xmlAttr *p;
848
849         for (p = node->properties; p; p = p->next)
850                 traverse(p->children);
851 }
852
853 static void visit(xmlNode *n)
854 {
855         visit_one_node(n);
856         traverse_properties(n);
857         traverse(n->children);
858 }
859
860 /*
861  * I'm sure this could be done as some fancy DTD rules.
862  * It's just not worth the headache.
863  */
864 static struct nesting {
865         const char *name;
866         void (*start)(void), (*end)(void);
867 } nesting[] = {
868         { "dive", dive_start, dive_end },
869         { "SUUNTO", suunto_start, suunto_end },
870         { "sample", sample_start, sample_end },
871         { "SAMPLE", sample_start, sample_end },
872         { "reading", sample_start, sample_end },
873         { "event", event_start, event_end },
874         { "gasmix", gasmix_start, gasmix_end },
875         { "pre_dive", uemis_start, uemis_end },
876         { NULL, }
877 };
878
879 static void traverse(xmlNode *root)
880 {
881         xmlNode *n;
882
883         for (n = root; n; n = n->next) {
884                 struct nesting *rule = nesting;
885
886                 do {
887                         if (!strcmp(rule->name, n->name))
888                                 break;
889                         rule++;
890                 } while (rule->name);
891
892                 if (rule->start)
893                         rule->start();
894                 visit(n);
895                 if (rule->end)
896                         rule->end();
897         }
898 }
899
900 /* Per-file reset */
901 static void reset_all(void)
902 {
903         /*
904          * We reset the units for each file. You'd think it was
905          * a per-dive property, but I'm not going to trust people
906          * to do per-dive setup. If the xml does have per-dive
907          * data within one file, we might have to reset it per
908          * dive for that format.
909          */
910         units = SI_units;
911         suunto = 0;
912         uemis = 0;
913 }
914
915 void parse_xml_file(const char *filename)
916 {
917         xmlDoc *doc;
918
919         doc = xmlReadFile(filename, NULL, 0);
920         if (!doc) {
921                 fprintf(stderr, "Failed to parse '%s'.\n", filename);
922                 return;
923         }
924
925         reset_all();
926         dive_start();
927         traverse(xmlDocGetRootElement(doc));
928         dive_end();
929         xmlFreeDoc(doc);
930         xmlCleanupParser();
931 }
932
933 void parse_xml_init(void)
934 {
935         LIBXML_TEST_VERSION
936 }