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