7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
13 * Some silly typedefs to make our units very explicit.
15 * Also, the units are chosen so that values can be expressible as
16 * integers, so that we never have FP rounding issues. And they
17 * are small enough that converting to/from imperial units doesn't
20 * We also strive to make '0' a meaningless number saying "not
21 * initialized", since many values are things that may not have
22 * been reported (eg tank pressure or temperature from dive
23 * computers that don't support them). But sometimes -1 is an even
24 * more explicit way of saying "not there".
26 * Thus "millibar" for pressure, for example, or "millikelvin" for
27 * temperatures. Doing temperatures in celsius or fahrenheit would
28 * make for loss of precision when converting from one to the other,
29 * and using millikelvin is SI-like but also means that a temperature
30 * of '0' is clearly just a missing temperature or tank pressure.
32 * Also strive to use units that can not possibly be mistaken for a
33 * valid value in a "normal" system without conversion. If the max
34 * depth of a dive is '20000', you probably didn't convert from mm on
35 * output, or if the max depth gets reported as "0.2ft" it was either
36 * a really boring dive, or there was some missing input conversion,
37 * and a 60-ft dive got recorded as 60mm.
39 * Doing these as "structs containing value" means that we always
40 * have to explicitly write out those units in order to get at the
41 * actual value. So there is hopefully little fear of using a value
42 * in millikelvin as Fahrenheit by mistake.
44 * We don't actually use these all yet, so maybe they'll change, but
45 * I made a number of types as guidelines.
89 temperature_t temperature;
90 pressure_t tankpressure;
96 depth_t maxdepth, meandepth;
97 duration_t duration, surfacetime;
99 temperature_t airtemp, watertemp;
100 pressure_t beginning_pressure, end_pressure;
102 struct sample sample[];
105 static void record_dive(struct dive *dive)
110 tm = gmtime(&dive->when);
112 printf("Dive %d with %d samples at %02d:%02d:%02d %04d-%02d-%02d\n",
114 tm->tm_hour, tm->tm_min, tm->tm_sec,
115 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
118 static void nonmatch(const char *type, const char *fullname, const char *name, char *buffer)
121 printf("Unable to match %s '(%.*s)%s' (%s)\n", type,
122 (int) (name - fullname), fullname, name,
127 static const char *last_part(const char *name)
129 const char *p = strrchr(name, '.');
130 return p ? p+1 : name;
133 typedef void (*matchfn_t)(char *buffer, void *);
135 static int match(const char *pattern, const char *name, matchfn_t fn, char *buf, void *data)
137 if (strcasecmp(pattern, name))
144 * Dive info as it is being built up..
146 static int alloc_samples;
147 static struct dive *dive;
148 static struct sample *sample;
151 static time_t utc_mktime(struct tm *tm)
153 static const int mdays[] = {
154 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
156 int year = tm->tm_year;
157 int month = tm->tm_mon;
158 int day = tm->tm_mday;
162 else if (year > 1900)
165 /* Normalized to Jan 1, 1970: unix time */
168 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
170 if (month < 0 || month > 11) /* array bounds */
172 if (month < 2 || (year + 2) % 4)
174 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
176 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
177 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
180 static void divedate(char *buffer, void *_when)
183 time_t *when = _when;
185 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
189 if (tm.tm_sec | tm.tm_min | tm.tm_hour)
190 *when = utc_mktime(&tm);
195 static void divetime(char *buffer, void *_when)
198 time_t *when = _when;
200 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
205 *when = utc_mktime(&tm);
221 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
227 /* Integer or floating point? */
228 val = strtol(buffer, &end, 10);
229 if (val < 0 || end == buffer)
232 /* Looks like it might be floating point? */
235 fp = strtod(buffer, &end);
246 static void pressure(char *buffer, void *_press)
248 pressure_t *pressure = _press;
249 union int_or_float val;
251 switch (integer_or_float(buffer, &val)) {
253 /* Maybe it's in Bar? */
254 if (val.fp < 500.0) {
255 pressure->mbar = val.fp * 1000;
258 printf("Unknown fractional pressure reading %s\n", buffer);
263 * Random integer? Maybe in PSI? Or millibar already?
265 * We assume that 5 bar is a ridiculous tank pressure,
266 * so if it's smaller than 5000, it's in PSI..
269 pressure->mbar = val.i * 68.95;
272 pressure->mbar = val.i;
275 printf("Strange pressure reading %s\n", buffer);
280 static void depth(char *buffer, void *_depth)
282 depth_t *depth = _depth;
283 union int_or_float val;
285 switch (integer_or_float(buffer, &val)) {
286 /* Integer values are probably in feet */
288 depth->mm = 304.8 * val.i;
290 /* Float? Probably meters.. */
292 depth->mm = val.fp * 1000;
295 printf("Strange depth reading %s\n", buffer);
300 static void temperature(char *buffer, void *_temperature)
302 temperature_t *temperature = _temperature;
303 union int_or_float val;
305 switch (integer_or_float(buffer, &val)) {
306 /* C or F? Who knows? Let's default to Celsius */
311 /* Ignore zero. It means "none" */
316 temperature->mkelvin = (val.fp + 273.16) * 1000;
320 if (val.fp < 212.0) {
321 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
324 /* Kelvin or already millikelvin */
327 temperature->mkelvin = val.fp;
330 printf("Strange temperature reading %s\n", buffer);
335 static void sampletime(char *buffer, void *_time)
337 duration_t *time = _time;
338 union int_or_float val;
340 switch (integer_or_float(buffer, &val)) {
341 /* C or F? Who knows? Let's default to Celsius */
343 time->seconds = val.i * 1000;
346 printf("Strange sample time reading %s\n", buffer);
351 /* We're in samples - try to convert the random xml value to something useful */
352 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
354 const char *last = last_part(name);
356 if (match("pressure", last, pressure, buf, &sample->tankpressure))
358 if (match("cylpress", last, pressure, buf, &sample->tankpressure))
360 if (match("depth", last, depth, buf, &sample->depth))
362 if (match("temperature", last, temperature, buf, &sample->temperature))
364 if (match("sampletime", last, sampletime, buf, &sample->time))
366 if (match("time", last, sampletime, buf, &sample->time))
369 nonmatch("sample", name, last, buf);
372 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
373 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
375 const char *last = last_part(name);
377 if (match("date", last, divedate, buf, &dive->when))
379 if (match("time", last, divetime, buf, &dive->when))
381 nonmatch("dive", name, last, buf);
384 static unsigned int dive_size(int samples)
386 return sizeof(struct dive) + samples*sizeof(struct sample);
390 * File boundaries are dive boundaries. But sometimes there are
391 * multiple dives per file, so there can be other events too that
392 * trigger a "new dive" marker and you may get some nesting due
393 * to that. Just ignore nesting levels.
395 static void dive_start(void)
403 size = dive_size(alloc_samples);
407 memset(dive, 0, size);
408 memset(&tm, 0, sizeof(tm));
411 static void dive_end(void)
419 static void sample_start(void)
426 if (nr >= alloc_samples) {
429 alloc_samples = (alloc_samples * 3)/2 + 10;
430 size = dive_size(alloc_samples);
431 dive = realloc(dive, size);
435 sample = dive->sample + nr;
436 memset(sample, 0, sizeof(*sample));
439 static void sample_end(void)
447 static void entry(const char *name, int size, const char *raw)
449 char *buf = malloc(size+1);
453 memcpy(buf, raw, size);
456 try_to_fill_sample(sample, name, buf);
460 try_to_fill_dive(dive, name, buf);
465 static const char *nodename(xmlNode *node, char *buf, int len)
468 if (!node || !node->name)
476 const char *name = node->name;
477 int i = strlen(name);
479 unsigned char c = name[i];
485 if (!node || !node->name)
495 static void visit_one_node(xmlNode *node)
498 const unsigned char *content;
499 char buffer[MAXNAME];
502 content = node->content;
506 /* Trim whitespace at beginning */
507 while (isspace(*content))
510 /* Trim whitespace at end */
511 len = strlen(content);
512 while (len && isspace(content[len-1]))
518 /* Don't print out the node name if it is "text" */
519 if (!strcmp(node->name, "text"))
522 name = nodename(node, buffer, sizeof(buffer));
524 entry(name, len, content);
527 static void traverse(xmlNode *node)
531 for (n = node; n; n = n->next) {
532 /* XML from libdivecomputer: 'dive' per new dive */
533 if (!strcmp(n->name, "dive")) {
535 traverse(n->children);
541 * At least both libdivecomputer and Suunto
544 * Well - almost. Ignore case.
546 if (!strcasecmp(n->name, "sample")) {
548 traverse(n->children);
553 /* Anything else - just visit it and recurse */
555 traverse(n->children);
559 static void parse(const char *filename)
563 doc = xmlReadFile(filename, NULL, 0);
565 fprintf(stderr, "Failed to parse '%s'.\n", filename);
570 traverse(xmlDocGetRootElement(doc));
576 static void parse_argument(const char *arg)
578 const char *p = arg+1;
586 fprintf(stderr, "Bad argument '%s'\n", arg);
592 int main(int argc, char **argv)
598 for (i = 1; i < argc; i++) {
599 const char *a = argv[i];