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 * Dive info as it is being built up..
68 static int alloc_samples;
69 static struct dive *dive;
70 static struct sample *sample;
73 static int event_index, gasmix_index;
75 static time_t utc_mktime(struct tm *tm)
77 static const int mdays[] = {
78 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
80 int year = tm->tm_year;
81 int month = tm->tm_mon;
82 int day = tm->tm_mday;
84 /* First normalize relative to 1900 */
90 /* Normalized to Jan 1, 1970: unix time */
93 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
95 if (month < 0 || month > 11) /* array bounds */
97 if (month < 2 || (year + 2) % 4)
99 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
101 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
102 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
105 static void divedate(char *buffer, void *_when)
108 time_t *when = _when;
110 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
114 if (tm.tm_sec | tm.tm_min | tm.tm_hour)
115 *when = utc_mktime(&tm);
120 static void divetime(char *buffer, void *_when)
123 time_t *when = _when;
125 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
130 *when = utc_mktime(&tm);
135 /* Libdivecomputer: "2011-03-20 10:22:38" */
136 static void divedatetime(char *buffer, void *_when)
140 time_t *when = _when;
142 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
143 &y, &m, &d, &hr, &min, &sec) == 6) {
150 *when = utc_mktime(&tm);
166 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
172 /* Integer or floating point? */
173 val = strtol(buffer, &end, 10);
174 if (val < 0 || end == buffer)
177 /* Looks like it might be floating point? */
180 fp = strtod(buffer, &end);
191 static void pressure(char *buffer, void *_press)
193 pressure_t *pressure = _press;
194 union int_or_float val;
196 switch (integer_or_float(buffer, &val)) {
198 /* Maybe it's in Bar? */
199 if (val.fp < 500.0) {
200 pressure->mbar = val.fp * 1000 + 0.5;
203 printf("Unknown fractional pressure reading %s\n", buffer);
208 * Random integer? Maybe in PSI? Or millibar already?
210 * We assume that 5 bar is a ridiculous tank pressure,
211 * so if it's smaller than 5000, it's in PSI..
214 pressure->mbar = val.i * 68.95;
217 pressure->mbar = val.i;
220 printf("Strange pressure reading %s\n", buffer);
225 static void depth(char *buffer, void *_depth)
227 depth_t *depth = _depth;
228 union int_or_float val;
230 switch (integer_or_float(buffer, &val)) {
231 /* All values are probably in meters */
236 depth->mm = val.fp * 1000 + 0.5;
239 printf("Strange depth reading %s\n", buffer);
244 static void temperature(char *buffer, void *_temperature)
246 temperature_t *temperature = _temperature;
247 union int_or_float val;
249 switch (integer_or_float(buffer, &val)) {
250 /* C or F? Who knows? Let's default to Celsius */
255 /* Ignore zero. It means "none" */
260 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
264 if (val.fp < 212.0) {
265 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
268 /* Kelvin or already millikelvin */
271 temperature->mkelvin = val.fp;
274 printf("Strange temperature reading %s\n", buffer);
279 static void sampletime(char *buffer, void *_time)
283 duration_t *time = _time;
285 i = sscanf(buffer, "%d:%d", &min, &sec);
292 time->seconds = sec + min*60;
295 printf("Strange sample time reading %s\n", buffer);
300 static void duration(char *buffer, void *_time)
302 sampletime(buffer, _time);
305 static void percent(char *buffer, void *_fraction)
307 fraction_t *fraction = _fraction;
308 union int_or_float val;
310 switch (integer_or_float(buffer, &val)) {
311 /* C or F? Who knows? Let's default to Celsius */
317 fraction->permille = val.fp * 10 + 0.5;
321 printf("Strange percentage reading %s\n", buffer);
327 static void gasmix(char *buffer, void *_fraction)
329 /* libdivecomputer does negative percentages. */
332 if (gasmix_index < MAX_MIXES)
333 percent(buffer, _fraction);
336 static void gasmix_nitrogen(char *buffer, void *_gasmix)
338 /* Ignore n2 percentages. There's no value in them. */
341 #define MATCH(pattern, fn, dest) \
342 match(pattern, strlen(pattern), name, len, fn, buf, dest)
344 /* We're in samples - try to convert the random xml value to something useful */
345 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
347 int len = strlen(name);
349 start_match("sample", name, buf);
350 if (MATCH(".sample.pressure", pressure, &sample->tankpressure))
352 if (MATCH(".sample.cylpress", pressure, &sample->tankpressure))
354 if (MATCH(".sample.depth", depth, &sample->depth))
356 if (MATCH(".sample.temp", temperature, &sample->temperature))
358 if (MATCH(".sample.temperature", temperature, &sample->temperature))
360 if (MATCH(".sample.sampletime", sampletime, &sample->time))
362 if (MATCH(".sample.time", sampletime, &sample->time))
365 nonmatch("sample", name, buf);
369 * Crazy suunto xml. Look at how those o2/he things match up.
371 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
373 return MATCH(".o2pct", percent, &dive->gasmix[0].o2) ||
374 MATCH(".hepct_0", percent, &dive->gasmix[0].he) ||
375 MATCH(".o2pct_2", percent, &dive->gasmix[1].o2) ||
376 MATCH(".hepct_1", percent, &dive->gasmix[1].he) ||
377 MATCH(".o2pct_3", percent, &dive->gasmix[2].o2) ||
378 MATCH(".hepct_2", percent, &dive->gasmix[2].he) ||
379 MATCH(".o2pct_4", percent, &dive->gasmix[3].o2) ||
380 MATCH(".hepct_3", percent, &dive->gasmix[3].he);
383 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
384 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
386 int len = strlen(name);
388 start_match("dive", name, buf);
389 if (MATCH(".date", divedate, &dive->when))
391 if (MATCH(".time", divetime, &dive->when))
393 if (MATCH(".datetime", divedatetime, &dive->when))
395 if (MATCH(".maxdepth", depth, &dive->maxdepth))
397 if (MATCH(".meandepth", depth, &dive->meandepth))
399 if (MATCH(".divetime", duration, &dive->duration))
401 if (MATCH(".divetimesec", duration, &dive->duration))
403 if (MATCH(".surfacetime", duration, &dive->surfacetime))
405 if (MATCH(".airtemp", temperature, &dive->airtemp))
407 if (MATCH(".watertemp", temperature, &dive->watertemp))
409 if (MATCH(".cylinderstartpressure", pressure, &dive->beginning_pressure))
411 if (MATCH(".cylinderendpressure", pressure, &dive->end_pressure))
414 if (MATCH(".o2", gasmix, &dive->gasmix[gasmix_index].o2))
416 if (MATCH(".n2", gasmix_nitrogen, &dive->gasmix[gasmix_index]))
418 if (MATCH(".he", gasmix, &dive->gasmix[gasmix_index].he))
421 /* Suunto XML files are some crazy sh*t. */
422 if (suunto && suunto_dive_match(dive, name, len, buf))
425 nonmatch("dive", name, buf);
428 static unsigned int dive_size(int samples)
430 return sizeof(struct dive) + samples*sizeof(struct sample);
434 * File boundaries are dive boundaries. But sometimes there are
435 * multiple dives per file, so there can be other events too that
436 * trigger a "new dive" marker and you may get some nesting due
437 * to that. Just ignore nesting levels.
439 static void dive_start(void)
447 size = dive_size(alloc_samples);
451 memset(dive, 0, size);
452 memset(&tm, 0, sizeof(tm));
455 static char *generate_name(struct dive *dive)
459 char buffer[256], *p;
461 tm = gmtime(&dive->when);
463 len = snprintf(buffer, sizeof(buffer),
467 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
468 tm->tm_hour, tm->tm_min, tm->tm_sec,
469 to_feet(dive->maxdepth), dive->duration.seconds / 60);
473 memcpy(p, buffer, len+1);
477 static void sanitize_gasmix(struct dive *dive)
481 for (i = 0; i < MAX_MIXES; i++) {
482 gasmix_t *mix = dive->gasmix+i;
485 o2 = mix->o2.permille;
486 he = mix->he.permille;
488 /* Regular air: leave empty */
492 /* 20.9% or 21% O2 is just air */
493 if (o2 >= 209 && o2 <= 210) {
494 mix->o2.permille = 0;
500 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
502 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
503 memset(mix, 0, sizeof(*mix));
507 static void dive_end(void)
512 dive->name = generate_name(dive);
513 sanitize_gasmix(dive);
519 static void suunto_start(void)
524 static void suunto_end(void)
529 static void event_start(void)
533 static void event_end(void)
538 static void gasmix_start(void)
542 static void gasmix_end(void)
547 static void sample_start(void)
554 if (nr >= alloc_samples) {
557 alloc_samples = (alloc_samples * 3)/2 + 10;
558 size = dive_size(alloc_samples);
559 dive = realloc(dive, size);
563 sample = dive->sample + nr;
564 memset(sample, 0, sizeof(*sample));
568 static void sample_end(void)
573 if (sample->time.seconds > dive->duration.seconds) {
574 if (sample->depth.mm)
575 dive->duration = sample->time;
578 if (sample->depth.mm > dive->maxdepth.mm)
579 dive->maxdepth.mm = sample->depth.mm;
585 static void entry(const char *name, int size, const char *raw)
587 char *buf = malloc(size+1);
591 memcpy(buf, raw, size);
594 try_to_fill_sample(sample, name, buf);
598 try_to_fill_dive(dive, name, buf);
603 static const char *nodename(xmlNode *node, char *buf, int len)
605 if (!node || !node->name)
613 const char *name = node->name;
614 int i = strlen(name);
616 unsigned char c = name[i];
622 if (!node || !node->name)
632 static void visit_one_node(xmlNode *node)
635 const unsigned char *content;
636 char buffer[MAXNAME];
639 content = node->content;
643 /* Trim whitespace at beginning */
644 while (isspace(*content))
647 /* Trim whitespace at end */
648 len = strlen(content);
649 while (len && isspace(content[len-1]))
655 /* Don't print out the node name if it is "text" */
656 if (!strcmp(node->name, "text"))
659 name = nodename(node, buffer, sizeof(buffer));
661 entry(name, len, content);
664 static void traverse(xmlNode *root);
666 static void traverse_properties(xmlNode *node)
670 for (p = node->properties; p; p = p->next)
671 traverse(p->children);
674 static void visit(xmlNode *n)
677 traverse_properties(n);
678 traverse(n->children);
682 * I'm sure this could be done as some fancy DTD rules.
683 * It's just not worth the headache.
685 static struct nesting {
687 void (*start)(void), (*end)(void);
689 { "dive", dive_start, dive_end },
690 { "SUUNTO", suunto_start, suunto_end },
691 { "sample", sample_start, sample_end },
692 { "SAMPLE", sample_start, sample_end },
693 { "event", event_start, event_end },
694 { "gasmix", gasmix_start, gasmix_end },
698 static void traverse(xmlNode *root)
702 for (n = root; n; n = n->next) {
703 struct nesting *rule = nesting;
706 if (!strcmp(rule->name, n->name))
709 } while (rule->name);
719 void parse_xml_file(const char *filename)
723 doc = xmlReadFile(filename, NULL, 0);
725 fprintf(stderr, "Failed to parse '%s'.\n", filename);
730 traverse(xmlDocGetRootElement(doc));
736 void parse_xml_init(void)