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 delete_dive_renumber(struct dive **dives, int i, int nr)
44 struct dive *dive = dives[i];
45 int number = dive->number, j;
51 * Check that all numbered dives after the deleted
52 * ones are consecutive, return without renumbering
53 * if that is not the case.
55 for (j = i+1; j < nr; j++) {
56 struct dive *next = dives[j];
60 if (next->number != number)
65 * Ok, we hit the end of the dives or a unnumbered
68 for (j = i+1 ; j < nr; j++) {
69 struct dive *next = dives[j];
77 * Remove a dive from the dive_table array
79 void delete_dive(struct dive *dive)
81 int nr = dive_table.nr, i;
82 struct dive **dives = dive_table.dives;
85 * Stupid. We know the dive table is sorted by date,
86 * we could do a binary lookup. Sue me.
88 for (i = 0; i < nr; i++) {
89 struct dive *d = dives[i];
92 /* should we re-number? */
93 delete_dive_renumber(dives, i, nr);
94 memmove(dives+i, dives+i+1, sizeof(struct dive *)*(nr-i-1));
101 static void start_match(const char *type, const char *name, char *buffer)
104 printf("Matching %s '%s' (%s)\n",
108 static void nonmatch(const char *type, const char *name, char *buffer)
111 printf("Unable to match %s '%s' (%s)\n",
116 typedef void (*matchfn_t)(char *buffer, void *);
118 static int match(const char *pattern, int plen,
119 const char *name, int nlen,
120 matchfn_t fn, char *buf, void *data)
124 if (memcmp(pattern, name + nlen - plen, plen))
131 struct units input_units;
134 * We're going to default to SI units for input. Yes,
135 * technically the SI unit for pressure is Pascal, but
136 * we default to bar (10^5 pascal), which people
137 * actually use. Similarly, C instead of Kelvin.
138 * And kg instead of g.
140 const struct units SI_units = {
144 .temperature = CELSIUS,
148 const struct units IMPERIAL_units = {
152 .temperature = FAHRENHEIT,
157 * Dive info as it is being built up..
159 static struct dive *cur_dive;
160 static struct sample *cur_sample;
164 int type, flags, value;
167 static struct tm cur_tm;
168 static int cur_cylinder_index, cur_ws_index;
170 static enum import_source {
178 time_t utc_mktime(struct tm *tm)
180 static const int mdays[] = {
181 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
183 int year = tm->tm_year;
184 int month = tm->tm_mon;
185 int day = tm->tm_mday;
187 /* First normalize relative to 1900 */
190 else if (year > 1900)
193 /* Normalized to Jan 1, 1970: unix time */
196 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
198 if (month < 0 || month > 11) /* array bounds */
200 if (month < 2 || (year + 2) % 4)
202 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
204 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
205 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
208 static void divedate(char *buffer, void *_when)
211 time_t *when = _when;
214 success = cur_tm.tm_sec | cur_tm.tm_min | cur_tm.tm_hour;
215 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
219 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
224 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
229 *when = utc_mktime(&cur_tm);
234 static void divetime(char *buffer, void *_when)
237 time_t *when = _when;
239 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
244 *when = utc_mktime(&cur_tm);
249 /* Libdivecomputer: "2011-03-20 10:22:38" */
250 static void divedatetime(char *buffer, void *_when)
254 time_t *when = _when;
256 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
257 &y, &m, &d, &hr, &min, &sec) == 6) {
264 *when = utc_mktime(&cur_tm);
278 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
284 /* Integer or floating point? */
285 val = strtol(buffer, &end, 10);
286 if (val < 0 || end == buffer)
289 /* Looks like it might be floating point? */
292 fp = strtod(buffer, &end);
303 static void pressure(char *buffer, void *_press)
306 pressure_t *pressure = _press;
307 union int_or_float val;
309 switch (integer_or_float(buffer, &val)) {
311 /* Just ignore zero values */
314 switch (input_units.pressure) {
319 /* Assume mbar, but if it's really small, it's bar */
325 mbar = val.fp * 68.95;
328 if (mbar > 5 && mbar < 500000) {
329 pressure->mbar = mbar + 0.5;
334 printf("Strange pressure reading %s\n", buffer);
339 static void depth(char *buffer, void *_depth)
341 depth_t *depth = _depth;
342 union int_or_float val;
344 switch (integer_or_float(buffer, &val)) {
346 switch (input_units.length) {
348 depth->mm = val.fp * 1000 + 0.5;
351 depth->mm = val.fp * 304.8 + 0.5;
356 printf("Strange depth reading %s\n", buffer);
361 static void weight(char *buffer, void *_weight)
363 weight_t *weight = _weight;
364 union int_or_float val;
366 switch (integer_or_float(buffer, &val)) {
368 switch (input_units.weight) {
370 weight->grams = val.fp * 1000 + 0.5;
373 weight->grams = val.fp * 453.6 + 0.5;
378 printf("Strange depth reading %s\n", buffer);
382 static void temperature(char *buffer, void *_temperature)
384 temperature_t *temperature = _temperature;
385 union int_or_float val;
387 switch (integer_or_float(buffer, &val)) {
389 /* Ignore zero. It means "none" */
393 switch (input_units.temperature) {
395 temperature->mkelvin = val.fp * 1000;
398 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
401 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
406 printf("Strange temperature reading %s\n", buffer);
411 static void sampletime(char *buffer, void *_time)
415 duration_t *time = _time;
417 i = sscanf(buffer, "%d:%d", &min, &sec);
424 time->seconds = sec + min*60;
427 printf("Strange sample time reading %s\n", buffer);
432 static void duration(char *buffer, void *_time)
434 sampletime(buffer, _time);
437 static void percent(char *buffer, void *_fraction)
439 fraction_t *fraction = _fraction;
440 union int_or_float val;
442 switch (integer_or_float(buffer, &val)) {
445 fraction->permille = val.fp * 10 + 0.5;
449 printf("Strange percentage reading %s\n", buffer);
455 static void gasmix(char *buffer, void *_fraction)
457 /* libdivecomputer does negative percentages. */
460 if (cur_cylinder_index < MAX_CYLINDERS)
461 percent(buffer, _fraction);
464 static void gasmix_nitrogen(char *buffer, void *_gasmix)
466 /* Ignore n2 percentages. There's no value in them. */
469 static void cylindersize(char *buffer, void *_volume)
471 volume_t *volume = _volume;
472 union int_or_float val;
474 switch (integer_or_float(buffer, &val)) {
476 volume->mliter = val.fp * 1000 + 0.5;
480 printf("Strange volume reading %s\n", buffer);
486 static void utf8_string(char *buffer, void *_res)
488 *(char **)_res = buffer;
492 * Uemis water_pressure. In centibar. And when converting to
493 * depth, I'm just going to always use saltwater, because I
494 * think "true depth" is just stupid. From a diving standpoint,
495 * "true depth" is pretty much completely pointless, unless
496 * you're doing some kind of underwater surveying work.
498 * So I give water depths in "pressure depth", always assuming
499 * salt water. So one atmosphere per 10m.
501 static void water_pressure(char *buffer, void *_depth)
503 depth_t *depth = _depth;
504 union int_or_float val;
507 switch (integer_or_float(buffer, &val)) {
512 atm = bar_to_atm(val.fp * 10);
514 * atm to cm. Why not mm? The precision just isn't
517 cm = 100 * atm + 0.5;
519 depth->mm = 10 * (long)cm;
523 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
528 #define MATCH(pattern, fn, dest) \
529 match(pattern, strlen(pattern), name, len, fn, buf, dest)
531 static void get_index(char *buffer, void *_i)
538 static void centibar(char *buffer, void *_pressure)
540 pressure_t *pressure = _pressure;
541 union int_or_float val;
543 switch (integer_or_float(buffer, &val)) {
545 pressure->mbar = val.fp * 10 + 0.5;
548 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
553 static void decicelsius(char *buffer, void *_temp)
555 temperature_t *temp = _temp;
556 union int_or_float val;
558 switch (integer_or_float(buffer, &val)) {
560 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
563 fprintf(stderr, "Strange julian date: %s", buffer);
568 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
570 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
571 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
572 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
573 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
574 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
579 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
580 * for the sample temperatures, that are in Fahrenheit.
583 * Oh, and I think Diving Log *internally* probably kept them
584 * in celsius, because I'm seeing entries like
588 * in there. Which is freezing, aka 0 degC. I bet the "0" is
589 * what Diving Log uses for "no temperature".
591 * So throw away crap like that.
593 static void fahrenheit(char *buffer, void *_temperature)
595 temperature_t *temperature = _temperature;
596 union int_or_float val;
598 switch (integer_or_float(buffer, &val)) {
600 /* Floating point equality is evil, but works for small integers */
603 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
606 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
612 * Did I mention how bat-shit crazy divinglog is? The sample
613 * pressures are in PSI. But the tank working pressure is in
616 * Crazy stuff like this is why subsurface has everything in
617 * these inconvenient typed structures, and you have to say
618 * "pressure->mbar" to get the actual value. Exactly so that
619 * you can never have unit confusion.
621 static void psi(char *buffer, void *_pressure)
623 pressure_t *pressure = _pressure;
624 union int_or_float val;
626 switch (integer_or_float(buffer, &val)) {
628 pressure->mbar = val.fp * 68.95 + 0.5;
631 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
636 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
638 return MATCH(".p.time", sampletime, &sample->time) ||
639 MATCH(".p.depth", depth, &sample->depth) ||
640 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
641 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
645 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
647 return MATCH(".divetime", sampletime, &sample->time) ||
648 MATCH(".depth", depth, &sample->depth) ||
649 MATCH(".temperature", temperature, &sample->temperature) ||
650 MATCH(".tankpressure", pressure, &sample->cylinderpressure) ||
654 static void eventtime(char *buffer, void *_duration)
656 duration_t *duration = _duration;
657 sampletime(buffer, duration);
659 duration->seconds += cur_sample->time.seconds;
662 static void try_to_fill_event(const char *name, char *buf)
664 int len = strlen(name);
666 start_match("event", name, buf);
667 if (MATCH(".event", utf8_string, &cur_event.name))
669 if (MATCH(".name", utf8_string, &cur_event.name))
671 if (MATCH(".time", eventtime, &cur_event.time))
673 if (MATCH(".type", get_index, &cur_event.type))
675 if (MATCH(".flags", get_index, &cur_event.flags))
677 if (MATCH(".value", get_index, &cur_event.value))
679 nonmatch("event", name, buf);
682 /* We're in samples - try to convert the random xml value to something useful */
683 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
685 int len = strlen(name);
687 start_match("sample", name, buf);
688 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
690 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
692 if (MATCH(".sample.cylinderindex", get_index, &sample->cylinderindex))
694 if (MATCH(".sample.depth", depth, &sample->depth))
696 if (MATCH(".sample.temp", temperature, &sample->temperature))
698 if (MATCH(".sample.temperature", temperature, &sample->temperature))
700 if (MATCH(".sample.sampletime", sampletime, &sample->time))
702 if (MATCH(".sample.time", sampletime, &sample->time))
705 switch (import_source) {
707 if (uemis_fill_sample(sample, name, len, buf))
712 if (divinglog_fill_sample(sample, name, len, buf))
717 if (uddf_fill_sample(sample, name, len, buf))
725 nonmatch("sample", name, buf);
728 static const char *country, *city;
730 static void divinglog_place(char *place, void *_location)
732 char **location = _location;
733 char buffer[256], *p;
736 len = snprintf(buffer, sizeof(buffer),
742 country ? country : "");
745 memcpy(p, buffer, len+1);
752 static int divinglog_dive_match(struct dive **divep, const char *name, int len, char *buf)
754 struct dive *dive = *divep;
756 return MATCH(".divedate", divedate, &dive->when) ||
757 MATCH(".entrytime", divetime, &dive->when) ||
758 MATCH(".depth", depth, &dive->maxdepth) ||
759 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
760 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
761 MATCH(".comments", utf8_string, &dive->notes) ||
762 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
763 MATCH(".country.name", utf8_string, &country) ||
764 MATCH(".city.name", utf8_string, &city) ||
765 MATCH(".place.name", divinglog_place, &dive->location) ||
769 static int buffer_value(char *buffer)
771 int val = atoi(buffer);
776 static void uemis_length_unit(char *buffer, void *_unused)
778 input_units.length = buffer_value(buffer) ? FEET : METERS;
781 static void uemis_volume_unit(char *buffer, void *_unused)
783 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
786 static void uemis_pressure_unit(char *buffer, void *_unused)
789 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
793 static void uemis_temperature_unit(char *buffer, void *_unused)
795 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
798 static void uemis_weight_unit(char *buffer, void *_unused)
800 input_units.weight = buffer_value(buffer) ? LBS : KG;
803 static void uemis_time_unit(char *buffer, void *_unused)
807 static void uemis_date_unit(char *buffer, void *_unused)
811 /* Modified julian day, yay! */
812 static void uemis_date_time(char *buffer, void *_when)
814 time_t *when = _when;
815 union int_or_float val;
817 switch (integer_or_float(buffer, &val)) {
819 *when = (val.fp - 40587) * 86400;
822 fprintf(stderr, "Strange julian date: %s", buffer);
828 * Uemis doesn't know time zones. You need to do them as
829 * minutes, not hours.
831 * But that's ok, we don't track timezones yet either. We
832 * just turn everything into "localtime expressed as UTC".
834 static void uemis_time_zone(char *buffer, void *_when)
836 #if 0 /* seems like this is only used to display it correctly
837 * the stored time appears to be UTC */
839 time_t *when = _when;
840 signed char tz = atoi(buffer);
846 static void uemis_ts(char *buffer, void *_when)
849 time_t *when = _when;
851 memset(&tm, 0, sizeof(tm));
852 sscanf(buffer,"%d-%d-%dT%d:%d:%d",
853 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
854 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
857 *when = utc_mktime(&tm);
861 static void uemis_duration(char *buffer, void *_duration)
863 duration_t *duration = _duration;
864 duration->seconds = atof(buffer) * 60 + 0.5;
867 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
868 static int uemis_gas_template;
871 * Christ. Uemis tank data is a total mess.
873 * We're passed a "virtual cylinder" (0 - 6) for the different
874 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
875 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
876 * into the actual cylinder data depending on the gas template,
877 * and ignore the ones that are irrelevant for that template.
879 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
880 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
881 * (which are the three "nitrox_3" tanks), and we turn virtual
882 * tanks 2/3 into actual tanks 0/1.
886 static int uemis_cylinder_index(void *_cylinder)
888 cylinder_t *cylinder = _cylinder;
889 unsigned int index = cylinder - cur_dive->cylinder;
892 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
895 switch(uemis_gas_template) {
896 case 1: /* Dive uses tank 1 */
899 case 0: /* Dive uses tank 0 */
903 case 2: /* Dive uses tanks 2-3 */
908 case 3: /* Dive uses tanks 4-6 */
917 static void uemis_cylindersize(char *buffer, void *_cylinder)
919 int index = uemis_cylinder_index(_cylinder);
921 cylindersize(buffer, &cur_dive->cylinder[index].type.size);
924 static void uemis_percent(char *buffer, void *_cylinder)
926 int index = uemis_cylinder_index(_cylinder);
928 percent(buffer, &cur_dive->cylinder[index].gasmix.o2);
931 static int uemis_dive_match(struct dive **divep, const char *name, int len, char *buf)
933 struct dive *dive = *divep;
935 return MATCH(".units.length", uemis_length_unit, &input_units) ||
936 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
937 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
938 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
939 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
940 MATCH(".units.time", uemis_time_unit, &input_units) ||
941 MATCH(".units.date", uemis_date_unit, &input_units) ||
942 MATCH(".date_time", uemis_date_time, &dive->when) ||
943 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
944 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
945 MATCH(".gas.template", get_index, &uemis_gas_template) ||
946 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
947 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
948 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
949 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
950 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
951 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
952 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
953 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
954 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
955 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
956 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
957 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
958 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
959 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
960 MATCH(".dive.val.float", uemis_duration, &dive->duration) ||
961 MATCH(".dive.val.ts", uemis_ts, &dive->when) ||
962 MATCH(".dive.val.bin", uemis_parse_divelog_binary, divep) ||
967 * Uddf specifies ISO 8601 time format.
969 * There are many variations on that. This handles the useful cases.
971 static void uddf_datetime(char *buffer, void *_when)
975 time_t *when = _when;
976 struct tm tm = { 0 };
979 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
986 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
993 printf("Bad date time %s\n", buffer);
998 if (c != 'T' && c != ' ')
1006 *when = utc_mktime(&tm);
1010 static int uddf_dive_match(struct dive **divep, const char *name, int len, char *buf)
1012 struct dive *dive = *divep;
1014 return MATCH(".datetime", uddf_datetime, &dive->when) ||
1015 MATCH(".diveduration", duration, &dive->duration) ||
1016 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
1020 static void gps_location(char *buffer, void *_dive)
1023 struct dive *dive = _dive;
1024 double latitude, longitude;
1026 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
1028 dive->latitude = latitude;
1029 dive->longitude = longitude;
1034 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
1035 static void try_to_fill_dive(struct dive **divep, const char *name, char *buf)
1037 int len = strlen(name);
1039 start_match("dive", name, buf);
1041 switch (import_source) {
1043 if (uemis_dive_match(divep, name, len, buf))
1048 if (divinglog_dive_match(divep, name, len, buf))
1053 if (uddf_dive_match(divep, name, len, buf))
1061 struct dive *dive = *divep;
1063 if (MATCH(".number", get_index, &dive->number))
1065 if (MATCH(".date", divedate, &dive->when))
1067 if (MATCH(".time", divetime, &dive->when))
1069 if (MATCH(".datetime", divedatetime, &dive->when))
1071 if (MATCH(".maxdepth", depth, &dive->maxdepth))
1073 if (MATCH(".meandepth", depth, &dive->meandepth))
1075 if (MATCH(".depth.max", depth, &dive->maxdepth))
1077 if (MATCH(".depth.mean", depth, &dive->meandepth))
1079 if (MATCH(".duration", duration, &dive->duration))
1081 if (MATCH(".divetime", duration, &dive->duration))
1083 if (MATCH(".divetimesec", duration, &dive->duration))
1085 if (MATCH(".surfacetime", duration, &dive->surfacetime))
1087 if (MATCH(".airtemp", temperature, &dive->airtemp))
1089 if (MATCH(".watertemp", temperature, &dive->watertemp))
1091 if (MATCH(".temperature.air", temperature, &dive->airtemp))
1093 if (MATCH(".temperature.water", temperature, &dive->watertemp))
1095 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
1097 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1099 if (MATCH(".gps", gps_location, dive))
1101 if (MATCH(".location", utf8_string, &dive->location))
1103 if (MATCH(".suit", utf8_string, &dive->suit))
1105 if (MATCH(".notes", utf8_string, &dive->notes))
1107 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1109 if (MATCH(".buddy", utf8_string, &dive->buddy))
1111 if (MATCH(".rating", get_index, &dive->rating))
1113 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cur_cylinder_index].type.size))
1115 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cur_cylinder_index].type.workingpressure))
1117 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cur_cylinder_index].type.description))
1119 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cur_cylinder_index].start))
1121 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cur_cylinder_index].end))
1123 if (MATCH(".weightsystem.description", utf8_string, &dive->weightsystem[cur_ws_index].description))
1125 if (MATCH(".weightsystem.weight", weight, &dive->weightsystem[cur_ws_index].weight))
1127 if (MATCH("weight", weight, &dive->weightsystem[cur_ws_index].weight))
1129 if (MATCH(".o2", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.o2))
1131 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cur_cylinder_index].gasmix))
1133 if (MATCH(".he", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.he))
1136 nonmatch("dive", name, buf);
1140 * File boundaries are dive boundaries. But sometimes there are
1141 * multiple dives per file, so there can be other events too that
1142 * trigger a "new dive" marker and you may get some nesting due
1143 * to that. Just ignore nesting levels.
1145 static void dive_start(void)
1149 cur_dive = alloc_dive();
1150 memset(&cur_tm, 0, sizeof(cur_tm));
1153 static void dive_end(void)
1157 record_dive(cur_dive);
1159 cur_cylinder_index = 0;
1163 static void event_start(void)
1165 memset(&cur_event, 0, sizeof(cur_event));
1166 cur_event.active = 1;
1169 static void event_end(void)
1171 if (cur_event.name && strcmp(cur_event.name, "surface") != 0)
1172 add_event(cur_dive, cur_event.time.seconds,
1173 cur_event.type, cur_event.flags,
1174 cur_event.value, cur_event.name);
1175 cur_event.active = 0;
1178 static void cylinder_start(void)
1182 static void cylinder_end(void)
1184 cur_cylinder_index++;
1187 static void ws_start(void)
1191 static void ws_end(void)
1196 static void sample_start(void)
1198 cur_sample = prepare_sample(&cur_dive);
1201 static void sample_end(void)
1206 finish_sample(cur_dive);
1210 static void entry(const char *name, int size, const char *raw)
1212 char *buf = malloc(size+1);
1216 memcpy(buf, raw, size);
1218 if (cur_event.active) {
1219 try_to_fill_event(name, buf);
1223 try_to_fill_sample(cur_sample, name, buf);
1227 try_to_fill_dive(&cur_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 uemis_importer(void)
1312 import_source = UEMIS;
1313 input_units = SI_units;
1316 static void DivingLog_importer(void)
1318 import_source = DIVINGLOG;
1321 * Diving Log units are really strange.
1323 * Temperatures are in C, except in samples,
1324 * when they are in Fahrenheit. Depths are in
1325 * meters, an dpressure is in PSI in the samples,
1326 * but in bar when it comes to working pressure.
1328 * Crazy f*%^ morons.
1330 input_units = SI_units;
1333 static void uddf_importer(void)
1335 import_source = UDDF;
1336 input_units = SI_units;
1337 input_units.pressure = PASCAL;
1338 input_units.temperature = KELVIN;
1342 * I'm sure this could be done as some fancy DTD rules.
1343 * It's just not worth the headache.
1345 static struct nesting {
1347 void (*start)(void), (*end)(void);
1349 { "dive", dive_start, dive_end },
1350 { "Dive", dive_start, dive_end },
1351 { "sample", sample_start, sample_end },
1352 { "waypoint", sample_start, sample_end },
1353 { "SAMPLE", sample_start, sample_end },
1354 { "reading", sample_start, sample_end },
1355 { "event", event_start, event_end },
1356 { "gasmix", cylinder_start, cylinder_end },
1357 { "cylinder", cylinder_start, cylinder_end },
1358 { "weightsystem", ws_start, ws_end },
1359 { "P", sample_start, sample_end },
1361 /* Import type recognition */
1362 { "Divinglog", DivingLog_importer },
1363 { "pre_dive", uemis_importer },
1364 { "dives", uemis_importer },
1365 { "uddf", uddf_importer },
1370 static void traverse(xmlNode *root)
1374 for (n = root; n; n = n->next) {
1375 struct nesting *rule = nesting;
1378 if (!strcmp(rule->name, n->name))
1381 } while (rule->name);
1391 /* Per-file reset */
1392 static void reset_all(void)
1395 * We reset the units for each file. You'd think it was
1396 * a per-dive property, but I'm not going to trust people
1397 * to do per-dive setup. If the xml does have per-dive
1398 * data within one file, we might have to reset it per
1399 * dive for that format.
1401 input_units = SI_units;
1402 import_source = UNKNOWN;
1405 void parse_xml_buffer(const char *url, const char *buffer, int size, GError **error)
1409 doc = xmlReadMemory(buffer, size, url, NULL, 0);
1411 fprintf(stderr, "Failed to parse '%s'.\n", url);
1414 *error = g_error_new(g_quark_from_string("subsurface"),
1416 "Failed to parse '%s'",
1421 /* we assume that the last (or only) filename passed as argument is a
1422 * great filename to use as default when saving the dives */
1427 doc = test_xslt_transforms(doc);
1429 traverse(xmlDocGetRootElement(doc));
1435 void parse_xml_init(void)
1442 /* Maybe we'll want a environment variable that can override this.. */
1443 static const char *xslt_path = XSLT ":xslt:.";
1445 static xsltStylesheetPtr try_get_stylesheet(const char *path, int len, const char *name)
1447 xsltStylesheetPtr ret;
1448 int namelen = strlen(name);
1449 char *filename = malloc(len+1+namelen+1);
1454 memcpy(filename, path, len);
1455 filename[len] = G_DIR_SEPARATOR;
1456 memcpy(filename + len + 1, name, namelen+1);
1459 if (!access(filename, R_OK))
1460 ret = xsltParseStylesheetFile(filename);
1466 static xsltStylesheetPtr get_stylesheet(const char *name)
1468 const char *path, *next;
1470 path = getenv("SUBSURFACE_XSLT_PATH");
1476 xsltStylesheetPtr ret;
1478 next = strchr(path, ':');
1484 ret = try_get_stylesheet(path, len, name);
1487 } while ((path = next) != NULL);
1492 static struct xslt_files {
1496 { "SUUNTO", "SuuntoSDM.xslt" },
1497 { "JDiveLog", "jdivelog2subsurface.xslt" },
1501 xmlDoc *test_xslt_transforms(xmlDoc *doc)
1503 struct xslt_files *info = xslt_files;
1504 xmlDoc *transformed;
1505 xsltStylesheetPtr xslt = NULL;
1506 xmlNode *root_element = xmlDocGetRootElement(doc);
1508 while ((info->root) && (strcasecmp(root_element->name, info->root) != 0)) {
1513 xmlSubstituteEntitiesDefault(1);
1514 xslt = get_stylesheet(info->file);
1517 transformed = xsltApplyStylesheet(xslt, doc, NULL);
1519 xsltFreeStylesheet(xslt);