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;
98 int type, flags, value;
102 static int cylinder_index;
104 static enum import_source {
113 time_t utc_mktime(struct tm *tm)
115 static const int mdays[] = {
116 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
118 int year = tm->tm_year;
119 int month = tm->tm_mon;
120 int day = tm->tm_mday;
122 /* First normalize relative to 1900 */
125 else if (year > 1900)
128 /* Normalized to Jan 1, 1970: unix time */
131 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
133 if (month < 0 || month > 11) /* array bounds */
135 if (month < 2 || (year + 2) % 4)
137 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
139 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
140 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
143 static void divedate(char *buffer, void *_when)
146 time_t *when = _when;
149 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
150 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
154 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
159 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
164 *when = utc_mktime(&tm);
169 static void divetime(char *buffer, void *_when)
172 time_t *when = _when;
174 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
179 *when = utc_mktime(&tm);
184 /* Libdivecomputer: "2011-03-20 10:22:38" */
185 static void divedatetime(char *buffer, void *_when)
189 time_t *when = _when;
191 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
192 &y, &m, &d, &hr, &min, &sec) == 6) {
199 *when = utc_mktime(&tm);
213 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
219 /* Integer or floating point? */
220 val = strtol(buffer, &end, 10);
221 if (val < 0 || end == buffer)
224 /* Looks like it might be floating point? */
227 fp = strtod(buffer, &end);
238 static void pressure(char *buffer, void *_press)
241 pressure_t *pressure = _press;
242 union int_or_float val;
244 switch (integer_or_float(buffer, &val)) {
246 /* Just ignore zero values */
249 switch (input_units.pressure) {
254 /* Assume mbar, but if it's really small, it's bar */
260 mbar = val.fp * 68.95;
263 if (mbar > 5 && mbar < 500000) {
264 pressure->mbar = mbar + 0.5;
269 printf("Strange pressure reading %s\n", buffer);
274 static void depth(char *buffer, void *_depth)
276 depth_t *depth = _depth;
277 union int_or_float val;
279 switch (integer_or_float(buffer, &val)) {
281 switch (input_units.length) {
283 depth->mm = val.fp * 1000 + 0.5;
286 depth->mm = val.fp * 304.8 + 0.5;
291 printf("Strange depth reading %s\n", buffer);
296 static void temperature(char *buffer, void *_temperature)
298 temperature_t *temperature = _temperature;
299 union int_or_float val;
301 switch (integer_or_float(buffer, &val)) {
303 /* Ignore zero. It means "none" */
307 switch (input_units.temperature) {
309 temperature->mkelvin = val.fp * 1000;
312 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
315 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
320 printf("Strange temperature reading %s\n", buffer);
325 static void sampletime(char *buffer, void *_time)
329 duration_t *time = _time;
331 i = sscanf(buffer, "%d:%d", &min, &sec);
338 time->seconds = sec + min*60;
341 printf("Strange sample time reading %s\n", buffer);
346 static void duration(char *buffer, void *_time)
348 sampletime(buffer, _time);
351 static void percent(char *buffer, void *_fraction)
353 fraction_t *fraction = _fraction;
354 union int_or_float val;
356 switch (integer_or_float(buffer, &val)) {
359 fraction->permille = val.fp * 10 + 0.5;
363 printf("Strange percentage reading %s\n", buffer);
369 static void gasmix(char *buffer, void *_fraction)
371 /* libdivecomputer does negative percentages. */
374 if (cylinder_index < MAX_CYLINDERS)
375 percent(buffer, _fraction);
378 static void gasmix_nitrogen(char *buffer, void *_gasmix)
380 /* Ignore n2 percentages. There's no value in them. */
383 static void cylindersize(char *buffer, void *_volume)
385 volume_t *volume = _volume;
386 union int_or_float val;
388 switch (integer_or_float(buffer, &val)) {
390 volume->mliter = val.fp * 1000 + 0.5;
394 printf("Strange volume reading %s\n", buffer);
400 static void utf8_string(char *buffer, void *_res)
402 *(char **)_res = buffer;
406 * Uemis water_pressure. In centibar. And when converting to
407 * depth, I'm just going to always use saltwater, because I
408 * think "true depth" is just stupid. From a diving standpoint,
409 * "true depth" is pretty much completely pointless, unless
410 * you're doing some kind of underwater surveying work.
412 * So I give water depths in "pressure depth", always assuming
413 * salt water. So one atmosphere per 10m.
415 static void water_pressure(char *buffer, void *_depth)
417 depth_t *depth = _depth;
418 union int_or_float val;
421 switch (integer_or_float(buffer, &val)) {
426 atm = (val.fp / 100) / 1.01325;
428 * atm to cm. Why not mm? The precision just isn't
431 cm = 100 * atm + 0.5;
433 depth->mm = 10 * (long)cm;
437 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
442 #define MATCH(pattern, fn, dest) \
443 match(pattern, strlen(pattern), name, len, fn, buf, dest)
445 static void get_index(char *buffer, void *_i)
452 static void centibar(char *buffer, void *_pressure)
454 pressure_t *pressure = _pressure;
455 union int_or_float val;
457 switch (integer_or_float(buffer, &val)) {
459 pressure->mbar = val.fp * 10 + 0.5;
462 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
467 static void decicelsius(char *buffer, void *_temp)
469 temperature_t *temp = _temp;
470 union int_or_float val;
472 switch (integer_or_float(buffer, &val)) {
474 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
477 fprintf(stderr, "Strange julian date: %s", buffer);
482 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
484 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
485 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
486 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
487 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
488 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
493 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
494 * for the sample temperatures, that are in Fahrenheit.
497 * Oh, and I think Diving Log *internally* probably kept them
498 * in celsius, because I'm seeing entries like
502 * in there. Which is freezing, aka 0 degC. I bet the "0" is
503 * what Diving Log uses for "no temperature".
505 * So throw away crap like that.
507 static void fahrenheit(char *buffer, void *_temperature)
509 temperature_t *temperature = _temperature;
510 union int_or_float val;
512 switch (integer_or_float(buffer, &val)) {
514 /* Floating point equality is evil, but works for small integers */
517 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
520 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
526 * Did I mention how bat-shit crazy divinglog is? The sample
527 * pressures are in PSI. But the tank working pressure is in
530 * Crazy stuff like this is why subsurface has everything in
531 * these inconvenient typed structures, and you have to say
532 * "pressure->mbar" to get the actual value. Exactly so that
533 * you can never have unit confusion.
535 static void psi(char *buffer, void *_pressure)
537 pressure_t *pressure = _pressure;
538 union int_or_float val;
540 switch (integer_or_float(buffer, &val)) {
542 pressure->mbar = val.fp * 68.95 + 0.5;
545 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
550 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
552 return MATCH(".p.time", sampletime, &sample->time) ||
553 MATCH(".p.depth", depth, &sample->depth) ||
554 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
555 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
559 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
561 return MATCH(".divetime", sampletime, &sample->time) ||
562 MATCH(".depth", depth, &sample->depth) ||
563 MATCH(".temperature", temperature, &sample->temperature) ||
567 static void eventtime(char *buffer, void *_duration)
569 duration_t *duration = _duration;
570 sampletime(buffer, duration);
572 duration->seconds += sample->time.seconds;
575 static void try_to_fill_event(const char *name, char *buf)
577 int len = strlen(name);
579 start_match("event", name, buf);
580 if (MATCH(".event", utf8_string, &event.name))
582 if (MATCH(".name", utf8_string, &event.name))
584 if (MATCH(".time", eventtime, &event.time))
586 if (MATCH(".type", get_index, &event.type))
588 if (MATCH(".flags", get_index, &event.flags))
590 if (MATCH(".value", get_index, &event.value))
592 nonmatch("event", name, buf);
595 /* We're in samples - try to convert the random xml value to something useful */
596 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
598 int len = strlen(name);
600 start_match("sample", name, buf);
601 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
603 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
605 if (MATCH(".sample.depth", depth, &sample->depth))
607 if (MATCH(".sample.temp", temperature, &sample->temperature))
609 if (MATCH(".sample.temperature", temperature, &sample->temperature))
611 if (MATCH(".sample.sampletime", sampletime, &sample->time))
613 if (MATCH(".sample.time", sampletime, &sample->time))
616 switch (import_source) {
618 if (uemis_fill_sample(sample, name, len, buf))
623 if (divinglog_fill_sample(sample, name, len, buf))
628 if (uddf_fill_sample(sample, name, len, buf))
636 nonmatch("sample", name, buf);
640 * Crazy suunto xml. Look at how those o2/he things match up.
642 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
644 return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
645 MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
646 MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
647 MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
648 MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
649 MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
650 MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
651 MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
652 MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
653 MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
657 static const char *country, *city;
659 static void divinglog_place(char *place, void *_location)
661 char **location = _location;
662 char buffer[256], *p;
665 len = snprintf(buffer, sizeof(buffer),
671 country ? country : "");
674 memcpy(p, buffer, len+1);
681 static int divinglog_dive_match(struct dive *dive, const char *name, int len, char *buf)
683 return MATCH(".divedate", divedate, &dive->when) ||
684 MATCH(".entrytime", divetime, &dive->when) ||
685 MATCH(".depth", depth, &dive->maxdepth) ||
686 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
687 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
688 MATCH(".comments", utf8_string, &dive->notes) ||
689 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
690 MATCH(".country.name", utf8_string, &country) ||
691 MATCH(".city.name", utf8_string, &city) ||
692 MATCH(".place.name", divinglog_place, &dive->location) ||
696 static int buffer_value(char *buffer)
698 int val = atoi(buffer);
703 static void uemis_length_unit(char *buffer, void *_unused)
705 input_units.length = buffer_value(buffer) ? FEET : METERS;
708 static void uemis_volume_unit(char *buffer, void *_unused)
710 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
713 static void uemis_pressure_unit(char *buffer, void *_unused)
716 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
720 static void uemis_temperature_unit(char *buffer, void *_unused)
722 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
725 static void uemis_weight_unit(char *buffer, void *_unused)
727 input_units.weight = buffer_value(buffer) ? LBS : KG;
730 static void uemis_time_unit(char *buffer, void *_unused)
734 static void uemis_date_unit(char *buffer, void *_unused)
738 /* Modified julian day, yay! */
739 static void uemis_date_time(char *buffer, void *_when)
741 time_t *when = _when;
742 union int_or_float val;
744 switch (integer_or_float(buffer, &val)) {
746 *when = (val.fp - 40587) * 86400;
749 fprintf(stderr, "Strange julian date: %s", buffer);
755 * Uemis doesn't know time zones. You need to do them as
756 * minutes, not hours.
758 * But that's ok, we don't track timezones yet either. We
759 * just turn everything into "localtime expressed as UTC".
761 static void uemis_time_zone(char *buffer, void *_when)
763 #if 0 /* seems like this is only used to display it correctly
764 * the stored time appears to be UTC */
766 time_t *when = _when;
767 signed char tz = atoi(buffer);
773 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
774 static int uemis_gas_template;
777 * Christ. Uemis tank data is a total mess.
779 * We're passed a "virtual cylinder" (0 - 6) for the different
780 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
781 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
782 * into the actual cylinder data depending on the gas template,
783 * and ignore the ones that are irrelevant for that template.
785 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
786 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
787 * (which are the three "nitrox_3" tanks), and we turn virtual
788 * tanks 2/3 into actual tanks 0/1.
792 static int uemis_cylinder_index(void *_cylinder)
794 cylinder_t *cylinder = _cylinder;
795 unsigned int index = cylinder - dive->cylinder;
798 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
801 switch(uemis_gas_template) {
802 case 1: /* Dive uses tank 1 */
805 case 0: /* Dive uses tank 0 */
809 case 2: /* Dive uses tanks 2-3 */
814 case 3: /* Dive uses tanks 4-6 */
823 static void uemis_cylindersize(char *buffer, void *_cylinder)
825 int index = uemis_cylinder_index(_cylinder);
827 cylindersize(buffer, &dive->cylinder[index].type.size);
830 static void uemis_percent(char *buffer, void *_cylinder)
832 int index = uemis_cylinder_index(_cylinder);
834 percent(buffer, &dive->cylinder[index].gasmix.o2);
837 static int uemis_dive_match(struct dive *dive, const char *name, int len, char *buf)
839 return MATCH(".units.length", uemis_length_unit, &input_units) ||
840 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
841 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
842 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
843 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
844 MATCH(".units.time", uemis_time_unit, &input_units) ||
845 MATCH(".units.date", uemis_date_unit, &input_units) ||
846 MATCH(".date_time", uemis_date_time, &dive->when) ||
847 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
848 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
849 MATCH(".gas.template", get_index, &uemis_gas_template) ||
850 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
851 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
852 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
853 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
854 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
855 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
856 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
857 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
858 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
859 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
860 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
861 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
862 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
863 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
868 * Uddf specifies ISO 8601 time format.
870 * There are many variations on that. This handles the useful cases.
872 static void uddf_datetime(char *buffer, void *_when)
876 time_t *when = _when;
877 struct tm tm = { 0 };
880 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
887 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
894 printf("Bad date time %s\n", buffer);
899 if (c != 'T' && c != ' ')
907 *when = utc_mktime(&tm);
911 static int uddf_dive_match(struct dive *dive, const char *name, int len, char *buf)
913 return MATCH(".datetime", uddf_datetime, &dive->when) ||
914 MATCH(".diveduration", duration, &dive->duration) ||
915 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
919 static void gps_location(char *buffer, void *_dive)
922 struct dive *dive = _dive;
923 double latitude, longitude;
925 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
927 dive->latitude = latitude;
928 dive->longitude = longitude;
933 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
934 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
936 int len = strlen(name);
938 start_match("dive", name, buf);
940 switch (import_source) {
942 if (suunto_dive_match(dive, name, len, buf))
947 if (uemis_dive_match(dive, name, len, buf))
952 if (divinglog_dive_match(dive, name, len, buf))
957 if (uddf_dive_match(dive, name, len, buf))
965 if (MATCH(".number", get_index, &dive->number))
967 if (MATCH(".date", divedate, &dive->when))
969 if (MATCH(".time", divetime, &dive->when))
971 if (MATCH(".datetime", divedatetime, &dive->when))
973 if (MATCH(".maxdepth", depth, &dive->maxdepth))
975 if (MATCH(".meandepth", depth, &dive->meandepth))
977 if (MATCH(".depth.max", depth, &dive->maxdepth))
979 if (MATCH(".depth.mean", depth, &dive->meandepth))
981 if (MATCH(".duration", duration, &dive->duration))
983 if (MATCH(".divetime", duration, &dive->duration))
985 if (MATCH(".divetimesec", duration, &dive->duration))
987 if (MATCH(".surfacetime", duration, &dive->surfacetime))
989 if (MATCH(".airtemp", temperature, &dive->airtemp))
991 if (MATCH(".watertemp", temperature, &dive->watertemp))
993 if (MATCH(".temperature.air", temperature, &dive->airtemp))
995 if (MATCH(".temperature.water", temperature, &dive->watertemp))
997 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
999 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1001 if (MATCH(".gps", gps_location, dive))
1003 if (MATCH(".location", utf8_string, &dive->location))
1005 if (MATCH(".notes", utf8_string, &dive->notes))
1007 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1009 if (MATCH(".buddy", utf8_string, &dive->buddy))
1012 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
1014 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
1016 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cylinder_index].type.description))
1018 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cylinder_index].start))
1020 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cylinder_index].end))
1023 if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
1025 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
1027 if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
1030 nonmatch("dive", name, buf);
1034 * File boundaries are dive boundaries. But sometimes there are
1035 * multiple dives per file, so there can be other events too that
1036 * trigger a "new dive" marker and you may get some nesting due
1037 * to that. Just ignore nesting levels.
1039 static void dive_start(void)
1043 dive = alloc_dive();
1044 memset(&tm, 0, sizeof(tm));
1047 static void sanitize_gasmix(struct gasmix *mix)
1049 unsigned int o2, he;
1051 o2 = mix->o2.permille;
1052 he = mix->he.permille;
1054 /* Regular air: leave empty */
1058 /* 20.9% or 21% O2 is just air */
1059 if (o2 >= 209 && o2 <= 210) {
1060 mix->o2.permille = 0;
1066 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
1068 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
1069 memset(mix, 0, sizeof(*mix));
1073 * See if the size/workingpressure looks like some standard cylinder
1076 static void match_standard_cylinder(cylinder_type_t *type)
1081 char buffer[20], *p;
1083 /* Do we already have a cylinder description? */
1084 if (type->description)
1087 cuft = type->size.mliter / 28317.0;
1088 cuft *= to_ATM(type->workingpressure);
1089 psi = type->workingpressure.mbar / 68.95;
1092 case 2300 ... 2500: /* 2400 psi: LP tank */
1095 case 2600 ... 2700: /* 2640 psi: LP+10% */
1098 case 2900 ... 3100: /* 3000 psi: ALx tank */
1101 case 3400 ... 3500: /* 3442 psi: HP tank */
1104 case 3700 ... 3850: /* HP+10% */
1110 len = snprintf(buffer, sizeof(buffer), fmt, (int) (cuft+0.5));
1114 memcpy(p, buffer, len+1);
1115 type->description = p;
1120 * There are two ways to give cylinder size information:
1121 * - total amount of gas in cuft (depends on working pressure and physical size)
1124 * where "physical size" is the one that actually matters and is sane.
1126 * We internally use physical size only. But we save the workingpressure
1127 * so that we can do the conversion if required.
1129 static void sanitize_cylinder_type(cylinder_type_t *type)
1131 double volume_of_air, atm, volume;
1133 /* If we have no working pressure, it had *better* be just a physical size! */
1134 if (!type->workingpressure.mbar)
1137 /* No size either? Nothing to go on */
1138 if (!type->size.mliter)
1141 if (input_units.volume == CUFT || import_source == SUUNTO) {
1142 volume_of_air = type->size.mliter * 28.317; /* milli-cu ft to milliliter */
1143 atm = to_ATM(type->workingpressure); /* working pressure in atm */
1144 volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
1145 type->size.mliter = volume + 0.5;
1148 /* Ok, we have both size and pressure: try to match a description */
1149 match_standard_cylinder(type);
1152 static void sanitize_cylinder_info(struct dive *dive)
1156 for (i = 0; i < MAX_CYLINDERS; i++) {
1157 sanitize_gasmix(&dive->cylinder[i].gasmix);
1158 sanitize_cylinder_type(&dive->cylinder[i].type);
1162 static void dive_end(void)
1166 sanitize_cylinder_info(dive);
1172 static void event_start(void)
1174 memset(&event, 0, sizeof(event));
1178 static void event_end(void)
1180 if (event.name && strcmp(event.name, "surface") != 0)
1181 add_event(dive, event.time.seconds, event.type, event.flags, event.value, event.name);
1185 static void cylinder_start(void)
1189 static void cylinder_end(void)
1194 static void sample_start(void)
1196 sample = prepare_sample(&dive);
1199 static void sample_end(void)
1204 finish_sample(dive, sample);
1208 static void entry(const char *name, int size, const char *raw)
1210 char *buf = malloc(size+1);
1214 memcpy(buf, raw, size);
1217 try_to_fill_event(name, buf);
1221 try_to_fill_sample(sample, name, buf);
1225 try_to_fill_dive(dive, name, buf);
1230 static const char *nodename(xmlNode *node, char *buf, int len)
1232 if (!node || !node->name)
1240 const char *name = node->name;
1241 int i = strlen(name);
1243 unsigned char c = name[i];
1244 *--buf = tolower(c);
1248 node = node->parent;
1249 if (!node || !node->name)
1259 static void visit_one_node(xmlNode *node)
1262 const unsigned char *content;
1263 char buffer[MAXNAME];
1266 content = node->content;
1270 /* Trim whitespace at beginning */
1271 while (isspace(*content))
1274 /* Trim whitespace at end */
1275 len = strlen(content);
1276 while (len && isspace(content[len-1]))
1282 /* Don't print out the node name if it is "text" */
1283 if (!strcmp(node->name, "text"))
1284 node = node->parent;
1286 name = nodename(node, buffer, sizeof(buffer));
1288 entry(name, len, content);
1291 static void traverse(xmlNode *root);
1293 static void traverse_properties(xmlNode *node)
1297 for (p = node->properties; p; p = p->next)
1298 traverse(p->children);
1301 static void visit(xmlNode *n)
1304 traverse_properties(n);
1305 traverse(n->children);
1308 static void suunto_importer(void)
1310 import_source = SUUNTO;
1311 input_units = SI_units;
1314 static void uemis_importer(void)
1316 import_source = UEMIS;
1317 input_units = SI_units;
1320 static void DivingLog_importer(void)
1322 import_source = DIVINGLOG;
1325 * Diving Log units are really strange.
1327 * Temperatures are in C, except in samples,
1328 * when they are in Fahrenheit. Depths are in
1329 * meters, an dpressure is in PSI in the samples,
1330 * but in bar when it comes to working pressure.
1332 * Crazy f*%^ morons.
1334 input_units = SI_units;
1337 static void uddf_importer(void)
1339 import_source = UDDF;
1340 input_units = SI_units;
1341 input_units.pressure = PASCAL;
1342 input_units.temperature = KELVIN;
1346 * I'm sure this could be done as some fancy DTD rules.
1347 * It's just not worth the headache.
1349 static struct nesting {
1351 void (*start)(void), (*end)(void);
1353 { "dive", dive_start, dive_end },
1354 { "Dive", dive_start, dive_end },
1355 { "sample", sample_start, sample_end },
1356 { "waypoint", sample_start, sample_end },
1357 { "SAMPLE", sample_start, sample_end },
1358 { "reading", sample_start, sample_end },
1359 { "event", event_start, event_end },
1360 { "gasmix", cylinder_start, cylinder_end },
1361 { "cylinder", cylinder_start, cylinder_end },
1362 { "P", sample_start, sample_end },
1364 /* Import type recognition */
1365 { "SUUNTO", suunto_importer },
1366 { "Divinglog", DivingLog_importer },
1367 { "pre_dive", uemis_importer },
1368 { "uddf", uddf_importer },
1373 static void traverse(xmlNode *root)
1377 for (n = root; n; n = n->next) {
1378 struct nesting *rule = nesting;
1381 if (!strcmp(rule->name, n->name))
1384 } while (rule->name);
1394 /* Per-file reset */
1395 static void reset_all(void)
1398 * We reset the units for each file. You'd think it was
1399 * a per-dive property, but I'm not going to trust people
1400 * to do per-dive setup. If the xml does have per-dive
1401 * data within one file, we might have to reset it per
1402 * dive for that format.
1404 input_units = SI_units;
1405 import_source = UNKNOWN;
1408 void parse_xml_file(const char *filename, GError **error)
1412 doc = xmlReadFile(filename, NULL, 0);
1414 fprintf(stderr, "Failed to parse '%s'.\n", filename);
1417 *error = g_error_new(g_quark_from_string("subsurface"),
1419 "Failed to parse '%s'",
1424 /* we assume that the last (or only) filename passed as argument is a
1425 * great filename to use as default when saving the dives */
1426 set_filename(filename);
1429 traverse(xmlDocGetRootElement(doc));
1435 void parse_xml_init(void)