8 #include <libxml/parser.h>
9 #include <libxml/tree.h>
16 struct dive_table dive_table;
19 * Add a dive into the dive_table array
21 void record_dive(struct dive *dive)
23 int nr = dive_table.nr, allocated = dive_table.allocated;
24 struct dive **dives = dive_table.dives;
26 if (nr >= allocated) {
27 allocated = (nr + 32) * 3 / 2;
28 dives = realloc(dives, allocated * sizeof(struct dive *));
31 dive_table.dives = dives;
32 dive_table.allocated = allocated;
34 dives[nr] = fixup_dive(dive);
38 static void start_match(const char *type, const char *name, char *buffer)
41 printf("Matching %s '%s' (%s)\n",
45 static void nonmatch(const char *type, const char *name, char *buffer)
48 printf("Unable to match %s '%s' (%s)\n",
53 typedef void (*matchfn_t)(char *buffer, void *);
55 static int match(const char *pattern, int plen,
56 const char *name, int nlen,
57 matchfn_t fn, char *buf, void *data)
61 if (memcmp(pattern, name + nlen - plen, plen))
68 struct units input_units;
71 * We're going to default to SI units for input. Yes,
72 * technically the SI unit for pressure is Pascal, but
73 * we default to bar (10^5 pascal), which people
74 * actually use. Similarly, C instead of Kelvin.
76 const struct units SI_units = {
80 .temperature = CELSIUS,
84 const struct units IMPERIAL_units = {
88 .temperature = FAHRENHEIT,
93 * Dive info as it is being built up..
95 static struct dive *dive;
96 static struct sample *sample;
100 int type, flags, value;
104 static int cylinder_index;
106 static enum import_source {
115 time_t utc_mktime(struct tm *tm)
117 static const int mdays[] = {
118 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
120 int year = tm->tm_year;
121 int month = tm->tm_mon;
122 int day = tm->tm_mday;
124 /* First normalize relative to 1900 */
127 else if (year > 1900)
130 /* Normalized to Jan 1, 1970: unix time */
133 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
135 if (month < 0 || month > 11) /* array bounds */
137 if (month < 2 || (year + 2) % 4)
139 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
141 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
142 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
145 static void divedate(char *buffer, void *_when)
148 time_t *when = _when;
151 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
152 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
156 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
161 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
166 *when = utc_mktime(&tm);
171 static void divetime(char *buffer, void *_when)
174 time_t *when = _when;
176 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
181 *when = utc_mktime(&tm);
186 /* Libdivecomputer: "2011-03-20 10:22:38" */
187 static void divedatetime(char *buffer, void *_when)
191 time_t *when = _when;
193 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
194 &y, &m, &d, &hr, &min, &sec) == 6) {
201 *when = utc_mktime(&tm);
215 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
221 /* Integer or floating point? */
222 val = strtol(buffer, &end, 10);
223 if (val < 0 || end == buffer)
226 /* Looks like it might be floating point? */
229 fp = strtod(buffer, &end);
240 static void pressure(char *buffer, void *_press)
243 pressure_t *pressure = _press;
244 union int_or_float val;
246 switch (integer_or_float(buffer, &val)) {
248 /* Just ignore zero values */
251 switch (input_units.pressure) {
256 /* Assume mbar, but if it's really small, it's bar */
262 mbar = val.fp * 68.95;
265 if (mbar > 5 && mbar < 500000) {
266 pressure->mbar = mbar + 0.5;
271 printf("Strange pressure reading %s\n", buffer);
276 static void depth(char *buffer, void *_depth)
278 depth_t *depth = _depth;
279 union int_or_float val;
281 switch (integer_or_float(buffer, &val)) {
283 switch (input_units.length) {
285 depth->mm = val.fp * 1000 + 0.5;
288 depth->mm = val.fp * 304.8 + 0.5;
293 printf("Strange depth reading %s\n", buffer);
298 static void temperature(char *buffer, void *_temperature)
300 temperature_t *temperature = _temperature;
301 union int_or_float val;
303 switch (integer_or_float(buffer, &val)) {
305 /* Ignore zero. It means "none" */
309 switch (input_units.temperature) {
311 temperature->mkelvin = val.fp * 1000;
314 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
317 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
322 printf("Strange temperature reading %s\n", buffer);
327 static void sampletime(char *buffer, void *_time)
331 duration_t *time = _time;
333 i = sscanf(buffer, "%d:%d", &min, &sec);
340 time->seconds = sec + min*60;
343 printf("Strange sample time reading %s\n", buffer);
348 static void duration(char *buffer, void *_time)
350 sampletime(buffer, _time);
353 static void percent(char *buffer, void *_fraction)
355 fraction_t *fraction = _fraction;
356 union int_or_float val;
358 switch (integer_or_float(buffer, &val)) {
361 fraction->permille = val.fp * 10 + 0.5;
365 printf("Strange percentage reading %s\n", buffer);
371 static void gasmix(char *buffer, void *_fraction)
373 /* libdivecomputer does negative percentages. */
376 if (cylinder_index < MAX_CYLINDERS)
377 percent(buffer, _fraction);
380 static void gasmix_nitrogen(char *buffer, void *_gasmix)
382 /* Ignore n2 percentages. There's no value in them. */
385 static void cylindersize(char *buffer, void *_volume)
387 volume_t *volume = _volume;
388 union int_or_float val;
390 switch (integer_or_float(buffer, &val)) {
392 volume->mliter = val.fp * 1000 + 0.5;
396 printf("Strange volume reading %s\n", buffer);
402 static void utf8_string(char *buffer, void *_res)
404 *(char **)_res = buffer;
408 * Uemis water_pressure. In centibar. And when converting to
409 * depth, I'm just going to always use saltwater, because I
410 * think "true depth" is just stupid. From a diving standpoint,
411 * "true depth" is pretty much completely pointless, unless
412 * you're doing some kind of underwater surveying work.
414 * So I give water depths in "pressure depth", always assuming
415 * salt water. So one atmosphere per 10m.
417 static void water_pressure(char *buffer, void *_depth)
419 depth_t *depth = _depth;
420 union int_or_float val;
423 switch (integer_or_float(buffer, &val)) {
428 atm = (val.fp / 100) / 1.01325;
430 * atm to cm. Why not mm? The precision just isn't
433 cm = 100 * atm + 0.5;
435 depth->mm = 10 * (long)cm;
439 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
444 #define MATCH(pattern, fn, dest) \
445 match(pattern, strlen(pattern), name, len, fn, buf, dest)
447 static void get_index(char *buffer, void *_i)
454 static void centibar(char *buffer, void *_pressure)
456 pressure_t *pressure = _pressure;
457 union int_or_float val;
459 switch (integer_or_float(buffer, &val)) {
461 pressure->mbar = val.fp * 10 + 0.5;
464 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
469 static void decicelsius(char *buffer, void *_temp)
471 temperature_t *temp = _temp;
472 union int_or_float val;
474 switch (integer_or_float(buffer, &val)) {
476 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
479 fprintf(stderr, "Strange julian date: %s", buffer);
484 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
486 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
487 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
488 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
489 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
490 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
495 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
496 * for the sample temperatures, that are in Fahrenheit.
499 * Oh, and I think Diving Log *internally* probably kept them
500 * in celsius, because I'm seeing entries like
504 * in there. Which is freezing, aka 0 degC. I bet the "0" is
505 * what Diving Log uses for "no temperature".
507 * So throw away crap like that.
509 static void fahrenheit(char *buffer, void *_temperature)
511 temperature_t *temperature = _temperature;
512 union int_or_float val;
514 switch (integer_or_float(buffer, &val)) {
516 /* Floating point equality is evil, but works for small integers */
519 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
522 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
528 * Did I mention how bat-shit crazy divinglog is? The sample
529 * pressures are in PSI. But the tank working pressure is in
532 * Crazy stuff like this is why subsurface has everything in
533 * these inconvenient typed structures, and you have to say
534 * "pressure->mbar" to get the actual value. Exactly so that
535 * you can never have unit confusion.
537 static void psi(char *buffer, void *_pressure)
539 pressure_t *pressure = _pressure;
540 union int_or_float val;
542 switch (integer_or_float(buffer, &val)) {
544 pressure->mbar = val.fp * 68.95 + 0.5;
547 fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
552 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
554 return MATCH(".p.time", sampletime, &sample->time) ||
555 MATCH(".p.depth", depth, &sample->depth) ||
556 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
557 MATCH(".p.press1", psi, &sample->cylinderpressure) ||
561 static int uddf_fill_sample(struct sample *sample, const char *name, int len, char *buf)
563 return MATCH(".divetime", sampletime, &sample->time) ||
564 MATCH(".depth", depth, &sample->depth) ||
565 MATCH(".temperature", temperature, &sample->temperature) ||
569 static void eventtime(char *buffer, void *_duration)
571 duration_t *duration = _duration;
572 sampletime(buffer, duration);
574 duration->seconds += sample->time.seconds;
577 static void try_to_fill_event(const char *name, char *buf)
579 int len = strlen(name);
581 start_match("event", name, buf);
582 if (MATCH(".event", utf8_string, &event.name))
584 if (MATCH(".name", utf8_string, &event.name))
586 if (MATCH(".time", eventtime, &event.time))
588 if (MATCH(".type", get_index, &event.type))
590 if (MATCH(".flags", get_index, &event.flags))
592 if (MATCH(".value", get_index, &event.value))
594 nonmatch("event", name, buf);
597 /* We're in samples - try to convert the random xml value to something useful */
598 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
600 int len = strlen(name);
602 start_match("sample", name, buf);
603 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
605 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
607 if (MATCH(".sample.cylinderindex", get_index, &sample->cylinderindex))
609 if (MATCH(".sample.depth", depth, &sample->depth))
611 if (MATCH(".sample.temp", temperature, &sample->temperature))
613 if (MATCH(".sample.temperature", temperature, &sample->temperature))
615 if (MATCH(".sample.sampletime", sampletime, &sample->time))
617 if (MATCH(".sample.time", sampletime, &sample->time))
620 switch (import_source) {
622 if (uemis_fill_sample(sample, name, len, buf))
627 if (divinglog_fill_sample(sample, name, len, buf))
632 if (uddf_fill_sample(sample, name, len, buf))
640 nonmatch("sample", name, buf);
644 * Crazy suunto xml. Look at how those o2/he things match up.
646 static int suunto_dive_match(struct dive **divep, const char *name, int len, char *buf)
648 struct dive *dive = *divep;
650 return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
651 MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
652 MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
653 MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
654 MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
655 MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
656 MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
657 MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
658 MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
659 MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
663 static const char *country, *city;
665 static void divinglog_place(char *place, void *_location)
667 char **location = _location;
668 char buffer[256], *p;
671 len = snprintf(buffer, sizeof(buffer),
677 country ? country : "");
680 memcpy(p, buffer, len+1);
687 static int divinglog_dive_match(struct dive **divep, const char *name, int len, char *buf)
689 struct dive *dive = *divep;
691 return MATCH(".divedate", divedate, &dive->when) ||
692 MATCH(".entrytime", divetime, &dive->when) ||
693 MATCH(".depth", depth, &dive->maxdepth) ||
694 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
695 MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
696 MATCH(".comments", utf8_string, &dive->notes) ||
697 MATCH(".buddy.names", utf8_string, &dive->buddy) ||
698 MATCH(".country.name", utf8_string, &country) ||
699 MATCH(".city.name", utf8_string, &city) ||
700 MATCH(".place.name", divinglog_place, &dive->location) ||
704 static int buffer_value(char *buffer)
706 int val = atoi(buffer);
711 static void uemis_length_unit(char *buffer, void *_unused)
713 input_units.length = buffer_value(buffer) ? FEET : METERS;
716 static void uemis_volume_unit(char *buffer, void *_unused)
718 input_units.volume = buffer_value(buffer) ? CUFT : LITER;
721 static void uemis_pressure_unit(char *buffer, void *_unused)
724 input_units.pressure = buffer_value(buffer) ? PSI : BAR;
728 static void uemis_temperature_unit(char *buffer, void *_unused)
730 input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
733 static void uemis_weight_unit(char *buffer, void *_unused)
735 input_units.weight = buffer_value(buffer) ? LBS : KG;
738 static void uemis_time_unit(char *buffer, void *_unused)
742 static void uemis_date_unit(char *buffer, void *_unused)
746 /* Modified julian day, yay! */
747 static void uemis_date_time(char *buffer, void *_when)
749 time_t *when = _when;
750 union int_or_float val;
752 switch (integer_or_float(buffer, &val)) {
754 *when = (val.fp - 40587) * 86400;
757 fprintf(stderr, "Strange julian date: %s", buffer);
763 * Uemis doesn't know time zones. You need to do them as
764 * minutes, not hours.
766 * But that's ok, we don't track timezones yet either. We
767 * just turn everything into "localtime expressed as UTC".
769 static void uemis_time_zone(char *buffer, void *_when)
771 #if 0 /* seems like this is only used to display it correctly
772 * the stored time appears to be UTC */
774 time_t *when = _when;
775 signed char tz = atoi(buffer);
781 static void uemis_ts(char *buffer, void *_when)
784 time_t *when = _when;
786 strptime(buffer, "%Y-%m-%dT%H:%M:%S", &tm);
787 *when = utc_mktime(&tm);
790 static void uemis_duration(char *buffer, void *_duration)
792 duration_t *duration = _duration;
793 duration->seconds = atof(buffer) * 60 + 0.5;
796 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
797 static int uemis_gas_template;
800 * Christ. Uemis tank data is a total mess.
802 * We're passed a "virtual cylinder" (0 - 6) for the different
803 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
804 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
805 * into the actual cylinder data depending on the gas template,
806 * and ignore the ones that are irrelevant for that template.
808 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
809 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
810 * (which are the three "nitrox_3" tanks), and we turn virtual
811 * tanks 2/3 into actual tanks 0/1.
815 static int uemis_cylinder_index(void *_cylinder)
817 cylinder_t *cylinder = _cylinder;
818 unsigned int index = cylinder - dive->cylinder;
821 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
824 switch(uemis_gas_template) {
825 case 1: /* Dive uses tank 1 */
828 case 0: /* Dive uses tank 0 */
832 case 2: /* Dive uses tanks 2-3 */
837 case 3: /* Dive uses tanks 4-6 */
846 static void uemis_cylindersize(char *buffer, void *_cylinder)
848 int index = uemis_cylinder_index(_cylinder);
850 cylindersize(buffer, &dive->cylinder[index].type.size);
853 static void uemis_percent(char *buffer, void *_cylinder)
855 int index = uemis_cylinder_index(_cylinder);
857 percent(buffer, &dive->cylinder[index].gasmix.o2);
860 static int uemis_dive_match(struct dive **divep, const char *name, int len, char *buf)
862 struct dive *dive = *divep;
864 return MATCH(".units.length", uemis_length_unit, &input_units) ||
865 MATCH(".units.volume", uemis_volume_unit, &input_units) ||
866 MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
867 MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
868 MATCH(".units.weight", uemis_weight_unit, &input_units) ||
869 MATCH(".units.time", uemis_time_unit, &input_units) ||
870 MATCH(".units.date", uemis_date_unit, &input_units) ||
871 MATCH(".date_time", uemis_date_time, &dive->when) ||
872 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
873 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
874 MATCH(".gas.template", get_index, &uemis_gas_template) ||
875 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
876 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
877 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
878 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
879 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
880 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
881 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
882 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
883 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
884 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
885 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
886 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
887 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
888 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
889 MATCH(".dive.val.float", uemis_duration, &dive->duration) ||
890 MATCH(".dive.val.ts", uemis_ts, &dive->when) ||
891 MATCH(".dive.val.bin", uemis_parse_divelog_binary, divep) ||
896 * Uddf specifies ISO 8601 time format.
898 * There are many variations on that. This handles the useful cases.
900 static void uddf_datetime(char *buffer, void *_when)
904 time_t *when = _when;
905 struct tm tm = { 0 };
908 i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
915 i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
922 printf("Bad date time %s\n", buffer);
927 if (c != 'T' && c != ' ')
935 *when = utc_mktime(&tm);
939 static int uddf_dive_match(struct dive **divep, const char *name, int len, char *buf)
941 struct dive *dive = *divep;
943 return MATCH(".datetime", uddf_datetime, &dive->when) ||
944 MATCH(".diveduration", duration, &dive->duration) ||
945 MATCH(".greatestdepth", depth, &dive->maxdepth) ||
949 static void gps_location(char *buffer, void *_dive)
952 struct dive *dive = _dive;
953 double latitude, longitude;
955 i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
957 dive->latitude = latitude;
958 dive->longitude = longitude;
963 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
964 static void try_to_fill_dive(struct dive **divep, const char *name, char *buf)
966 int len = strlen(name);
968 start_match("dive", name, buf);
970 switch (import_source) {
972 if (suunto_dive_match(divep, name, len, buf))
977 if (uemis_dive_match(divep, name, len, buf))
982 if (divinglog_dive_match(divep, name, len, buf))
987 if (uddf_dive_match(divep, name, len, buf))
995 struct dive *dive = *divep;
997 if (MATCH(".number", get_index, &dive->number))
999 if (MATCH(".date", divedate, &dive->when))
1001 if (MATCH(".time", divetime, &dive->when))
1003 if (MATCH(".datetime", divedatetime, &dive->when))
1005 if (MATCH(".maxdepth", depth, &dive->maxdepth))
1007 if (MATCH(".meandepth", depth, &dive->meandepth))
1009 if (MATCH(".depth.max", depth, &dive->maxdepth))
1011 if (MATCH(".depth.mean", depth, &dive->meandepth))
1013 if (MATCH(".duration", duration, &dive->duration))
1015 if (MATCH(".divetime", duration, &dive->duration))
1017 if (MATCH(".divetimesec", duration, &dive->duration))
1019 if (MATCH(".surfacetime", duration, &dive->surfacetime))
1021 if (MATCH(".airtemp", temperature, &dive->airtemp))
1023 if (MATCH(".watertemp", temperature, &dive->watertemp))
1025 if (MATCH(".temperature.air", temperature, &dive->airtemp))
1027 if (MATCH(".temperature.water", temperature, &dive->watertemp))
1029 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
1031 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
1033 if (MATCH(".gps", gps_location, dive))
1035 if (MATCH(".location", utf8_string, &dive->location))
1037 if (MATCH(".notes", utf8_string, &dive->notes))
1039 if (MATCH(".divemaster", utf8_string, &dive->divemaster))
1041 if (MATCH(".buddy", utf8_string, &dive->buddy))
1044 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
1046 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
1048 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cylinder_index].type.description))
1050 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cylinder_index].start))
1052 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cylinder_index].end))
1055 if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
1057 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
1059 if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
1062 nonmatch("dive", name, buf);
1066 * File boundaries are dive boundaries. But sometimes there are
1067 * multiple dives per file, so there can be other events too that
1068 * trigger a "new dive" marker and you may get some nesting due
1069 * to that. Just ignore nesting levels.
1071 static void dive_start(void)
1075 dive = alloc_dive();
1076 memset(&tm, 0, sizeof(tm));
1079 static void sanitize_gasmix(struct gasmix *mix)
1081 unsigned int o2, he;
1083 o2 = mix->o2.permille;
1084 he = mix->he.permille;
1086 /* Regular air: leave empty */
1090 /* 20.9% or 21% O2 is just air */
1091 if (o2 >= 209 && o2 <= 210) {
1092 mix->o2.permille = 0;
1098 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
1100 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
1101 memset(mix, 0, sizeof(*mix));
1105 * See if the size/workingpressure looks like some standard cylinder
1108 static void match_standard_cylinder(cylinder_type_t *type)
1113 char buffer[20], *p;
1115 /* Do we already have a cylinder description? */
1116 if (type->description)
1119 cuft = type->size.mliter / 28317.0;
1120 cuft *= to_ATM(type->workingpressure);
1121 psi = type->workingpressure.mbar / 68.95;
1124 case 2300 ... 2500: /* 2400 psi: LP tank */
1127 case 2600 ... 2700: /* 2640 psi: LP+10% */
1130 case 2900 ... 3100: /* 3000 psi: ALx tank */
1133 case 3400 ... 3500: /* 3442 psi: HP tank */
1136 case 3700 ... 3850: /* HP+10% */
1142 len = snprintf(buffer, sizeof(buffer), fmt, (int) (cuft+0.5));
1146 memcpy(p, buffer, len+1);
1147 type->description = p;
1152 * There are two ways to give cylinder size information:
1153 * - total amount of gas in cuft (depends on working pressure and physical size)
1156 * where "physical size" is the one that actually matters and is sane.
1158 * We internally use physical size only. But we save the workingpressure
1159 * so that we can do the conversion if required.
1161 static void sanitize_cylinder_type(cylinder_type_t *type)
1163 double volume_of_air, atm, volume;
1165 /* If we have no working pressure, it had *better* be just a physical size! */
1166 if (!type->workingpressure.mbar)
1169 /* No size either? Nothing to go on */
1170 if (!type->size.mliter)
1173 if (input_units.volume == CUFT || import_source == SUUNTO) {
1174 volume_of_air = type->size.mliter * 28.317; /* milli-cu ft to milliliter */
1175 atm = to_ATM(type->workingpressure); /* working pressure in atm */
1176 volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
1177 type->size.mliter = volume + 0.5;
1180 /* Ok, we have both size and pressure: try to match a description */
1181 match_standard_cylinder(type);
1184 static void sanitize_cylinder_info(struct dive *dive)
1188 for (i = 0; i < MAX_CYLINDERS; i++) {
1189 sanitize_gasmix(&dive->cylinder[i].gasmix);
1190 sanitize_cylinder_type(&dive->cylinder[i].type);
1194 static void dive_end(void)
1198 sanitize_cylinder_info(dive);
1204 static void event_start(void)
1206 memset(&event, 0, sizeof(event));
1210 static void event_end(void)
1212 if (event.name && strcmp(event.name, "surface") != 0)
1213 add_event(dive, event.time.seconds, event.type, event.flags, event.value, event.name);
1217 static void cylinder_start(void)
1221 static void cylinder_end(void)
1226 static void sample_start(void)
1228 sample = prepare_sample(&dive);
1231 static void sample_end(void)
1236 finish_sample(dive, sample);
1240 static void entry(const char *name, int size, const char *raw)
1242 char *buf = malloc(size+1);
1246 memcpy(buf, raw, size);
1249 try_to_fill_event(name, buf);
1253 try_to_fill_sample(sample, name, buf);
1257 try_to_fill_dive(&dive, name, buf);
1262 static const char *nodename(xmlNode *node, char *buf, int len)
1264 if (!node || !node->name)
1272 const char *name = node->name;
1273 int i = strlen(name);
1275 unsigned char c = name[i];
1276 *--buf = tolower(c);
1280 node = node->parent;
1281 if (!node || !node->name)
1291 static void visit_one_node(xmlNode *node)
1294 const unsigned char *content;
1295 char buffer[MAXNAME];
1298 content = node->content;
1302 /* Trim whitespace at beginning */
1303 while (isspace(*content))
1306 /* Trim whitespace at end */
1307 len = strlen(content);
1308 while (len && isspace(content[len-1]))
1314 /* Don't print out the node name if it is "text" */
1315 if (!strcmp(node->name, "text"))
1316 node = node->parent;
1318 name = nodename(node, buffer, sizeof(buffer));
1320 entry(name, len, content);
1323 static void traverse(xmlNode *root);
1325 static void traverse_properties(xmlNode *node)
1329 for (p = node->properties; p; p = p->next)
1330 traverse(p->children);
1333 static void visit(xmlNode *n)
1336 traverse_properties(n);
1337 traverse(n->children);
1340 static void suunto_importer(void)
1342 import_source = SUUNTO;
1343 input_units = SI_units;
1346 static void uemis_importer(void)
1348 import_source = UEMIS;
1349 input_units = SI_units;
1352 static void DivingLog_importer(void)
1354 import_source = DIVINGLOG;
1357 * Diving Log units are really strange.
1359 * Temperatures are in C, except in samples,
1360 * when they are in Fahrenheit. Depths are in
1361 * meters, an dpressure is in PSI in the samples,
1362 * but in bar when it comes to working pressure.
1364 * Crazy f*%^ morons.
1366 input_units = SI_units;
1369 static void uddf_importer(void)
1371 import_source = UDDF;
1372 input_units = SI_units;
1373 input_units.pressure = PASCAL;
1374 input_units.temperature = KELVIN;
1378 * I'm sure this could be done as some fancy DTD rules.
1379 * It's just not worth the headache.
1381 static struct nesting {
1383 void (*start)(void), (*end)(void);
1385 { "dive", dive_start, dive_end },
1386 { "Dive", dive_start, dive_end },
1387 { "sample", sample_start, sample_end },
1388 { "waypoint", sample_start, sample_end },
1389 { "SAMPLE", sample_start, sample_end },
1390 { "reading", sample_start, sample_end },
1391 { "event", event_start, event_end },
1392 { "gasmix", cylinder_start, cylinder_end },
1393 { "cylinder", cylinder_start, cylinder_end },
1394 { "P", sample_start, sample_end },
1396 /* Import type recognition */
1397 { "SUUNTO", suunto_importer },
1398 { "Divinglog", DivingLog_importer },
1399 { "pre_dive", uemis_importer },
1400 { "dives", uemis_importer },
1401 { "uddf", uddf_importer },
1406 static void traverse(xmlNode *root)
1410 for (n = root; n; n = n->next) {
1411 struct nesting *rule = nesting;
1414 if (!strcmp(rule->name, n->name))
1417 } while (rule->name);
1427 /* Per-file reset */
1428 static void reset_all(void)
1431 * We reset the units for each file. You'd think it was
1432 * a per-dive property, but I'm not going to trust people
1433 * to do per-dive setup. If the xml does have per-dive
1434 * data within one file, we might have to reset it per
1435 * dive for that format.
1437 input_units = SI_units;
1438 import_source = UNKNOWN;
1441 void parse_xml_file(const char *filename, GError **error)
1445 doc = xmlReadFile(filename, NULL, 0);
1447 fprintf(stderr, "Failed to parse '%s'.\n", filename);
1450 *error = g_error_new(g_quark_from_string("subsurface"),
1452 "Failed to parse '%s'",
1457 /* we assume that the last (or only) filename passed as argument is a
1458 * great filename to use as default when saving the dives */
1459 set_filename(filename);
1462 traverse(xmlDocGetRootElement(doc));
1468 void parse_xml_init(void)