7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
15 struct dive_table dive_table;
18 * Add a dive into the dive_table array
20 void record_dive(struct dive *dive)
22 int nr = dive_table.nr, allocated = dive_table.allocated;
23 struct dive **dives = dive_table.dives;
25 if (nr >= allocated) {
26 allocated = (nr + 32) * 3 / 2;
27 dives = realloc(dives, allocated * sizeof(struct dive *));
30 dive_table.dives = dives;
31 dive_table.allocated = allocated;
33 dives[nr] = fixup_dive(dive);
37 static void start_match(const char *type, const char *name, char *buffer)
40 printf("Matching %s '%s' (%s)\n",
44 static void nonmatch(const char *type, const char *name, char *buffer)
47 printf("Unable to match %s '%s' (%s)\n",
52 typedef void (*matchfn_t)(char *buffer, void *);
54 static int match(const char *pattern, int plen,
55 const char *name, int nlen,
56 matchfn_t fn, char *buf, void *data)
60 if (memcmp(pattern, name + nlen - plen, plen))
67 struct units input_units;
70 * We're going to default to SI units for input. Yes,
71 * technically the SI unit for pressure is Pascal, but
72 * we default to bar (10^5 pascal), which people
73 * actually use. Similarly, C instead of Kelvin.
75 const struct units SI_units = {
79 .temperature = CELSIUS,
83 const struct units IMPERIAL_units = {
87 .temperature = FAHRENHEIT,
92 * Dive info as it is being built up..
94 static struct dive *dive;
95 static struct sample *sample;
99 int type, flags, value;
103 static int cylinder_index;
105 static enum import_source {
114 time_t utc_mktime(struct tm *tm)
116 static const int mdays[] = {
117 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
119 int year = tm->tm_year;
120 int month = tm->tm_mon;
121 int day = tm->tm_mday;
123 /* First normalize relative to 1900 */
126 else if (year > 1900)
129 /* Normalized to Jan 1, 1970: unix time */
132 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
134 if (month < 0 || month > 11) /* array bounds */
136 if (month < 2 || (year + 2) % 4)
138 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
140 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
141 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
144 static void divedate(char *buffer, void *_when)
147 time_t *when = _when;
150 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
151 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
155 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
160 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
165 *when = utc_mktime(&tm);
170 static void divetime(char *buffer, void *_when)
173 time_t *when = _when;
175 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
180 *when = utc_mktime(&tm);
185 /* Libdivecomputer: "2011-03-20 10:22:38" */
186 static void divedatetime(char *buffer, void *_when)
190 time_t *when = _when;
192 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
193 &y, &m, &d, &hr, &min, &sec) == 6) {
200 *when = utc_mktime(&tm);
214 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
220 /* Integer or floating point? */
221 val = strtol(buffer, &end, 10);
222 if (val < 0 || end == buffer)
225 /* Looks like it might be floating point? */
228 fp = strtod(buffer, &end);
239 static void pressure(char *buffer, void *_press)
242 pressure_t *pressure = _press;
243 union int_or_float val;
245 switch (integer_or_float(buffer, &val)) {
247 /* Just ignore zero values */
250 switch (input_units.pressure) {
255 /* Assume mbar, but if it's really small, it's bar */
261 mbar = val.fp * 68.95;
264 if (mbar > 5 && mbar < 500000) {
265 pressure->mbar = mbar + 0.5;
270 printf("Strange pressure reading %s\n", buffer);
275 static void depth(char *buffer, void *_depth)
277 depth_t *depth = _depth;
278 union int_or_float val;
280 switch (integer_or_float(buffer, &val)) {
282 switch (input_units.length) {
284 depth->mm = val.fp * 1000 + 0.5;
287 depth->mm = val.fp * 304.8 + 0.5;
292 printf("Strange depth reading %s\n", buffer);
297 static void temperature(char *buffer, void *_temperature)
299 temperature_t *temperature = _temperature;
300 union int_or_float val;
302 switch (integer_or_float(buffer, &val)) {
304 /* Ignore zero. It means "none" */
308 switch (input_units.temperature) {
310 temperature->mkelvin = val.fp * 1000;
313 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
316 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
321 printf("Strange temperature reading %s\n", buffer);
326 static void sampletime(char *buffer, void *_time)
330 duration_t *time = _time;
332 i = sscanf(buffer, "%d:%d", &min, &sec);
339 time->seconds = sec + min*60;
342 printf("Strange sample time reading %s\n", buffer);
347 static void duration(char *buffer, void *_time)
349 sampletime(buffer, _time);
352 static void percent(char *buffer, void *_fraction)
354 fraction_t *fraction = _fraction;
355 union int_or_float val;
357 switch (integer_or_float(buffer, &val)) {
360 fraction->permille = val.fp * 10 + 0.5;
364 printf("Strange percentage reading %s\n", buffer);
370 static void gasmix(char *buffer, void *_fraction)
372 /* libdivecomputer does negative percentages. */
375 if (cylinder_index < MAX_CYLINDERS)
376 percent(buffer, _fraction);
379 static void gasmix_nitrogen(char *buffer, void *_gasmix)
381 /* Ignore n2 percentages. There's no value in them. */
384 static void cylindersize(char *buffer, void *_volume)
386 volume_t *volume = _volume;
387 union int_or_float val;
389 switch (integer_or_float(buffer, &val)) {
391 volume->mliter = val.fp * 1000 + 0.5;
395 printf("Strange volume reading %s\n", buffer);
401 static void utf8_string(char *buffer, void *_res)
403 *(char **)_res = buffer;
407 * Uemis water_pressure. In centibar. And when converting to
408 * depth, I'm just going to always use saltwater, because I
409 * think "true depth" is just stupid. From a diving standpoint,
410 * "true depth" is pretty much completely pointless, unless
411 * you're doing some kind of underwater surveying work.
413 * So I give water depths in "pressure depth", always assuming
414 * salt water. So one atmosphere per 10m.
416 static void water_pressure(char *buffer, void *_depth)
418 depth_t *depth = _depth;
419 union int_or_float val;
422 switch (integer_or_float(buffer, &val)) {
427 atm = (val.fp / 100) / 1.01325;
429 * atm to cm. Why not mm? The precision just isn't
432 cm = 100 * atm + 0.5;
434 depth->mm = 10 * (long)cm;
438 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
443 #define MATCH(pattern, fn, dest) \
444 match(pattern, strlen(pattern), name, len, fn, buf, dest)
446 static void get_index(char *buffer, void *_i)
453 static void centibar(char *buffer, void *_pressure)
455 pressure_t *pressure = _pressure;
456 union int_or_float val;
458 switch (integer_or_float(buffer, &val)) {
460 pressure->mbar = val.fp * 10 + 0.5;
463 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
468 static void decicelsius(char *buffer, void *_temp)
470 temperature_t *temp = _temp;
471 union int_or_float val;
473 switch (integer_or_float(buffer, &val)) {
475 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
478 fprintf(stderr, "Strange julian date: %s", buffer);
483 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
485 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
486 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
487 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
488 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
489 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
494 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
495 * for the sample temperatures, that are in Fahrenheit.
498 * Oh, and I think Diving Log *internally* probably kept them
499 * in celsius, because I'm seeing entries like
503 * in there. Which is freezing, aka 0 degC. I bet the "0" is
504 * what Diving Log uses for "no temperature".
506 * So throw away crap like that.
508 static void fahrenheit(char *buffer, void *_temperature)
510 temperature_t *temperature = _temperature;
511 union int_or_float val;
513 switch (integer_or_float(buffer, &val)) {
515 /* Floating point equality is evil, but works for small integers */
518 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
521 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
527 * Did I mention how bat-shit crazy divinglog is? The sample
528 * pressures are in PSI. But the tank working pressure is in
531 * Crazy stuff like this is why subsurface has everything in
532 * these inconvenient typed structures, and you have to say
533 * "pressure->mbar" to get the actual value. Exactly so that
534 * you can never have unit confusion.
536 static void psi(char *buffer, void *_pressure)
538 pressure_t *pressure = _pressure;
539 union int_or_float val;
541 switch (integer_or_float(buffer, &val)) {
543 pressure->mbar = val.fp * 68.95 + 0.5;
546 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
551 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
553 return MATCH(".p.time", sampletime, &sample->time) ||
554 MATCH(".p.depth", depth, &sample->depth) ||
555 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
556 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
560 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
562 return MATCH(".divetime", sampletime, &sample->time) ||
563 MATCH(".depth", depth, &sample->depth) ||
564 MATCH(".temperature", temperature, &sample->temperature) ||
568 static void eventtime(char *buffer, void *_duration)
570 duration_t *duration = _duration;
571 sampletime(buffer, duration);
573 duration->seconds += sample->time.seconds;
576 static void try_to_fill_event(const char *name, char *buf)
578 int len = strlen(name);
580 start_match("event", name, buf);
581 if (MATCH(".event", utf8_string, &event.name))
583 if (MATCH(".name", utf8_string, &event.name))
585 if (MATCH(".time", eventtime, &event.time))
587 if (MATCH(".type", get_index, &event.type))
589 if (MATCH(".flags", get_index, &event.flags))
591 if (MATCH(".value", get_index, &event.value))
593 nonmatch("event", name, buf);
596 /* We're in samples - try to convert the random xml value to something useful */
597 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
599 int len = strlen(name);
601 start_match("sample", name, buf);
602 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
604 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
606 if (MATCH(".sample.depth", depth, &sample->depth))
608 if (MATCH(".sample.temp", temperature, &sample->temperature))
610 if (MATCH(".sample.temperature", temperature, &sample->temperature))
612 if (MATCH(".sample.sampletime", sampletime, &sample->time))
614 if (MATCH(".sample.time", sampletime, &sample->time))
617 switch (import_source) {
619 if (uemis_fill_sample(sample, name, len, buf))
624 if (divinglog_fill_sample(sample, name, len, buf))
629 if (uddf_fill_sample(sample, name, len, buf))
637 nonmatch("sample", name, buf);
641 * Crazy suunto xml. Look at how those o2/he things match up.
643 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
645 return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
646 MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
647 MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
648 MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
649 MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
650 MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
651 MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
652 MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
653 MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
654 MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
658 static const char *country, *city;
660 static void divinglog_place(char *place, void *_location)
662 char **location = _location;
663 char buffer[256], *p;
666 len = snprintf(buffer, sizeof(buffer),
672 country ? country : "");
675 memcpy(p, buffer, len+1);
682 static int divinglog_dive_match(struct dive *dive, const char *name, int len, char *buf)
684 return MATCH(".divedate", divedate, &dive->when) ||
685 MATCH(".entrytime", divetime, &dive->when) ||
686 MATCH(".depth", depth, &dive->maxdepth) ||
687 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
688 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
689 MATCH(".comments", utf8_string, &dive->notes) ||
690 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
691 MATCH(".country.name", utf8_string, &country) ||
692 MATCH(".city.name", utf8_string, &city) ||
693 MATCH(".place.name", divinglog_place, &dive->location) ||
697 static int buffer_value(char *buffer)
699 int val = atoi(buffer);
704 static void uemis_length_unit(char *buffer, void *_unused)
706 input_units.length = buffer_value(buffer) ? FEET : METERS;
709 static void uemis_volume_unit(char *buffer, void *_unused)
711 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
714 static void uemis_pressure_unit(char *buffer, void *_unused)
717 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
721 static void uemis_temperature_unit(char *buffer, void *_unused)
723 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
726 static void uemis_weight_unit(char *buffer, void *_unused)
728 input_units.weight = buffer_value(buffer) ? LBS : KG;
731 static void uemis_time_unit(char *buffer, void *_unused)
735 static void uemis_date_unit(char *buffer, void *_unused)
739 /* Modified julian day, yay! */
740 static void uemis_date_time(char *buffer, void *_when)
742 time_t *when = _when;
743 union int_or_float val;
745 switch (integer_or_float(buffer, &val)) {
747 *when = (val.fp - 40587) * 86400;
750 fprintf(stderr, "Strange julian date: %s", buffer);
756 * Uemis doesn't know time zones. You need to do them as
757 * minutes, not hours.
759 * But that's ok, we don't track timezones yet either. We
760 * just turn everything into "localtime expressed as UTC".
762 static void uemis_time_zone(char *buffer, void *_when)
764 #if 0 /* seems like this is only used to display it correctly
765 * the stored time appears to be UTC */
767 time_t *when = _when;
768 signed char tz = atoi(buffer);
774 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
775 static int uemis_gas_template;
778 * Christ. Uemis tank data is a total mess.
780 * We're passed a "virtual cylinder" (0 - 6) for the different
781 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
782 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
783 * into the actual cylinder data depending on the gas template,
784 * and ignore the ones that are irrelevant for that template.
786 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
787 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
788 * (which are the three "nitrox_3" tanks), and we turn virtual
789 * tanks 2/3 into actual tanks 0/1.
793 static int uemis_cylinder_index(void *_cylinder)
795 cylinder_t *cylinder = _cylinder;
796 unsigned int index = cylinder - dive->cylinder;
799 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
802 switch(uemis_gas_template) {
803 case 1: /* Dive uses tank 1 */
806 case 0: /* Dive uses tank 0 */
810 case 2: /* Dive uses tanks 2-3 */
815 case 3: /* Dive uses tanks 4-6 */
824 static void uemis_cylindersize(char *buffer, void *_cylinder)
826 int index = uemis_cylinder_index(_cylinder);
828 cylindersize(buffer, &dive->cylinder[index].type.size);
831 static void uemis_percent(char *buffer, void *_cylinder)
833 int index = uemis_cylinder_index(_cylinder);
835 percent(buffer, &dive->cylinder[index].gasmix.o2);
838 static int uemis_dive_match(struct dive *dive, const char *name, int len, char *buf)
840 return MATCH(".units.length", uemis_length_unit, &input_units) ||
841 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
842 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
843 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
844 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
845 MATCH(".units.time", uemis_time_unit, &input_units) ||
846 MATCH(".units.date", uemis_date_unit, &input_units) ||
847 MATCH(".date_time", uemis_date_time, &dive->when) ||
848 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
849 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
850 MATCH(".gas.template", get_index, &uemis_gas_template) ||
851 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
852 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
853 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
854 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
855 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
856 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
857 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
858 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
859 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
860 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
861 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
862 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
863 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
864 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
865 MATCH(".dive.val.bin", uemis_parse_divelog_binary, &dive) ||
870 * Uddf specifies ISO 8601 time format.
872 * There are many variations on that. This handles the useful cases.
874 static void uddf_datetime(char *buffer, void *_when)
878 time_t *when = _when;
879 struct tm tm = { 0 };
882 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
889 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
896 printf("Bad date time %s\n", buffer);
901 if (c != 'T' && c != ' ')
909 *when = utc_mktime(&tm);
913 static int uddf_dive_match(struct dive *dive, const char *name, int len, char *buf)
915 return MATCH(".datetime", uddf_datetime, &dive->when) ||
916 MATCH(".diveduration", duration, &dive->duration) ||
917 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
921 static void gps_location(char *buffer, void *_dive)
924 struct dive *dive = _dive;
925 double latitude, longitude;
927 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
929 dive->latitude = latitude;
930 dive->longitude = longitude;
935 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
936 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
938 int len = strlen(name);
940 start_match("dive", name, buf);
942 switch (import_source) {
944 if (suunto_dive_match(dive, name, len, buf))
949 if (uemis_dive_match(dive, name, len, buf))
954 if (divinglog_dive_match(dive, name, len, buf))
959 if (uddf_dive_match(dive, name, len, buf))
967 if (MATCH(".number", get_index, &dive->number))
969 if (MATCH(".date", divedate, &dive->when))
971 if (MATCH(".time", divetime, &dive->when))
973 if (MATCH(".datetime", divedatetime, &dive->when))
975 if (MATCH(".maxdepth", depth, &dive->maxdepth))
977 if (MATCH(".meandepth", depth, &dive->meandepth))
979 if (MATCH(".depth.max", depth, &dive->maxdepth))
981 if (MATCH(".depth.mean", depth, &dive->meandepth))
983 if (MATCH(".duration", duration, &dive->duration))
985 if (MATCH(".divetime", duration, &dive->duration))
987 if (MATCH(".divetimesec", duration, &dive->duration))
989 if (MATCH(".surfacetime", duration, &dive->surfacetime))
991 if (MATCH(".airtemp", temperature, &dive->airtemp))
993 if (MATCH(".watertemp", temperature, &dive->watertemp))
995 if (MATCH(".temperature.air", temperature, &dive->airtemp))
997 if (MATCH(".temperature.water", temperature, &dive->watertemp))
999 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
1001 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1003 if (MATCH(".gps", gps_location, dive))
1005 if (MATCH(".location", utf8_string, &dive->location))
1007 if (MATCH(".notes", utf8_string, &dive->notes))
1009 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1011 if (MATCH(".buddy", utf8_string, &dive->buddy))
1014 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
1016 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
1018 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cylinder_index].type.description))
1020 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cylinder_index].start))
1022 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cylinder_index].end))
1025 if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
1027 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
1029 if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
1032 nonmatch("dive", name, buf);
1036 * File boundaries are dive boundaries. But sometimes there are
1037 * multiple dives per file, so there can be other events too that
1038 * trigger a "new dive" marker and you may get some nesting due
1039 * to that. Just ignore nesting levels.
1041 static void dive_start(void)
1045 dive = alloc_dive();
1046 memset(&tm, 0, sizeof(tm));
1049 static void sanitize_gasmix(struct gasmix *mix)
1051 unsigned int o2, he;
1053 o2 = mix->o2.permille;
1054 he = mix->he.permille;
1056 /* Regular air: leave empty */
1060 /* 20.9% or 21% O2 is just air */
1061 if (o2 >= 209 && o2 <= 210) {
1062 mix->o2.permille = 0;
1068 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
1070 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
1071 memset(mix, 0, sizeof(*mix));
1075 * See if the size/workingpressure looks like some standard cylinder
1078 static void match_standard_cylinder(cylinder_type_t *type)
1083 char buffer[20], *p;
1085 /* Do we already have a cylinder description? */
1086 if (type->description)
1089 cuft = type->size.mliter / 28317.0;
1090 cuft *= to_ATM(type->workingpressure);
1091 psi = type->workingpressure.mbar / 68.95;
1094 case 2300 ... 2500: /* 2400 psi: LP tank */
1097 case 2600 ... 2700: /* 2640 psi: LP+10% */
1100 case 2900 ... 3100: /* 3000 psi: ALx tank */
1103 case 3400 ... 3500: /* 3442 psi: HP tank */
1106 case 3700 ... 3850: /* HP+10% */
1112 len = snprintf(buffer, sizeof(buffer), fmt, (int) (cuft+0.5));
1116 memcpy(p, buffer, len+1);
1117 type->description = p;
1122 * There are two ways to give cylinder size information:
1123 * - total amount of gas in cuft (depends on working pressure and physical size)
1126 * where "physical size" is the one that actually matters and is sane.
1128 * We internally use physical size only. But we save the workingpressure
1129 * so that we can do the conversion if required.
1131 static void sanitize_cylinder_type(cylinder_type_t *type)
1133 double volume_of_air, atm, volume;
1135 /* If we have no working pressure, it had *better* be just a physical size! */
1136 if (!type->workingpressure.mbar)
1139 /* No size either? Nothing to go on */
1140 if (!type->size.mliter)
1143 if (input_units.volume == CUFT || import_source == SUUNTO) {
1144 volume_of_air = type->size.mliter * 28.317; /* milli-cu ft to milliliter */
1145 atm = to_ATM(type->workingpressure); /* working pressure in atm */
1146 volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
1147 type->size.mliter = volume + 0.5;
1150 /* Ok, we have both size and pressure: try to match a description */
1151 match_standard_cylinder(type);
1154 static void sanitize_cylinder_info(struct dive *dive)
1158 for (i = 0; i < MAX_CYLINDERS; i++) {
1159 sanitize_gasmix(&dive->cylinder[i].gasmix);
1160 sanitize_cylinder_type(&dive->cylinder[i].type);
1164 static void dive_end(void)
1168 sanitize_cylinder_info(dive);
1174 static void event_start(void)
1176 memset(&event, 0, sizeof(event));
1180 static void event_end(void)
1182 if (event.name && strcmp(event.name, "surface") != 0)
1183 add_event(dive, event.time.seconds, event.type, event.flags, event.value, event.name);
1187 static void cylinder_start(void)
1191 static void cylinder_end(void)
1196 static void sample_start(void)
1198 sample = prepare_sample(&dive);
1201 static void sample_end(void)
1206 finish_sample(dive, sample);
1210 static void entry(const char *name, int size, const char *raw)
1212 char *buf = malloc(size+1);
1216 memcpy(buf, raw, size);
1219 try_to_fill_event(name, buf);
1223 try_to_fill_sample(sample, name, buf);
1227 try_to_fill_dive(dive, name, buf);
1232 static const char *nodename(xmlNode *node, char *buf, int len)
1234 if (!node || !node->name)
1242 const char *name = node->name;
1243 int i = strlen(name);
1245 unsigned char c = name[i];
1246 *--buf = tolower(c);
1250 node = node->parent;
1251 if (!node || !node->name)
1261 static void visit_one_node(xmlNode *node)
1264 const unsigned char *content;
1265 char buffer[MAXNAME];
1268 content = node->content;
1272 /* Trim whitespace at beginning */
1273 while (isspace(*content))
1276 /* Trim whitespace at end */
1277 len = strlen(content);
1278 while (len && isspace(content[len-1]))
1284 /* Don't print out the node name if it is "text" */
1285 if (!strcmp(node->name, "text"))
1286 node = node->parent;
1288 name = nodename(node, buffer, sizeof(buffer));
1290 entry(name, len, content);
1293 static void traverse(xmlNode *root);
1295 static void traverse_properties(xmlNode *node)
1299 for (p = node->properties; p; p = p->next)
1300 traverse(p->children);
1303 static void visit(xmlNode *n)
1306 traverse_properties(n);
1307 traverse(n->children);
1310 static void suunto_importer(void)
1312 import_source = SUUNTO;
1313 input_units = SI_units;
1316 static void uemis_importer(void)
1318 import_source = UEMIS;
1319 input_units = SI_units;
1322 static void DivingLog_importer(void)
1324 import_source = DIVINGLOG;
1327 * Diving Log units are really strange.
1329 * Temperatures are in C, except in samples,
1330 * when they are in Fahrenheit. Depths are in
1331 * meters, an dpressure is in PSI in the samples,
1332 * but in bar when it comes to working pressure.
1334 * Crazy f*%^ morons.
1336 input_units = SI_units;
1339 static void uddf_importer(void)
1341 import_source = UDDF;
1342 input_units = SI_units;
1343 input_units.pressure = PASCAL;
1344 input_units.temperature = KELVIN;
1348 * I'm sure this could be done as some fancy DTD rules.
1349 * It's just not worth the headache.
1351 static struct nesting {
1353 void (*start)(void), (*end)(void);
1355 { "dive", dive_start, dive_end },
1356 { "Dive", dive_start, dive_end },
1357 { "sample", sample_start, sample_end },
1358 { "waypoint", sample_start, sample_end },
1359 { "SAMPLE", sample_start, sample_end },
1360 { "reading", sample_start, sample_end },
1361 { "event", event_start, event_end },
1362 { "gasmix", cylinder_start, cylinder_end },
1363 { "cylinder", cylinder_start, cylinder_end },
1364 { "P", sample_start, sample_end },
1366 /* Import type recognition */
1367 { "SUUNTO", suunto_importer },
1368 { "Divinglog", DivingLog_importer },
1369 { "pre_dive", uemis_importer },
1370 { "dives", uemis_importer },
1371 { "uddf", uddf_importer },
1376 static void traverse(xmlNode *root)
1380 for (n = root; n; n = n->next) {
1381 struct nesting *rule = nesting;
1384 if (!strcmp(rule->name, n->name))
1387 } while (rule->name);
1397 /* Per-file reset */
1398 static void reset_all(void)
1401 * We reset the units for each file. You'd think it was
1402 * a per-dive property, but I'm not going to trust people
1403 * to do per-dive setup. If the xml does have per-dive
1404 * data within one file, we might have to reset it per
1405 * dive for that format.
1407 input_units = SI_units;
1408 import_source = UNKNOWN;
1411 void parse_xml_file(const char *filename, GError **error)
1415 doc = xmlReadFile(filename, NULL, 0);
1417 fprintf(stderr, "Failed to parse '%s'.\n", filename);
1420 *error = g_error_new(g_quark_from_string("subsurface"),
1422 "Failed to parse '%s'",
1427 /* we assume that the last (or only) filename passed as argument is a
1428 * great filename to use as default when saving the dives */
1429 set_filename(filename);
1432 traverse(xmlDocGetRootElement(doc));
1438 void parse_xml_init(void)