9 #include <libxml/parser.h>
10 #include <libxml/tree.h>
12 #include <libxslt/transform.h>
20 struct dive_table dive_table;
23 * Add a dive into the dive_table array
25 void record_dive(struct dive *dive)
27 int nr = dive_table.nr, allocated = dive_table.allocated;
28 struct dive **dives = dive_table.dives;
30 if (nr >= allocated) {
31 allocated = (nr + 32) * 3 / 2;
32 dives = realloc(dives, allocated * sizeof(struct dive *));
35 dive_table.dives = dives;
36 dive_table.allocated = allocated;
38 dives[nr] = fixup_dive(dive);
42 static void start_match(const char *type, const char *name, char *buffer)
45 printf("Matching %s '%s' (%s)\n",
49 static void nonmatch(const char *type, const char *name, char *buffer)
52 printf("Unable to match %s '%s' (%s)\n",
57 typedef void (*matchfn_t)(char *buffer, void *);
59 static int match(const char *pattern, int plen,
60 const char *name, int nlen,
61 matchfn_t fn, char *buf, void *data)
65 if (memcmp(pattern, name + nlen - plen, plen))
72 struct units input_units;
75 * We're going to default to SI units for input. Yes,
76 * technically the SI unit for pressure is Pascal, but
77 * we default to bar (10^5 pascal), which people
78 * actually use. Similarly, C instead of Kelvin.
80 const struct units SI_units = {
84 .temperature = CELSIUS,
88 const struct units IMPERIAL_units = {
92 .temperature = FAHRENHEIT,
97 * Dive info as it is being built up..
99 static struct dive *dive;
100 static struct sample *sample;
104 int type, flags, value;
108 static int cylinder_index;
110 static enum import_source {
118 time_t utc_mktime(struct tm *tm)
120 static const int mdays[] = {
121 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
123 int year = tm->tm_year;
124 int month = tm->tm_mon;
125 int day = tm->tm_mday;
127 /* First normalize relative to 1900 */
130 else if (year > 1900)
133 /* Normalized to Jan 1, 1970: unix time */
136 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
138 if (month < 0 || month > 11) /* array bounds */
140 if (month < 2 || (year + 2) % 4)
142 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
144 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
145 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
148 static void divedate(char *buffer, void *_when)
151 time_t *when = _when;
154 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
155 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
159 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
164 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
169 *when = utc_mktime(&tm);
174 static void divetime(char *buffer, void *_when)
177 time_t *when = _when;
179 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
184 *when = utc_mktime(&tm);
189 /* Libdivecomputer: "2011-03-20 10:22:38" */
190 static void divedatetime(char *buffer, void *_when)
194 time_t *when = _when;
196 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
197 &y, &m, &d, &hr, &min, &sec) == 6) {
204 *when = utc_mktime(&tm);
218 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
224 /* Integer or floating point? */
225 val = strtol(buffer, &end, 10);
226 if (val < 0 || end == buffer)
229 /* Looks like it might be floating point? */
232 fp = strtod(buffer, &end);
243 static void pressure(char *buffer, void *_press)
246 pressure_t *pressure = _press;
247 union int_or_float val;
249 switch (integer_or_float(buffer, &val)) {
251 /* Just ignore zero values */
254 switch (input_units.pressure) {
259 /* Assume mbar, but if it's really small, it's bar */
265 mbar = val.fp * 68.95;
268 if (mbar > 5 && mbar < 500000) {
269 pressure->mbar = mbar + 0.5;
274 printf("Strange pressure reading %s\n", buffer);
279 static void depth(char *buffer, void *_depth)
281 depth_t *depth = _depth;
282 union int_or_float val;
284 switch (integer_or_float(buffer, &val)) {
286 switch (input_units.length) {
288 depth->mm = val.fp * 1000 + 0.5;
291 depth->mm = val.fp * 304.8 + 0.5;
296 printf("Strange depth reading %s\n", buffer);
301 static void temperature(char *buffer, void *_temperature)
303 temperature_t *temperature = _temperature;
304 union int_or_float val;
306 switch (integer_or_float(buffer, &val)) {
308 /* Ignore zero. It means "none" */
312 switch (input_units.temperature) {
314 temperature->mkelvin = val.fp * 1000;
317 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
320 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
325 printf("Strange temperature reading %s\n", buffer);
330 static void sampletime(char *buffer, void *_time)
334 duration_t *time = _time;
336 i = sscanf(buffer, "%d:%d", &min, &sec);
343 time->seconds = sec + min*60;
346 printf("Strange sample time reading %s\n", buffer);
351 static void duration(char *buffer, void *_time)
353 sampletime(buffer, _time);
356 static void percent(char *buffer, void *_fraction)
358 fraction_t *fraction = _fraction;
359 union int_or_float val;
361 switch (integer_or_float(buffer, &val)) {
364 fraction->permille = val.fp * 10 + 0.5;
368 printf("Strange percentage reading %s\n", buffer);
374 static void gasmix(char *buffer, void *_fraction)
376 /* libdivecomputer does negative percentages. */
379 if (cylinder_index < MAX_CYLINDERS)
380 percent(buffer, _fraction);
383 static void gasmix_nitrogen(char *buffer, void *_gasmix)
385 /* Ignore n2 percentages. There's no value in them. */
388 static void cylindersize(char *buffer, void *_volume)
390 volume_t *volume = _volume;
391 union int_or_float val;
393 switch (integer_or_float(buffer, &val)) {
395 volume->mliter = val.fp * 1000 + 0.5;
399 printf("Strange volume reading %s\n", buffer);
405 static void utf8_string(char *buffer, void *_res)
407 *(char **)_res = buffer;
411 * Uemis water_pressure. In centibar. And when converting to
412 * depth, I'm just going to always use saltwater, because I
413 * think "true depth" is just stupid. From a diving standpoint,
414 * "true depth" is pretty much completely pointless, unless
415 * you're doing some kind of underwater surveying work.
417 * So I give water depths in "pressure depth", always assuming
418 * salt water. So one atmosphere per 10m.
420 static void water_pressure(char *buffer, void *_depth)
422 depth_t *depth = _depth;
423 union int_or_float val;
426 switch (integer_or_float(buffer, &val)) {
431 atm = bar_to_atm(val.fp * 10);
433 * atm to cm. Why not mm? The precision just isn't
436 cm = 100 * atm + 0.5;
438 depth->mm = 10 * (long)cm;
442 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
447 #define MATCH(pattern, fn, dest) \
448 match(pattern, strlen(pattern), name, len, fn, buf, dest)
450 static void get_index(char *buffer, void *_i)
457 static void centibar(char *buffer, void *_pressure)
459 pressure_t *pressure = _pressure;
460 union int_or_float val;
462 switch (integer_or_float(buffer, &val)) {
464 pressure->mbar = val.fp * 10 + 0.5;
467 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
472 static void decicelsius(char *buffer, void *_temp)
474 temperature_t *temp = _temp;
475 union int_or_float val;
477 switch (integer_or_float(buffer, &val)) {
479 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
482 fprintf(stderr, "Strange julian date: %s", buffer);
487 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
489 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
490 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
491 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
492 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
493 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
498 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
499 * for the sample temperatures, that are in Fahrenheit.
502 * Oh, and I think Diving Log *internally* probably kept them
503 * in celsius, because I'm seeing entries like
507 * in there. Which is freezing, aka 0 degC. I bet the "0" is
508 * what Diving Log uses for "no temperature".
510 * So throw away crap like that.
512 static void fahrenheit(char *buffer, void *_temperature)
514 temperature_t *temperature = _temperature;
515 union int_or_float val;
517 switch (integer_or_float(buffer, &val)) {
519 /* Floating point equality is evil, but works for small integers */
522 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
525 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
531 * Did I mention how bat-shit crazy divinglog is? The sample
532 * pressures are in PSI. But the tank working pressure is in
535 * Crazy stuff like this is why subsurface has everything in
536 * these inconvenient typed structures, and you have to say
537 * "pressure->mbar" to get the actual value. Exactly so that
538 * you can never have unit confusion.
540 static void psi(char *buffer, void *_pressure)
542 pressure_t *pressure = _pressure;
543 union int_or_float val;
545 switch (integer_or_float(buffer, &val)) {
547 pressure->mbar = val.fp * 68.95 + 0.5;
550 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
555 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
557 return MATCH(".p.time", sampletime, &sample->time) ||
558 MATCH(".p.depth", depth, &sample->depth) ||
559 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
560 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
564 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
566 return MATCH(".divetime", sampletime, &sample->time) ||
567 MATCH(".depth", depth, &sample->depth) ||
568 MATCH(".temperature", temperature, &sample->temperature) ||
572 static void eventtime(char *buffer, void *_duration)
574 duration_t *duration = _duration;
575 sampletime(buffer, duration);
577 duration->seconds += sample->time.seconds;
580 static void try_to_fill_event(const char *name, char *buf)
582 int len = strlen(name);
584 start_match("event", name, buf);
585 if (MATCH(".event", utf8_string, &event.name))
587 if (MATCH(".name", utf8_string, &event.name))
589 if (MATCH(".time", eventtime, &event.time))
591 if (MATCH(".type", get_index, &event.type))
593 if (MATCH(".flags", get_index, &event.flags))
595 if (MATCH(".value", get_index, &event.value))
597 nonmatch("event", name, buf);
600 /* We're in samples - try to convert the random xml value to something useful */
601 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
603 int len = strlen(name);
605 start_match("sample", name, buf);
606 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
608 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
610 if (MATCH(".sample.cylinderindex", get_index, &sample->cylinderindex))
612 if (MATCH(".sample.depth", depth, &sample->depth))
614 if (MATCH(".sample.temp", temperature, &sample->temperature))
616 if (MATCH(".sample.temperature", temperature, &sample->temperature))
618 if (MATCH(".sample.sampletime", sampletime, &sample->time))
620 if (MATCH(".sample.time", sampletime, &sample->time))
623 switch (import_source) {
625 if (uemis_fill_sample(sample, name, len, buf))
630 if (divinglog_fill_sample(sample, name, len, buf))
635 if (uddf_fill_sample(sample, name, len, buf))
643 nonmatch("sample", name, buf);
646 static const char *country, *city;
648 static void divinglog_place(char *place, void *_location)
650 char **location = _location;
651 char buffer[256], *p;
654 len = snprintf(buffer, sizeof(buffer),
660 country ? country : "");
663 memcpy(p, buffer, len+1);
670 static int divinglog_dive_match(struct dive **divep, const char *name, int len, char *buf)
672 struct dive *dive = *divep;
674 return MATCH(".divedate", divedate, &dive->when) ||
675 MATCH(".entrytime", divetime, &dive->when) ||
676 MATCH(".depth", depth, &dive->maxdepth) ||
677 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
678 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
679 MATCH(".comments", utf8_string, &dive->notes) ||
680 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
681 MATCH(".country.name", utf8_string, &country) ||
682 MATCH(".city.name", utf8_string, &city) ||
683 MATCH(".place.name", divinglog_place, &dive->location) ||
687 static int buffer_value(char *buffer)
689 int val = atoi(buffer);
694 static void uemis_length_unit(char *buffer, void *_unused)
696 input_units.length = buffer_value(buffer) ? FEET : METERS;
699 static void uemis_volume_unit(char *buffer, void *_unused)
701 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
704 static void uemis_pressure_unit(char *buffer, void *_unused)
707 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
711 static void uemis_temperature_unit(char *buffer, void *_unused)
713 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
716 static void uemis_weight_unit(char *buffer, void *_unused)
718 input_units.weight = buffer_value(buffer) ? LBS : KG;
721 static void uemis_time_unit(char *buffer, void *_unused)
725 static void uemis_date_unit(char *buffer, void *_unused)
729 /* Modified julian day, yay! */
730 static void uemis_date_time(char *buffer, void *_when)
732 time_t *when = _when;
733 union int_or_float val;
735 switch (integer_or_float(buffer, &val)) {
737 *when = (val.fp - 40587) * 86400;
740 fprintf(stderr, "Strange julian date: %s", buffer);
746 * Uemis doesn't know time zones. You need to do them as
747 * minutes, not hours.
749 * But that's ok, we don't track timezones yet either. We
750 * just turn everything into "localtime expressed as UTC".
752 static void uemis_time_zone(char *buffer, void *_when)
754 #if 0 /* seems like this is only used to display it correctly
755 * the stored time appears to be UTC */
757 time_t *when = _when;
758 signed char tz = atoi(buffer);
764 static void uemis_ts(char *buffer, void *_when)
767 time_t *when = _when;
769 memset(&tm, 0, sizeof(tm));
770 sscanf(buffer,"%d-%d-%dT%d:%d:%d",
771 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
772 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
775 *when = utc_mktime(&tm);
779 static void uemis_duration(char *buffer, void *_duration)
781 duration_t *duration = _duration;
782 duration->seconds = atof(buffer) * 60 + 0.5;
785 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
786 static int uemis_gas_template;
789 * Christ. Uemis tank data is a total mess.
791 * We're passed a "virtual cylinder" (0 - 6) for the different
792 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
793 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
794 * into the actual cylinder data depending on the gas template,
795 * and ignore the ones that are irrelevant for that template.
797 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
798 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
799 * (which are the three "nitrox_3" tanks), and we turn virtual
800 * tanks 2/3 into actual tanks 0/1.
804 static int uemis_cylinder_index(void *_cylinder)
806 cylinder_t *cylinder = _cylinder;
807 unsigned int index = cylinder - dive->cylinder;
810 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
813 switch(uemis_gas_template) {
814 case 1: /* Dive uses tank 1 */
817 case 0: /* Dive uses tank 0 */
821 case 2: /* Dive uses tanks 2-3 */
826 case 3: /* Dive uses tanks 4-6 */
835 static void uemis_cylindersize(char *buffer, void *_cylinder)
837 int index = uemis_cylinder_index(_cylinder);
839 cylindersize(buffer, &dive->cylinder[index].type.size);
842 static void uemis_percent(char *buffer, void *_cylinder)
844 int index = uemis_cylinder_index(_cylinder);
846 percent(buffer, &dive->cylinder[index].gasmix.o2);
849 static int uemis_dive_match(struct dive **divep, const char *name, int len, char *buf)
851 struct dive *dive = *divep;
853 return MATCH(".units.length", uemis_length_unit, &input_units) ||
854 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
855 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
856 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
857 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
858 MATCH(".units.time", uemis_time_unit, &input_units) ||
859 MATCH(".units.date", uemis_date_unit, &input_units) ||
860 MATCH(".date_time", uemis_date_time, &dive->when) ||
861 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
862 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
863 MATCH(".gas.template", get_index, &uemis_gas_template) ||
864 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
865 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
866 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
867 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
868 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
869 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
870 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
871 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
872 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
873 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
874 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
875 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
876 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
877 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
878 MATCH(".dive.val.float", uemis_duration, &dive->duration) ||
879 MATCH(".dive.val.ts", uemis_ts, &dive->when) ||
880 MATCH(".dive.val.bin", uemis_parse_divelog_binary, divep) ||
885 * Uddf specifies ISO 8601 time format.
887 * There are many variations on that. This handles the useful cases.
889 static void uddf_datetime(char *buffer, void *_when)
893 time_t *when = _when;
894 struct tm tm = { 0 };
897 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
904 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
911 printf("Bad date time %s\n", buffer);
916 if (c != 'T' && c != ' ')
924 *when = utc_mktime(&tm);
928 static int uddf_dive_match(struct dive **divep, const char *name, int len, char *buf)
930 struct dive *dive = *divep;
932 return MATCH(".datetime", uddf_datetime, &dive->when) ||
933 MATCH(".diveduration", duration, &dive->duration) ||
934 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
938 static void gps_location(char *buffer, void *_dive)
941 struct dive *dive = _dive;
942 double latitude, longitude;
944 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
946 dive->latitude = latitude;
947 dive->longitude = longitude;
952 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
953 static void try_to_fill_dive(struct dive **divep, const char *name, char *buf)
955 int len = strlen(name);
957 start_match("dive", name, buf);
959 switch (import_source) {
961 if (uemis_dive_match(divep, name, len, buf))
966 if (divinglog_dive_match(divep, name, len, buf))
971 if (uddf_dive_match(divep, name, len, buf))
979 struct dive *dive = *divep;
981 if (MATCH(".number", get_index, &dive->number))
983 if (MATCH(".date", divedate, &dive->when))
985 if (MATCH(".time", divetime, &dive->when))
987 if (MATCH(".datetime", divedatetime, &dive->when))
989 if (MATCH(".maxdepth", depth, &dive->maxdepth))
991 if (MATCH(".meandepth", depth, &dive->meandepth))
993 if (MATCH(".depth.max", depth, &dive->maxdepth))
995 if (MATCH(".depth.mean", depth, &dive->meandepth))
997 if (MATCH(".duration", duration, &dive->duration))
999 if (MATCH(".divetime", duration, &dive->duration))
1001 if (MATCH(".divetimesec", duration, &dive->duration))
1003 if (MATCH(".surfacetime", duration, &dive->surfacetime))
1005 if (MATCH(".airtemp", temperature, &dive->airtemp))
1007 if (MATCH(".watertemp", temperature, &dive->watertemp))
1009 if (MATCH(".temperature.air", temperature, &dive->airtemp))
1011 if (MATCH(".temperature.water", temperature, &dive->watertemp))
1013 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
1015 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1017 if (MATCH(".gps", gps_location, dive))
1019 if (MATCH(".location", utf8_string, &dive->location))
1021 if (MATCH(".notes", utf8_string, &dive->notes))
1023 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1025 if (MATCH(".buddy", utf8_string, &dive->buddy))
1027 if (MATCH(".rating", get_index, &dive->rating))
1029 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
1031 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
1033 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cylinder_index].type.description))
1035 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cylinder_index].start))
1037 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cylinder_index].end))
1040 if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
1042 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
1044 if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
1047 nonmatch("dive", name, buf);
1051 * File boundaries are dive boundaries. But sometimes there are
1052 * multiple dives per file, so there can be other events too that
1053 * trigger a "new dive" marker and you may get some nesting due
1054 * to that. Just ignore nesting levels.
1056 static void dive_start(void)
1060 dive = alloc_dive();
1061 memset(&tm, 0, sizeof(tm));
1064 static void sanitize_gasmix(struct gasmix *mix)
1066 unsigned int o2, he;
1068 o2 = mix->o2.permille;
1069 he = mix->he.permille;
1071 /* Regular air: leave empty */
1075 /* 20.9% or 21% O2 is just air */
1076 if (o2 >= 209 && o2 <= 210) {
1077 mix->o2.permille = 0;
1083 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
1085 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
1086 memset(mix, 0, sizeof(*mix));
1090 * See if the size/workingpressure looks like some standard cylinder
1093 static void match_standard_cylinder(cylinder_type_t *type)
1098 char buffer[20], *p;
1100 /* Do we already have a cylinder description? */
1101 if (type->description)
1104 cuft = ml_to_cuft(type->size.mliter);
1105 cuft *= to_ATM(type->workingpressure);
1106 psi = to_PSI(type->workingpressure);
1109 case 2300 ... 2500: /* 2400 psi: LP tank */
1112 case 2600 ... 2700: /* 2640 psi: LP+10% */
1115 case 2900 ... 3100: /* 3000 psi: ALx tank */
1118 case 3400 ... 3500: /* 3442 psi: HP tank */
1121 case 3700 ... 3850: /* HP+10% */
1127 len = snprintf(buffer, sizeof(buffer), fmt, (int) (cuft+0.5));
1131 memcpy(p, buffer, len+1);
1132 type->description = p;
1137 * There are two ways to give cylinder size information:
1138 * - total amount of gas in cuft (depends on working pressure and physical size)
1141 * where "physical size" is the one that actually matters and is sane.
1143 * We internally use physical size only. But we save the workingpressure
1144 * so that we can do the conversion if required.
1146 static void sanitize_cylinder_type(cylinder_type_t *type)
1148 double volume_of_air, atm, volume;
1150 /* If we have no working pressure, it had *better* be just a physical size! */
1151 if (!type->workingpressure.mbar)
1154 /* No size either? Nothing to go on */
1155 if (!type->size.mliter)
1158 if (input_units.volume == CUFT) {
1159 /* confusing - we don't really start from ml but millicuft !*/
1160 volume_of_air = cuft_to_l(type->size.mliter);
1161 atm = to_ATM(type->workingpressure); /* working pressure in atm */
1162 volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
1163 type->size.mliter = volume + 0.5;
1166 /* Ok, we have both size and pressure: try to match a description */
1167 match_standard_cylinder(type);
1170 static void sanitize_cylinder_info(struct dive *dive)
1174 for (i = 0; i < MAX_CYLINDERS; i++) {
1175 sanitize_gasmix(&dive->cylinder[i].gasmix);
1176 sanitize_cylinder_type(&dive->cylinder[i].type);
1180 static void dive_end(void)
1184 sanitize_cylinder_info(dive);
1190 static void event_start(void)
1192 memset(&event, 0, sizeof(event));
1196 static void event_end(void)
1198 if (event.name && strcmp(event.name, "surface") != 0)
1199 add_event(dive, event.time.seconds, event.type, event.flags, event.value, event.name);
1203 static void cylinder_start(void)
1207 static void cylinder_end(void)
1212 static void sample_start(void)
1214 sample = prepare_sample(&dive);
1217 static void sample_end(void)
1222 finish_sample(dive, sample);
1226 static void entry(const char *name, int size, const char *raw)
1228 char *buf = malloc(size+1);
1232 memcpy(buf, raw, size);
1235 try_to_fill_event(name, buf);
1239 try_to_fill_sample(sample, name, buf);
1243 try_to_fill_dive(&dive, name, buf);
1248 static const char *nodename(xmlNode *node, char *buf, int len)
1250 if (!node || !node->name)
1258 const char *name = node->name;
1259 int i = strlen(name);
1261 unsigned char c = name[i];
1262 *--buf = tolower(c);
1266 node = node->parent;
1267 if (!node || !node->name)
1277 static void visit_one_node(xmlNode *node)
1280 const unsigned char *content;
1281 char buffer[MAXNAME];
1284 content = node->content;
1288 /* Trim whitespace at beginning */
1289 while (isspace(*content))
1292 /* Trim whitespace at end */
1293 len = strlen(content);
1294 while (len && isspace(content[len-1]))
1300 /* Don't print out the node name if it is "text" */
1301 if (!strcmp(node->name, "text"))
1302 node = node->parent;
1304 name = nodename(node, buffer, sizeof(buffer));
1306 entry(name, len, content);
1309 static void traverse(xmlNode *root);
1311 static void traverse_properties(xmlNode *node)
1315 for (p = node->properties; p; p = p->next)
1316 traverse(p->children);
1319 static void visit(xmlNode *n)
1322 traverse_properties(n);
1323 traverse(n->children);
1326 static void uemis_importer(void)
1328 import_source = UEMIS;
1329 input_units = SI_units;
1332 static void DivingLog_importer(void)
1334 import_source = DIVINGLOG;
1337 * Diving Log units are really strange.
1339 * Temperatures are in C, except in samples,
1340 * when they are in Fahrenheit. Depths are in
1341 * meters, an dpressure is in PSI in the samples,
1342 * but in bar when it comes to working pressure.
1344 * Crazy f*%^ morons.
1346 input_units = SI_units;
1349 static void uddf_importer(void)
1351 import_source = UDDF;
1352 input_units = SI_units;
1353 input_units.pressure = PASCAL;
1354 input_units.temperature = KELVIN;
1358 * I'm sure this could be done as some fancy DTD rules.
1359 * It's just not worth the headache.
1361 static struct nesting {
1363 void (*start)(void), (*end)(void);
1365 { "dive", dive_start, dive_end },
1366 { "Dive", dive_start, dive_end },
1367 { "sample", sample_start, sample_end },
1368 { "waypoint", sample_start, sample_end },
1369 { "SAMPLE", sample_start, sample_end },
1370 { "reading", sample_start, sample_end },
1371 { "event", event_start, event_end },
1372 { "gasmix", cylinder_start, cylinder_end },
1373 { "cylinder", cylinder_start, cylinder_end },
1374 { "P", sample_start, sample_end },
1376 /* Import type recognition */
1377 { "Divinglog", DivingLog_importer },
1378 { "pre_dive", uemis_importer },
1379 { "dives", uemis_importer },
1380 { "uddf", uddf_importer },
1385 static void traverse(xmlNode *root)
1389 for (n = root; n; n = n->next) {
1390 struct nesting *rule = nesting;
1393 if (!strcmp(rule->name, n->name))
1396 } while (rule->name);
1406 /* Per-file reset */
1407 static void reset_all(void)
1410 * We reset the units for each file. You'd think it was
1411 * a per-dive property, but I'm not going to trust people
1412 * to do per-dive setup. If the xml does have per-dive
1413 * data within one file, we might have to reset it per
1414 * dive for that format.
1416 input_units = SI_units;
1417 import_source = UNKNOWN;
1420 void parse_xml_file(const char *filename, GError **error)
1424 doc = xmlReadFile(filename, NULL, 0);
1426 fprintf(stderr, "Failed to parse '%s'.\n", filename);
1429 *error = g_error_new(g_quark_from_string("subsurface"),
1431 "Failed to parse '%s'",
1436 /* we assume that the last (or only) filename passed as argument is a
1437 * great filename to use as default when saving the dives */
1438 set_filename(filename);
1442 doc = test_xslt_transforms(doc);
1444 traverse(xmlDocGetRootElement(doc));
1450 void parse_xml_init(void)
1457 /* Maybe we'll want a environment variable that can override this.. */
1458 static const char *xslt_path = XSLT ":xslt:.";
1460 static xsltStylesheetPtr try_get_stylesheet(const char *path, int len, const char *name)
1462 xsltStylesheetPtr ret;
1463 int namelen = strlen(name);
1464 char *filename = malloc(len+1+namelen+1);
1469 memcpy(filename, path, len);
1470 filename[len] = G_DIR_SEPARATOR;
1471 memcpy(filename + len + 1, name, namelen+1);
1474 if (!access(filename, R_OK))
1475 ret = xsltParseStylesheetFile(filename);
1481 static xsltStylesheetPtr get_stylesheet(const char *name)
1483 const char *path = xslt_path, *next;
1487 xsltStylesheetPtr ret;
1489 next = strchr(path, ':');
1495 ret = try_get_stylesheet(path, len, name);
1498 } while ((path = next) != NULL);
1503 static struct xslt_files {
1507 { "SUUNTO", "SuuntoSDM.xslt" },
1508 { "JDiveLog", "jdivelog2subsurface.xslt" },
1512 xmlDoc *test_xslt_transforms(xmlDoc *doc)
1514 struct xslt_files *info = xslt_files;
1515 xmlDoc *transformed;
1516 xsltStylesheetPtr xslt = NULL;
1517 xmlNode *root_element = xmlDocGetRootElement(doc);
1519 while ((info->root) && (strcasecmp(root_element->name, info->root) != 0)) {
1524 xmlSubstituteEntitiesDefault(1);
1525 xslt = get_stylesheet(info->file);
1528 transformed = xsltApplyStylesheet(xslt, doc, NULL);
1530 xsltFreeStylesheet(xslt);