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;
32 dives[nr] = fixup_dive(dive);
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 struct units input_units;
69 * We're going to default to SI units for input. Yes,
70 * technically the SI unit for pressure is Pascal, but
71 * we default to bar (10^5 pascal), which people
72 * actually use. Similarly, C instead of Kelvin.
74 const struct units SI_units = {
78 .temperature = CELSIUS,
82 const struct units IMPERIAL_units = {
86 .temperature = FAHRENHEIT,
91 * Dive info as it is being built up..
93 static int alloc_samples;
94 static struct dive *dive;
95 static struct sample *sample;
97 static int event_index, cylinder_index;
99 static enum import_source {
108 static time_t utc_mktime(struct tm *tm)
110 static const int mdays[] = {
111 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
113 int year = tm->tm_year;
114 int month = tm->tm_mon;
115 int day = tm->tm_mday;
117 /* First normalize relative to 1900 */
120 else if (year > 1900)
123 /* Normalized to Jan 1, 1970: unix time */
126 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
128 if (month < 0 || month > 11) /* array bounds */
130 if (month < 2 || (year + 2) % 4)
132 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
134 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
135 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
138 static void divedate(char *buffer, void *_when)
141 time_t *when = _when;
144 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
145 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
149 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
154 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
159 *when = utc_mktime(&tm);
164 static void divetime(char *buffer, void *_when)
167 time_t *when = _when;
169 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
174 *when = utc_mktime(&tm);
179 /* Libdivecomputer: "2011-03-20 10:22:38" */
180 static void divedatetime(char *buffer, void *_when)
184 time_t *when = _when;
186 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
187 &y, &m, &d, &hr, &min, &sec) == 6) {
194 *when = utc_mktime(&tm);
208 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
214 /* Integer or floating point? */
215 val = strtol(buffer, &end, 10);
216 if (val < 0 || end == buffer)
219 /* Looks like it might be floating point? */
222 fp = strtod(buffer, &end);
233 static void pressure(char *buffer, void *_press)
236 pressure_t *pressure = _press;
237 union int_or_float val;
239 switch (integer_or_float(buffer, &val)) {
241 /* Just ignore zero values */
244 switch (input_units.pressure) {
249 /* Assume mbar, but if it's really small, it's bar */
255 mbar = val.fp * 68.95;
258 if (mbar > 5 && mbar < 500000) {
259 pressure->mbar = mbar + 0.5;
264 printf("Strange pressure reading %s\n", buffer);
269 static void depth(char *buffer, void *_depth)
271 depth_t *depth = _depth;
272 union int_or_float val;
274 switch (integer_or_float(buffer, &val)) {
276 switch (input_units.length) {
278 depth->mm = val.fp * 1000 + 0.5;
281 depth->mm = val.fp * 304.8 + 0.5;
286 printf("Strange depth reading %s\n", buffer);
291 static void temperature(char *buffer, void *_temperature)
293 temperature_t *temperature = _temperature;
294 union int_or_float val;
296 switch (integer_or_float(buffer, &val)) {
298 /* Ignore zero. It means "none" */
302 switch (input_units.temperature) {
304 temperature->mkelvin = val.fp * 1000;
307 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
310 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
315 printf("Strange temperature reading %s\n", buffer);
320 static void sampletime(char *buffer, void *_time)
324 duration_t *time = _time;
326 i = sscanf(buffer, "%d:%d", &min, &sec);
333 time->seconds = sec + min*60;
336 printf("Strange sample time reading %s\n", buffer);
341 static void duration(char *buffer, void *_time)
343 sampletime(buffer, _time);
346 static void percent(char *buffer, void *_fraction)
348 fraction_t *fraction = _fraction;
349 union int_or_float val;
351 switch (integer_or_float(buffer, &val)) {
354 fraction->permille = val.fp * 10 + 0.5;
358 printf("Strange percentage reading %s\n", buffer);
364 static void gasmix(char *buffer, void *_fraction)
366 /* libdivecomputer does negative percentages. */
369 if (cylinder_index < MAX_CYLINDERS)
370 percent(buffer, _fraction);
373 static void gasmix_nitrogen(char *buffer, void *_gasmix)
375 /* Ignore n2 percentages. There's no value in them. */
378 static void cylindersize(char *buffer, void *_volume)
380 volume_t *volume = _volume;
381 union int_or_float val;
383 switch (integer_or_float(buffer, &val)) {
385 volume->mliter = val.fp * 1000 + 0.5;
389 printf("Strange volume reading %s\n", buffer);
395 static void utf8_string(char *buffer, void *_res)
397 *(char **)_res = buffer;
401 * Uemis water_pressure. In centibar. And when converting to
402 * depth, I'm just going to always use saltwater, because I
403 * think "true depth" is just stupid. From a diving standpoint,
404 * "true depth" is pretty much completely pointless, unless
405 * you're doing some kind of underwater surveying work.
407 * So I give water depths in "pressure depth", always assuming
408 * salt water. So one atmosphere per 10m.
410 static void water_pressure(char *buffer, void *_depth)
412 depth_t *depth = _depth;
413 union int_or_float val;
416 switch (integer_or_float(buffer, &val)) {
421 atm = (val.fp / 100) / 1.01325;
423 * atm to cm. Why not mm? The precision just isn't
426 cm = 100 * (atm - 1) + 0.5;
428 depth->mm = 10 * (long)cm;
432 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
437 #define MATCH(pattern, fn, dest) \
438 match(pattern, strlen(pattern), name, len, fn, buf, dest)
440 static void get_index(char *buffer, void *_i)
447 static void centibar(char *buffer, void *_pressure)
449 pressure_t *pressure = _pressure;
450 union int_or_float val;
452 switch (integer_or_float(buffer, &val)) {
454 pressure->mbar = val.fp * 10 + 0.5;
457 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
462 static void decicelsius(char *buffer, void *_temp)
464 temperature_t *temp = _temp;
465 union int_or_float val;
467 switch (integer_or_float(buffer, &val)) {
469 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
472 fprintf(stderr, "Strange julian date: %s", buffer);
477 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
479 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
480 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
481 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
482 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
483 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
488 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
489 * for the sample temperatures, that are in Fahrenheit.
492 static void fahrenheit(char *buffer, void *_temperature)
494 temperature_t *temperature = _temperature;
495 union int_or_float val;
497 switch (integer_or_float(buffer, &val)) {
499 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
502 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
507 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
509 return MATCH(".p.time", sampletime, &sample->time) ||
510 MATCH(".p.depth", depth, &sample->depth) ||
511 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
512 MATCH(".p.press1", pressure, &sample->cylinderpressure) ||
516 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
518 return MATCH(".divetime", sampletime, &sample->time) ||
519 MATCH(".depth", depth, &sample->depth) ||
520 MATCH(".temperature", temperature, &sample->temperature) ||
524 /* We're in samples - try to convert the random xml value to something useful */
525 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
527 int len = strlen(name);
529 start_match("sample", name, buf);
530 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
532 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
534 if (MATCH(".sample.depth", depth, &sample->depth))
536 if (MATCH(".sample.temp", temperature, &sample->temperature))
538 if (MATCH(".sample.temperature", temperature, &sample->temperature))
540 if (MATCH(".sample.sampletime", sampletime, &sample->time))
542 if (MATCH(".sample.time", sampletime, &sample->time))
545 switch (import_source) {
547 if (uemis_fill_sample(sample, name, len, buf))
552 if (divinglog_fill_sample(sample, name, len, buf))
557 if (uddf_fill_sample(sample, name, len, buf))
565 nonmatch("sample", name, buf);
569 * Crazy suunto xml. Look at how those o2/he things match up.
571 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
573 return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
574 MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
575 MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
576 MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
577 MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
578 MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
579 MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
580 MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
581 MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
582 MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
586 static const char *country, *city;
588 static void divinglog_place(char *place, void *_location)
590 char **location = _location;
591 char buffer[256], *p;
594 len = snprintf(buffer, sizeof(buffer),
600 country ? country : "");
603 memcpy(p, buffer, len+1);
610 static int divinglog_dive_match(struct dive *dive, const char *name, int len, char *buf)
612 return MATCH(".id", get_index, &dive->nr) ||
613 MATCH(".divedate", divedate, &dive->when) ||
614 MATCH(".entrytime", divetime, &dive->when) ||
615 MATCH(".depth", depth, &dive->maxdepth) ||
616 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
617 MATCH(".tanktype", utf8_string, &dive->cylinder[0].type.description) ||
618 MATCH(".comments", utf8_string, &dive->notes) ||
619 MATCH(".country.name", utf8_string, &country) ||
620 MATCH(".city.name", utf8_string, &city) ||
621 MATCH(".place.name", divinglog_place, &dive->location) ||
625 static int buffer_value(char *buffer)
627 int val = atoi(buffer);
632 static void uemis_length_unit(char *buffer, void *_unused)
634 input_units.length = buffer_value(buffer) ? FEET : METERS;
637 static void uemis_volume_unit(char *buffer, void *_unused)
639 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
642 static void uemis_pressure_unit(char *buffer, void *_unused)
645 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
649 static void uemis_temperature_unit(char *buffer, void *_unused)
651 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
654 static void uemis_weight_unit(char *buffer, void *_unused)
656 input_units.weight = buffer_value(buffer) ? LBS : KG;
659 static void uemis_time_unit(char *buffer, void *_unused)
663 static void uemis_date_unit(char *buffer, void *_unused)
667 /* Modified julian day, yay! */
668 static void uemis_date_time(char *buffer, void *_when)
670 time_t *when = _when;
671 union int_or_float val;
673 switch (integer_or_float(buffer, &val)) {
675 *when = (val.fp - 40587) * 86400;
678 fprintf(stderr, "Strange julian date: %s", buffer);
684 * Uemis doesn't know time zones. You need to do them as
685 * minutes, not hours.
687 * But that's ok, we don't track timezones yet either. We
688 * just turn everything into "localtime expressed as UTC".
690 static void uemis_time_zone(char *buffer, void *_when)
692 #if 0 /* seems like this is only used to display it correctly
693 * the stored time appears to be UTC */
695 time_t *when = _when;
696 signed char tz = atoi(buffer);
702 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
703 static int uemis_gas_template;
706 * Christ. Uemis tank data is a total mess.
708 * We're passed a "virtual cylinder" (0 - 6) for the different
709 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
710 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
711 * into the actual cylinder data depending on the gas template,
712 * and ignore the ones that are irrelevant for that template.
714 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
715 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
716 * (which are the three "nitrox_3" tanks), and we turn virtual
717 * tanks 2/3 into actual tanks 0/1.
721 static int uemis_cylinder_index(void *_cylinder)
723 cylinder_t *cylinder = _cylinder;
724 unsigned int index = cylinder - dive->cylinder;
727 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
730 switch(uemis_gas_template) {
731 case 1: /* Dive uses tank 1 */
734 case 0: /* Dive uses tank 0 */
738 case 2: /* Dive uses tanks 2-3 */
743 case 3: /* Dive uses tanks 4-6 */
752 static void uemis_cylindersize(char *buffer, void *_cylinder)
754 int index = uemis_cylinder_index(_cylinder);
756 cylindersize(buffer, &dive->cylinder[index].type.size);
759 static void uemis_percent(char *buffer, void *_cylinder)
761 int index = uemis_cylinder_index(_cylinder);
763 percent(buffer, &dive->cylinder[index].gasmix.o2);
766 static int uemis_dive_match(struct dive *dive, const char *name, int len, char *buf)
768 return MATCH(".units.length", uemis_length_unit, &input_units) ||
769 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
770 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
771 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
772 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
773 MATCH(".units.time", uemis_time_unit, &input_units) ||
774 MATCH(".units.date", uemis_date_unit, &input_units) ||
775 MATCH(".date_time", uemis_date_time, &dive->when) ||
776 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
777 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
778 MATCH(".gas.template", get_index, &uemis_gas_template) ||
779 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
780 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
781 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
782 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
783 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
784 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
785 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
786 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
787 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
788 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
789 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
790 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
791 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
792 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
797 * Uddf specifies ISO 8601 time format.
799 * There are many variations on that. This handles the useful cases.
801 static void uddf_datetime(char *buffer, void *_when)
805 time_t *when = _when;
806 struct tm tm = { 0 };
809 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
816 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
823 printf("Bad date time %s\n", buffer);
828 if (c != 'T' && c != ' ')
836 *when = utc_mktime(&tm);
840 static int uddf_dive_match(struct dive *dive, const char *name, int len, char *buf)
842 return MATCH(".datetime", uddf_datetime, &dive->when) ||
843 MATCH(".diveduration", duration, &dive->duration) ||
844 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
848 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
849 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
851 int len = strlen(name);
853 start_match("dive", name, buf);
855 switch (import_source) {
857 if (suunto_dive_match(dive, name, len, buf))
862 if (uemis_dive_match(dive, name, len, buf))
867 if (divinglog_dive_match(dive, name, len, buf))
872 if (uddf_dive_match(dive, name, len, buf))
880 if (MATCH(".nr", get_index, &dive->nr))
882 if (MATCH(".date", divedate, &dive->when))
884 if (MATCH(".time", divetime, &dive->when))
886 if (MATCH(".datetime", divedatetime, &dive->when))
888 if (MATCH(".maxdepth", depth, &dive->maxdepth))
890 if (MATCH(".meandepth", depth, &dive->meandepth))
892 if (MATCH(".depth.max", depth, &dive->maxdepth))
894 if (MATCH(".depth.mean", depth, &dive->meandepth))
896 if (MATCH(".duration", duration, &dive->duration))
898 if (MATCH(".divetime", duration, &dive->duration))
900 if (MATCH(".divetimesec", duration, &dive->duration))
902 if (MATCH(".surfacetime", duration, &dive->surfacetime))
904 if (MATCH(".airtemp", temperature, &dive->airtemp))
906 if (MATCH(".watertemp", temperature, &dive->watertemp))
908 if (MATCH(".temperature.air", temperature, &dive->airtemp))
910 if (MATCH(".temperature.water", temperature, &dive->watertemp))
912 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
914 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
916 if (MATCH(".location", utf8_string, &dive->location))
918 if (MATCH(".notes", utf8_string, &dive->notes))
921 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
923 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
925 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cylinder_index].type.description))
927 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cylinder_index].start))
929 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cylinder_index].end))
932 if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
934 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
936 if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
939 nonmatch("dive", name, buf);
943 * File boundaries are dive boundaries. But sometimes there are
944 * multiple dives per file, so there can be other events too that
945 * trigger a "new dive" marker and you may get some nesting due
946 * to that. Just ignore nesting levels.
948 static void dive_start(void)
956 size = dive_size(alloc_samples);
960 memset(dive, 0, size);
961 memset(&tm, 0, sizeof(tm));
964 static void sanitize_gasmix(gasmix_t *mix)
968 o2 = mix->o2.permille;
969 he = mix->he.permille;
971 /* Regular air: leave empty */
975 /* 20.9% or 21% O2 is just air */
976 if (o2 >= 209 && o2 <= 210) {
977 mix->o2.permille = 0;
983 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
985 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
986 memset(mix, 0, sizeof(*mix));
990 * See if the size/workingpressure looks like some standard cylinder
993 static void match_standard_cylinder(cylinder_type_t *type)
999 /* Do we already have a cylinder description? */
1000 if (type->description)
1003 cuft = type->size.mliter / 1000;
1004 psi = type->workingpressure.mbar / 68.95;
1007 case 2300 ... 2500: /* 2400 psi: LP tank */
1010 case 2600 ... 2700: /* 2640 psi: LP+10% */
1013 case 2900 ... 3100: /* 3000 psi: ALx tank */
1016 case 3400 ... 3500: /* 3442 psi: HP tank */
1019 case 3700 ... 3850: /* HP+10% */
1025 len = snprintf(buffer, sizeof(buffer), fmt, cuft);
1029 memcpy(p, buffer, len+1);
1030 type->description = p;
1035 * There are two ways to give cylinder size information:
1036 * - total amount of gas in cuft (depends on working pressure and physical size)
1039 * where "physical size" is the one that actually matters and is sane.
1041 * We internally use physical size only. But we save the workingpressure
1042 * so that we can do the conversion if required.
1044 static void sanitize_cylinder_type(cylinder_type_t *type)
1046 double volume_of_air, atm, volume;
1048 /* If we have no working pressure, it had *better* be just a physical size! */
1049 if (!type->workingpressure.mbar)
1052 /* No size either? Nothing to go on */
1053 if (!type->size.mliter)
1056 /* Ok, we have both size and pressure: try to match a description */
1057 match_standard_cylinder(type);
1059 if (input_units.volume == CUFT || import_source == SUUNTO) {
1060 volume_of_air = type->size.mliter * 28.317; /* milli-cu ft to milliliter */
1061 atm = type->workingpressure.mbar / 1013.25; /* working pressure in atm */
1062 volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
1063 type->size.mliter = volume + 0.5;
1067 static void sanitize_cylinder_info(struct dive *dive)
1071 for (i = 0; i < MAX_CYLINDERS; i++) {
1072 sanitize_gasmix(&dive->cylinder[i].gasmix);
1073 sanitize_cylinder_type(&dive->cylinder[i].type);
1077 static void dive_end(void)
1081 sanitize_cylinder_info(dive);
1087 static void event_start(void)
1091 static void event_end(void)
1096 static void cylinder_start(void)
1100 static void cylinder_end(void)
1105 static void sample_start(void)
1112 if (nr >= alloc_samples) {
1115 alloc_samples = (alloc_samples * 3)/2 + 10;
1116 size = dive_size(alloc_samples);
1117 dive = realloc(dive, size);
1121 sample = dive->sample + nr;
1122 memset(sample, 0, sizeof(*sample));
1126 static void sample_end(void)
1135 static void entry(const char *name, int size, const char *raw)
1137 char *buf = malloc(size+1);
1141 memcpy(buf, raw, size);
1144 try_to_fill_sample(sample, name, buf);
1148 try_to_fill_dive(dive, name, buf);
1153 static const char *nodename(xmlNode *node, char *buf, int len)
1155 if (!node || !node->name)
1163 const char *name = node->name;
1164 int i = strlen(name);
1166 unsigned char c = name[i];
1167 *--buf = tolower(c);
1171 node = node->parent;
1172 if (!node || !node->name)
1182 static void visit_one_node(xmlNode *node)
1185 const unsigned char *content;
1186 char buffer[MAXNAME];
1189 content = node->content;
1193 /* Trim whitespace at beginning */
1194 while (isspace(*content))
1197 /* Trim whitespace at end */
1198 len = strlen(content);
1199 while (len && isspace(content[len-1]))
1205 /* Don't print out the node name if it is "text" */
1206 if (!strcmp(node->name, "text"))
1207 node = node->parent;
1209 name = nodename(node, buffer, sizeof(buffer));
1211 entry(name, len, content);
1214 static void traverse(xmlNode *root);
1216 static void traverse_properties(xmlNode *node)
1220 for (p = node->properties; p; p = p->next)
1221 traverse(p->children);
1224 static void visit(xmlNode *n)
1227 traverse_properties(n);
1228 traverse(n->children);
1231 static void suunto_importer(void)
1233 import_source = SUUNTO;
1234 input_units = SI_units;
1237 static void uemis_importer(void)
1239 import_source = UEMIS;
1240 input_units = SI_units;
1243 static void DivingLog_importer(void)
1245 import_source = DIVINGLOG;
1248 * Diving Log units are really strange.
1250 * Temperatures are in C, except in samples,
1251 * when they are in Fahrenheit. Depths are in
1252 * meters, but pressure is in PSI.
1254 input_units = SI_units;
1255 input_units.pressure = PSI;
1258 static void uddf_importer(void)
1260 import_source = UDDF;
1261 input_units = SI_units;
1262 input_units.pressure = PASCAL;
1263 input_units.temperature = KELVIN;
1267 * I'm sure this could be done as some fancy DTD rules.
1268 * It's just not worth the headache.
1270 static struct nesting {
1272 void (*start)(void), (*end)(void);
1274 { "dive", dive_start, dive_end },
1275 { "Dive", dive_start, dive_end },
1276 { "sample", sample_start, sample_end },
1277 { "waypoint", sample_start, sample_end },
1278 { "SAMPLE", sample_start, sample_end },
1279 { "reading", sample_start, sample_end },
1280 { "event", event_start, event_end },
1281 { "gasmix", cylinder_start, cylinder_end },
1282 { "cylinder", cylinder_start, cylinder_end },
1283 { "P", sample_start, sample_end },
1285 /* Import type recognition */
1286 { "SUUNTO", suunto_importer },
1287 { "Divinglog", DivingLog_importer },
1288 { "pre_dive", uemis_importer },
1289 { "uddf", uddf_importer },
1294 static void traverse(xmlNode *root)
1298 for (n = root; n; n = n->next) {
1299 struct nesting *rule = nesting;
1302 if (!strcmp(rule->name, n->name))
1305 } while (rule->name);
1315 /* Per-file reset */
1316 static void reset_all(void)
1319 * We reset the units for each file. You'd think it was
1320 * a per-dive property, but I'm not going to trust people
1321 * to do per-dive setup. If the xml does have per-dive
1322 * data within one file, we might have to reset it per
1323 * dive for that format.
1325 input_units = SI_units;
1326 import_source = UNKNOWN;
1329 void parse_xml_file(const char *filename, GError **error)
1333 doc = xmlReadFile(filename, NULL, 0);
1335 fprintf(stderr, "Failed to parse '%s'.\n", filename);
1338 *error = g_error_new(g_quark_from_string("divelog"),
1340 "Failed to parse '%s'",
1348 traverse(xmlDocGetRootElement(doc));
1354 void parse_xml_init(void)