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) ||
653 static void eventtime(char *buffer, void *_duration)
655 duration_t *duration = _duration;
656 sampletime(buffer, duration);
658 duration->seconds += cur_sample->time.seconds;
661 static void try_to_fill_event(const char *name, char *buf)
663 int len = strlen(name);
665 start_match("event", name, buf);
666 if (MATCH(".event", utf8_string, &cur_event.name))
668 if (MATCH(".name", utf8_string, &cur_event.name))
670 if (MATCH(".time", eventtime, &cur_event.time))
672 if (MATCH(".type", get_index, &cur_event.type))
674 if (MATCH(".flags", get_index, &cur_event.flags))
676 if (MATCH(".value", get_index, &cur_event.value))
678 nonmatch("event", name, buf);
681 /* We're in samples - try to convert the random xml value to something useful */
682 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
684 int len = strlen(name);
686 start_match("sample", name, buf);
687 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
689 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
691 if (MATCH(".sample.cylinderindex", get_index, &sample->cylinderindex))
693 if (MATCH(".sample.depth", depth, &sample->depth))
695 if (MATCH(".sample.temp", temperature, &sample->temperature))
697 if (MATCH(".sample.temperature", temperature, &sample->temperature))
699 if (MATCH(".sample.sampletime", sampletime, &sample->time))
701 if (MATCH(".sample.time", sampletime, &sample->time))
704 switch (import_source) {
706 if (uemis_fill_sample(sample, name, len, buf))
711 if (divinglog_fill_sample(sample, name, len, buf))
716 if (uddf_fill_sample(sample, name, len, buf))
724 nonmatch("sample", name, buf);
727 static const char *country, *city;
729 static void divinglog_place(char *place, void *_location)
731 char **location = _location;
732 char buffer[256], *p;
735 len = snprintf(buffer, sizeof(buffer),
741 country ? country : "");
744 memcpy(p, buffer, len+1);
751 static int divinglog_dive_match(struct dive **divep, const char *name, int len, char *buf)
753 struct dive *dive = *divep;
755 return MATCH(".divedate", divedate, &dive->when) ||
756 MATCH(".entrytime", divetime, &dive->when) ||
757 MATCH(".depth", depth, &dive->maxdepth) ||
758 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
759 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
760 MATCH(".comments", utf8_string, &dive->notes) ||
761 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
762 MATCH(".country.name", utf8_string, &country) ||
763 MATCH(".city.name", utf8_string, &city) ||
764 MATCH(".place.name", divinglog_place, &dive->location) ||
768 static int buffer_value(char *buffer)
770 int val = atoi(buffer);
775 static void uemis_length_unit(char *buffer, void *_unused)
777 input_units.length = buffer_value(buffer) ? FEET : METERS;
780 static void uemis_volume_unit(char *buffer, void *_unused)
782 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
785 static void uemis_pressure_unit(char *buffer, void *_unused)
788 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
792 static void uemis_temperature_unit(char *buffer, void *_unused)
794 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
797 static void uemis_weight_unit(char *buffer, void *_unused)
799 input_units.weight = buffer_value(buffer) ? LBS : KG;
802 static void uemis_time_unit(char *buffer, void *_unused)
806 static void uemis_date_unit(char *buffer, void *_unused)
810 /* Modified julian day, yay! */
811 static void uemis_date_time(char *buffer, void *_when)
813 time_t *when = _when;
814 union int_or_float val;
816 switch (integer_or_float(buffer, &val)) {
818 *when = (val.fp - 40587) * 86400;
821 fprintf(stderr, "Strange julian date: %s", buffer);
827 * Uemis doesn't know time zones. You need to do them as
828 * minutes, not hours.
830 * But that's ok, we don't track timezones yet either. We
831 * just turn everything into "localtime expressed as UTC".
833 static void uemis_time_zone(char *buffer, void *_when)
835 #if 0 /* seems like this is only used to display it correctly
836 * the stored time appears to be UTC */
838 time_t *when = _when;
839 signed char tz = atoi(buffer);
845 static void uemis_ts(char *buffer, void *_when)
848 time_t *when = _when;
850 memset(&tm, 0, sizeof(tm));
851 sscanf(buffer,"%d-%d-%dT%d:%d:%d",
852 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
853 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
856 *when = utc_mktime(&tm);
860 static void uemis_duration(char *buffer, void *_duration)
862 duration_t *duration = _duration;
863 duration->seconds = atof(buffer) * 60 + 0.5;
866 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
867 static int uemis_gas_template;
870 * Christ. Uemis tank data is a total mess.
872 * We're passed a "virtual cylinder" (0 - 6) for the different
873 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
874 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
875 * into the actual cylinder data depending on the gas template,
876 * and ignore the ones that are irrelevant for that template.
878 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
879 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
880 * (which are the three "nitrox_3" tanks), and we turn virtual
881 * tanks 2/3 into actual tanks 0/1.
885 static int uemis_cylinder_index(void *_cylinder)
887 cylinder_t *cylinder = _cylinder;
888 unsigned int index = cylinder - cur_dive->cylinder;
891 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
894 switch(uemis_gas_template) {
895 case 1: /* Dive uses tank 1 */
898 case 0: /* Dive uses tank 0 */
902 case 2: /* Dive uses tanks 2-3 */
907 case 3: /* Dive uses tanks 4-6 */
916 static void uemis_cylindersize(char *buffer, void *_cylinder)
918 int index = uemis_cylinder_index(_cylinder);
920 cylindersize(buffer, &cur_dive->cylinder[index].type.size);
923 static void uemis_percent(char *buffer, void *_cylinder)
925 int index = uemis_cylinder_index(_cylinder);
927 percent(buffer, &cur_dive->cylinder[index].gasmix.o2);
930 static int uemis_dive_match(struct dive **divep, const char *name, int len, char *buf)
932 struct dive *dive = *divep;
934 return MATCH(".units.length", uemis_length_unit, &input_units) ||
935 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
936 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
937 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
938 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
939 MATCH(".units.time", uemis_time_unit, &input_units) ||
940 MATCH(".units.date", uemis_date_unit, &input_units) ||
941 MATCH(".date_time", uemis_date_time, &dive->when) ||
942 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
943 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
944 MATCH(".gas.template", get_index, &uemis_gas_template) ||
945 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
946 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
947 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
948 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
949 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
950 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
951 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
952 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
953 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
954 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
955 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
956 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
957 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
958 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
959 MATCH(".dive.val.float", uemis_duration, &dive->duration) ||
960 MATCH(".dive.val.ts", uemis_ts, &dive->when) ||
961 MATCH(".dive.val.bin", uemis_parse_divelog_binary, divep) ||
966 * Uddf specifies ISO 8601 time format.
968 * There are many variations on that. This handles the useful cases.
970 static void uddf_datetime(char *buffer, void *_when)
974 time_t *when = _when;
975 struct tm tm = { 0 };
978 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
985 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
992 printf("Bad date time %s\n", buffer);
997 if (c != 'T' && c != ' ')
1005 *when = utc_mktime(&tm);
1009 static int uddf_dive_match(struct dive **divep, const char *name, int len, char *buf)
1011 struct dive *dive = *divep;
1013 return MATCH(".datetime", uddf_datetime, &dive->when) ||
1014 MATCH(".diveduration", duration, &dive->duration) ||
1015 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
1019 static void gps_location(char *buffer, void *_dive)
1022 struct dive *dive = _dive;
1023 double latitude, longitude;
1025 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
1027 dive->latitude = latitude;
1028 dive->longitude = longitude;
1033 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
1034 static void try_to_fill_dive(struct dive **divep, const char *name, char *buf)
1036 int len = strlen(name);
1038 start_match("dive", name, buf);
1040 switch (import_source) {
1042 if (uemis_dive_match(divep, name, len, buf))
1047 if (divinglog_dive_match(divep, name, len, buf))
1052 if (uddf_dive_match(divep, name, len, buf))
1060 struct dive *dive = *divep;
1062 if (MATCH(".number", get_index, &dive->number))
1064 if (MATCH(".date", divedate, &dive->when))
1066 if (MATCH(".time", divetime, &dive->when))
1068 if (MATCH(".datetime", divedatetime, &dive->when))
1070 if (MATCH(".maxdepth", depth, &dive->maxdepth))
1072 if (MATCH(".meandepth", depth, &dive->meandepth))
1074 if (MATCH(".depth.max", depth, &dive->maxdepth))
1076 if (MATCH(".depth.mean", depth, &dive->meandepth))
1078 if (MATCH(".duration", duration, &dive->duration))
1080 if (MATCH(".divetime", duration, &dive->duration))
1082 if (MATCH(".divetimesec", duration, &dive->duration))
1084 if (MATCH(".surfacetime", duration, &dive->surfacetime))
1086 if (MATCH(".airtemp", temperature, &dive->airtemp))
1088 if (MATCH(".watertemp", temperature, &dive->watertemp))
1090 if (MATCH(".temperature.air", temperature, &dive->airtemp))
1092 if (MATCH(".temperature.water", temperature, &dive->watertemp))
1094 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
1096 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1098 if (MATCH(".gps", gps_location, dive))
1100 if (MATCH(".location", utf8_string, &dive->location))
1102 if (MATCH(".notes", utf8_string, &dive->notes))
1104 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1106 if (MATCH(".buddy", utf8_string, &dive->buddy))
1108 if (MATCH(".rating", get_index, &dive->rating))
1110 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cur_cylinder_index].type.size))
1112 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cur_cylinder_index].type.workingpressure))
1114 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cur_cylinder_index].type.description))
1116 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cur_cylinder_index].start))
1118 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cur_cylinder_index].end))
1120 if (MATCH(".weightsystem.description", utf8_string, &dive->weightsystem[cur_ws_index].description))
1122 if (MATCH(".weightsystem.weight", weight, &dive->weightsystem[cur_ws_index].weight))
1124 if (MATCH("weight", weight, &dive->weightsystem[cur_ws_index].weight))
1126 if (MATCH(".o2", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.o2))
1128 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cur_cylinder_index].gasmix))
1130 if (MATCH(".he", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.he))
1133 nonmatch("dive", name, buf);
1137 * File boundaries are dive boundaries. But sometimes there are
1138 * multiple dives per file, so there can be other events too that
1139 * trigger a "new dive" marker and you may get some nesting due
1140 * to that. Just ignore nesting levels.
1142 static void dive_start(void)
1146 cur_dive = alloc_dive();
1147 memset(&cur_tm, 0, sizeof(cur_tm));
1150 static void dive_end(void)
1154 record_dive(cur_dive);
1156 cur_cylinder_index = 0;
1160 static void event_start(void)
1162 memset(&cur_event, 0, sizeof(cur_event));
1163 cur_event.active = 1;
1166 static void event_end(void)
1168 if (cur_event.name && strcmp(cur_event.name, "surface") != 0)
1169 add_event(cur_dive, cur_event.time.seconds,
1170 cur_event.type, cur_event.flags,
1171 cur_event.value, cur_event.name);
1172 cur_event.active = 0;
1175 static void cylinder_start(void)
1179 static void cylinder_end(void)
1181 cur_cylinder_index++;
1184 static void ws_start(void)
1188 static void ws_end(void)
1193 static void sample_start(void)
1195 cur_sample = prepare_sample(&cur_dive);
1198 static void sample_end(void)
1203 finish_sample(cur_dive);
1207 static void entry(const char *name, int size, const char *raw)
1209 char *buf = malloc(size+1);
1213 memcpy(buf, raw, size);
1215 if (cur_event.active) {
1216 try_to_fill_event(name, buf);
1220 try_to_fill_sample(cur_sample, name, buf);
1224 try_to_fill_dive(&cur_dive, name, buf);
1229 static const char *nodename(xmlNode *node, char *buf, int len)
1231 if (!node || !node->name)
1239 const char *name = node->name;
1240 int i = strlen(name);
1242 unsigned char c = name[i];
1243 *--buf = tolower(c);
1247 node = node->parent;
1248 if (!node || !node->name)
1258 static void visit_one_node(xmlNode *node)
1261 const unsigned char *content;
1262 char buffer[MAXNAME];
1265 content = node->content;
1269 /* Trim whitespace at beginning */
1270 while (isspace(*content))
1273 /* Trim whitespace at end */
1274 len = strlen(content);
1275 while (len && isspace(content[len-1]))
1281 /* Don't print out the node name if it is "text" */
1282 if (!strcmp(node->name, "text"))
1283 node = node->parent;
1285 name = nodename(node, buffer, sizeof(buffer));
1287 entry(name, len, content);
1290 static void traverse(xmlNode *root);
1292 static void traverse_properties(xmlNode *node)
1296 for (p = node->properties; p; p = p->next)
1297 traverse(p->children);
1300 static void visit(xmlNode *n)
1303 traverse_properties(n);
1304 traverse(n->children);
1307 static void uemis_importer(void)
1309 import_source = UEMIS;
1310 input_units = SI_units;
1313 static void DivingLog_importer(void)
1315 import_source = DIVINGLOG;
1318 * Diving Log units are really strange.
1320 * Temperatures are in C, except in samples,
1321 * when they are in Fahrenheit. Depths are in
1322 * meters, an dpressure is in PSI in the samples,
1323 * but in bar when it comes to working pressure.
1325 * Crazy f*%^ morons.
1327 input_units = SI_units;
1330 static void uddf_importer(void)
1332 import_source = UDDF;
1333 input_units = SI_units;
1334 input_units.pressure = PASCAL;
1335 input_units.temperature = KELVIN;
1339 * I'm sure this could be done as some fancy DTD rules.
1340 * It's just not worth the headache.
1342 static struct nesting {
1344 void (*start)(void), (*end)(void);
1346 { "dive", dive_start, dive_end },
1347 { "Dive", dive_start, dive_end },
1348 { "sample", sample_start, sample_end },
1349 { "waypoint", sample_start, sample_end },
1350 { "SAMPLE", sample_start, sample_end },
1351 { "reading", sample_start, sample_end },
1352 { "event", event_start, event_end },
1353 { "gasmix", cylinder_start, cylinder_end },
1354 { "cylinder", cylinder_start, cylinder_end },
1355 { "weightsystem", ws_start, ws_end },
1356 { "P", sample_start, sample_end },
1358 /* Import type recognition */
1359 { "Divinglog", DivingLog_importer },
1360 { "pre_dive", uemis_importer },
1361 { "dives", uemis_importer },
1362 { "uddf", uddf_importer },
1367 static void traverse(xmlNode *root)
1371 for (n = root; n; n = n->next) {
1372 struct nesting *rule = nesting;
1375 if (!strcmp(rule->name, n->name))
1378 } while (rule->name);
1388 /* Per-file reset */
1389 static void reset_all(void)
1392 * We reset the units for each file. You'd think it was
1393 * a per-dive property, but I'm not going to trust people
1394 * to do per-dive setup. If the xml does have per-dive
1395 * data within one file, we might have to reset it per
1396 * dive for that format.
1398 input_units = SI_units;
1399 import_source = UNKNOWN;
1402 void parse_xml_buffer(const char *url, const char *buffer, int size, GError **error)
1406 doc = xmlReadMemory(buffer, size, url, NULL, 0);
1408 fprintf(stderr, "Failed to parse '%s'.\n", url);
1411 *error = g_error_new(g_quark_from_string("subsurface"),
1413 "Failed to parse '%s'",
1418 /* we assume that the last (or only) filename passed as argument is a
1419 * great filename to use as default when saving the dives */
1424 doc = test_xslt_transforms(doc);
1426 traverse(xmlDocGetRootElement(doc));
1432 void parse_xml_init(void)
1439 /* Maybe we'll want a environment variable that can override this.. */
1440 static const char *xslt_path = XSLT ":xslt:.";
1442 static xsltStylesheetPtr try_get_stylesheet(const char *path, int len, const char *name)
1444 xsltStylesheetPtr ret;
1445 int namelen = strlen(name);
1446 char *filename = malloc(len+1+namelen+1);
1451 memcpy(filename, path, len);
1452 filename[len] = G_DIR_SEPARATOR;
1453 memcpy(filename + len + 1, name, namelen+1);
1456 if (!access(filename, R_OK))
1457 ret = xsltParseStylesheetFile(filename);
1463 static xsltStylesheetPtr get_stylesheet(const char *name)
1465 const char *path = xslt_path, *next;
1469 xsltStylesheetPtr ret;
1471 next = strchr(path, ':');
1477 ret = try_get_stylesheet(path, len, name);
1480 } while ((path = next) != NULL);
1485 static struct xslt_files {
1489 { "SUUNTO", "SuuntoSDM.xslt" },
1490 { "JDiveLog", "jdivelog2subsurface.xslt" },
1494 xmlDoc *test_xslt_transforms(xmlDoc *doc)
1496 struct xslt_files *info = xslt_files;
1497 xmlDoc *transformed;
1498 xsltStylesheetPtr xslt = NULL;
1499 xmlNode *root_element = xmlDocGetRootElement(doc);
1501 while ((info->root) && (strcasecmp(root_element->name, info->root) != 0)) {
1506 xmlSubstituteEntitiesDefault(1);
1507 xslt = get_stylesheet(info->file);
1510 transformed = xsltApplyStylesheet(xslt, doc, NULL);
1512 xsltFreeStylesheet(xslt);