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.
79 * And kg instead of g.
81 const struct units SI_units = {
85 .temperature = CELSIUS,
89 const struct units IMPERIAL_units = {
93 .temperature = FAHRENHEIT,
98 * Dive info as it is being built up..
100 static struct dive *cur_dive;
101 static struct sample *cur_sample;
105 int type, flags, value;
108 static struct tm cur_tm;
109 static int cur_cylinder_index, cur_ws_index;
111 static enum import_source {
119 time_t utc_mktime(struct tm *tm)
121 static const int mdays[] = {
122 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
124 int year = tm->tm_year;
125 int month = tm->tm_mon;
126 int day = tm->tm_mday;
128 /* First normalize relative to 1900 */
131 else if (year > 1900)
134 /* Normalized to Jan 1, 1970: unix time */
137 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
139 if (month < 0 || month > 11) /* array bounds */
141 if (month < 2 || (year + 2) % 4)
143 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
145 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
146 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
149 static void divedate(char *buffer, void *_when)
152 time_t *when = _when;
155 success = cur_tm.tm_sec | cur_tm.tm_min | cur_tm.tm_hour;
156 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
160 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
165 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
170 *when = utc_mktime(&cur_tm);
175 static void divetime(char *buffer, void *_when)
178 time_t *when = _when;
180 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
185 *when = utc_mktime(&cur_tm);
190 /* Libdivecomputer: "2011-03-20 10:22:38" */
191 static void divedatetime(char *buffer, void *_when)
195 time_t *when = _when;
197 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
198 &y, &m, &d, &hr, &min, &sec) == 6) {
205 *when = utc_mktime(&cur_tm);
219 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
225 /* Integer or floating point? */
226 val = strtol(buffer, &end, 10);
227 if (val < 0 || end == buffer)
230 /* Looks like it might be floating point? */
233 fp = strtod(buffer, &end);
244 static void pressure(char *buffer, void *_press)
247 pressure_t *pressure = _press;
248 union int_or_float val;
250 switch (integer_or_float(buffer, &val)) {
252 /* Just ignore zero values */
255 switch (input_units.pressure) {
260 /* Assume mbar, but if it's really small, it's bar */
266 mbar = val.fp * 68.95;
269 if (mbar > 5 && mbar < 500000) {
270 pressure->mbar = mbar + 0.5;
275 printf("Strange pressure reading %s\n", buffer);
280 static void depth(char *buffer, void *_depth)
282 depth_t *depth = _depth;
283 union int_or_float val;
285 switch (integer_or_float(buffer, &val)) {
287 switch (input_units.length) {
289 depth->mm = val.fp * 1000 + 0.5;
292 depth->mm = val.fp * 304.8 + 0.5;
297 printf("Strange depth reading %s\n", buffer);
302 static void weight(char *buffer, void *_weight)
304 weight_t *weight = _weight;
305 union int_or_float val;
307 switch (integer_or_float(buffer, &val)) {
309 switch (input_units.weight) {
311 weight->grams = val.fp * 1000 + 0.5;
314 weight->grams = val.fp * 453.6 + 0.5;
319 printf("Strange depth reading %s\n", buffer);
323 static void temperature(char *buffer, void *_temperature)
325 temperature_t *temperature = _temperature;
326 union int_or_float val;
328 switch (integer_or_float(buffer, &val)) {
330 /* Ignore zero. It means "none" */
334 switch (input_units.temperature) {
336 temperature->mkelvin = val.fp * 1000;
339 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
342 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
347 printf("Strange temperature reading %s\n", buffer);
352 static void sampletime(char *buffer, void *_time)
356 duration_t *time = _time;
358 i = sscanf(buffer, "%d:%d", &min, &sec);
365 time->seconds = sec + min*60;
368 printf("Strange sample time reading %s\n", buffer);
373 static void duration(char *buffer, void *_time)
375 sampletime(buffer, _time);
378 static void percent(char *buffer, void *_fraction)
380 fraction_t *fraction = _fraction;
381 union int_or_float val;
383 switch (integer_or_float(buffer, &val)) {
386 fraction->permille = val.fp * 10 + 0.5;
390 printf("Strange percentage reading %s\n", buffer);
396 static void gasmix(char *buffer, void *_fraction)
398 /* libdivecomputer does negative percentages. */
401 if (cur_cylinder_index < MAX_CYLINDERS)
402 percent(buffer, _fraction);
405 static void gasmix_nitrogen(char *buffer, void *_gasmix)
407 /* Ignore n2 percentages. There's no value in them. */
410 static void cylindersize(char *buffer, void *_volume)
412 volume_t *volume = _volume;
413 union int_or_float val;
415 switch (integer_or_float(buffer, &val)) {
417 volume->mliter = val.fp * 1000 + 0.5;
421 printf("Strange volume reading %s\n", buffer);
427 static void utf8_string(char *buffer, void *_res)
429 *(char **)_res = buffer;
433 * Uemis water_pressure. In centibar. And when converting to
434 * depth, I'm just going to always use saltwater, because I
435 * think "true depth" is just stupid. From a diving standpoint,
436 * "true depth" is pretty much completely pointless, unless
437 * you're doing some kind of underwater surveying work.
439 * So I give water depths in "pressure depth", always assuming
440 * salt water. So one atmosphere per 10m.
442 static void water_pressure(char *buffer, void *_depth)
444 depth_t *depth = _depth;
445 union int_or_float val;
448 switch (integer_or_float(buffer, &val)) {
453 atm = bar_to_atm(val.fp * 10);
455 * atm to cm. Why not mm? The precision just isn't
458 cm = 100 * atm + 0.5;
460 depth->mm = 10 * (long)cm;
464 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
469 #define MATCH(pattern, fn, dest) \
470 match(pattern, strlen(pattern), name, len, fn, buf, dest)
472 static void get_index(char *buffer, void *_i)
479 static void centibar(char *buffer, void *_pressure)
481 pressure_t *pressure = _pressure;
482 union int_or_float val;
484 switch (integer_or_float(buffer, &val)) {
486 pressure->mbar = val.fp * 10 + 0.5;
489 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
494 static void decicelsius(char *buffer, void *_temp)
496 temperature_t *temp = _temp;
497 union int_or_float val;
499 switch (integer_or_float(buffer, &val)) {
501 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
504 fprintf(stderr, "Strange julian date: %s", buffer);
509 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
511 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
512 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
513 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
514 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
515 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
520 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
521 * for the sample temperatures, that are in Fahrenheit.
524 * Oh, and I think Diving Log *internally* probably kept them
525 * in celsius, because I'm seeing entries like
529 * in there. Which is freezing, aka 0 degC. I bet the "0" is
530 * what Diving Log uses for "no temperature".
532 * So throw away crap like that.
534 static void fahrenheit(char *buffer, void *_temperature)
536 temperature_t *temperature = _temperature;
537 union int_or_float val;
539 switch (integer_or_float(buffer, &val)) {
541 /* Floating point equality is evil, but works for small integers */
544 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
547 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
553 * Did I mention how bat-shit crazy divinglog is? The sample
554 * pressures are in PSI. But the tank working pressure is in
557 * Crazy stuff like this is why subsurface has everything in
558 * these inconvenient typed structures, and you have to say
559 * "pressure->mbar" to get the actual value. Exactly so that
560 * you can never have unit confusion.
562 static void psi(char *buffer, void *_pressure)
564 pressure_t *pressure = _pressure;
565 union int_or_float val;
567 switch (integer_or_float(buffer, &val)) {
569 pressure->mbar = val.fp * 68.95 + 0.5;
572 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
577 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
579 return MATCH(".p.time", sampletime, &sample->time) ||
580 MATCH(".p.depth", depth, &sample->depth) ||
581 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
582 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
586 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
588 return MATCH(".divetime", sampletime, &sample->time) ||
589 MATCH(".depth", depth, &sample->depth) ||
590 MATCH(".temperature", temperature, &sample->temperature) ||
594 static void eventtime(char *buffer, void *_duration)
596 duration_t *duration = _duration;
597 sampletime(buffer, duration);
599 duration->seconds += cur_sample->time.seconds;
602 static void try_to_fill_event(const char *name, char *buf)
604 int len = strlen(name);
606 start_match("event", name, buf);
607 if (MATCH(".event", utf8_string, &cur_event.name))
609 if (MATCH(".name", utf8_string, &cur_event.name))
611 if (MATCH(".time", eventtime, &cur_event.time))
613 if (MATCH(".type", get_index, &cur_event.type))
615 if (MATCH(".flags", get_index, &cur_event.flags))
617 if (MATCH(".value", get_index, &cur_event.value))
619 nonmatch("event", name, buf);
622 /* We're in samples - try to convert the random xml value to something useful */
623 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
625 int len = strlen(name);
627 start_match("sample", name, buf);
628 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
630 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
632 if (MATCH(".sample.cylinderindex", get_index, &sample->cylinderindex))
634 if (MATCH(".sample.depth", depth, &sample->depth))
636 if (MATCH(".sample.temp", temperature, &sample->temperature))
638 if (MATCH(".sample.temperature", temperature, &sample->temperature))
640 if (MATCH(".sample.sampletime", sampletime, &sample->time))
642 if (MATCH(".sample.time", sampletime, &sample->time))
645 switch (import_source) {
647 if (uemis_fill_sample(sample, name, len, buf))
652 if (divinglog_fill_sample(sample, name, len, buf))
657 if (uddf_fill_sample(sample, name, len, buf))
665 nonmatch("sample", name, buf);
668 static const char *country, *city;
670 static void divinglog_place(char *place, void *_location)
672 char **location = _location;
673 char buffer[256], *p;
676 len = snprintf(buffer, sizeof(buffer),
682 country ? country : "");
685 memcpy(p, buffer, len+1);
692 static int divinglog_dive_match(struct dive **divep, const char *name, int len, char *buf)
694 struct dive *dive = *divep;
696 return MATCH(".divedate", divedate, &dive->when) ||
697 MATCH(".entrytime", divetime, &dive->when) ||
698 MATCH(".depth", depth, &dive->maxdepth) ||
699 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
700 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
701 MATCH(".comments", utf8_string, &dive->notes) ||
702 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
703 MATCH(".country.name", utf8_string, &country) ||
704 MATCH(".city.name", utf8_string, &city) ||
705 MATCH(".place.name", divinglog_place, &dive->location) ||
709 static int buffer_value(char *buffer)
711 int val = atoi(buffer);
716 static void uemis_length_unit(char *buffer, void *_unused)
718 input_units.length = buffer_value(buffer) ? FEET : METERS;
721 static void uemis_volume_unit(char *buffer, void *_unused)
723 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
726 static void uemis_pressure_unit(char *buffer, void *_unused)
729 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
733 static void uemis_temperature_unit(char *buffer, void *_unused)
735 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
738 static void uemis_weight_unit(char *buffer, void *_unused)
740 input_units.weight = buffer_value(buffer) ? LBS : KG;
743 static void uemis_time_unit(char *buffer, void *_unused)
747 static void uemis_date_unit(char *buffer, void *_unused)
751 /* Modified julian day, yay! */
752 static void uemis_date_time(char *buffer, void *_when)
754 time_t *when = _when;
755 union int_or_float val;
757 switch (integer_or_float(buffer, &val)) {
759 *when = (val.fp - 40587) * 86400;
762 fprintf(stderr, "Strange julian date: %s", buffer);
768 * Uemis doesn't know time zones. You need to do them as
769 * minutes, not hours.
771 * But that's ok, we don't track timezones yet either. We
772 * just turn everything into "localtime expressed as UTC".
774 static void uemis_time_zone(char *buffer, void *_when)
776 #if 0 /* seems like this is only used to display it correctly
777 * the stored time appears to be UTC */
779 time_t *when = _when;
780 signed char tz = atoi(buffer);
786 static void uemis_ts(char *buffer, void *_when)
789 time_t *when = _when;
791 memset(&tm, 0, sizeof(tm));
792 sscanf(buffer,"%d-%d-%dT%d:%d:%d",
793 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
794 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
797 *when = utc_mktime(&tm);
801 static void uemis_duration(char *buffer, void *_duration)
803 duration_t *duration = _duration;
804 duration->seconds = atof(buffer) * 60 + 0.5;
807 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
808 static int uemis_gas_template;
811 * Christ. Uemis tank data is a total mess.
813 * We're passed a "virtual cylinder" (0 - 6) for the different
814 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
815 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
816 * into the actual cylinder data depending on the gas template,
817 * and ignore the ones that are irrelevant for that template.
819 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
820 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
821 * (which are the three "nitrox_3" tanks), and we turn virtual
822 * tanks 2/3 into actual tanks 0/1.
826 static int uemis_cylinder_index(void *_cylinder)
828 cylinder_t *cylinder = _cylinder;
829 unsigned int index = cylinder - cur_dive->cylinder;
832 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
835 switch(uemis_gas_template) {
836 case 1: /* Dive uses tank 1 */
839 case 0: /* Dive uses tank 0 */
843 case 2: /* Dive uses tanks 2-3 */
848 case 3: /* Dive uses tanks 4-6 */
857 static void uemis_cylindersize(char *buffer, void *_cylinder)
859 int index = uemis_cylinder_index(_cylinder);
861 cylindersize(buffer, &cur_dive->cylinder[index].type.size);
864 static void uemis_percent(char *buffer, void *_cylinder)
866 int index = uemis_cylinder_index(_cylinder);
868 percent(buffer, &cur_dive->cylinder[index].gasmix.o2);
871 static int uemis_dive_match(struct dive **divep, const char *name, int len, char *buf)
873 struct dive *dive = *divep;
875 return MATCH(".units.length", uemis_length_unit, &input_units) ||
876 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
877 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
878 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
879 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
880 MATCH(".units.time", uemis_time_unit, &input_units) ||
881 MATCH(".units.date", uemis_date_unit, &input_units) ||
882 MATCH(".date_time", uemis_date_time, &dive->when) ||
883 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
884 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
885 MATCH(".gas.template", get_index, &uemis_gas_template) ||
886 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
887 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
888 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
889 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
890 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
891 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
892 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
893 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
894 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
895 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
896 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
897 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
898 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
899 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
900 MATCH(".dive.val.float", uemis_duration, &dive->duration) ||
901 MATCH(".dive.val.ts", uemis_ts, &dive->when) ||
902 MATCH(".dive.val.bin", uemis_parse_divelog_binary, divep) ||
907 * Uddf specifies ISO 8601 time format.
909 * There are many variations on that. This handles the useful cases.
911 static void uddf_datetime(char *buffer, void *_when)
915 time_t *when = _when;
916 struct tm tm = { 0 };
919 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
926 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
933 printf("Bad date time %s\n", buffer);
938 if (c != 'T' && c != ' ')
946 *when = utc_mktime(&tm);
950 static int uddf_dive_match(struct dive **divep, const char *name, int len, char *buf)
952 struct dive *dive = *divep;
954 return MATCH(".datetime", uddf_datetime, &dive->when) ||
955 MATCH(".diveduration", duration, &dive->duration) ||
956 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
960 static void gps_location(char *buffer, void *_dive)
963 struct dive *dive = _dive;
964 double latitude, longitude;
966 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
968 dive->latitude = latitude;
969 dive->longitude = longitude;
974 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
975 static void try_to_fill_dive(struct dive **divep, const char *name, char *buf)
977 int len = strlen(name);
979 start_match("dive", name, buf);
981 switch (import_source) {
983 if (uemis_dive_match(divep, name, len, buf))
988 if (divinglog_dive_match(divep, name, len, buf))
993 if (uddf_dive_match(divep, name, len, buf))
1001 struct dive *dive = *divep;
1003 if (MATCH(".number", get_index, &dive->number))
1005 if (MATCH(".date", divedate, &dive->when))
1007 if (MATCH(".time", divetime, &dive->when))
1009 if (MATCH(".datetime", divedatetime, &dive->when))
1011 if (MATCH(".maxdepth", depth, &dive->maxdepth))
1013 if (MATCH(".meandepth", depth, &dive->meandepth))
1015 if (MATCH(".depth.max", depth, &dive->maxdepth))
1017 if (MATCH(".depth.mean", depth, &dive->meandepth))
1019 if (MATCH(".duration", duration, &dive->duration))
1021 if (MATCH(".divetime", duration, &dive->duration))
1023 if (MATCH(".divetimesec", duration, &dive->duration))
1025 if (MATCH(".surfacetime", duration, &dive->surfacetime))
1027 if (MATCH(".airtemp", temperature, &dive->airtemp))
1029 if (MATCH(".watertemp", temperature, &dive->watertemp))
1031 if (MATCH(".temperature.air", temperature, &dive->airtemp))
1033 if (MATCH(".temperature.water", temperature, &dive->watertemp))
1035 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
1037 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1039 if (MATCH(".gps", gps_location, dive))
1041 if (MATCH(".location", utf8_string, &dive->location))
1043 if (MATCH(".notes", utf8_string, &dive->notes))
1045 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1047 if (MATCH(".buddy", utf8_string, &dive->buddy))
1049 if (MATCH(".rating", get_index, &dive->rating))
1051 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cur_cylinder_index].type.size))
1053 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cur_cylinder_index].type.workingpressure))
1055 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cur_cylinder_index].type.description))
1057 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cur_cylinder_index].start))
1059 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cur_cylinder_index].end))
1061 if (MATCH(".weightsystem.description", utf8_string, &dive->weightsystem[cur_ws_index].description))
1063 if (MATCH(".weightsystem.weight", weight, &dive->weightsystem[cur_ws_index].weight))
1065 if (MATCH("weight", weight, &dive->weightsystem[cur_ws_index].weight))
1067 if (MATCH(".o2", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.o2))
1069 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cur_cylinder_index].gasmix))
1071 if (MATCH(".he", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.he))
1074 nonmatch("dive", name, buf);
1078 * File boundaries are dive boundaries. But sometimes there are
1079 * multiple dives per file, so there can be other events too that
1080 * trigger a "new dive" marker and you may get some nesting due
1081 * to that. Just ignore nesting levels.
1083 static void dive_start(void)
1087 cur_dive = alloc_dive();
1088 memset(&cur_tm, 0, sizeof(cur_tm));
1091 static void dive_end(void)
1095 record_dive(cur_dive);
1097 cur_cylinder_index = 0;
1101 static void event_start(void)
1103 memset(&cur_event, 0, sizeof(cur_event));
1104 cur_event.active = 1;
1107 static void event_end(void)
1109 if (cur_event.name && strcmp(cur_event.name, "surface") != 0)
1110 add_event(cur_dive, cur_event.time.seconds,
1111 cur_event.type, cur_event.flags,
1112 cur_event.value, cur_event.name);
1113 cur_event.active = 0;
1116 static void cylinder_start(void)
1120 static void cylinder_end(void)
1122 cur_cylinder_index++;
1125 static void ws_start(void)
1129 static void ws_end(void)
1134 static void sample_start(void)
1136 cur_sample = prepare_sample(&cur_dive);
1139 static void sample_end(void)
1144 finish_sample(cur_dive);
1148 static void entry(const char *name, int size, const char *raw)
1150 char *buf = malloc(size+1);
1154 memcpy(buf, raw, size);
1156 if (cur_event.active) {
1157 try_to_fill_event(name, buf);
1161 try_to_fill_sample(cur_sample, name, buf);
1165 try_to_fill_dive(&cur_dive, name, buf);
1170 static const char *nodename(xmlNode *node, char *buf, int len)
1172 if (!node || !node->name)
1180 const char *name = node->name;
1181 int i = strlen(name);
1183 unsigned char c = name[i];
1184 *--buf = tolower(c);
1188 node = node->parent;
1189 if (!node || !node->name)
1199 static void visit_one_node(xmlNode *node)
1202 const unsigned char *content;
1203 char buffer[MAXNAME];
1206 content = node->content;
1210 /* Trim whitespace at beginning */
1211 while (isspace(*content))
1214 /* Trim whitespace at end */
1215 len = strlen(content);
1216 while (len && isspace(content[len-1]))
1222 /* Don't print out the node name if it is "text" */
1223 if (!strcmp(node->name, "text"))
1224 node = node->parent;
1226 name = nodename(node, buffer, sizeof(buffer));
1228 entry(name, len, content);
1231 static void traverse(xmlNode *root);
1233 static void traverse_properties(xmlNode *node)
1237 for (p = node->properties; p; p = p->next)
1238 traverse(p->children);
1241 static void visit(xmlNode *n)
1244 traverse_properties(n);
1245 traverse(n->children);
1248 static void uemis_importer(void)
1250 import_source = UEMIS;
1251 input_units = SI_units;
1254 static void DivingLog_importer(void)
1256 import_source = DIVINGLOG;
1259 * Diving Log units are really strange.
1261 * Temperatures are in C, except in samples,
1262 * when they are in Fahrenheit. Depths are in
1263 * meters, an dpressure is in PSI in the samples,
1264 * but in bar when it comes to working pressure.
1266 * Crazy f*%^ morons.
1268 input_units = SI_units;
1271 static void uddf_importer(void)
1273 import_source = UDDF;
1274 input_units = SI_units;
1275 input_units.pressure = PASCAL;
1276 input_units.temperature = KELVIN;
1280 * I'm sure this could be done as some fancy DTD rules.
1281 * It's just not worth the headache.
1283 static struct nesting {
1285 void (*start)(void), (*end)(void);
1287 { "dive", dive_start, dive_end },
1288 { "Dive", dive_start, dive_end },
1289 { "sample", sample_start, sample_end },
1290 { "waypoint", sample_start, sample_end },
1291 { "SAMPLE", sample_start, sample_end },
1292 { "reading", sample_start, sample_end },
1293 { "event", event_start, event_end },
1294 { "gasmix", cylinder_start, cylinder_end },
1295 { "cylinder", cylinder_start, cylinder_end },
1296 { "weightsystem", ws_start, ws_end },
1297 { "P", sample_start, sample_end },
1299 /* Import type recognition */
1300 { "Divinglog", DivingLog_importer },
1301 { "pre_dive", uemis_importer },
1302 { "dives", uemis_importer },
1303 { "uddf", uddf_importer },
1308 static void traverse(xmlNode *root)
1312 for (n = root; n; n = n->next) {
1313 struct nesting *rule = nesting;
1316 if (!strcmp(rule->name, n->name))
1319 } while (rule->name);
1329 /* Per-file reset */
1330 static void reset_all(void)
1333 * We reset the units for each file. You'd think it was
1334 * a per-dive property, but I'm not going to trust people
1335 * to do per-dive setup. If the xml does have per-dive
1336 * data within one file, we might have to reset it per
1337 * dive for that format.
1339 input_units = SI_units;
1340 import_source = UNKNOWN;
1343 void parse_xml_buffer(const char *url, const char *buffer, int size, GError **error)
1347 doc = xmlReadMemory(buffer, size, url, NULL, 0);
1349 fprintf(stderr, "Failed to parse '%s'.\n", url);
1352 *error = g_error_new(g_quark_from_string("subsurface"),
1354 "Failed to parse '%s'",
1359 /* we assume that the last (or only) filename passed as argument is a
1360 * great filename to use as default when saving the dives */
1365 doc = test_xslt_transforms(doc);
1367 traverse(xmlDocGetRootElement(doc));
1373 void parse_xml_init(void)
1380 /* Maybe we'll want a environment variable that can override this.. */
1381 static const char *xslt_path = XSLT ":xslt:.";
1383 static xsltStylesheetPtr try_get_stylesheet(const char *path, int len, const char *name)
1385 xsltStylesheetPtr ret;
1386 int namelen = strlen(name);
1387 char *filename = malloc(len+1+namelen+1);
1392 memcpy(filename, path, len);
1393 filename[len] = G_DIR_SEPARATOR;
1394 memcpy(filename + len + 1, name, namelen+1);
1397 if (!access(filename, R_OK))
1398 ret = xsltParseStylesheetFile(filename);
1404 static xsltStylesheetPtr get_stylesheet(const char *name)
1406 const char *path = xslt_path, *next;
1410 xsltStylesheetPtr ret;
1412 next = strchr(path, ':');
1418 ret = try_get_stylesheet(path, len, name);
1421 } while ((path = next) != NULL);
1426 static struct xslt_files {
1430 { "SUUNTO", "SuuntoSDM.xslt" },
1431 { "JDiveLog", "jdivelog2subsurface.xslt" },
1435 xmlDoc *test_xslt_transforms(xmlDoc *doc)
1437 struct xslt_files *info = xslt_files;
1438 xmlDoc *transformed;
1439 xsltStylesheetPtr xslt = NULL;
1440 xmlNode *root_element = xmlDocGetRootElement(doc);
1442 while ((info->root) && (strcasecmp(root_element->name, info->root) != 0)) {
1447 xmlSubstituteEntitiesDefault(1);
1448 xslt = get_stylesheet(info->file);
1451 transformed = xsltApplyStylesheet(xslt, doc, NULL);
1453 xsltFreeStylesheet(xslt);