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;
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;
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.16) * 1000;
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 if (gasmix_index < MAX_MIXES)
330 percent(buffer, _fraction);
334 #define MATCH(pattern, fn, dest) \
335 match(pattern, strlen(pattern), name, len, fn, buf, dest)
337 /* We're in samples - try to convert the random xml value to something useful */
338 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
340 int len = strlen(name);
342 start_match("sample", name, buf);
343 if (MATCH(".sample.pressure", pressure, &sample->tankpressure))
345 if (MATCH(".sample.cylpress", pressure, &sample->tankpressure))
347 if (MATCH(".sample.depth", depth, &sample->depth))
349 if (MATCH(".sample.temperature", temperature, &sample->temperature))
351 if (MATCH(".sample.sampletime", sampletime, &sample->time))
353 if (MATCH(".sample.time", sampletime, &sample->time))
356 nonmatch("sample", name, buf);
360 * Crazy suunto xml. Look at how those o2/he things match up.
362 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
364 return MATCH(".o2pct", percent, &dive->gasmix[0].o2) ||
365 MATCH(".hepct_0", percent, &dive->gasmix[0].he) ||
366 MATCH(".o2pct_2", percent, &dive->gasmix[1].o2) ||
367 MATCH(".hepct_1", percent, &dive->gasmix[1].he) ||
368 MATCH(".o2pct_3", percent, &dive->gasmix[2].o2) ||
369 MATCH(".hepct_2", percent, &dive->gasmix[2].he) ||
370 MATCH(".o2pct_4", percent, &dive->gasmix[3].o2) ||
371 MATCH(".hepct_3", percent, &dive->gasmix[3].he);
374 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
375 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
377 int len = strlen(name);
379 start_match("dive", name, buf);
380 if (MATCH(".date", divedate, &dive->when))
382 if (MATCH(".time", divetime, &dive->when))
384 if (MATCH(".datetime", divedatetime, &dive->when))
386 if (MATCH(".maxdepth", depth, &dive->maxdepth))
388 if (MATCH(".meandepth", depth, &dive->meandepth))
390 if (MATCH(".divetime", duration, &dive->duration))
392 if (MATCH(".divetimesec", duration, &dive->duration))
394 if (MATCH(".surfacetime", duration, &dive->surfacetime))
396 if (MATCH(".airtemp", temperature, &dive->airtemp))
398 if (MATCH(".watertemp", temperature, &dive->watertemp))
400 if (MATCH(".cylinderstartpressure", pressure, &dive->beginning_pressure))
402 if (MATCH(".cylinderendpressure", pressure, &dive->end_pressure))
405 if (MATCH(".o2", gasmix, &dive->gasmix[gasmix_index].o2))
407 if (MATCH(".n2", gasmix, &dive->gasmix[gasmix_index].n2))
409 if (MATCH(".he", gasmix, &dive->gasmix[gasmix_index].he))
412 /* Suunto XML files are some crazy sh*t. */
413 if (suunto && suunto_dive_match(dive, name, len, buf))
416 nonmatch("dive", name, buf);
419 static unsigned int dive_size(int samples)
421 return sizeof(struct dive) + samples*sizeof(struct sample);
425 * File boundaries are dive boundaries. But sometimes there are
426 * multiple dives per file, so there can be other events too that
427 * trigger a "new dive" marker and you may get some nesting due
428 * to that. Just ignore nesting levels.
430 static void dive_start(void)
438 size = dive_size(alloc_samples);
442 memset(dive, 0, size);
443 memset(&tm, 0, sizeof(tm));
446 static char *generate_name(struct dive *dive)
450 char buffer[256], *p;
452 tm = gmtime(&dive->when);
454 len = snprintf(buffer, sizeof(buffer),
458 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
459 tm->tm_hour, tm->tm_min, tm->tm_sec,
460 to_feet(dive->maxdepth), dive->duration.seconds / 60);
464 memcpy(p, buffer, len+1);
468 static void dive_end(void)
473 dive->name = generate_name(dive);
479 static void suunto_start(void)
484 static void suunto_end(void)
489 static void event_start(void)
493 static void event_end(void)
498 static void gasmix_start(void)
502 static void gasmix_end(void)
507 static void sample_start(void)
514 if (nr >= alloc_samples) {
517 alloc_samples = (alloc_samples * 3)/2 + 10;
518 size = dive_size(alloc_samples);
519 dive = realloc(dive, size);
523 sample = dive->sample + nr;
524 memset(sample, 0, sizeof(*sample));
528 static void sample_end(void)
533 if (sample->time.seconds > dive->duration.seconds) {
534 if (sample->depth.mm)
535 dive->duration = sample->time;
538 if (sample->depth.mm > dive->maxdepth.mm)
539 dive->maxdepth.mm = sample->depth.mm;
545 static void entry(const char *name, int size, const char *raw)
547 char *buf = malloc(size+1);
551 memcpy(buf, raw, size);
554 try_to_fill_sample(sample, name, buf);
558 try_to_fill_dive(dive, name, buf);
563 static const char *nodename(xmlNode *node, char *buf, int len)
565 if (!node || !node->name)
573 const char *name = node->name;
574 int i = strlen(name);
576 unsigned char c = name[i];
582 if (!node || !node->name)
592 static void visit_one_node(xmlNode *node)
595 const unsigned char *content;
596 char buffer[MAXNAME];
599 content = node->content;
603 /* Trim whitespace at beginning */
604 while (isspace(*content))
607 /* Trim whitespace at end */
608 len = strlen(content);
609 while (len && isspace(content[len-1]))
615 /* Don't print out the node name if it is "text" */
616 if (!strcmp(node->name, "text"))
619 name = nodename(node, buffer, sizeof(buffer));
621 entry(name, len, content);
624 static void traverse(xmlNode *root);
626 static void traverse_properties(xmlNode *node)
630 for (p = node->properties; p; p = p->next)
631 traverse(p->children);
634 static void visit(xmlNode *n)
637 traverse_properties(n);
638 traverse(n->children);
642 * I'm sure this could be done as some fancy DTD rules.
643 * It's just not worth the headache.
645 static struct nesting {
647 void (*start)(void), (*end)(void);
649 { "dive", dive_start, dive_end },
650 { "SUUNTO", suunto_start, suunto_end },
651 { "sample", sample_start, sample_end },
652 { "SAMPLE", sample_start, sample_end },
653 { "event", event_start, event_end },
654 { "gasmix", gasmix_start, gasmix_end },
658 static void traverse(xmlNode *root)
662 for (n = root; n; n = n->next) {
663 struct nesting *rule = nesting;
666 if (!strcmp(rule->name, n->name))
669 } while (rule->name);
679 void parse_xml_file(const char *filename)
683 doc = xmlReadFile(filename, NULL, 0);
685 fprintf(stderr, "Failed to parse '%s'.\n", filename);
690 traverse(xmlDocGetRootElement(doc));
696 void parse_xml_init(void)