]> git.tdb.fi Git - ext/subsurface.git/blob - parse.c
Start parsing dive dates
[ext/subsurface.git] / parse.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <time.h>
5 #include <libxml/parser.h>
6 #include <libxml/tree.h>
7
8 /*
9  * Some silly typedefs to make our units very explicit.
10  *
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
14  * really matter.
15  *
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".
21  *
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.
27  *
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.
34  *
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.
39  *
40  * We don't actually use these all yet, so maybe they'll change, but
41  * I made a number of types as guidelines.
42  */
43 typedef struct {
44         int seconds;
45 } duration_t;
46
47 typedef struct {
48         int mm;
49 } depth_t;
50
51 typedef struct {
52         int mbar;
53 } pressure_t;
54
55 typedef struct {
56         int mkelvin;
57 } temperature_t;
58
59 typedef struct {
60         int mliter;
61 } volume_t;
62
63 typedef struct {
64         int permille;
65 } fraction_t;
66
67 typedef struct {
68         int grams;
69 } weight_t;
70
71 typedef struct {
72         fraction_t o2;
73         fraction_t n2;
74         fraction_t he2;
75 } gasmix_t;
76
77 typedef struct {
78         volume_t size;
79         pressure_t pressure;
80 } tank_type_t;
81
82 struct sample {
83         duration_t time;
84         depth_t depth;
85         temperature_t temperature;
86         pressure_t tankpressure;
87         int tankindex;
88 };
89
90 struct dive {
91         time_t when;
92         depth_t maxdepth, meandepth;
93         duration_t duration, surfacetime;
94         depth_t visibility;
95         temperature_t airtemp, watertemp;
96         pressure_t beginning_pressure, end_pressure;
97         int samples;
98         struct sample sample[];
99 };
100
101 static void record_dive(struct dive *dive)
102 {
103         static int nr;
104         struct tm *tm;
105
106         tm = gmtime(&dive->when);
107
108         printf("Dive %d with %d samples at %02d:%02d:%02d %04d-%02d-%02d\n",
109                 ++nr, dive->samples,
110                 tm->tm_hour, tm->tm_min, tm->tm_sec,
111                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
112 }
113
114 static void nonmatch(const char *type, const char *fullname, const char *name, char *buffer)
115 {
116         printf("Unable to match %s '(%.*s)%s' (%s)\n", type,
117                 (int) (name - fullname), fullname, name,
118                 buffer);
119         free(buffer);
120 }
121
122 static const char *last_part(const char *name)
123 {
124         const char *p = strrchr(name, '.');
125         return p ? p+1 : name;
126 }
127
128 typedef void (*matchfn_t)(char *buffer, void *);
129
130 static int match(const char *pattern, const char *name, matchfn_t fn, char *buf, void *data)
131 {
132         if (strcasecmp(pattern, name))
133                 return 0;
134         fn(buf, data);
135         return 1;
136 }
137
138 /*
139  * Dive info as it is being built up..
140  */
141 static int alloc_samples;
142 static struct dive *dive;
143 static struct sample *sample;
144 static struct tm tm;
145
146 static time_t utc_mktime(struct tm *tm)
147 {
148         static const int mdays[] = {
149             0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
150         };
151         int year = tm->tm_year;
152         int month = tm->tm_mon;
153         int day = tm->tm_mday;
154
155         if (year < 70)
156                 year += 100;
157         else if (year > 1900)
158                 year -= 1900;
159
160         /* Normalized to Jan 1, 1970: unix time */
161         year -= 70;
162
163         if (year < 0 || year > 129) /* algo only works for 1970-2099 */
164                 return -1;
165         if (month < 0 || month > 11) /* array bounds */
166                 return -1;
167         if (month < 2 || (year + 2) % 4)
168                 day--;
169         if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
170                 return -1;
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;
173 }
174
175 static void divedate(char *buffer, void *_when)
176 {
177         int d,m,y;
178         time_t *when = _when;
179
180         if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
181                 tm.tm_year = y;
182                 tm.tm_mon = m-1;
183                 tm.tm_mday = d;
184                 if (tm.tm_sec | tm.tm_min | tm.tm_hour)
185                         *when = utc_mktime(&tm);
186         }
187         free(buffer);
188 }
189
190 static void divetime(char *buffer, void *_when)
191 {
192         int h,m,s = 0;
193         time_t *when = _when;
194
195         if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
196                 tm.tm_hour = h;
197                 tm.tm_min = m;
198                 tm.tm_sec = s;
199                 if (tm.tm_year)
200                         *when = utc_mktime(&tm);
201         }
202         free(buffer);
203 }
204
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)
207 {
208         const char *last = last_part(name);
209         nonmatch("sample", name, last, buf);
210 }
211
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)
214 {
215         const char *last = last_part(name);
216
217         if (match("date", last, divedate, buf, &dive->when))
218                 return;
219         if (match("time", last, divetime, buf, &dive->when))
220                 return;
221         nonmatch("dive", name, last, buf);
222 }
223
224 static unsigned int dive_size(int samples)
225 {
226         return sizeof(struct dive) + samples*sizeof(struct sample);
227 }
228
229 /*
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.
234  */
235 static void dive_start(void)
236 {
237         unsigned int size;
238
239         if (dive)
240                 return;
241
242         alloc_samples = 5;
243         size = dive_size(alloc_samples);
244         dive = malloc(size);
245         if (!dive)
246                 exit(1);
247         memset(dive, 0, size);
248         memset(&tm, 0, sizeof(tm));
249 }
250
251 static void dive_end(void)
252 {
253         if (!dive)
254                 return;
255         record_dive(dive);
256         dive = NULL;
257 }
258
259 static void sample_start(void)
260 {
261         int nr;
262
263         if (!dive)
264                 return;
265         nr = dive->samples;
266         if (nr >= alloc_samples) {
267                 unsigned int size;
268
269                 alloc_samples = (alloc_samples * 3)/2 + 10;
270                 size = dive_size(alloc_samples);
271                 dive = realloc(dive, size);
272                 if (!dive)
273                         return;
274         }
275         sample = dive->sample + nr;
276 }
277
278 static void sample_end(void)
279 {
280         sample = NULL;
281         if (!dive)
282                 return;
283         dive->samples++;
284 }
285
286 static void entry(const char *name, int size, const char *raw)
287 {
288         char *buf = malloc(size+1);
289
290         if (!buf)
291                 return;
292         memcpy(buf, raw, size);
293         buf[size] = 0;
294         if (sample) {
295                 try_to_fill_sample(sample, name, buf);
296                 return;
297         }
298         if (dive) {
299                 try_to_fill_dive(dive, name, buf);
300                 return;
301         }
302 }
303
304 static const char *nodename(xmlNode *node, char *buf, int len)
305 {
306
307         if (!node || !node->name)
308                 return "root";
309
310         buf += len;
311         *--buf = 0;
312         len--;
313
314         for(;;) {
315                 const char *name = node->name;
316                 int i = strlen(name);
317                 while (--i >= 0) {
318                         unsigned char c = name[i];
319                         *--buf = tolower(c);
320                         if (!--len)
321                                 return buf;
322                 }
323                 node = node->parent;
324                 if (!node || !node->name)
325                         return buf;
326                 *--buf = '.';
327                 if (!--len)
328                         return buf;
329         }
330 }
331
332 #define MAXNAME 64
333
334 static void visit_one_node(xmlNode *node)
335 {
336         int len;
337         const unsigned char *content;
338         char buffer[MAXNAME];
339         const char *name;
340
341         content = node->content;
342         if (!content)
343                 return;
344
345         /* Trim whitespace at beginning */
346         while (isspace(*content))
347                 content++;
348
349         /* Trim whitespace at end */
350         len = strlen(content);
351         while (len && isspace(content[len-1]))
352                 len--;
353
354         if (!len)
355                 return;
356
357         /* Don't print out the node name if it is "text" */
358         if (!strcmp(node->name, "text"))
359                 node = node->parent;
360
361         name = nodename(node, buffer, sizeof(buffer));
362
363         entry(name, len, content);
364 }
365
366 static void traverse(xmlNode *node)
367 {
368         xmlNode *n;
369
370         for (n = node; n; n = n->next) {
371                 /* XML from libdivecomputer: 'dive' per new dive */
372                 if (!strcmp(n->name, "dive")) {
373                         dive_start();
374                         traverse(n->children);
375                         dive_end();
376                         continue;
377                 }
378
379                 /*
380                  * At least both libdivecomputer and Suunto
381                  * agree on "sample".
382                  *
383                  * Well - almost. Ignore case.
384                  */
385                 if (!strcasecmp(n->name, "sample")) {
386                         sample_start();
387                         traverse(n->children);
388                         sample_end();
389                         continue;
390                 }
391
392                 /* Anything else - just visit it and recurse */
393                 visit_one_node(n);
394                 traverse(n->children);
395         }
396 }
397
398 static void parse(const char *filename)
399 {
400         xmlDoc *doc;
401
402         doc = xmlReadFile(filename, NULL, 0);
403         if (!doc) {
404                 fprintf(stderr, "Failed to parse '%s'.\n", filename);
405                 return;
406         }
407
408         dive_start();
409         traverse(xmlDocGetRootElement(doc));
410         dive_end();
411         xmlFreeDoc(doc);
412         xmlCleanupParser();
413 }
414
415 int main(int argc, char **argv)
416 {
417         int i;
418
419         LIBXML_TEST_VERSION
420
421         for (i = 1; i < argc; i++)
422                 parse(argv[i]);
423         return 0;
424 }