5 #include <libxml/parser.h>
6 #include <libxml/tree.h>
9 * Some silly typedefs to make our units very explicit.
11 * Also, the units are chosen so that values can be expressible as
12 * integers, so that we never have FP rounding issues. And they
13 * are small enough that converting to/from imperial units doesn't
16 * We also strive to make '0' a meaningless number saying "not
17 * initialized", since many values are things that may not have
18 * been reported (eg tank pressure or temperature from dive
19 * computers that don't support them). But sometimes -1 is an even
20 * more explicit way of saying "not there".
22 * Thus "millibar" for pressure, for example, or "millikelvin" for
23 * temperatures. Doing temperatures in celsius or fahrenheit would
24 * make for loss of precision when converting from one to the other,
25 * and using millikelvin is SI-like but also means that a temperature
26 * of '0' is clearly just a missing temperature or tank pressure.
28 * Also strive to use units that can not possibly be mistaken for a
29 * valid value in a "normal" system without conversion. If the max
30 * depth of a dive is '20000', you probably didn't convert from mm on
31 * output, or if the max depth gets reported as "0.2ft" it was either
32 * a really boring dive, or there was some missing input conversion,
33 * and a 60-ft dive got recorded as 60mm.
35 * Doing these as "structs containing value" means that we always
36 * have to explicitly write out those units in order to get at the
37 * actual value. So there is hopefully little fear of using a value
38 * in millikelvin as Fahrenheit by mistake.
40 * We don't actually use these all yet, so maybe they'll change, but
41 * I made a number of types as guidelines.
85 temperature_t temperature;
86 pressure_t tankpressure;
92 depth_t maxdepth, meandepth;
93 duration_t duration, surfacetime;
95 temperature_t airtemp, watertemp;
96 pressure_t beginning_pressure, end_pressure;
98 struct sample sample[];
101 static void record_dive(struct dive *dive)
106 tm = gmtime(&dive->when);
108 printf("Dive %d with %d samples at %02d:%02d:%02d %04d-%02d-%02d\n",
110 tm->tm_hour, tm->tm_min, tm->tm_sec,
111 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
114 static void nonmatch(const char *type, const char *fullname, const char *name, char *buffer)
116 printf("Unable to match %s '(%.*s)%s' (%s)\n", type,
117 (int) (name - fullname), fullname, name,
122 static const char *last_part(const char *name)
124 const char *p = strrchr(name, '.');
125 return p ? p+1 : name;
128 typedef void (*matchfn_t)(char *buffer, void *);
130 static int match(const char *pattern, const char *name, matchfn_t fn, char *buf, void *data)
132 if (strcasecmp(pattern, name))
139 * Dive info as it is being built up..
141 static int alloc_samples;
142 static struct dive *dive;
143 static struct sample *sample;
146 static time_t utc_mktime(struct tm *tm)
148 static const int mdays[] = {
149 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
151 int year = tm->tm_year;
152 int month = tm->tm_mon;
153 int day = tm->tm_mday;
157 else if (year > 1900)
160 /* Normalized to Jan 1, 1970: unix time */
163 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
165 if (month < 0 || month > 11) /* array bounds */
167 if (month < 2 || (year + 2) % 4)
169 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
171 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
172 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
175 static void divedate(char *buffer, void *_when)
178 time_t *when = _when;
180 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
184 if (tm.tm_sec | tm.tm_min | tm.tm_hour)
185 *when = utc_mktime(&tm);
190 static void divetime(char *buffer, void *_when)
193 time_t *when = _when;
195 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
200 *when = utc_mktime(&tm);
205 /* We're in samples - try to convert the random xml value to something useful */
206 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
208 const char *last = last_part(name);
209 nonmatch("sample", name, last, buf);
212 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
213 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
215 const char *last = last_part(name);
217 if (match("date", last, divedate, buf, &dive->when))
219 if (match("time", last, divetime, buf, &dive->when))
221 nonmatch("dive", name, last, buf);
224 static unsigned int dive_size(int samples)
226 return sizeof(struct dive) + samples*sizeof(struct sample);
230 * File boundaries are dive boundaries. But sometimes there are
231 * multiple dives per file, so there can be other events too that
232 * trigger a "new dive" marker and you may get some nesting due
233 * to that. Just ignore nesting levels.
235 static void dive_start(void)
243 size = dive_size(alloc_samples);
247 memset(dive, 0, size);
248 memset(&tm, 0, sizeof(tm));
251 static void dive_end(void)
259 static void sample_start(void)
266 if (nr >= alloc_samples) {
269 alloc_samples = (alloc_samples * 3)/2 + 10;
270 size = dive_size(alloc_samples);
271 dive = realloc(dive, size);
275 sample = dive->sample + nr;
278 static void sample_end(void)
286 static void entry(const char *name, int size, const char *raw)
288 char *buf = malloc(size+1);
292 memcpy(buf, raw, size);
295 try_to_fill_sample(sample, name, buf);
299 try_to_fill_dive(dive, name, buf);
304 static const char *nodename(xmlNode *node, char *buf, int len)
307 if (!node || !node->name)
315 const char *name = node->name;
316 int i = strlen(name);
318 unsigned char c = name[i];
324 if (!node || !node->name)
334 static void visit_one_node(xmlNode *node)
337 const unsigned char *content;
338 char buffer[MAXNAME];
341 content = node->content;
345 /* Trim whitespace at beginning */
346 while (isspace(*content))
349 /* Trim whitespace at end */
350 len = strlen(content);
351 while (len && isspace(content[len-1]))
357 /* Don't print out the node name if it is "text" */
358 if (!strcmp(node->name, "text"))
361 name = nodename(node, buffer, sizeof(buffer));
363 entry(name, len, content);
366 static void traverse(xmlNode *node)
370 for (n = node; n; n = n->next) {
371 /* XML from libdivecomputer: 'dive' per new dive */
372 if (!strcmp(n->name, "dive")) {
374 traverse(n->children);
380 * At least both libdivecomputer and Suunto
383 * Well - almost. Ignore case.
385 if (!strcasecmp(n->name, "sample")) {
387 traverse(n->children);
392 /* Anything else - just visit it and recurse */
394 traverse(n->children);
398 static void parse(const char *filename)
402 doc = xmlReadFile(filename, NULL, 0);
404 fprintf(stderr, "Failed to parse '%s'.\n", filename);
409 traverse(xmlDocGetRootElement(doc));
415 int main(int argc, char **argv)
421 for (i = 1; i < argc; i++)