7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
14 struct dive_table dive_table;
17 * Add a dive into the dive_table array
19 static void record_dive(struct dive *dive)
21 int nr = dive_table.nr, allocated = dive_table.allocated;
22 struct dive **dives = dive_table.dives;
24 if (nr >= allocated) {
25 allocated = (nr + 32) * 3 / 2;
26 dives = realloc(dives, allocated * sizeof(struct dive *));
29 dive_table.dives = dives;
30 dive_table.allocated = allocated;
36 static void start_match(const char *type, const char *name, char *buffer)
39 printf("Matching %s '%s' (%s)\n",
43 static void nonmatch(const char *type, const char *name, char *buffer)
46 printf("Unable to match %s '%s' (%s)\n",
51 typedef void (*matchfn_t)(char *buffer, void *);
53 static int match(const char *pattern, int plen,
54 const char *name, int nlen,
55 matchfn_t fn, char *buf, void *data)
59 if (memcmp(pattern, name + nlen - plen, plen))
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.
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;
78 /* We're going to default to SI units for input */
79 static const struct units SI_units = {
83 .temperature = CELSIUS,
88 * Dive info as it is being built up..
90 static int alloc_samples;
91 static struct dive *dive;
92 static struct sample *sample;
94 static int suunto, uemis;
95 static int event_index, gasmix_index;
97 static time_t utc_mktime(struct tm *tm)
99 static const int mdays[] = {
100 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
102 int year = tm->tm_year;
103 int month = tm->tm_mon;
104 int day = tm->tm_mday;
106 /* First normalize relative to 1900 */
109 else if (year > 1900)
112 /* Normalized to Jan 1, 1970: unix time */
115 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
117 if (month < 0 || month > 11) /* array bounds */
119 if (month < 2 || (year + 2) % 4)
121 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
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;
127 static void divedate(char *buffer, void *_when)
130 time_t *when = _when;
133 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
134 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
138 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
143 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
148 *when = utc_mktime(&tm);
153 static void divetime(char *buffer, void *_when)
156 time_t *when = _when;
158 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
163 *when = utc_mktime(&tm);
168 /* Libdivecomputer: "2011-03-20 10:22:38" */
169 static void divedatetime(char *buffer, void *_when)
173 time_t *when = _when;
175 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
176 &y, &m, &d, &hr, &min, &sec) == 6) {
183 *when = utc_mktime(&tm);
197 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
203 /* Integer or floating point? */
204 val = strtol(buffer, &end, 10);
205 if (val < 0 || end == buffer)
208 /* Looks like it might be floating point? */
211 fp = strtod(buffer, &end);
222 static void pressure(char *buffer, void *_press)
225 pressure_t *pressure = _press;
226 union int_or_float val;
228 switch (integer_or_float(buffer, &val)) {
230 /* Just ignore zero values */
233 switch (units.pressure) {
235 /* Assume mbar, but if it's really small, it's bar */
241 mbar = val.fp * 68.95;
244 if (mbar > 5 && mbar < 500000) {
245 pressure->mbar = mbar + 0.5;
250 printf("Strange pressure reading %s\n", buffer);
255 static void depth(char *buffer, void *_depth)
257 depth_t *depth = _depth;
258 union int_or_float val;
260 switch (integer_or_float(buffer, &val)) {
262 switch (units.length) {
264 depth->mm = val.fp * 1000 + 0.5;
267 depth->mm = val.fp * 304.8 + 0.5;
272 printf("Strange depth reading %s\n", buffer);
277 static void temperature(char *buffer, void *_temperature)
279 temperature_t *temperature = _temperature;
280 union int_or_float val;
282 switch (integer_or_float(buffer, &val)) {
284 /* Ignore zero. It means "none" */
288 switch (units.temperature) {
290 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
293 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
298 printf("Strange temperature reading %s\n", buffer);
303 static void sampletime(char *buffer, void *_time)
307 duration_t *time = _time;
309 i = sscanf(buffer, "%d:%d", &min, &sec);
316 time->seconds = sec + min*60;
319 printf("Strange sample time reading %s\n", buffer);
324 static void duration(char *buffer, void *_time)
326 sampletime(buffer, _time);
329 static void percent(char *buffer, void *_fraction)
331 fraction_t *fraction = _fraction;
332 union int_or_float val;
334 switch (integer_or_float(buffer, &val)) {
337 fraction->permille = val.fp * 10 + 0.5;
341 printf("Strange percentage reading %s\n", buffer);
347 static void gasmix(char *buffer, void *_fraction)
349 /* libdivecomputer does negative percentages. */
352 if (gasmix_index < MAX_MIXES)
353 percent(buffer, _fraction);
356 static void gasmix_nitrogen(char *buffer, void *_gasmix)
358 /* Ignore n2 percentages. There's no value in them. */
361 static void utf8_string(char *buffer, void *_res)
363 *(char **)_res = buffer;
367 * Uemis water_pressure. In centibar. And when converting to
368 * depth, I'm just going to always use saltwater, because I
369 * think "true depth" is just stupid. From a diving standpoint,
370 * "true depth" is pretty much completely pointless, unless
371 * you're doing some kind of underwater surveying work.
373 * So I give water depths in "pressure depth", always assuming
374 * salt water. So one atmosphere per 10m.
376 static void water_pressure(char *buffer, void *_depth)
378 depth_t *depth = _depth;
379 union int_or_float val;
382 switch (integer_or_float(buffer, &val)) {
384 switch (units.pressure) {
386 /* It's actually centibar! */
387 atm = (val.fp / 100) / 1.01325;
390 /* I think it's centiPSI too. Crazy. */
391 atm = (val.fp / 100) * 0.0680459639;
395 depth->mm = 10000 * atm;
398 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
403 #define MATCH(pattern, fn, dest) \
404 match(pattern, strlen(pattern), name, len, fn, buf, dest)
406 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
408 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
409 MATCH(".reading.water_pressure", water_pressure, &sample->depth);
412 /* We're in samples - try to convert the random xml value to something useful */
413 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
415 int len = strlen(name);
417 start_match("sample", name, buf);
418 if (MATCH(".sample.pressure", pressure, &sample->tankpressure))
420 if (MATCH(".sample.cylpress", pressure, &sample->tankpressure))
422 if (MATCH(".sample.depth", depth, &sample->depth))
424 if (MATCH(".sample.temp", temperature, &sample->temperature))
426 if (MATCH(".sample.temperature", temperature, &sample->temperature))
428 if (MATCH(".sample.sampletime", sampletime, &sample->time))
430 if (MATCH(".sample.time", sampletime, &sample->time))
434 if (uemis_fill_sample(sample, name, len, buf))
438 nonmatch("sample", name, buf);
442 * Crazy suunto xml. Look at how those o2/he things match up.
444 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
446 return MATCH(".o2pct", percent, &dive->gasmix[0].o2) ||
447 MATCH(".hepct_0", percent, &dive->gasmix[0].he) ||
448 MATCH(".o2pct_2", percent, &dive->gasmix[1].o2) ||
449 MATCH(".hepct_1", percent, &dive->gasmix[1].he) ||
450 MATCH(".o2pct_3", percent, &dive->gasmix[2].o2) ||
451 MATCH(".hepct_2", percent, &dive->gasmix[2].he) ||
452 MATCH(".o2pct_4", percent, &dive->gasmix[3].o2) ||
453 MATCH(".hepct_3", percent, &dive->gasmix[3].he);
456 static int buffer_value(char *buffer)
458 int val = atoi(buffer);
463 static void uemis_length_unit(char *buffer, void *_unused)
465 units.length = buffer_value(buffer) ? FEET : METERS;
468 static void uemis_volume_unit(char *buffer, void *_unused)
470 units.volume = buffer_value(buffer) ? CUFT : LITER;
473 static void uemis_pressure_unit(char *buffer, void *_unused)
475 units.pressure = buffer_value(buffer) ? PSI : BAR;
478 static void uemis_temperature_unit(char *buffer, void *_unused)
480 units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
483 static void uemis_weight_unit(char *buffer, void *_unused)
485 units.weight = buffer_value(buffer) ? LBS : KG;
488 static void uemis_time_unit(char *buffer, void *_unused)
492 static void uemis_date_unit(char *buffer, void *_unused)
496 /* Modified julian day, yay! */
497 static void uemis_date_time(char *buffer, void *_when)
499 time_t *when = _when;
500 union int_or_float val;
502 switch (integer_or_float(buffer, &val)) {
504 *when = (val.fp - 40587.5) * 86400;
507 fprintf(stderr, "Strange julian date: %s", buffer);
513 * Uemis doesn't know time zones. You need to do them as
514 * minutes, not hours.
516 * But that's ok, we don't track timezones yet either. We
517 * just turn everything into "localtime expressed as UTC".
519 static void uemis_time_zone(char *buffer, void *_when)
521 time_t *when = _when;
522 signed char tz = atoi(buffer);
527 static int uemis_dive_match(struct dive *dive, const char *name, int len, char *buf)
529 return MATCH(".units.length", uemis_length_unit, &units) ||
530 MATCH(".units.volume", uemis_volume_unit, &units) ||
531 MATCH(".units.pressure", uemis_pressure_unit, &units) ||
532 MATCH(".units.temperature", uemis_temperature_unit, &units) ||
533 MATCH(".units.weight", uemis_weight_unit, &units) ||
534 MATCH(".units.time", uemis_time_unit, &units) ||
535 MATCH(".units.date", uemis_date_unit, &units) ||
536 MATCH(".date_time", uemis_date_time, &dive->when) ||
537 MATCH(".time_zone", uemis_time_zone, &dive->when);
540 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
541 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
543 int len = strlen(name);
545 start_match("dive", name, buf);
546 if (MATCH(".date", divedate, &dive->when))
548 if (MATCH(".time", divetime, &dive->when))
550 if (MATCH(".datetime", divedatetime, &dive->when))
552 if (MATCH(".maxdepth", depth, &dive->maxdepth))
554 if (MATCH(".meandepth", depth, &dive->meandepth))
556 if (MATCH(".duration", duration, &dive->duration))
558 if (MATCH(".divetime", duration, &dive->duration))
560 if (MATCH(".divetimesec", duration, &dive->duration))
562 if (MATCH(".surfacetime", duration, &dive->surfacetime))
564 if (MATCH(".airtemp", temperature, &dive->airtemp))
566 if (MATCH(".watertemp", temperature, &dive->watertemp))
568 if (MATCH(".cylinderstartpressure", pressure, &dive->beginning_pressure))
570 if (MATCH(".cylinderendpressure", pressure, &dive->end_pressure))
572 if (MATCH(".location", utf8_string, &dive->location))
574 if (MATCH(".notes", utf8_string, &dive->notes))
577 if (MATCH(".o2", gasmix, &dive->gasmix[gasmix_index].o2))
579 if (MATCH(".n2", gasmix_nitrogen, &dive->gasmix[gasmix_index]))
581 if (MATCH(".he", gasmix, &dive->gasmix[gasmix_index].he))
584 /* Suunto XML files are some crazy sh*t. */
585 if (suunto && suunto_dive_match(dive, name, len, buf))
588 if (uemis && uemis_dive_match(dive, name, len, buf))
591 nonmatch("dive", name, buf);
594 static unsigned int dive_size(int samples)
596 return sizeof(struct dive) + samples*sizeof(struct sample);
600 * File boundaries are dive boundaries. But sometimes there are
601 * multiple dives per file, so there can be other events too that
602 * trigger a "new dive" marker and you may get some nesting due
603 * to that. Just ignore nesting levels.
605 static void dive_start(void)
613 size = dive_size(alloc_samples);
617 memset(dive, 0, size);
618 memset(&tm, 0, sizeof(tm));
621 static char *generate_name(struct dive *dive)
625 char buffer[256], *p;
627 tm = gmtime(&dive->when);
629 len = snprintf(buffer, sizeof(buffer),
633 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
634 tm->tm_hour, tm->tm_min, tm->tm_sec,
635 to_feet(dive->maxdepth), dive->duration.seconds / 60);
639 memcpy(p, buffer, len+1);
643 static void sanitize_gasmix(struct dive *dive)
647 for (i = 0; i < MAX_MIXES; i++) {
648 gasmix_t *mix = dive->gasmix+i;
651 o2 = mix->o2.permille;
652 he = mix->he.permille;
654 /* Regular air: leave empty */
658 /* 20.9% or 21% O2 is just air */
659 if (o2 >= 209 && o2 <= 210) {
660 mix->o2.permille = 0;
666 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
668 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
669 memset(mix, 0, sizeof(*mix));
673 static void dive_end(void)
678 dive->name = generate_name(dive);
679 sanitize_gasmix(dive);
685 static void suunto_start(void)
691 static void suunto_end(void)
696 static void uemis_start(void)
702 static void uemis_end(void)
706 static void event_start(void)
710 static void event_end(void)
715 static void gasmix_start(void)
719 static void gasmix_end(void)
724 static void sample_start(void)
731 if (nr >= alloc_samples) {
734 alloc_samples = (alloc_samples * 3)/2 + 10;
735 size = dive_size(alloc_samples);
736 dive = realloc(dive, size);
740 sample = dive->sample + nr;
741 memset(sample, 0, sizeof(*sample));
745 static void sample_end(void)
750 if (sample->time.seconds > dive->duration.seconds) {
751 if (sample->depth.mm)
752 dive->duration = sample->time;
755 if (sample->depth.mm > dive->maxdepth.mm)
756 dive->maxdepth.mm = sample->depth.mm;
758 if (sample->temperature.mkelvin) {
759 if (!dive->watertemp.mkelvin || dive->watertemp.mkelvin > sample->temperature.mkelvin)
760 dive->watertemp = sample->temperature;
767 static void entry(const char *name, int size, const char *raw)
769 char *buf = malloc(size+1);
773 memcpy(buf, raw, size);
776 try_to_fill_sample(sample, name, buf);
780 try_to_fill_dive(dive, name, buf);
785 static const char *nodename(xmlNode *node, char *buf, int len)
787 if (!node || !node->name)
795 const char *name = node->name;
796 int i = strlen(name);
798 unsigned char c = name[i];
804 if (!node || !node->name)
814 static void visit_one_node(xmlNode *node)
817 const unsigned char *content;
818 char buffer[MAXNAME];
821 content = node->content;
825 /* Trim whitespace at beginning */
826 while (isspace(*content))
829 /* Trim whitespace at end */
830 len = strlen(content);
831 while (len && isspace(content[len-1]))
837 /* Don't print out the node name if it is "text" */
838 if (!strcmp(node->name, "text"))
841 name = nodename(node, buffer, sizeof(buffer));
843 entry(name, len, content);
846 static void traverse(xmlNode *root);
848 static void traverse_properties(xmlNode *node)
852 for (p = node->properties; p; p = p->next)
853 traverse(p->children);
856 static void visit(xmlNode *n)
859 traverse_properties(n);
860 traverse(n->children);
864 * I'm sure this could be done as some fancy DTD rules.
865 * It's just not worth the headache.
867 static struct nesting {
869 void (*start)(void), (*end)(void);
871 { "dive", dive_start, dive_end },
872 { "SUUNTO", suunto_start, suunto_end },
873 { "sample", sample_start, sample_end },
874 { "SAMPLE", sample_start, sample_end },
875 { "reading", sample_start, sample_end },
876 { "event", event_start, event_end },
877 { "gasmix", gasmix_start, gasmix_end },
878 { "pre_dive", uemis_start, uemis_end },
882 static void traverse(xmlNode *root)
886 for (n = root; n; n = n->next) {
887 struct nesting *rule = nesting;
890 if (!strcmp(rule->name, n->name))
893 } while (rule->name);
904 static void reset_all(void)
907 * We reset the units for each file. You'd think it was
908 * a per-dive property, but I'm not going to trust people
909 * to do per-dive setup. If the xml does have per-dive
910 * data within one file, we might have to reset it per
911 * dive for that format.
918 void parse_xml_file(const char *filename)
922 doc = xmlReadFile(filename, NULL, 0);
924 fprintf(stderr, "Failed to parse '%s'.\n", filename);
930 traverse(xmlDocGetRootElement(doc));
936 void parse_xml_init(void)