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;
111 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
112 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
116 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
121 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
126 *when = utc_mktime(&tm);
131 static void divetime(char *buffer, void *_when)
134 time_t *when = _when;
136 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
141 *when = utc_mktime(&tm);
146 /* Libdivecomputer: "2011-03-20 10:22:38" */
147 static void divedatetime(char *buffer, void *_when)
151 time_t *when = _when;
153 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
154 &y, &m, &d, &hr, &min, &sec) == 6) {
161 *when = utc_mktime(&tm);
177 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
183 /* Integer or floating point? */
184 val = strtol(buffer, &end, 10);
185 if (val < 0 || end == buffer)
188 /* Looks like it might be floating point? */
191 fp = strtod(buffer, &end);
202 static void pressure(char *buffer, void *_press)
204 pressure_t *pressure = _press;
205 union int_or_float val;
207 switch (integer_or_float(buffer, &val)) {
209 /* Maybe it's in Bar? */
210 if (val.fp < 500.0) {
211 pressure->mbar = val.fp * 1000 + 0.5;
214 printf("Unknown fractional pressure reading %s\n", buffer);
219 * Random integer? Maybe in PSI? Or millibar already?
221 * We assume that 5 bar is a ridiculous tank pressure,
222 * so if it's smaller than 5000, it's in PSI..
225 pressure->mbar = val.i * 68.95;
228 pressure->mbar = val.i;
231 printf("Strange pressure reading %s\n", buffer);
236 static void depth(char *buffer, void *_depth)
238 depth_t *depth = _depth;
239 union int_or_float val;
241 switch (integer_or_float(buffer, &val)) {
242 /* All values are probably in meters */
247 depth->mm = val.fp * 1000 + 0.5;
250 printf("Strange depth reading %s\n", buffer);
255 static void temperature(char *buffer, void *_temperature)
257 temperature_t *temperature = _temperature;
258 union int_or_float val;
260 switch (integer_or_float(buffer, &val)) {
261 /* C or F? Who knows? Let's default to Celsius */
266 /* Ignore zero. It means "none" */
271 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
275 if (val.fp < 212.0) {
276 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
279 /* Kelvin or already millikelvin */
282 temperature->mkelvin = val.fp;
285 printf("Strange temperature reading %s\n", buffer);
290 static void sampletime(char *buffer, void *_time)
294 duration_t *time = _time;
296 i = sscanf(buffer, "%d:%d", &min, &sec);
303 time->seconds = sec + min*60;
306 printf("Strange sample time reading %s\n", buffer);
311 static void duration(char *buffer, void *_time)
313 sampletime(buffer, _time);
316 static void percent(char *buffer, void *_fraction)
318 fraction_t *fraction = _fraction;
319 union int_or_float val;
321 switch (integer_or_float(buffer, &val)) {
322 /* C or F? Who knows? Let's default to Celsius */
328 fraction->permille = val.fp * 10 + 0.5;
332 printf("Strange percentage reading %s\n", buffer);
338 static void gasmix(char *buffer, void *_fraction)
340 /* libdivecomputer does negative percentages. */
343 if (gasmix_index < MAX_MIXES)
344 percent(buffer, _fraction);
347 static void gasmix_nitrogen(char *buffer, void *_gasmix)
349 /* Ignore n2 percentages. There's no value in them. */
352 #define MATCH(pattern, fn, dest) \
353 match(pattern, strlen(pattern), name, len, fn, buf, dest)
355 /* We're in samples - try to convert the random xml value to something useful */
356 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
358 int len = strlen(name);
360 start_match("sample", name, buf);
361 if (MATCH(".sample.pressure", pressure, &sample->tankpressure))
363 if (MATCH(".sample.cylpress", pressure, &sample->tankpressure))
365 if (MATCH(".sample.depth", depth, &sample->depth))
367 if (MATCH(".sample.temp", temperature, &sample->temperature))
369 if (MATCH(".sample.temperature", temperature, &sample->temperature))
371 if (MATCH(".sample.sampletime", sampletime, &sample->time))
373 if (MATCH(".sample.time", sampletime, &sample->time))
376 nonmatch("sample", name, buf);
380 * Crazy suunto xml. Look at how those o2/he things match up.
382 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
384 return MATCH(".o2pct", percent, &dive->gasmix[0].o2) ||
385 MATCH(".hepct_0", percent, &dive->gasmix[0].he) ||
386 MATCH(".o2pct_2", percent, &dive->gasmix[1].o2) ||
387 MATCH(".hepct_1", percent, &dive->gasmix[1].he) ||
388 MATCH(".o2pct_3", percent, &dive->gasmix[2].o2) ||
389 MATCH(".hepct_2", percent, &dive->gasmix[2].he) ||
390 MATCH(".o2pct_4", percent, &dive->gasmix[3].o2) ||
391 MATCH(".hepct_3", percent, &dive->gasmix[3].he);
394 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
395 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
397 int len = strlen(name);
399 start_match("dive", name, buf);
400 if (MATCH(".date", divedate, &dive->when))
402 if (MATCH(".time", divetime, &dive->when))
404 if (MATCH(".datetime", divedatetime, &dive->when))
406 if (MATCH(".maxdepth", depth, &dive->maxdepth))
408 if (MATCH(".meandepth", depth, &dive->meandepth))
410 if (MATCH(".duration", duration, &dive->duration))
412 if (MATCH(".divetime", duration, &dive->duration))
414 if (MATCH(".divetimesec", duration, &dive->duration))
416 if (MATCH(".surfacetime", duration, &dive->surfacetime))
418 if (MATCH(".airtemp", temperature, &dive->airtemp))
420 if (MATCH(".watertemp", temperature, &dive->watertemp))
422 if (MATCH(".cylinderstartpressure", pressure, &dive->beginning_pressure))
424 if (MATCH(".cylinderendpressure", pressure, &dive->end_pressure))
427 if (MATCH(".o2", gasmix, &dive->gasmix[gasmix_index].o2))
429 if (MATCH(".n2", gasmix_nitrogen, &dive->gasmix[gasmix_index]))
431 if (MATCH(".he", gasmix, &dive->gasmix[gasmix_index].he))
434 /* Suunto XML files are some crazy sh*t. */
435 if (suunto && suunto_dive_match(dive, name, len, buf))
438 nonmatch("dive", name, buf);
441 static unsigned int dive_size(int samples)
443 return sizeof(struct dive) + samples*sizeof(struct sample);
447 * File boundaries are dive boundaries. But sometimes there are
448 * multiple dives per file, so there can be other events too that
449 * trigger a "new dive" marker and you may get some nesting due
450 * to that. Just ignore nesting levels.
452 static void dive_start(void)
460 size = dive_size(alloc_samples);
464 memset(dive, 0, size);
465 memset(&tm, 0, sizeof(tm));
468 static char *generate_name(struct dive *dive)
472 char buffer[256], *p;
474 tm = gmtime(&dive->when);
476 len = snprintf(buffer, sizeof(buffer),
480 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
481 tm->tm_hour, tm->tm_min, tm->tm_sec,
482 to_feet(dive->maxdepth), dive->duration.seconds / 60);
486 memcpy(p, buffer, len+1);
490 static void sanitize_gasmix(struct dive *dive)
494 for (i = 0; i < MAX_MIXES; i++) {
495 gasmix_t *mix = dive->gasmix+i;
498 o2 = mix->o2.permille;
499 he = mix->he.permille;
501 /* Regular air: leave empty */
505 /* 20.9% or 21% O2 is just air */
506 if (o2 >= 209 && o2 <= 210) {
507 mix->o2.permille = 0;
513 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
515 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
516 memset(mix, 0, sizeof(*mix));
520 static void dive_end(void)
525 dive->name = generate_name(dive);
526 sanitize_gasmix(dive);
532 static void suunto_start(void)
537 static void suunto_end(void)
542 static void event_start(void)
546 static void event_end(void)
551 static void gasmix_start(void)
555 static void gasmix_end(void)
560 static void sample_start(void)
567 if (nr >= alloc_samples) {
570 alloc_samples = (alloc_samples * 3)/2 + 10;
571 size = dive_size(alloc_samples);
572 dive = realloc(dive, size);
576 sample = dive->sample + nr;
577 memset(sample, 0, sizeof(*sample));
581 static void sample_end(void)
586 if (sample->time.seconds > dive->duration.seconds) {
587 if (sample->depth.mm)
588 dive->duration = sample->time;
591 if (sample->depth.mm > dive->maxdepth.mm)
592 dive->maxdepth.mm = sample->depth.mm;
598 static void entry(const char *name, int size, const char *raw)
600 char *buf = malloc(size+1);
604 memcpy(buf, raw, size);
607 try_to_fill_sample(sample, name, buf);
611 try_to_fill_dive(dive, name, buf);
616 static const char *nodename(xmlNode *node, char *buf, int len)
618 if (!node || !node->name)
626 const char *name = node->name;
627 int i = strlen(name);
629 unsigned char c = name[i];
635 if (!node || !node->name)
645 static void visit_one_node(xmlNode *node)
648 const unsigned char *content;
649 char buffer[MAXNAME];
652 content = node->content;
656 /* Trim whitespace at beginning */
657 while (isspace(*content))
660 /* Trim whitespace at end */
661 len = strlen(content);
662 while (len && isspace(content[len-1]))
668 /* Don't print out the node name if it is "text" */
669 if (!strcmp(node->name, "text"))
672 name = nodename(node, buffer, sizeof(buffer));
674 entry(name, len, content);
677 static void traverse(xmlNode *root);
679 static void traverse_properties(xmlNode *node)
683 for (p = node->properties; p; p = p->next)
684 traverse(p->children);
687 static void visit(xmlNode *n)
690 traverse_properties(n);
691 traverse(n->children);
695 * I'm sure this could be done as some fancy DTD rules.
696 * It's just not worth the headache.
698 static struct nesting {
700 void (*start)(void), (*end)(void);
702 { "dive", dive_start, dive_end },
703 { "SUUNTO", suunto_start, suunto_end },
704 { "sample", sample_start, sample_end },
705 { "SAMPLE", sample_start, sample_end },
706 { "event", event_start, event_end },
707 { "gasmix", gasmix_start, gasmix_end },
711 static void traverse(xmlNode *root)
715 for (n = root; n; n = n->next) {
716 struct nesting *rule = nesting;
719 if (!strcmp(rule->name, n->name))
722 } while (rule->name);
732 void parse_xml_file(const char *filename)
736 doc = xmlReadFile(filename, NULL, 0);
738 fprintf(stderr, "Failed to parse '%s'.\n", filename);
743 traverse(xmlDocGetRootElement(doc));
749 void parse_xml_init(void)