7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
11 * Some silly typedefs to make our units very explicit.
13 * Also, the units are chosen so that values can be expressible as
14 * integers, so that we never have FP rounding issues. And they
15 * are small enough that converting to/from imperial units doesn't
18 * We also strive to make '0' a meaningless number saying "not
19 * initialized", since many values are things that may not have
20 * been reported (eg tank pressure or temperature from dive
21 * computers that don't support them). But sometimes -1 is an even
22 * more explicit way of saying "not there".
24 * Thus "millibar" for pressure, for example, or "millikelvin" for
25 * temperatures. Doing temperatures in celsius or fahrenheit would
26 * make for loss of precision when converting from one to the other,
27 * and using millikelvin is SI-like but also means that a temperature
28 * of '0' is clearly just a missing temperature or tank pressure.
30 * Also strive to use units that can not possibly be mistaken for a
31 * valid value in a "normal" system without conversion. If the max
32 * depth of a dive is '20000', you probably didn't convert from mm on
33 * output, or if the max depth gets reported as "0.2ft" it was either
34 * a really boring dive, or there was some missing input conversion,
35 * and a 60-ft dive got recorded as 60mm.
37 * Doing these as "structs containing value" means that we always
38 * have to explicitly write out those units in order to get at the
39 * actual value. So there is hopefully little fear of using a value
40 * in millikelvin as Fahrenheit by mistake.
42 * We don't actually use these all yet, so maybe they'll change, but
43 * I made a number of types as guidelines.
87 temperature_t temperature;
88 pressure_t tankpressure;
94 depth_t maxdepth, meandepth;
95 duration_t duration, surfacetime;
97 temperature_t airtemp, watertemp;
98 pressure_t beginning_pressure, end_pressure;
100 struct sample sample[];
103 static void record_dive(struct dive *dive)
108 tm = gmtime(&dive->when);
110 printf("Dive %d with %d samples at %02d:%02d:%02d %04d-%02d-%02d\n",
112 tm->tm_hour, tm->tm_min, tm->tm_sec,
113 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
116 static void nonmatch(const char *type, const char *fullname, const char *name, char *buffer)
118 printf("Unable to match %s '(%.*s)%s' (%s)\n", type,
119 (int) (name - fullname), fullname, name,
124 static const char *last_part(const char *name)
126 const char *p = strrchr(name, '.');
127 return p ? p+1 : name;
130 typedef void (*matchfn_t)(char *buffer, void *);
132 static int match(const char *pattern, const char *name, matchfn_t fn, char *buf, void *data)
134 if (strcasecmp(pattern, name))
141 * Dive info as it is being built up..
143 static int alloc_samples;
144 static struct dive *dive;
145 static struct sample *sample;
148 static time_t utc_mktime(struct tm *tm)
150 static const int mdays[] = {
151 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
153 int year = tm->tm_year;
154 int month = tm->tm_mon;
155 int day = tm->tm_mday;
159 else if (year > 1900)
162 /* Normalized to Jan 1, 1970: unix time */
165 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
167 if (month < 0 || month > 11) /* array bounds */
169 if (month < 2 || (year + 2) % 4)
171 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
173 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
174 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
177 static void divedate(char *buffer, void *_when)
180 time_t *when = _when;
182 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
186 if (tm.tm_sec | tm.tm_min | tm.tm_hour)
187 *when = utc_mktime(&tm);
192 static void divetime(char *buffer, void *_when)
195 time_t *when = _when;
197 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
202 *when = utc_mktime(&tm);
218 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
224 /* Integer or floating point? */
225 val = strtol(buffer, &end, 10);
226 if (val < 0 || end == buffer)
229 /* Looks like it might be floating point? */
232 fp = strtod(buffer, &end);
243 static void pressure(char *buffer, void *_press)
245 pressure_t *pressure = _press;
246 union int_or_float val;
248 switch (integer_or_float(buffer, &val)) {
250 /* Maybe it's in Bar? */
251 if (val.fp < 500.0) {
252 pressure->mbar = val.fp * 1000;
255 printf("Unknown fractional pressure reading %s\n", buffer);
260 * Random integer? Maybe in PSI? Or millibar already?
262 * We assume that 5 bar is a ridiculous tank pressure,
263 * so if it's smaller than 5000, it's in PSI..
266 pressure->mbar = val.i * 68.95;
269 pressure->mbar = val.i;
272 printf("Strange pressure reading %s\n", buffer);
277 static void depth(char *buffer, void *_depth)
279 depth_t *depth = _depth;
280 union int_or_float val;
282 switch (integer_or_float(buffer, &val)) {
283 /* Integer values are probably in feet */
285 depth->mm = 304.8 * val.i;
287 /* Float? Probably meters.. */
289 depth->mm = val.fp * 1000;
292 printf("Strange depth reading %s\n", buffer);
297 static void temperature(char *buffer, void *_temperature)
299 temperature_t *temperature = _temperature;
300 union int_or_float val;
302 switch (integer_or_float(buffer, &val)) {
303 /* C or F? Who knows? Let's default to Celsius */
308 /* Ignore zero. It means "none" */
313 temperature->mkelvin = (val.fp + 273.16) * 1000;
317 if (val.fp < 212.0) {
318 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
321 /* Kelvin or already millikelvin */
324 temperature->mkelvin = val.fp;
327 printf("Strange temperature reading %s\n", buffer);
332 static void sampletime(char *buffer, void *_time)
334 duration_t *time = _time;
335 union int_or_float val;
337 switch (integer_or_float(buffer, &val)) {
338 /* C or F? Who knows? Let's default to Celsius */
340 time->seconds = val.i * 1000;
343 printf("Strange sample time reading %s\n", buffer);
348 /* We're in samples - try to convert the random xml value to something useful */
349 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
351 const char *last = last_part(name);
353 if (match("pressure", last, pressure, buf, &sample->tankpressure))
355 if (match("cylpress", last, pressure, buf, &sample->tankpressure))
357 if (match("depth", last, depth, buf, &sample->depth))
359 if (match("temperature", last, temperature, buf, &sample->temperature))
361 if (match("sampletime", last, sampletime, buf, &sample->time))
363 if (match("time", last, sampletime, buf, &sample->time))
366 nonmatch("sample", name, last, buf);
369 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
370 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
372 const char *last = last_part(name);
374 if (match("date", last, divedate, buf, &dive->when))
376 if (match("time", last, divetime, buf, &dive->when))
378 nonmatch("dive", name, last, buf);
381 static unsigned int dive_size(int samples)
383 return sizeof(struct dive) + samples*sizeof(struct sample);
387 * File boundaries are dive boundaries. But sometimes there are
388 * multiple dives per file, so there can be other events too that
389 * trigger a "new dive" marker and you may get some nesting due
390 * to that. Just ignore nesting levels.
392 static void dive_start(void)
400 size = dive_size(alloc_samples);
404 memset(dive, 0, size);
405 memset(&tm, 0, sizeof(tm));
408 static void dive_end(void)
416 static void sample_start(void)
423 if (nr >= alloc_samples) {
426 alloc_samples = (alloc_samples * 3)/2 + 10;
427 size = dive_size(alloc_samples);
428 dive = realloc(dive, size);
432 sample = dive->sample + nr;
433 memset(sample, 0, sizeof(*sample));
436 static void sample_end(void)
444 static void entry(const char *name, int size, const char *raw)
446 char *buf = malloc(size+1);
450 memcpy(buf, raw, size);
453 try_to_fill_sample(sample, name, buf);
457 try_to_fill_dive(dive, name, buf);
462 static const char *nodename(xmlNode *node, char *buf, int len)
465 if (!node || !node->name)
473 const char *name = node->name;
474 int i = strlen(name);
476 unsigned char c = name[i];
482 if (!node || !node->name)
492 static void visit_one_node(xmlNode *node)
495 const unsigned char *content;
496 char buffer[MAXNAME];
499 content = node->content;
503 /* Trim whitespace at beginning */
504 while (isspace(*content))
507 /* Trim whitespace at end */
508 len = strlen(content);
509 while (len && isspace(content[len-1]))
515 /* Don't print out the node name if it is "text" */
516 if (!strcmp(node->name, "text"))
519 name = nodename(node, buffer, sizeof(buffer));
521 entry(name, len, content);
524 static void traverse(xmlNode *node)
528 for (n = node; n; n = n->next) {
529 /* XML from libdivecomputer: 'dive' per new dive */
530 if (!strcmp(n->name, "dive")) {
532 traverse(n->children);
538 * At least both libdivecomputer and Suunto
541 * Well - almost. Ignore case.
543 if (!strcasecmp(n->name, "sample")) {
545 traverse(n->children);
550 /* Anything else - just visit it and recurse */
552 traverse(n->children);
556 static void parse(const char *filename)
560 doc = xmlReadFile(filename, NULL, 0);
562 fprintf(stderr, "Failed to parse '%s'.\n", filename);
567 traverse(xmlDocGetRootElement(doc));
573 int main(int argc, char **argv)
579 for (i = 1; i < argc; i++)