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 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 struct dive *dive;
94 static struct sample *sample;
96 static int event_index, cylinder_index;
98 static enum import_source {
107 time_t utc_mktime(struct tm *tm)
109 static const int mdays[] = {
110 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
112 int year = tm->tm_year;
113 int month = tm->tm_mon;
114 int day = tm->tm_mday;
116 /* First normalize relative to 1900 */
119 else if (year > 1900)
122 /* Normalized to Jan 1, 1970: unix time */
125 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
127 if (month < 0 || month > 11) /* array bounds */
129 if (month < 2 || (year + 2) % 4)
131 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
133 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
134 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
137 static void divedate(char *buffer, void *_when)
140 time_t *when = _when;
143 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
144 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
148 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
153 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
158 *when = utc_mktime(&tm);
163 static void divetime(char *buffer, void *_when)
166 time_t *when = _when;
168 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
173 *when = utc_mktime(&tm);
178 /* Libdivecomputer: "2011-03-20 10:22:38" */
179 static void divedatetime(char *buffer, void *_when)
183 time_t *when = _when;
185 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
186 &y, &m, &d, &hr, &min, &sec) == 6) {
193 *when = utc_mktime(&tm);
207 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
213 /* Integer or floating point? */
214 val = strtol(buffer, &end, 10);
215 if (val < 0 || end == buffer)
218 /* Looks like it might be floating point? */
221 fp = strtod(buffer, &end);
232 static void pressure(char *buffer, void *_press)
235 pressure_t *pressure = _press;
236 union int_or_float val;
238 switch (integer_or_float(buffer, &val)) {
240 /* Just ignore zero values */
243 switch (input_units.pressure) {
248 /* Assume mbar, but if it's really small, it's bar */
254 mbar = val.fp * 68.95;
257 if (mbar > 5 && mbar < 500000) {
258 pressure->mbar = mbar + 0.5;
263 printf("Strange pressure reading %s\n", buffer);
268 static void depth(char *buffer, void *_depth)
270 depth_t *depth = _depth;
271 union int_or_float val;
273 switch (integer_or_float(buffer, &val)) {
275 switch (input_units.length) {
277 depth->mm = val.fp * 1000 + 0.5;
280 depth->mm = val.fp * 304.8 + 0.5;
285 printf("Strange depth reading %s\n", buffer);
290 static void temperature(char *buffer, void *_temperature)
292 temperature_t *temperature = _temperature;
293 union int_or_float val;
295 switch (integer_or_float(buffer, &val)) {
297 /* Ignore zero. It means "none" */
301 switch (input_units.temperature) {
303 temperature->mkelvin = val.fp * 1000;
306 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
309 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
314 printf("Strange temperature reading %s\n", buffer);
319 static void sampletime(char *buffer, void *_time)
323 duration_t *time = _time;
325 i = sscanf(buffer, "%d:%d", &min, &sec);
332 time->seconds = sec + min*60;
335 printf("Strange sample time reading %s\n", buffer);
340 static void duration(char *buffer, void *_time)
342 sampletime(buffer, _time);
345 static void percent(char *buffer, void *_fraction)
347 fraction_t *fraction = _fraction;
348 union int_or_float val;
350 switch (integer_or_float(buffer, &val)) {
353 fraction->permille = val.fp * 10 + 0.5;
357 printf("Strange percentage reading %s\n", buffer);
363 static void gasmix(char *buffer, void *_fraction)
365 /* libdivecomputer does negative percentages. */
368 if (cylinder_index < MAX_CYLINDERS)
369 percent(buffer, _fraction);
372 static void gasmix_nitrogen(char *buffer, void *_gasmix)
374 /* Ignore n2 percentages. There's no value in them. */
377 static void cylindersize(char *buffer, void *_volume)
379 volume_t *volume = _volume;
380 union int_or_float val;
382 switch (integer_or_float(buffer, &val)) {
384 volume->mliter = val.fp * 1000 + 0.5;
388 printf("Strange volume reading %s\n", buffer);
394 static void utf8_string(char *buffer, void *_res)
396 *(char **)_res = buffer;
400 * Uemis water_pressure. In centibar. And when converting to
401 * depth, I'm just going to always use saltwater, because I
402 * think "true depth" is just stupid. From a diving standpoint,
403 * "true depth" is pretty much completely pointless, unless
404 * you're doing some kind of underwater surveying work.
406 * So I give water depths in "pressure depth", always assuming
407 * salt water. So one atmosphere per 10m.
409 static void water_pressure(char *buffer, void *_depth)
411 depth_t *depth = _depth;
412 union int_or_float val;
415 switch (integer_or_float(buffer, &val)) {
420 atm = (val.fp / 100) / 1.01325;
422 * atm to cm. Why not mm? The precision just isn't
425 cm = 100 * (atm - 1) + 0.5;
427 depth->mm = 10 * (long)cm;
431 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
436 #define MATCH(pattern, fn, dest) \
437 match(pattern, strlen(pattern), name, len, fn, buf, dest)
439 static void get_index(char *buffer, void *_i)
446 static void centibar(char *buffer, void *_pressure)
448 pressure_t *pressure = _pressure;
449 union int_or_float val;
451 switch (integer_or_float(buffer, &val)) {
453 pressure->mbar = val.fp * 10 + 0.5;
456 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
461 static void decicelsius(char *buffer, void *_temp)
463 temperature_t *temp = _temp;
464 union int_or_float val;
466 switch (integer_or_float(buffer, &val)) {
468 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
471 fprintf(stderr, "Strange julian date: %s", buffer);
476 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
478 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
479 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
480 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
481 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
482 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
487 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
488 * for the sample temperatures, that are in Fahrenheit.
491 * Oh, and I think Diving Log *internally* probably kept them
492 * in celsius, because I'm seeing entries like
496 * in there. Which is freezing, aka 0 degC. I bet the "0" is
497 * what Diving Log uses for "no temperature".
499 * So throw away crap like that.
501 static void fahrenheit(char *buffer, void *_temperature)
503 temperature_t *temperature = _temperature;
504 union int_or_float val;
506 switch (integer_or_float(buffer, &val)) {
508 /* Floating point equality is evil, but works for small integers */
511 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
514 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
520 * Did I mention how bat-shit crazy divinglog is? The sample
521 * pressures are in PSI. But the tank working pressure is in
524 * Crazy stuff like this is why diveclog has everything in
525 * these inconvenient typed structures, and you have to say
526 * "pressure->mbar" to get the actual value. Exactly so that
527 * you can never have unit confusion.
529 static void psi(char *buffer, void *_pressure)
531 pressure_t *pressure = _pressure;
532 union int_or_float val;
534 switch (integer_or_float(buffer, &val)) {
536 pressure->mbar = val.fp * 68.95 + 0.5;
539 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
544 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
546 return MATCH(".p.time", sampletime, &sample->time) ||
547 MATCH(".p.depth", depth, &sample->depth) ||
548 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
549 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
553 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
555 return MATCH(".divetime", sampletime, &sample->time) ||
556 MATCH(".depth", depth, &sample->depth) ||
557 MATCH(".temperature", temperature, &sample->temperature) ||
561 /* We're in samples - try to convert the random xml value to something useful */
562 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
564 int len = strlen(name);
566 start_match("sample", name, buf);
567 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
569 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
571 if (MATCH(".sample.depth", depth, &sample->depth))
573 if (MATCH(".sample.temp", temperature, &sample->temperature))
575 if (MATCH(".sample.temperature", temperature, &sample->temperature))
577 if (MATCH(".sample.sampletime", sampletime, &sample->time))
579 if (MATCH(".sample.time", sampletime, &sample->time))
582 switch (import_source) {
584 if (uemis_fill_sample(sample, name, len, buf))
589 if (divinglog_fill_sample(sample, name, len, buf))
594 if (uddf_fill_sample(sample, name, len, buf))
602 nonmatch("sample", name, buf);
606 * Crazy suunto xml. Look at how those o2/he things match up.
608 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
610 return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
611 MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
612 MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
613 MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
614 MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
615 MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
616 MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
617 MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
618 MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
619 MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
623 static const char *country, *city;
625 static void divinglog_place(char *place, void *_location)
627 char **location = _location;
628 char buffer[256], *p;
631 len = snprintf(buffer, sizeof(buffer),
637 country ? country : "");
640 memcpy(p, buffer, len+1);
647 static int divinglog_dive_match(struct dive *dive, const char *name, int len, char *buf)
649 return MATCH(".divedate", divedate, &dive->when) ||
650 MATCH(".entrytime", divetime, &dive->when) ||
651 MATCH(".depth", depth, &dive->maxdepth) ||
652 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
653 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
654 MATCH(".comments", utf8_string, &dive->notes) ||
655 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
656 MATCH(".country.name", utf8_string, &country) ||
657 MATCH(".city.name", utf8_string, &city) ||
658 MATCH(".place.name", divinglog_place, &dive->location) ||
662 static int buffer_value(char *buffer)
664 int val = atoi(buffer);
669 static void uemis_length_unit(char *buffer, void *_unused)
671 input_units.length = buffer_value(buffer) ? FEET : METERS;
674 static void uemis_volume_unit(char *buffer, void *_unused)
676 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
679 static void uemis_pressure_unit(char *buffer, void *_unused)
682 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
686 static void uemis_temperature_unit(char *buffer, void *_unused)
688 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
691 static void uemis_weight_unit(char *buffer, void *_unused)
693 input_units.weight = buffer_value(buffer) ? LBS : KG;
696 static void uemis_time_unit(char *buffer, void *_unused)
700 static void uemis_date_unit(char *buffer, void *_unused)
704 /* Modified julian day, yay! */
705 static void uemis_date_time(char *buffer, void *_when)
707 time_t *when = _when;
708 union int_or_float val;
710 switch (integer_or_float(buffer, &val)) {
712 *when = (val.fp - 40587) * 86400;
715 fprintf(stderr, "Strange julian date: %s", buffer);
721 * Uemis doesn't know time zones. You need to do them as
722 * minutes, not hours.
724 * But that's ok, we don't track timezones yet either. We
725 * just turn everything into "localtime expressed as UTC".
727 static void uemis_time_zone(char *buffer, void *_when)
729 #if 0 /* seems like this is only used to display it correctly
730 * the stored time appears to be UTC */
732 time_t *when = _when;
733 signed char tz = atoi(buffer);
739 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
740 static int uemis_gas_template;
743 * Christ. Uemis tank data is a total mess.
745 * We're passed a "virtual cylinder" (0 - 6) for the different
746 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
747 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
748 * into the actual cylinder data depending on the gas template,
749 * and ignore the ones that are irrelevant for that template.
751 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
752 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
753 * (which are the three "nitrox_3" tanks), and we turn virtual
754 * tanks 2/3 into actual tanks 0/1.
758 static int uemis_cylinder_index(void *_cylinder)
760 cylinder_t *cylinder = _cylinder;
761 unsigned int index = cylinder - dive->cylinder;
764 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
767 switch(uemis_gas_template) {
768 case 1: /* Dive uses tank 1 */
771 case 0: /* Dive uses tank 0 */
775 case 2: /* Dive uses tanks 2-3 */
780 case 3: /* Dive uses tanks 4-6 */
789 static void uemis_cylindersize(char *buffer, void *_cylinder)
791 int index = uemis_cylinder_index(_cylinder);
793 cylindersize(buffer, &dive->cylinder[index].type.size);
796 static void uemis_percent(char *buffer, void *_cylinder)
798 int index = uemis_cylinder_index(_cylinder);
800 percent(buffer, &dive->cylinder[index].gasmix.o2);
803 static int uemis_dive_match(struct dive *dive, const char *name, int len, char *buf)
805 return MATCH(".units.length", uemis_length_unit, &input_units) ||
806 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
807 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
808 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
809 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
810 MATCH(".units.time", uemis_time_unit, &input_units) ||
811 MATCH(".units.date", uemis_date_unit, &input_units) ||
812 MATCH(".date_time", uemis_date_time, &dive->when) ||
813 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
814 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
815 MATCH(".gas.template", get_index, &uemis_gas_template) ||
816 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
817 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
818 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
819 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
820 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
821 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
822 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
823 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
824 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
825 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
826 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
827 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
828 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
829 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
834 * Uddf specifies ISO 8601 time format.
836 * There are many variations on that. This handles the useful cases.
838 static void uddf_datetime(char *buffer, void *_when)
842 time_t *when = _when;
843 struct tm tm = { 0 };
846 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
853 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
860 printf("Bad date time %s\n", buffer);
865 if (c != 'T' && c != ' ')
873 *when = utc_mktime(&tm);
877 static int uddf_dive_match(struct dive *dive, const char *name, int len, char *buf)
879 return MATCH(".datetime", uddf_datetime, &dive->when) ||
880 MATCH(".diveduration", duration, &dive->duration) ||
881 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
885 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
886 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
888 int len = strlen(name);
890 start_match("dive", name, buf);
892 switch (import_source) {
894 if (suunto_dive_match(dive, name, len, buf))
899 if (uemis_dive_match(dive, name, len, buf))
904 if (divinglog_dive_match(dive, name, len, buf))
909 if (uddf_dive_match(dive, name, len, buf))
917 if (MATCH(".number", get_index, &dive->number))
919 if (MATCH(".date", divedate, &dive->when))
921 if (MATCH(".time", divetime, &dive->when))
923 if (MATCH(".datetime", divedatetime, &dive->when))
925 if (MATCH(".maxdepth", depth, &dive->maxdepth))
927 if (MATCH(".meandepth", depth, &dive->meandepth))
929 if (MATCH(".depth.max", depth, &dive->maxdepth))
931 if (MATCH(".depth.mean", depth, &dive->meandepth))
933 if (MATCH(".duration", duration, &dive->duration))
935 if (MATCH(".divetime", duration, &dive->duration))
937 if (MATCH(".divetimesec", duration, &dive->duration))
939 if (MATCH(".surfacetime", duration, &dive->surfacetime))
941 if (MATCH(".airtemp", temperature, &dive->airtemp))
943 if (MATCH(".watertemp", temperature, &dive->watertemp))
945 if (MATCH(".temperature.air", temperature, &dive->airtemp))
947 if (MATCH(".temperature.water", temperature, &dive->watertemp))
949 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
951 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
953 if (MATCH(".location", utf8_string, &dive->location))
955 if (MATCH(".notes", utf8_string, &dive->notes))
957 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
959 if (MATCH(".buddy", utf8_string, &dive->buddy))
962 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
964 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
966 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cylinder_index].type.description))
968 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cylinder_index].start))
970 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cylinder_index].end))
973 if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
975 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
977 if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
980 nonmatch("dive", name, buf);
984 * File boundaries are dive boundaries. But sometimes there are
985 * multiple dives per file, so there can be other events too that
986 * trigger a "new dive" marker and you may get some nesting due
987 * to that. Just ignore nesting levels.
989 static void dive_start(void)
994 memset(&tm, 0, sizeof(tm));
997 static void sanitize_gasmix(struct gasmix *mix)
1001 o2 = mix->o2.permille;
1002 he = mix->he.permille;
1004 /* Regular air: leave empty */
1008 /* 20.9% or 21% O2 is just air */
1009 if (o2 >= 209 && o2 <= 210) {
1010 mix->o2.permille = 0;
1016 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
1018 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
1019 memset(mix, 0, sizeof(*mix));
1023 * See if the size/workingpressure looks like some standard cylinder
1026 static void match_standard_cylinder(cylinder_type_t *type)
1031 char buffer[20], *p;
1033 /* Do we already have a cylinder description? */
1034 if (type->description)
1037 cuft = type->size.mliter / 28317.0;
1038 cuft *= type->workingpressure.mbar / 1013.25;
1039 psi = type->workingpressure.mbar / 68.95;
1042 case 2300 ... 2500: /* 2400 psi: LP tank */
1045 case 2600 ... 2700: /* 2640 psi: LP+10% */
1048 case 2900 ... 3100: /* 3000 psi: ALx tank */
1051 case 3400 ... 3500: /* 3442 psi: HP tank */
1054 case 3700 ... 3850: /* HP+10% */
1060 len = snprintf(buffer, sizeof(buffer), fmt, (int) (cuft+0.5));
1064 memcpy(p, buffer, len+1);
1065 type->description = p;
1070 * There are two ways to give cylinder size information:
1071 * - total amount of gas in cuft (depends on working pressure and physical size)
1074 * where "physical size" is the one that actually matters and is sane.
1076 * We internally use physical size only. But we save the workingpressure
1077 * so that we can do the conversion if required.
1079 static void sanitize_cylinder_type(cylinder_type_t *type)
1081 double volume_of_air, atm, volume;
1083 /* If we have no working pressure, it had *better* be just a physical size! */
1084 if (!type->workingpressure.mbar)
1087 /* No size either? Nothing to go on */
1088 if (!type->size.mliter)
1091 if (input_units.volume == CUFT || import_source == SUUNTO) {
1092 volume_of_air = type->size.mliter * 28.317; /* milli-cu ft to milliliter */
1093 atm = type->workingpressure.mbar / 1013.25; /* working pressure in atm */
1094 volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
1095 type->size.mliter = volume + 0.5;
1098 /* Ok, we have both size and pressure: try to match a description */
1099 match_standard_cylinder(type);
1102 static void sanitize_cylinder_info(struct dive *dive)
1106 for (i = 0; i < MAX_CYLINDERS; i++) {
1107 sanitize_gasmix(&dive->cylinder[i].gasmix);
1108 sanitize_cylinder_type(&dive->cylinder[i].type);
1112 static void dive_end(void)
1116 sanitize_cylinder_info(dive);
1122 static void event_start(void)
1126 static void event_end(void)
1131 static void cylinder_start(void)
1135 static void cylinder_end(void)
1140 static void sample_start(void)
1142 sample = prepare_sample(&dive);
1146 static void sample_end(void)
1151 finish_sample(dive, sample);
1155 static void entry(const char *name, int size, const char *raw)
1157 char *buf = malloc(size+1);
1161 memcpy(buf, raw, size);
1164 try_to_fill_sample(sample, name, buf);
1168 try_to_fill_dive(dive, name, buf);
1173 static const char *nodename(xmlNode *node, char *buf, int len)
1175 if (!node || !node->name)
1183 const char *name = node->name;
1184 int i = strlen(name);
1186 unsigned char c = name[i];
1187 *--buf = tolower(c);
1191 node = node->parent;
1192 if (!node || !node->name)
1202 static void visit_one_node(xmlNode *node)
1205 const unsigned char *content;
1206 char buffer[MAXNAME];
1209 content = node->content;
1213 /* Trim whitespace at beginning */
1214 while (isspace(*content))
1217 /* Trim whitespace at end */
1218 len = strlen(content);
1219 while (len && isspace(content[len-1]))
1225 /* Don't print out the node name if it is "text" */
1226 if (!strcmp(node->name, "text"))
1227 node = node->parent;
1229 name = nodename(node, buffer, sizeof(buffer));
1231 entry(name, len, content);
1234 static void traverse(xmlNode *root);
1236 static void traverse_properties(xmlNode *node)
1240 for (p = node->properties; p; p = p->next)
1241 traverse(p->children);
1244 static void visit(xmlNode *n)
1247 traverse_properties(n);
1248 traverse(n->children);
1251 static void suunto_importer(void)
1253 import_source = SUUNTO;
1254 input_units = SI_units;
1257 static void uemis_importer(void)
1259 import_source = UEMIS;
1260 input_units = SI_units;
1263 static void DivingLog_importer(void)
1265 import_source = DIVINGLOG;
1268 * Diving Log units are really strange.
1270 * Temperatures are in C, except in samples,
1271 * when they are in Fahrenheit. Depths are in
1272 * meters, an dpressure is in PSI in the samples,
1273 * but in bar when it comes to working pressure.
1275 * Crazy f*%^ morons.
1277 input_units = SI_units;
1280 static void uddf_importer(void)
1282 import_source = UDDF;
1283 input_units = SI_units;
1284 input_units.pressure = PASCAL;
1285 input_units.temperature = KELVIN;
1289 * I'm sure this could be done as some fancy DTD rules.
1290 * It's just not worth the headache.
1292 static struct nesting {
1294 void (*start)(void), (*end)(void);
1296 { "dive", dive_start, dive_end },
1297 { "Dive", dive_start, dive_end },
1298 { "sample", sample_start, sample_end },
1299 { "waypoint", sample_start, sample_end },
1300 { "SAMPLE", sample_start, sample_end },
1301 { "reading", sample_start, sample_end },
1302 { "event", event_start, event_end },
1303 { "gasmix", cylinder_start, cylinder_end },
1304 { "cylinder", cylinder_start, cylinder_end },
1305 { "P", sample_start, sample_end },
1307 /* Import type recognition */
1308 { "SUUNTO", suunto_importer },
1309 { "Divinglog", DivingLog_importer },
1310 { "pre_dive", uemis_importer },
1311 { "uddf", uddf_importer },
1316 static void traverse(xmlNode *root)
1320 for (n = root; n; n = n->next) {
1321 struct nesting *rule = nesting;
1324 if (!strcmp(rule->name, n->name))
1327 } while (rule->name);
1337 /* Per-file reset */
1338 static void reset_all(void)
1341 * We reset the units for each file. You'd think it was
1342 * a per-dive property, but I'm not going to trust people
1343 * to do per-dive setup. If the xml does have per-dive
1344 * data within one file, we might have to reset it per
1345 * dive for that format.
1347 input_units = SI_units;
1348 import_source = UNKNOWN;
1351 void parse_xml_file(const char *filename, GError **error)
1355 doc = xmlReadFile(filename, NULL, 0);
1357 fprintf(stderr, "Failed to parse '%s'.\n", filename);
1360 *error = g_error_new(g_quark_from_string("divelog"),
1362 "Failed to parse '%s'",
1370 traverse(xmlDocGetRootElement(doc));
1376 void parse_xml_init(void)