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 void record_trip(struct dive *trip)
44 dive_trip_list = insert_trip(trip, dive_trip_list);
47 static void delete_dive_renumber(struct dive **dives, int i, int nr)
49 struct dive *dive = dives[i];
50 int number = dive->number, j;
56 * Check that all numbered dives after the deleted
57 * ones are consecutive, return without renumbering
58 * if that is not the case.
60 for (j = i+1; j < nr; j++) {
61 struct dive *next = dives[j];
65 if (next->number != number)
70 * Ok, we hit the end of the dives or a unnumbered
73 for (j = i+1 ; j < nr; j++) {
74 struct dive *next = dives[j];
82 * Remove a dive from the dive_table array
84 void delete_dive(struct dive *dive)
86 int nr = dive_table.nr, i;
87 struct dive **dives = dive_table.dives;
90 * Stupid. We know the dive table is sorted by date,
91 * we could do a binary lookup. Sue me.
93 for (i = 0; i < nr; i++) {
94 struct dive *d = dives[i];
97 /* should we re-number? */
98 delete_dive_renumber(dives, i, nr);
99 memmove(dives+i, dives+i+1, sizeof(struct dive *)*(nr-i-1));
101 dive_table.nr = nr-1;
106 static void start_match(const char *type, const char *name, char *buffer)
109 printf("Matching %s '%s' (%s)\n",
113 static void nonmatch(const char *type, const char *name, char *buffer)
116 printf("Unable to match %s '%s' (%s)\n",
121 typedef void (*matchfn_t)(char *buffer, void *);
123 static int match(const char *pattern, int plen,
124 const char *name, int nlen,
125 matchfn_t fn, char *buf, void *data)
129 if (memcmp(pattern, name + nlen - plen, plen))
136 struct units input_units;
139 * We're going to default to SI units for input. Yes,
140 * technically the SI unit for pressure is Pascal, but
141 * we default to bar (10^5 pascal), which people
142 * actually use. Similarly, C instead of Kelvin.
143 * And kg instead of g.
145 const struct units SI_units = {
149 .temperature = CELSIUS,
153 const struct units IMPERIAL_units = {
157 .temperature = FAHRENHEIT,
162 * Dive info as it is being built up..
164 static struct dive *cur_dive, *cur_trip = NULL;
165 static struct sample *cur_sample;
169 int type, flags, value;
172 static struct tm cur_tm;
173 static int cur_cylinder_index, cur_ws_index;
175 static enum import_source {
183 time_t utc_mktime(struct tm *tm)
185 static const int mdays[] = {
186 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
188 int year = tm->tm_year;
189 int month = tm->tm_mon;
190 int day = tm->tm_mday;
192 /* First normalize relative to 1900 */
195 else if (year > 1900)
198 /* Normalized to Jan 1, 1970: unix time */
201 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
203 if (month < 0 || month > 11) /* array bounds */
205 if (month < 2 || (year + 2) % 4)
207 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
209 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
210 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
213 static void divedate(char *buffer, void *_when)
216 time_t *when = _when;
219 success = cur_tm.tm_sec | cur_tm.tm_min | cur_tm.tm_hour;
220 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
224 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
229 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
234 *when = utc_mktime(&cur_tm);
239 static void divetime(char *buffer, void *_when)
242 time_t *when = _when;
244 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
249 *when = utc_mktime(&cur_tm);
254 /* Libdivecomputer: "2011-03-20 10:22:38" */
255 static void divedatetime(char *buffer, void *_when)
259 time_t *when = _when;
261 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
262 &y, &m, &d, &hr, &min, &sec) == 6) {
269 *when = utc_mktime(&cur_tm);
283 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
289 /* Integer or floating point? */
290 val = strtol(buffer, &end, 10);
291 if (val < 0 || end == buffer)
294 /* Looks like it might be floating point? */
297 fp = strtod(buffer, &end);
308 static void pressure(char *buffer, void *_press)
311 pressure_t *pressure = _press;
312 union int_or_float val;
314 switch (integer_or_float(buffer, &val)) {
316 /* Just ignore zero values */
319 switch (input_units.pressure) {
324 /* Assume mbar, but if it's really small, it's bar */
330 mbar = val.fp * 68.95;
333 if (mbar > 5 && mbar < 500000) {
334 pressure->mbar = mbar + 0.5;
339 printf("Strange pressure reading %s\n", buffer);
344 static void depth(char *buffer, void *_depth)
346 depth_t *depth = _depth;
347 union int_or_float val;
349 switch (integer_or_float(buffer, &val)) {
351 switch (input_units.length) {
353 depth->mm = val.fp * 1000 + 0.5;
356 depth->mm = val.fp * 304.8 + 0.5;
361 printf("Strange depth reading %s\n", buffer);
366 static void weight(char *buffer, void *_weight)
368 weight_t *weight = _weight;
369 union int_or_float val;
371 switch (integer_or_float(buffer, &val)) {
373 switch (input_units.weight) {
375 weight->grams = val.fp * 1000 + 0.5;
378 weight->grams = val.fp * 453.6 + 0.5;
383 printf("Strange depth reading %s\n", buffer);
387 static void temperature(char *buffer, void *_temperature)
389 temperature_t *temperature = _temperature;
390 union int_or_float val;
392 switch (integer_or_float(buffer, &val)) {
394 /* Ignore zero. It means "none" */
398 switch (input_units.temperature) {
400 temperature->mkelvin = val.fp * 1000;
403 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
406 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
411 printf("Strange temperature reading %s\n", buffer);
416 static void sampletime(char *buffer, void *_time)
420 duration_t *time = _time;
422 i = sscanf(buffer, "%d:%d", &min, &sec);
429 time->seconds = sec + min*60;
432 printf("Strange sample time reading %s\n", buffer);
437 static void duration(char *buffer, void *_time)
439 sampletime(buffer, _time);
442 static void percent(char *buffer, void *_fraction)
444 fraction_t *fraction = _fraction;
445 union int_or_float val;
447 switch (integer_or_float(buffer, &val)) {
450 fraction->permille = val.fp * 10 + 0.5;
454 printf("Strange percentage reading %s\n", buffer);
460 static void gasmix(char *buffer, void *_fraction)
462 /* libdivecomputer does negative percentages. */
465 if (cur_cylinder_index < MAX_CYLINDERS)
466 percent(buffer, _fraction);
469 static void gasmix_nitrogen(char *buffer, void *_gasmix)
471 /* Ignore n2 percentages. There's no value in them. */
474 static void cylindersize(char *buffer, void *_volume)
476 volume_t *volume = _volume;
477 union int_or_float val;
479 switch (integer_or_float(buffer, &val)) {
481 volume->mliter = val.fp * 1000 + 0.5;
485 printf("Strange volume reading %s\n", buffer);
491 static void utf8_string(char *buffer, void *_res)
493 *(char **)_res = buffer;
497 * Uemis water_pressure. In centibar. And when converting to
498 * depth, I'm just going to always use saltwater, because I
499 * think "true depth" is just stupid. From a diving standpoint,
500 * "true depth" is pretty much completely pointless, unless
501 * you're doing some kind of underwater surveying work.
503 * So I give water depths in "pressure depth", always assuming
504 * salt water. So one atmosphere per 10m.
506 static void water_pressure(char *buffer, void *_depth)
508 depth_t *depth = _depth;
509 union int_or_float val;
512 switch (integer_or_float(buffer, &val)) {
517 atm = bar_to_atm(val.fp * 10);
519 * atm to cm. Why not mm? The precision just isn't
522 cm = 100 * atm + 0.5;
524 depth->mm = 10 * (long)cm;
528 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
533 #define MATCH(pattern, fn, dest) \
534 match(pattern, strlen(pattern), name, len, fn, buf, dest)
536 static void get_index(char *buffer, void *_i)
543 static void get_tripflag(char *buffer, void *_tf)
545 tripflag_t *tf = _tf;
549 for (i = NO_TRIP; i < NUM_TRIPFLAGS; i++)
550 if(! strcmp(buffer, tripflag_names[i]))
554 static void centibar(char *buffer, void *_pressure)
556 pressure_t *pressure = _pressure;
557 union int_or_float val;
559 switch (integer_or_float(buffer, &val)) {
561 pressure->mbar = val.fp * 10 + 0.5;
564 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
569 static void decicelsius(char *buffer, void *_temp)
571 temperature_t *temp = _temp;
572 union int_or_float val;
574 switch (integer_or_float(buffer, &val)) {
576 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
579 fprintf(stderr, "Strange julian date: %s", buffer);
584 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
586 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
587 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
588 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
589 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
590 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
595 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
596 * for the sample temperatures, that are in Fahrenheit.
599 * Oh, and I think Diving Log *internally* probably kept them
600 * in celsius, because I'm seeing entries like
604 * in there. Which is freezing, aka 0 degC. I bet the "0" is
605 * what Diving Log uses for "no temperature".
607 * So throw away crap like that.
609 static void fahrenheit(char *buffer, void *_temperature)
611 temperature_t *temperature = _temperature;
612 union int_or_float val;
614 switch (integer_or_float(buffer, &val)) {
616 /* Floating point equality is evil, but works for small integers */
619 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
622 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
628 * Did I mention how bat-shit crazy divinglog is? The sample
629 * pressures are in PSI. But the tank working pressure is in
632 * Crazy stuff like this is why subsurface has everything in
633 * these inconvenient typed structures, and you have to say
634 * "pressure->mbar" to get the actual value. Exactly so that
635 * you can never have unit confusion.
637 static void psi(char *buffer, void *_pressure)
639 pressure_t *pressure = _pressure;
640 union int_or_float val;
642 switch (integer_or_float(buffer, &val)) {
644 pressure->mbar = val.fp * 68.95 + 0.5;
647 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
652 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
654 return MATCH(".p.time", sampletime, &sample->time) ||
655 MATCH(".p.depth", depth, &sample->depth) ||
656 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
657 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
661 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
663 return MATCH(".divetime", sampletime, &sample->time) ||
664 MATCH(".depth", depth, &sample->depth) ||
665 MATCH(".temperature", temperature, &sample->temperature) ||
666 MATCH(".tankpressure", pressure, &sample->cylinderpressure) ||
670 static void eventtime(char *buffer, void *_duration)
672 duration_t *duration = _duration;
673 sampletime(buffer, duration);
675 duration->seconds += cur_sample->time.seconds;
678 static void try_to_fill_event(const char *name, char *buf)
680 int len = strlen(name);
682 start_match("event", name, buf);
683 if (MATCH(".event", utf8_string, &cur_event.name))
685 if (MATCH(".name", utf8_string, &cur_event.name))
687 if (MATCH(".time", eventtime, &cur_event.time))
689 if (MATCH(".type", get_index, &cur_event.type))
691 if (MATCH(".flags", get_index, &cur_event.flags))
693 if (MATCH(".value", get_index, &cur_event.value))
695 nonmatch("event", name, buf);
698 /* We're in samples - try to convert the random xml value to something useful */
699 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
701 int len = strlen(name);
703 start_match("sample", name, buf);
704 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
706 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
708 if (MATCH(".sample.cylinderindex", get_index, &sample->cylinderindex))
710 if (MATCH(".sample.depth", depth, &sample->depth))
712 if (MATCH(".sample.temp", temperature, &sample->temperature))
714 if (MATCH(".sample.temperature", temperature, &sample->temperature))
716 if (MATCH(".sample.sampletime", sampletime, &sample->time))
718 if (MATCH(".sample.time", sampletime, &sample->time))
721 switch (import_source) {
723 if (uemis_fill_sample(sample, name, len, buf))
728 if (divinglog_fill_sample(sample, name, len, buf))
733 if (uddf_fill_sample(sample, name, len, buf))
741 nonmatch("sample", name, buf);
744 static const char *country, *city;
746 static void divinglog_place(char *place, void *_location)
748 char **location = _location;
749 char buffer[256], *p;
752 len = snprintf(buffer, sizeof(buffer),
758 country ? country : "");
761 memcpy(p, buffer, len+1);
768 static int divinglog_dive_match(struct dive **divep, const char *name, int len, char *buf)
770 struct dive *dive = *divep;
772 return MATCH(".divedate", divedate, &dive->when) ||
773 MATCH(".entrytime", divetime, &dive->when) ||
774 MATCH(".depth", depth, &dive->maxdepth) ||
775 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
776 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
777 MATCH(".comments", utf8_string, &dive->notes) ||
778 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
779 MATCH(".country.name", utf8_string, &country) ||
780 MATCH(".city.name", utf8_string, &city) ||
781 MATCH(".place.name", divinglog_place, &dive->location) ||
785 static int buffer_value(char *buffer)
787 int val = atoi(buffer);
792 static void uemis_length_unit(char *buffer, void *_unused)
794 input_units.length = buffer_value(buffer) ? FEET : METERS;
797 static void uemis_volume_unit(char *buffer, void *_unused)
799 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
802 static void uemis_pressure_unit(char *buffer, void *_unused)
805 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
809 static void uemis_temperature_unit(char *buffer, void *_unused)
811 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
814 static void uemis_weight_unit(char *buffer, void *_unused)
816 input_units.weight = buffer_value(buffer) ? LBS : KG;
819 static void uemis_time_unit(char *buffer, void *_unused)
823 static void uemis_date_unit(char *buffer, void *_unused)
827 /* Modified julian day, yay! */
828 static void uemis_date_time(char *buffer, void *_when)
830 time_t *when = _when;
831 union int_or_float val;
833 switch (integer_or_float(buffer, &val)) {
835 *when = (val.fp - 40587) * 86400;
838 fprintf(stderr, "Strange julian date: %s", buffer);
844 * Uemis doesn't know time zones. You need to do them as
845 * minutes, not hours.
847 * But that's ok, we don't track timezones yet either. We
848 * just turn everything into "localtime expressed as UTC".
850 static void uemis_time_zone(char *buffer, void *_when)
852 #if 0 /* seems like this is only used to display it correctly
853 * the stored time appears to be UTC */
855 time_t *when = _when;
856 signed char tz = atoi(buffer);
862 static void uemis_ts(char *buffer, void *_when)
865 time_t *when = _when;
867 memset(&tm, 0, sizeof(tm));
868 sscanf(buffer,"%d-%d-%dT%d:%d:%d",
869 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
870 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
873 *when = utc_mktime(&tm);
877 static void uemis_duration(char *buffer, void *_duration)
879 duration_t *duration = _duration;
880 duration->seconds = atof(buffer) * 60 + 0.5;
883 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
884 static int uemis_gas_template;
887 * Christ. Uemis tank data is a total mess.
889 * We're passed a "virtual cylinder" (0 - 6) for the different
890 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
891 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
892 * into the actual cylinder data depending on the gas template,
893 * and ignore the ones that are irrelevant for that template.
895 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
896 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
897 * (which are the three "nitrox_3" tanks), and we turn virtual
898 * tanks 2/3 into actual tanks 0/1.
902 static int uemis_cylinder_index(void *_cylinder)
904 cylinder_t *cylinder = _cylinder;
905 unsigned int index = cylinder - cur_dive->cylinder;
908 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
911 switch(uemis_gas_template) {
912 case 1: /* Dive uses tank 1 */
915 case 0: /* Dive uses tank 0 */
919 case 2: /* Dive uses tanks 2-3 */
924 case 3: /* Dive uses tanks 4-6 */
933 static void uemis_cylindersize(char *buffer, void *_cylinder)
935 int index = uemis_cylinder_index(_cylinder);
937 cylindersize(buffer, &cur_dive->cylinder[index].type.size);
940 static void uemis_percent(char *buffer, void *_cylinder)
942 int index = uemis_cylinder_index(_cylinder);
944 percent(buffer, &cur_dive->cylinder[index].gasmix.o2);
947 static int uemis_dive_match(struct dive **divep, const char *name, int len, char *buf)
949 struct dive *dive = *divep;
951 return MATCH(".units.length", uemis_length_unit, &input_units) ||
952 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
953 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
954 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
955 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
956 MATCH(".units.time", uemis_time_unit, &input_units) ||
957 MATCH(".units.date", uemis_date_unit, &input_units) ||
958 MATCH(".date_time", uemis_date_time, &dive->when) ||
959 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
960 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
961 MATCH(".gas.template", get_index, &uemis_gas_template) ||
962 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
963 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
964 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
965 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
966 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
967 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
968 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
969 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
970 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
971 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
972 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
973 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
974 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
975 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
976 MATCH(".dive.val.float", uemis_duration, &dive->duration) ||
977 MATCH(".dive.val.ts", uemis_ts, &dive->when) ||
978 MATCH(".dive.val.bin", uemis_parse_divelog_binary, divep) ||
983 * Uddf specifies ISO 8601 time format.
985 * There are many variations on that. This handles the useful cases.
987 static void uddf_datetime(char *buffer, void *_when)
991 time_t *when = _when;
992 struct tm tm = { 0 };
995 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
1002 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
1009 printf("Bad date time %s\n", buffer);
1014 if (c != 'T' && c != ' ')
1022 *when = utc_mktime(&tm);
1026 static int uddf_dive_match(struct dive **divep, const char *name, int len, char *buf)
1028 struct dive *dive = *divep;
1030 return MATCH(".datetime", uddf_datetime, &dive->when) ||
1031 MATCH(".diveduration", duration, &dive->duration) ||
1032 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
1036 static void gps_location(char *buffer, void *_dive)
1039 struct dive *dive = _dive;
1040 double latitude, longitude;
1042 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
1044 dive->latitude = latitude;
1045 dive->longitude = longitude;
1050 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
1051 static void try_to_fill_dive(struct dive **divep, const char *name, char *buf)
1053 int len = strlen(name);
1055 start_match("dive", name, buf);
1057 switch (import_source) {
1059 if (uemis_dive_match(divep, name, len, buf))
1064 if (divinglog_dive_match(divep, name, len, buf))
1069 if (uddf_dive_match(divep, name, len, buf))
1077 struct dive *dive = *divep;
1079 if (MATCH(".number", get_index, &dive->number))
1081 if (MATCH(".tripflag", get_tripflag, &dive->tripflag))
1083 if (MATCH(".date", divedate, &dive->when))
1085 if (MATCH(".time", divetime, &dive->when))
1087 if (MATCH(".datetime", divedatetime, &dive->when))
1089 if (MATCH(".maxdepth", depth, &dive->maxdepth))
1091 if (MATCH(".meandepth", depth, &dive->meandepth))
1093 if (MATCH(".depth.max", depth, &dive->maxdepth))
1095 if (MATCH(".depth.mean", depth, &dive->meandepth))
1097 if (MATCH(".duration", duration, &dive->duration))
1099 if (MATCH(".divetime", duration, &dive->duration))
1101 if (MATCH(".divetimesec", duration, &dive->duration))
1103 if (MATCH(".surfacetime", duration, &dive->surfacetime))
1105 if (MATCH(".airtemp", temperature, &dive->airtemp))
1107 if (MATCH(".watertemp", temperature, &dive->watertemp))
1109 if (MATCH(".temperature.air", temperature, &dive->airtemp))
1111 if (MATCH(".temperature.water", temperature, &dive->watertemp))
1113 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
1115 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1117 if (MATCH(".gps", gps_location, dive))
1119 if (MATCH(".location", utf8_string, &dive->location))
1121 if (MATCH(".suit", utf8_string, &dive->suit))
1123 if (MATCH(".divesuit", utf8_string, &dive->suit))
1125 if (MATCH(".notes", utf8_string, &dive->notes))
1127 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1129 if (MATCH(".buddy", utf8_string, &dive->buddy))
1131 if (MATCH(".rating", get_index, &dive->rating))
1133 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cur_cylinder_index].type.size))
1135 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cur_cylinder_index].type.workingpressure))
1137 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cur_cylinder_index].type.description))
1139 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cur_cylinder_index].start))
1141 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cur_cylinder_index].end))
1143 if (MATCH(".weightsystem.description", utf8_string, &dive->weightsystem[cur_ws_index].description))
1145 if (MATCH(".weightsystem.weight", weight, &dive->weightsystem[cur_ws_index].weight))
1147 if (MATCH("weight", weight, &dive->weightsystem[cur_ws_index].weight))
1149 if (MATCH(".o2", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.o2))
1151 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cur_cylinder_index].gasmix))
1153 if (MATCH(".he", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.he))
1156 nonmatch("dive", name, buf);
1159 /* We're in the top-level trip xml. Try to convert whatever value to a trip value */
1160 static void try_to_fill_trip(struct dive **divep, const char *name, char *buf)
1162 int len = strlen(name);
1164 start_match("trip", name, buf);
1166 struct dive *dive = *divep;
1168 if (MATCH(".date", divedate, &dive->when)) {
1169 dive->when = utc_mktime(&cur_tm);
1172 if (MATCH(".location", utf8_string, &dive->location))
1174 if (MATCH(".notes", utf8_string, &dive->notes))
1177 nonmatch("trip", name, buf);
1181 * File boundaries are dive boundaries. But sometimes there are
1182 * multiple dives per file, so there can be other events too that
1183 * trigger a "new dive" marker and you may get some nesting due
1184 * to that. Just ignore nesting levels.
1186 static void dive_start(void)
1190 cur_dive = alloc_dive();
1191 memset(&cur_tm, 0, sizeof(cur_tm));
1194 static void dive_end(void)
1198 record_dive(cur_dive);
1200 cur_cylinder_index = 0;
1204 static void trip_start(void)
1208 cur_trip = alloc_dive();
1209 memset(&cur_tm, 0, sizeof(cur_tm));
1212 static void trip_end(void)
1216 record_trip(cur_trip);
1220 static void event_start(void)
1222 memset(&cur_event, 0, sizeof(cur_event));
1223 cur_event.active = 1;
1226 static void event_end(void)
1228 if (cur_event.name && strcmp(cur_event.name, "surface") != 0)
1229 add_event(cur_dive, cur_event.time.seconds,
1230 cur_event.type, cur_event.flags,
1231 cur_event.value, cur_event.name);
1232 cur_event.active = 0;
1235 static void cylinder_start(void)
1239 static void cylinder_end(void)
1241 cur_cylinder_index++;
1244 static void ws_start(void)
1248 static void ws_end(void)
1253 static void sample_start(void)
1255 cur_sample = prepare_sample(&cur_dive);
1258 static void sample_end(void)
1263 finish_sample(cur_dive);
1267 static void entry(const char *name, int size, const char *raw)
1269 char *buf = malloc(size+1);
1273 memcpy(buf, raw, size);
1275 if (cur_event.active) {
1276 try_to_fill_event(name, buf);
1280 try_to_fill_sample(cur_sample, name, buf);
1284 try_to_fill_trip(&cur_trip, name, buf);
1288 try_to_fill_dive(&cur_dive, name, buf);
1293 static const char *nodename(xmlNode *node, char *buf, int len)
1295 if (!node || !node->name)
1303 const char *name = node->name;
1304 int i = strlen(name);
1306 unsigned char c = name[i];
1307 *--buf = tolower(c);
1311 node = node->parent;
1312 if (!node || !node->name)
1322 static void visit_one_node(xmlNode *node)
1325 const unsigned char *content;
1326 char buffer[MAXNAME];
1329 content = node->content;
1333 /* Trim whitespace at beginning */
1334 while (isspace(*content))
1337 /* Trim whitespace at end */
1338 len = strlen(content);
1339 while (len && isspace(content[len-1]))
1345 /* Don't print out the node name if it is "text" */
1346 if (!strcmp(node->name, "text"))
1347 node = node->parent;
1349 name = nodename(node, buffer, sizeof(buffer));
1351 entry(name, len, content);
1354 static void traverse(xmlNode *root);
1356 static void traverse_properties(xmlNode *node)
1360 for (p = node->properties; p; p = p->next)
1361 traverse(p->children);
1364 static void visit(xmlNode *n)
1367 traverse_properties(n);
1368 traverse(n->children);
1371 static void uemis_importer(void)
1373 import_source = UEMIS;
1374 input_units = SI_units;
1377 static void DivingLog_importer(void)
1379 import_source = DIVINGLOG;
1382 * Diving Log units are really strange.
1384 * Temperatures are in C, except in samples,
1385 * when they are in Fahrenheit. Depths are in
1386 * meters, an dpressure is in PSI in the samples,
1387 * but in bar when it comes to working pressure.
1389 * Crazy f*%^ morons.
1391 input_units = SI_units;
1394 static void uddf_importer(void)
1396 import_source = UDDF;
1397 input_units = SI_units;
1398 input_units.pressure = PASCAL;
1399 input_units.temperature = KELVIN;
1403 * I'm sure this could be done as some fancy DTD rules.
1404 * It's just not worth the headache.
1406 static struct nesting {
1408 void (*start)(void), (*end)(void);
1410 { "dive", dive_start, dive_end },
1411 { "Dive", dive_start, dive_end },
1412 { "trip", trip_start, trip_end },
1413 { "sample", sample_start, sample_end },
1414 { "waypoint", sample_start, sample_end },
1415 { "SAMPLE", sample_start, sample_end },
1416 { "reading", sample_start, sample_end },
1417 { "event", event_start, event_end },
1418 { "gasmix", cylinder_start, cylinder_end },
1419 { "cylinder", cylinder_start, cylinder_end },
1420 { "weightsystem", ws_start, ws_end },
1421 { "P", sample_start, sample_end },
1423 /* Import type recognition */
1424 { "Divinglog", DivingLog_importer },
1425 { "pre_dive", uemis_importer },
1426 { "dives", uemis_importer },
1427 { "uddf", uddf_importer },
1432 static void traverse(xmlNode *root)
1436 for (n = root; n; n = n->next) {
1437 struct nesting *rule = nesting;
1440 if (!strcmp(rule->name, n->name))
1443 } while (rule->name);
1453 /* Per-file reset */
1454 static void reset_all(void)
1457 * We reset the units for each file. You'd think it was
1458 * a per-dive property, but I'm not going to trust people
1459 * to do per-dive setup. If the xml does have per-dive
1460 * data within one file, we might have to reset it per
1461 * dive for that format.
1463 input_units = SI_units;
1464 import_source = UNKNOWN;
1467 void parse_xml_buffer(const char *url, const char *buffer, int size, GError **error)
1471 doc = xmlReadMemory(buffer, size, url, NULL, 0);
1473 fprintf(stderr, "Failed to parse '%s'.\n", url);
1476 *error = g_error_new(g_quark_from_string("subsurface"),
1478 "Failed to parse '%s'",
1483 /* we assume that the last (or only) filename passed as argument is a
1484 * great filename to use as default when saving the dives */
1489 doc = test_xslt_transforms(doc);
1491 traverse(xmlDocGetRootElement(doc));
1497 void parse_xml_init(void)
1504 /* Maybe we'll want a environment variable that can override this.. */
1505 static const char *xslt_path = XSLT ":xslt:.";
1507 static xsltStylesheetPtr try_get_stylesheet(const char *path, int len, const char *name)
1509 xsltStylesheetPtr ret;
1510 int namelen = strlen(name);
1511 char *filename = malloc(len+1+namelen+1);
1516 memcpy(filename, path, len);
1517 filename[len] = G_DIR_SEPARATOR;
1518 memcpy(filename + len + 1, name, namelen+1);
1521 if (!access(filename, R_OK))
1522 ret = xsltParseStylesheetFile(filename);
1528 static xsltStylesheetPtr get_stylesheet(const char *name)
1530 const char *path, *next;
1532 path = getenv("SUBSURFACE_XSLT_PATH");
1538 xsltStylesheetPtr ret;
1540 next = strchr(path, ':');
1546 ret = try_get_stylesheet(path, len, name);
1549 } while ((path = next) != NULL);
1554 static struct xslt_files {
1558 { "SUUNTO", "SuuntoSDM.xslt" },
1559 { "JDiveLog", "jdivelog2subsurface.xslt" },
1563 xmlDoc *test_xslt_transforms(xmlDoc *doc)
1565 struct xslt_files *info = xslt_files;
1566 xmlDoc *transformed;
1567 xsltStylesheetPtr xslt = NULL;
1568 xmlNode *root_element = xmlDocGetRootElement(doc);
1570 while ((info->root) && (strcasecmp(root_element->name, info->root) != 0)) {
1575 xmlSubstituteEntitiesDefault(1);
1576 xslt = get_stylesheet(info->file);
1579 transformed = xsltApplyStylesheet(xslt, doc, NULL);
1581 xsltFreeStylesheet(xslt);