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 time_t utc_mktime(struct tm *tm)
75 static const int mdays[] = {
76 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
78 int year = tm->tm_year;
79 int month = tm->tm_mon;
80 int day = tm->tm_mday;
82 /* First normalize relative to 1900 */
88 /* Normalized to Jan 1, 1970: unix time */
91 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
93 if (month < 0 || month > 11) /* array bounds */
95 if (month < 2 || (year + 2) % 4)
97 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
99 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
100 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
103 static void divedate(char *buffer, void *_when)
106 time_t *when = _when;
108 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
112 if (tm.tm_sec | tm.tm_min | tm.tm_hour)
113 *when = utc_mktime(&tm);
118 static void divetime(char *buffer, void *_when)
121 time_t *when = _when;
123 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
128 *when = utc_mktime(&tm);
133 /* Libdivecomputer: "2011-03-20 10:22:38" */
134 static void divedatetime(char *buffer, void *_when)
138 time_t *when = _when;
140 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
141 &y, &m, &d, &hr, &min, &sec) == 6) {
148 *when = utc_mktime(&tm);
164 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
170 /* Integer or floating point? */
171 val = strtol(buffer, &end, 10);
172 if (val < 0 || end == buffer)
175 /* Looks like it might be floating point? */
178 fp = strtod(buffer, &end);
189 static void pressure(char *buffer, void *_press)
191 pressure_t *pressure = _press;
192 union int_or_float val;
194 switch (integer_or_float(buffer, &val)) {
196 /* Maybe it's in Bar? */
197 if (val.fp < 500.0) {
198 pressure->mbar = val.fp * 1000;
201 printf("Unknown fractional pressure reading %s\n", buffer);
206 * Random integer? Maybe in PSI? Or millibar already?
208 * We assume that 5 bar is a ridiculous tank pressure,
209 * so if it's smaller than 5000, it's in PSI..
212 pressure->mbar = val.i * 68.95;
215 pressure->mbar = val.i;
218 printf("Strange pressure reading %s\n", buffer);
223 static void depth(char *buffer, void *_depth)
225 depth_t *depth = _depth;
226 union int_or_float val;
228 switch (integer_or_float(buffer, &val)) {
229 /* All values are probably in meters */
234 depth->mm = val.fp * 1000;
237 printf("Strange depth reading %s\n", buffer);
242 static void temperature(char *buffer, void *_temperature)
244 temperature_t *temperature = _temperature;
245 union int_or_float val;
247 switch (integer_or_float(buffer, &val)) {
248 /* C or F? Who knows? Let's default to Celsius */
253 /* Ignore zero. It means "none" */
258 temperature->mkelvin = (val.fp + 273.16) * 1000;
262 if (val.fp < 212.0) {
263 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
266 /* Kelvin or already millikelvin */
269 temperature->mkelvin = val.fp;
272 printf("Strange temperature reading %s\n", buffer);
277 static void sampletime(char *buffer, void *_time)
281 duration_t *time = _time;
283 i = sscanf(buffer, "%d:%d", &min, &sec);
290 time->seconds = sec + min*60;
293 printf("Strange sample time reading %s\n", buffer);
298 static void duration(char *buffer, void *_time)
300 sampletime(buffer, _time);
303 static void ignore(char *buffer, void *_time)
307 #define MATCH(pattern, fn, dest) \
308 match(pattern, strlen(pattern), name, len, fn, buf, dest)
310 /* We're in samples - try to convert the random xml value to something useful */
311 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
313 int len = strlen(name);
315 start_match("sample", name, buf);
316 if (MATCH(".sample.pressure", pressure, &sample->tankpressure))
318 if (MATCH(".sample.cylpress", pressure, &sample->tankpressure))
320 if (MATCH(".sample.depth", depth, &sample->depth))
322 if (MATCH(".sample.temperature", temperature, &sample->temperature))
324 if (MATCH(".sample.sampletime", sampletime, &sample->time))
326 if (MATCH(".sample.time", sampletime, &sample->time))
329 nonmatch("sample", name, buf);
332 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
333 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
335 int len = strlen(name);
337 start_match("dive", name, buf);
338 if (MATCH(".date", divedate, &dive->when))
340 if (MATCH(".time", divetime, &dive->when))
342 if (MATCH(".datetime", divedatetime, &dive->when))
344 if (MATCH(".maxdepth", depth, &dive->maxdepth))
346 if (MATCH(".meandepth", depth, &dive->meandepth))
348 if (MATCH(".divetime", duration, &dive->duration))
350 if (MATCH(".divetimesec", duration, &dive->duration))
352 if (MATCH(".surfacetime", duration, &dive->surfacetime))
354 if (MATCH(".airtemp", temperature, &dive->airtemp))
356 if (MATCH(".watertemp", temperature, &dive->watertemp))
358 if (MATCH(".cylinderstartpressure", pressure, &dive->beginning_pressure))
360 if (MATCH(".cylinderendpressure", pressure, &dive->end_pressure))
362 if (MATCH(".divenumber", ignore, NULL))
364 if (MATCH(".diveseries", ignore, NULL))
366 if (MATCH(".number", ignore, NULL))
368 if (MATCH(".size", ignore, NULL))
370 if (MATCH(".fingerprint", ignore, NULL))
372 nonmatch("dive", name, buf);
375 static unsigned int dive_size(int samples)
377 return sizeof(struct dive) + samples*sizeof(struct sample);
381 * File boundaries are dive boundaries. But sometimes there are
382 * multiple dives per file, so there can be other events too that
383 * trigger a "new dive" marker and you may get some nesting due
384 * to that. Just ignore nesting levels.
386 static void dive_start(void)
394 size = dive_size(alloc_samples);
398 memset(dive, 0, size);
399 memset(&tm, 0, sizeof(tm));
402 static char *generate_name(struct dive *dive)
406 char buffer[256], *p;
408 tm = gmtime(&dive->when);
410 len = snprintf(buffer, sizeof(buffer),
414 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
415 tm->tm_hour, tm->tm_min, tm->tm_sec,
416 to_feet(dive->maxdepth), dive->duration.seconds / 60);
420 memcpy(p, buffer, len+1);
424 static void dive_end(void)
429 dive->name = generate_name(dive);
434 static void sample_start(void)
441 if (nr >= alloc_samples) {
444 alloc_samples = (alloc_samples * 3)/2 + 10;
445 size = dive_size(alloc_samples);
446 dive = realloc(dive, size);
450 sample = dive->sample + nr;
451 memset(sample, 0, sizeof(*sample));
454 static void sample_end(void)
459 if (sample->time.seconds > dive->duration.seconds) {
460 if (sample->depth.mm)
461 dive->duration = sample->time;
464 if (sample->depth.mm > dive->maxdepth.mm)
465 dive->maxdepth.mm = sample->depth.mm;
471 static void entry(const char *name, int size, const char *raw)
473 char *buf = malloc(size+1);
477 memcpy(buf, raw, size);
480 try_to_fill_sample(sample, name, buf);
484 try_to_fill_dive(dive, name, buf);
489 static const char *nodename(xmlNode *node, char *buf, int len)
491 if (!node || !node->name)
499 const char *name = node->name;
500 int i = strlen(name);
502 unsigned char c = name[i];
508 if (!node || !node->name)
518 static void visit_one_node(xmlNode *node)
521 const unsigned char *content;
522 char buffer[MAXNAME];
525 content = node->content;
529 /* Trim whitespace at beginning */
530 while (isspace(*content))
533 /* Trim whitespace at end */
534 len = strlen(content);
535 while (len && isspace(content[len-1]))
541 /* Don't print out the node name if it is "text" */
542 if (!strcmp(node->name, "text"))
545 name = nodename(node, buffer, sizeof(buffer));
547 entry(name, len, content);
550 static void traverse(xmlNode *root);
552 static void traverse_properties(xmlNode *node)
556 for (p = node->properties; p; p = p->next)
557 traverse(p->children);
560 static void visit(xmlNode *n)
563 traverse_properties(n);
564 traverse(n->children);
567 static void traverse(xmlNode *root)
571 for (n = root; n; n = n->next) {
572 /* XML from libdivecomputer: 'dive' per new dive */
573 if (!strcmp(n->name, "dive")) {
581 * At least both libdivecomputer and Suunto
584 * Well - almost. Ignore case.
586 if (!strcasecmp(n->name, "sample")) {
593 /* Anything else - just visit it and recurse */
598 void parse_xml_file(const char *filename)
602 doc = xmlReadFile(filename, NULL, 0);
604 fprintf(stderr, "Failed to parse '%s'.\n", filename);
609 traverse(xmlDocGetRootElement(doc));
615 void parse_xml_init(void)