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);
43 * Remove a dive from the dive_table array
45 void delete_dive(struct dive *dive)
47 int nr = dive_table.nr, i;
48 struct dive **dives = dive_table.dives;
51 * Stupid. We know the dive table is sorted by date,
52 * we could do a binary lookup. Sue me.
54 for (i = 0; i < nr; i++) {
55 struct dive *d = dives[i];
58 memmove(dives+i, dives+i+1, sizeof(struct dive *)*(nr-i-1));
65 static void start_match(const char *type, const char *name, char *buffer)
68 printf("Matching %s '%s' (%s)\n",
72 static void nonmatch(const char *type, const char *name, char *buffer)
75 printf("Unable to match %s '%s' (%s)\n",
80 typedef void (*matchfn_t)(char *buffer, void *);
82 static int match(const char *pattern, int plen,
83 const char *name, int nlen,
84 matchfn_t fn, char *buf, void *data)
88 if (memcmp(pattern, name + nlen - plen, plen))
95 struct units input_units;
98 * We're going to default to SI units for input. Yes,
99 * technically the SI unit for pressure is Pascal, but
100 * we default to bar (10^5 pascal), which people
101 * actually use. Similarly, C instead of Kelvin.
102 * And kg instead of g.
104 const struct units SI_units = {
108 .temperature = CELSIUS,
112 const struct units IMPERIAL_units = {
116 .temperature = FAHRENHEIT,
121 * Dive info as it is being built up..
123 static struct dive *cur_dive;
124 static struct sample *cur_sample;
128 int type, flags, value;
131 static struct tm cur_tm;
132 static int cur_cylinder_index, cur_ws_index;
134 static enum import_source {
142 time_t utc_mktime(struct tm *tm)
144 static const int mdays[] = {
145 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
147 int year = tm->tm_year;
148 int month = tm->tm_mon;
149 int day = tm->tm_mday;
151 /* First normalize relative to 1900 */
154 else if (year > 1900)
157 /* Normalized to Jan 1, 1970: unix time */
160 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
162 if (month < 0 || month > 11) /* array bounds */
164 if (month < 2 || (year + 2) % 4)
166 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
168 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
169 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
172 static void divedate(char *buffer, void *_when)
175 time_t *when = _when;
178 success = cur_tm.tm_sec | cur_tm.tm_min | cur_tm.tm_hour;
179 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
183 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
188 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
193 *when = utc_mktime(&cur_tm);
198 static void divetime(char *buffer, void *_when)
201 time_t *when = _when;
203 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
208 *when = utc_mktime(&cur_tm);
213 /* Libdivecomputer: "2011-03-20 10:22:38" */
214 static void divedatetime(char *buffer, void *_when)
218 time_t *when = _when;
220 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
221 &y, &m, &d, &hr, &min, &sec) == 6) {
228 *when = utc_mktime(&cur_tm);
242 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
248 /* Integer or floating point? */
249 val = strtol(buffer, &end, 10);
250 if (val < 0 || end == buffer)
253 /* Looks like it might be floating point? */
256 fp = strtod(buffer, &end);
267 static void pressure(char *buffer, void *_press)
270 pressure_t *pressure = _press;
271 union int_or_float val;
273 switch (integer_or_float(buffer, &val)) {
275 /* Just ignore zero values */
278 switch (input_units.pressure) {
283 /* Assume mbar, but if it's really small, it's bar */
289 mbar = val.fp * 68.95;
292 if (mbar > 5 && mbar < 500000) {
293 pressure->mbar = mbar + 0.5;
298 printf("Strange pressure reading %s\n", buffer);
303 static void depth(char *buffer, void *_depth)
305 depth_t *depth = _depth;
306 union int_or_float val;
308 switch (integer_or_float(buffer, &val)) {
310 switch (input_units.length) {
312 depth->mm = val.fp * 1000 + 0.5;
315 depth->mm = val.fp * 304.8 + 0.5;
320 printf("Strange depth reading %s\n", buffer);
325 static void weight(char *buffer, void *_weight)
327 weight_t *weight = _weight;
328 union int_or_float val;
330 switch (integer_or_float(buffer, &val)) {
332 switch (input_units.weight) {
334 weight->grams = val.fp * 1000 + 0.5;
337 weight->grams = val.fp * 453.6 + 0.5;
342 printf("Strange depth reading %s\n", buffer);
346 static void temperature(char *buffer, void *_temperature)
348 temperature_t *temperature = _temperature;
349 union int_or_float val;
351 switch (integer_or_float(buffer, &val)) {
353 /* Ignore zero. It means "none" */
357 switch (input_units.temperature) {
359 temperature->mkelvin = val.fp * 1000;
362 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
365 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
370 printf("Strange temperature reading %s\n", buffer);
375 static void sampletime(char *buffer, void *_time)
379 duration_t *time = _time;
381 i = sscanf(buffer, "%d:%d", &min, &sec);
388 time->seconds = sec + min*60;
391 printf("Strange sample time reading %s\n", buffer);
396 static void duration(char *buffer, void *_time)
398 sampletime(buffer, _time);
401 static void percent(char *buffer, void *_fraction)
403 fraction_t *fraction = _fraction;
404 union int_or_float val;
406 switch (integer_or_float(buffer, &val)) {
409 fraction->permille = val.fp * 10 + 0.5;
413 printf("Strange percentage reading %s\n", buffer);
419 static void gasmix(char *buffer, void *_fraction)
421 /* libdivecomputer does negative percentages. */
424 if (cur_cylinder_index < MAX_CYLINDERS)
425 percent(buffer, _fraction);
428 static void gasmix_nitrogen(char *buffer, void *_gasmix)
430 /* Ignore n2 percentages. There's no value in them. */
433 static void cylindersize(char *buffer, void *_volume)
435 volume_t *volume = _volume;
436 union int_or_float val;
438 switch (integer_or_float(buffer, &val)) {
440 volume->mliter = val.fp * 1000 + 0.5;
444 printf("Strange volume reading %s\n", buffer);
450 static void utf8_string(char *buffer, void *_res)
452 *(char **)_res = buffer;
456 * Uemis water_pressure. In centibar. And when converting to
457 * depth, I'm just going to always use saltwater, because I
458 * think "true depth" is just stupid. From a diving standpoint,
459 * "true depth" is pretty much completely pointless, unless
460 * you're doing some kind of underwater surveying work.
462 * So I give water depths in "pressure depth", always assuming
463 * salt water. So one atmosphere per 10m.
465 static void water_pressure(char *buffer, void *_depth)
467 depth_t *depth = _depth;
468 union int_or_float val;
471 switch (integer_or_float(buffer, &val)) {
476 atm = bar_to_atm(val.fp * 10);
478 * atm to cm. Why not mm? The precision just isn't
481 cm = 100 * atm + 0.5;
483 depth->mm = 10 * (long)cm;
487 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
492 #define MATCH(pattern, fn, dest) \
493 match(pattern, strlen(pattern), name, len, fn, buf, dest)
495 static void get_index(char *buffer, void *_i)
502 static void centibar(char *buffer, void *_pressure)
504 pressure_t *pressure = _pressure;
505 union int_or_float val;
507 switch (integer_or_float(buffer, &val)) {
509 pressure->mbar = val.fp * 10 + 0.5;
512 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
517 static void decicelsius(char *buffer, void *_temp)
519 temperature_t *temp = _temp;
520 union int_or_float val;
522 switch (integer_or_float(buffer, &val)) {
524 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
527 fprintf(stderr, "Strange julian date: %s", buffer);
532 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
534 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
535 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
536 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
537 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
538 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
543 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
544 * for the sample temperatures, that are in Fahrenheit.
547 * Oh, and I think Diving Log *internally* probably kept them
548 * in celsius, because I'm seeing entries like
552 * in there. Which is freezing, aka 0 degC. I bet the "0" is
553 * what Diving Log uses for "no temperature".
555 * So throw away crap like that.
557 static void fahrenheit(char *buffer, void *_temperature)
559 temperature_t *temperature = _temperature;
560 union int_or_float val;
562 switch (integer_or_float(buffer, &val)) {
564 /* Floating point equality is evil, but works for small integers */
567 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
570 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
576 * Did I mention how bat-shit crazy divinglog is? The sample
577 * pressures are in PSI. But the tank working pressure is in
580 * Crazy stuff like this is why subsurface has everything in
581 * these inconvenient typed structures, and you have to say
582 * "pressure->mbar" to get the actual value. Exactly so that
583 * you can never have unit confusion.
585 static void psi(char *buffer, void *_pressure)
587 pressure_t *pressure = _pressure;
588 union int_or_float val;
590 switch (integer_or_float(buffer, &val)) {
592 pressure->mbar = val.fp * 68.95 + 0.5;
595 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
600 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
602 return MATCH(".p.time", sampletime, &sample->time) ||
603 MATCH(".p.depth", depth, &sample->depth) ||
604 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
605 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
609 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
611 return MATCH(".divetime", sampletime, &sample->time) ||
612 MATCH(".depth", depth, &sample->depth) ||
613 MATCH(".temperature", temperature, &sample->temperature) ||
617 static void eventtime(char *buffer, void *_duration)
619 duration_t *duration = _duration;
620 sampletime(buffer, duration);
622 duration->seconds += cur_sample->time.seconds;
625 static void try_to_fill_event(const char *name, char *buf)
627 int len = strlen(name);
629 start_match("event", name, buf);
630 if (MATCH(".event", utf8_string, &cur_event.name))
632 if (MATCH(".name", utf8_string, &cur_event.name))
634 if (MATCH(".time", eventtime, &cur_event.time))
636 if (MATCH(".type", get_index, &cur_event.type))
638 if (MATCH(".flags", get_index, &cur_event.flags))
640 if (MATCH(".value", get_index, &cur_event.value))
642 nonmatch("event", name, buf);
645 /* We're in samples - try to convert the random xml value to something useful */
646 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
648 int len = strlen(name);
650 start_match("sample", name, buf);
651 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
653 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
655 if (MATCH(".sample.cylinderindex", get_index, &sample->cylinderindex))
657 if (MATCH(".sample.depth", depth, &sample->depth))
659 if (MATCH(".sample.temp", temperature, &sample->temperature))
661 if (MATCH(".sample.temperature", temperature, &sample->temperature))
663 if (MATCH(".sample.sampletime", sampletime, &sample->time))
665 if (MATCH(".sample.time", sampletime, &sample->time))
668 switch (import_source) {
670 if (uemis_fill_sample(sample, name, len, buf))
675 if (divinglog_fill_sample(sample, name, len, buf))
680 if (uddf_fill_sample(sample, name, len, buf))
688 nonmatch("sample", name, buf);
691 static const char *country, *city;
693 static void divinglog_place(char *place, void *_location)
695 char **location = _location;
696 char buffer[256], *p;
699 len = snprintf(buffer, sizeof(buffer),
705 country ? country : "");
708 memcpy(p, buffer, len+1);
715 static int divinglog_dive_match(struct dive **divep, const char *name, int len, char *buf)
717 struct dive *dive = *divep;
719 return MATCH(".divedate", divedate, &dive->when) ||
720 MATCH(".entrytime", divetime, &dive->when) ||
721 MATCH(".depth", depth, &dive->maxdepth) ||
722 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
723 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
724 MATCH(".comments", utf8_string, &dive->notes) ||
725 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
726 MATCH(".country.name", utf8_string, &country) ||
727 MATCH(".city.name", utf8_string, &city) ||
728 MATCH(".place.name", divinglog_place, &dive->location) ||
732 static int buffer_value(char *buffer)
734 int val = atoi(buffer);
739 static void uemis_length_unit(char *buffer, void *_unused)
741 input_units.length = buffer_value(buffer) ? FEET : METERS;
744 static void uemis_volume_unit(char *buffer, void *_unused)
746 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
749 static void uemis_pressure_unit(char *buffer, void *_unused)
752 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
756 static void uemis_temperature_unit(char *buffer, void *_unused)
758 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
761 static void uemis_weight_unit(char *buffer, void *_unused)
763 input_units.weight = buffer_value(buffer) ? LBS : KG;
766 static void uemis_time_unit(char *buffer, void *_unused)
770 static void uemis_date_unit(char *buffer, void *_unused)
774 /* Modified julian day, yay! */
775 static void uemis_date_time(char *buffer, void *_when)
777 time_t *when = _when;
778 union int_or_float val;
780 switch (integer_or_float(buffer, &val)) {
782 *when = (val.fp - 40587) * 86400;
785 fprintf(stderr, "Strange julian date: %s", buffer);
791 * Uemis doesn't know time zones. You need to do them as
792 * minutes, not hours.
794 * But that's ok, we don't track timezones yet either. We
795 * just turn everything into "localtime expressed as UTC".
797 static void uemis_time_zone(char *buffer, void *_when)
799 #if 0 /* seems like this is only used to display it correctly
800 * the stored time appears to be UTC */
802 time_t *when = _when;
803 signed char tz = atoi(buffer);
809 static void uemis_ts(char *buffer, void *_when)
812 time_t *when = _when;
814 memset(&tm, 0, sizeof(tm));
815 sscanf(buffer,"%d-%d-%dT%d:%d:%d",
816 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
817 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
820 *when = utc_mktime(&tm);
824 static void uemis_duration(char *buffer, void *_duration)
826 duration_t *duration = _duration;
827 duration->seconds = atof(buffer) * 60 + 0.5;
830 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
831 static int uemis_gas_template;
834 * Christ. Uemis tank data is a total mess.
836 * We're passed a "virtual cylinder" (0 - 6) for the different
837 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
838 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
839 * into the actual cylinder data depending on the gas template,
840 * and ignore the ones that are irrelevant for that template.
842 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
843 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
844 * (which are the three "nitrox_3" tanks), and we turn virtual
845 * tanks 2/3 into actual tanks 0/1.
849 static int uemis_cylinder_index(void *_cylinder)
851 cylinder_t *cylinder = _cylinder;
852 unsigned int index = cylinder - cur_dive->cylinder;
855 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
858 switch(uemis_gas_template) {
859 case 1: /* Dive uses tank 1 */
862 case 0: /* Dive uses tank 0 */
866 case 2: /* Dive uses tanks 2-3 */
871 case 3: /* Dive uses tanks 4-6 */
880 static void uemis_cylindersize(char *buffer, void *_cylinder)
882 int index = uemis_cylinder_index(_cylinder);
884 cylindersize(buffer, &cur_dive->cylinder[index].type.size);
887 static void uemis_percent(char *buffer, void *_cylinder)
889 int index = uemis_cylinder_index(_cylinder);
891 percent(buffer, &cur_dive->cylinder[index].gasmix.o2);
894 static int uemis_dive_match(struct dive **divep, const char *name, int len, char *buf)
896 struct dive *dive = *divep;
898 return MATCH(".units.length", uemis_length_unit, &input_units) ||
899 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
900 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
901 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
902 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
903 MATCH(".units.time", uemis_time_unit, &input_units) ||
904 MATCH(".units.date", uemis_date_unit, &input_units) ||
905 MATCH(".date_time", uemis_date_time, &dive->when) ||
906 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
907 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
908 MATCH(".gas.template", get_index, &uemis_gas_template) ||
909 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
910 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
911 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
912 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
913 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
914 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
915 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
916 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
917 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
918 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
919 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
920 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
921 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
922 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
923 MATCH(".dive.val.float", uemis_duration, &dive->duration) ||
924 MATCH(".dive.val.ts", uemis_ts, &dive->when) ||
925 MATCH(".dive.val.bin", uemis_parse_divelog_binary, divep) ||
930 * Uddf specifies ISO 8601 time format.
932 * There are many variations on that. This handles the useful cases.
934 static void uddf_datetime(char *buffer, void *_when)
938 time_t *when = _when;
939 struct tm tm = { 0 };
942 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
949 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
956 printf("Bad date time %s\n", buffer);
961 if (c != 'T' && c != ' ')
969 *when = utc_mktime(&tm);
973 static int uddf_dive_match(struct dive **divep, const char *name, int len, char *buf)
975 struct dive *dive = *divep;
977 return MATCH(".datetime", uddf_datetime, &dive->when) ||
978 MATCH(".diveduration", duration, &dive->duration) ||
979 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
983 static void gps_location(char *buffer, void *_dive)
986 struct dive *dive = _dive;
987 double latitude, longitude;
989 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
991 dive->latitude = latitude;
992 dive->longitude = longitude;
997 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
998 static void try_to_fill_dive(struct dive **divep, const char *name, char *buf)
1000 int len = strlen(name);
1002 start_match("dive", name, buf);
1004 switch (import_source) {
1006 if (uemis_dive_match(divep, name, len, buf))
1011 if (divinglog_dive_match(divep, name, len, buf))
1016 if (uddf_dive_match(divep, name, len, buf))
1024 struct dive *dive = *divep;
1026 if (MATCH(".number", get_index, &dive->number))
1028 if (MATCH(".date", divedate, &dive->when))
1030 if (MATCH(".time", divetime, &dive->when))
1032 if (MATCH(".datetime", divedatetime, &dive->when))
1034 if (MATCH(".maxdepth", depth, &dive->maxdepth))
1036 if (MATCH(".meandepth", depth, &dive->meandepth))
1038 if (MATCH(".depth.max", depth, &dive->maxdepth))
1040 if (MATCH(".depth.mean", depth, &dive->meandepth))
1042 if (MATCH(".duration", duration, &dive->duration))
1044 if (MATCH(".divetime", duration, &dive->duration))
1046 if (MATCH(".divetimesec", duration, &dive->duration))
1048 if (MATCH(".surfacetime", duration, &dive->surfacetime))
1050 if (MATCH(".airtemp", temperature, &dive->airtemp))
1052 if (MATCH(".watertemp", temperature, &dive->watertemp))
1054 if (MATCH(".temperature.air", temperature, &dive->airtemp))
1056 if (MATCH(".temperature.water", temperature, &dive->watertemp))
1058 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
1060 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1062 if (MATCH(".gps", gps_location, dive))
1064 if (MATCH(".location", utf8_string, &dive->location))
1066 if (MATCH(".notes", utf8_string, &dive->notes))
1068 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1070 if (MATCH(".buddy", utf8_string, &dive->buddy))
1072 if (MATCH(".rating", get_index, &dive->rating))
1074 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cur_cylinder_index].type.size))
1076 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cur_cylinder_index].type.workingpressure))
1078 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cur_cylinder_index].type.description))
1080 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cur_cylinder_index].start))
1082 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cur_cylinder_index].end))
1084 if (MATCH(".weightsystem.description", utf8_string, &dive->weightsystem[cur_ws_index].description))
1086 if (MATCH(".weightsystem.weight", weight, &dive->weightsystem[cur_ws_index].weight))
1088 if (MATCH("weight", weight, &dive->weightsystem[cur_ws_index].weight))
1090 if (MATCH(".o2", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.o2))
1092 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cur_cylinder_index].gasmix))
1094 if (MATCH(".he", gasmix, &dive->cylinder[cur_cylinder_index].gasmix.he))
1097 nonmatch("dive", name, buf);
1101 * File boundaries are dive boundaries. But sometimes there are
1102 * multiple dives per file, so there can be other events too that
1103 * trigger a "new dive" marker and you may get some nesting due
1104 * to that. Just ignore nesting levels.
1106 static void dive_start(void)
1110 cur_dive = alloc_dive();
1111 memset(&cur_tm, 0, sizeof(cur_tm));
1114 static void dive_end(void)
1118 record_dive(cur_dive);
1120 cur_cylinder_index = 0;
1124 static void event_start(void)
1126 memset(&cur_event, 0, sizeof(cur_event));
1127 cur_event.active = 1;
1130 static void event_end(void)
1132 if (cur_event.name && strcmp(cur_event.name, "surface") != 0)
1133 add_event(cur_dive, cur_event.time.seconds,
1134 cur_event.type, cur_event.flags,
1135 cur_event.value, cur_event.name);
1136 cur_event.active = 0;
1139 static void cylinder_start(void)
1143 static void cylinder_end(void)
1145 cur_cylinder_index++;
1148 static void ws_start(void)
1152 static void ws_end(void)
1157 static void sample_start(void)
1159 cur_sample = prepare_sample(&cur_dive);
1162 static void sample_end(void)
1167 finish_sample(cur_dive);
1171 static void entry(const char *name, int size, const char *raw)
1173 char *buf = malloc(size+1);
1177 memcpy(buf, raw, size);
1179 if (cur_event.active) {
1180 try_to_fill_event(name, buf);
1184 try_to_fill_sample(cur_sample, name, buf);
1188 try_to_fill_dive(&cur_dive, name, buf);
1193 static const char *nodename(xmlNode *node, char *buf, int len)
1195 if (!node || !node->name)
1203 const char *name = node->name;
1204 int i = strlen(name);
1206 unsigned char c = name[i];
1207 *--buf = tolower(c);
1211 node = node->parent;
1212 if (!node || !node->name)
1222 static void visit_one_node(xmlNode *node)
1225 const unsigned char *content;
1226 char buffer[MAXNAME];
1229 content = node->content;
1233 /* Trim whitespace at beginning */
1234 while (isspace(*content))
1237 /* Trim whitespace at end */
1238 len = strlen(content);
1239 while (len && isspace(content[len-1]))
1245 /* Don't print out the node name if it is "text" */
1246 if (!strcmp(node->name, "text"))
1247 node = node->parent;
1249 name = nodename(node, buffer, sizeof(buffer));
1251 entry(name, len, content);
1254 static void traverse(xmlNode *root);
1256 static void traverse_properties(xmlNode *node)
1260 for (p = node->properties; p; p = p->next)
1261 traverse(p->children);
1264 static void visit(xmlNode *n)
1267 traverse_properties(n);
1268 traverse(n->children);
1271 static void uemis_importer(void)
1273 import_source = UEMIS;
1274 input_units = SI_units;
1277 static void DivingLog_importer(void)
1279 import_source = DIVINGLOG;
1282 * Diving Log units are really strange.
1284 * Temperatures are in C, except in samples,
1285 * when they are in Fahrenheit. Depths are in
1286 * meters, an dpressure is in PSI in the samples,
1287 * but in bar when it comes to working pressure.
1289 * Crazy f*%^ morons.
1291 input_units = SI_units;
1294 static void uddf_importer(void)
1296 import_source = UDDF;
1297 input_units = SI_units;
1298 input_units.pressure = PASCAL;
1299 input_units.temperature = KELVIN;
1303 * I'm sure this could be done as some fancy DTD rules.
1304 * It's just not worth the headache.
1306 static struct nesting {
1308 void (*start)(void), (*end)(void);
1310 { "dive", dive_start, dive_end },
1311 { "Dive", dive_start, dive_end },
1312 { "sample", sample_start, sample_end },
1313 { "waypoint", sample_start, sample_end },
1314 { "SAMPLE", sample_start, sample_end },
1315 { "reading", sample_start, sample_end },
1316 { "event", event_start, event_end },
1317 { "gasmix", cylinder_start, cylinder_end },
1318 { "cylinder", cylinder_start, cylinder_end },
1319 { "weightsystem", ws_start, ws_end },
1320 { "P", sample_start, sample_end },
1322 /* Import type recognition */
1323 { "Divinglog", DivingLog_importer },
1324 { "pre_dive", uemis_importer },
1325 { "dives", uemis_importer },
1326 { "uddf", uddf_importer },
1331 static void traverse(xmlNode *root)
1335 for (n = root; n; n = n->next) {
1336 struct nesting *rule = nesting;
1339 if (!strcmp(rule->name, n->name))
1342 } while (rule->name);
1352 /* Per-file reset */
1353 static void reset_all(void)
1356 * We reset the units for each file. You'd think it was
1357 * a per-dive property, but I'm not going to trust people
1358 * to do per-dive setup. If the xml does have per-dive
1359 * data within one file, we might have to reset it per
1360 * dive for that format.
1362 input_units = SI_units;
1363 import_source = UNKNOWN;
1366 void parse_xml_buffer(const char *url, const char *buffer, int size, GError **error)
1370 doc = xmlReadMemory(buffer, size, url, NULL, 0);
1372 fprintf(stderr, "Failed to parse '%s'.\n", url);
1375 *error = g_error_new(g_quark_from_string("subsurface"),
1377 "Failed to parse '%s'",
1382 /* we assume that the last (or only) filename passed as argument is a
1383 * great filename to use as default when saving the dives */
1388 doc = test_xslt_transforms(doc);
1390 traverse(xmlDocGetRootElement(doc));
1396 void parse_xml_init(void)
1403 /* Maybe we'll want a environment variable that can override this.. */
1404 static const char *xslt_path = XSLT ":xslt:.";
1406 static xsltStylesheetPtr try_get_stylesheet(const char *path, int len, const char *name)
1408 xsltStylesheetPtr ret;
1409 int namelen = strlen(name);
1410 char *filename = malloc(len+1+namelen+1);
1415 memcpy(filename, path, len);
1416 filename[len] = G_DIR_SEPARATOR;
1417 memcpy(filename + len + 1, name, namelen+1);
1420 if (!access(filename, R_OK))
1421 ret = xsltParseStylesheetFile(filename);
1427 static xsltStylesheetPtr get_stylesheet(const char *name)
1429 const char *path = xslt_path, *next;
1433 xsltStylesheetPtr ret;
1435 next = strchr(path, ':');
1441 ret = try_get_stylesheet(path, len, name);
1444 } while ((path = next) != NULL);
1449 static struct xslt_files {
1453 { "SUUNTO", "SuuntoSDM.xslt" },
1454 { "JDiveLog", "jdivelog2subsurface.xslt" },
1458 xmlDoc *test_xslt_transforms(xmlDoc *doc)
1460 struct xslt_files *info = xslt_files;
1461 xmlDoc *transformed;
1462 xsltStylesheetPtr xslt = NULL;
1463 xmlNode *root_element = xmlDocGetRootElement(doc);
1465 while ((info->root) && (strcasecmp(root_element->name, info->root) != 0)) {
1470 xmlSubstituteEntitiesDefault(1);
1471 xslt = get_stylesheet(info->file);
1474 transformed = xsltApplyStylesheet(xslt, doc, NULL);
1476 xsltFreeStylesheet(xslt);