+ return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
+ MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
+ MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
+ MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
+ MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
+ MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
+ MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
+ MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
+ MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
+ MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
+ 0;
+}
+
+static const char *country, *city;
+
+static void divinglog_place(char *place, void *_location)
+{
+ char **location = _location;
+ char buffer[256], *p;
+ int len;
+
+ len = snprintf(buffer, sizeof(buffer),
+ "%s%s%s%s%s",
+ place,
+ city ? ", " : "",
+ city ? city : "",
+ country ? ", " : "",
+ country ? country : "");
+
+ p = malloc(len+1);
+ memcpy(p, buffer, len+1);
+ *location = p;
+
+ city = NULL;
+ country = NULL;
+}
+
+static int divinglog_dive_match(struct dive *dive, const char *name, int len, char *buf)
+{
+ return MATCH(".divedate", divedate, &dive->when) ||
+ MATCH(".entrytime", divetime, &dive->when) ||
+ MATCH(".depth", depth, &dive->maxdepth) ||
+ MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
+ MATCH(".presw", pressure, &dive->cylinder[0].type.workingpressure) ||
+ MATCH(".comments", utf8_string, &dive->notes) ||
+ MATCH(".buddy.names", utf8_string, &dive->buddy) ||
+ MATCH(".country.name", utf8_string, &country) ||
+ MATCH(".city.name", utf8_string, &city) ||
+ MATCH(".place.name", divinglog_place, &dive->location) ||
+ 0;
+}
+
+static int buffer_value(char *buffer)
+{
+ int val = atoi(buffer);
+ free(buffer);
+ return val;
+}
+
+static void uemis_length_unit(char *buffer, void *_unused)
+{
+ input_units.length = buffer_value(buffer) ? FEET : METERS;
+}
+
+static void uemis_volume_unit(char *buffer, void *_unused)
+{
+ input_units.volume = buffer_value(buffer) ? CUFT : LITER;
+}
+
+static void uemis_pressure_unit(char *buffer, void *_unused)
+{
+#if 0
+ input_units.pressure = buffer_value(buffer) ? PSI : BAR;
+#endif
+}
+
+static void uemis_temperature_unit(char *buffer, void *_unused)
+{
+ input_units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
+}
+
+static void uemis_weight_unit(char *buffer, void *_unused)
+{
+ input_units.weight = buffer_value(buffer) ? LBS : KG;
+}
+
+static void uemis_time_unit(char *buffer, void *_unused)
+{
+}
+
+static void uemis_date_unit(char *buffer, void *_unused)
+{
+}
+
+/* Modified julian day, yay! */
+static void uemis_date_time(char *buffer, void *_when)
+{
+ time_t *when = _when;
+ union int_or_float val;
+
+ switch (integer_or_float(buffer, &val)) {
+ case FLOAT:
+ *when = (val.fp - 40587) * 86400;
+ break;
+ default:
+ fprintf(stderr, "Strange julian date: %s", buffer);
+ }
+ free(buffer);
+}
+
+/*
+ * Uemis doesn't know time zones. You need to do them as
+ * minutes, not hours.
+ *
+ * But that's ok, we don't track timezones yet either. We
+ * just turn everything into "localtime expressed as UTC".
+ */
+static void uemis_time_zone(char *buffer, void *_when)
+{
+#if 0 /* seems like this is only used to display it correctly
+ * the stored time appears to be UTC */
+
+ time_t *when = _when;
+ signed char tz = atoi(buffer);
+
+ *when += tz * 3600;
+#endif
+}
+
+/* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
+static int uemis_gas_template;
+
+/*
+ * Christ. Uemis tank data is a total mess.
+ *
+ * We're passed a "virtual cylinder" (0 - 6) for the different
+ * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
+ * and "nitrox_3.{bottom,deco,travel}". We need to turn that
+ * into the actual cylinder data depending on the gas template,
+ * and ignore the ones that are irrelevant for that template.
+ *
+ * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
+ * (which are "air" and "nitrox_1" respectively), and tanks 4-6
+ * (which are the three "nitrox_3" tanks), and we turn virtual
+ * tanks 2/3 into actual tanks 0/1.
+ *
+ * Confused yet?
+ */
+static int uemis_cylinder_index(void *_cylinder)
+{
+ cylinder_t *cylinder = _cylinder;
+ unsigned int index = cylinder - dive->cylinder;
+
+ if (index > 6) {
+ fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
+ return -1;
+ }
+ switch(uemis_gas_template) {
+ case 1: /* Dive uses tank 1 */
+ index -= 1;
+ /* Fallthrough */
+ case 0: /* Dive uses tank 0 */
+ if (index)
+ index = -1;
+ break;
+ case 2: /* Dive uses tanks 2-3 */
+ index -= 2;
+ if (index > 1)
+ index = -1;
+ break;
+ case 3: /* Dive uses tanks 4-6 */
+ index -= 4;
+ if (index > 2)
+ index = -1;
+ break;
+ }
+ return index;
+}
+
+static void uemis_cylindersize(char *buffer, void *_cylinder)
+{
+ int index = uemis_cylinder_index(_cylinder);
+ if (index >= 0)
+ cylindersize(buffer, &dive->cylinder[index].type.size);
+}
+
+static void uemis_percent(char *buffer, void *_cylinder)
+{
+ int index = uemis_cylinder_index(_cylinder);
+ if (index >= 0)
+ percent(buffer, &dive->cylinder[index].gasmix.o2);
+}
+
+static int uemis_dive_match(struct dive *dive, const char *name, int len, char *buf)
+{
+ return MATCH(".units.length", uemis_length_unit, &input_units) ||
+ MATCH(".units.volume", uemis_volume_unit, &input_units) ||
+ MATCH(".units.pressure", uemis_pressure_unit, &input_units) ||
+ MATCH(".units.temperature", uemis_temperature_unit, &input_units) ||
+ MATCH(".units.weight", uemis_weight_unit, &input_units) ||
+ MATCH(".units.time", uemis_time_unit, &input_units) ||
+ MATCH(".units.date", uemis_date_unit, &input_units) ||
+ MATCH(".date_time", uemis_date_time, &dive->when) ||
+ MATCH(".time_zone", uemis_time_zone, &dive->when) ||
+ MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
+ MATCH(".gas.template", get_index, &uemis_gas_template) ||
+ MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
+ MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
+ MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
+ MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
+ MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
+ MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
+ MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
+ MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
+ MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
+ MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
+ MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
+ MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
+ MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
+ MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
+ 0;
+}
+
+/*
+ * Uddf specifies ISO 8601 time format.
+ *
+ * There are many variations on that. This handles the useful cases.
+ */
+static void uddf_datetime(char *buffer, void *_when)
+{
+ char c;
+ int y,m,d,hh,mm,ss;
+ time_t *when = _when;
+ struct tm tm = { 0 };
+ int i;
+
+ i = sscanf(buffer, "%d-%d-%d%c%d:%d:%d", &y, &m, &d, &c, &hh, &mm, &ss);
+ if (i == 7)
+ goto success;
+ ss = 0;
+ if (i == 6)
+ goto success;
+
+ i = sscanf(buffer, "%04d%02d%02d%c%02d%02d%02d", &y, &m, &d, &c, &hh, &mm, &ss);
+ if (i == 7)
+ goto success;
+ ss = 0;
+ if (i == 6)
+ goto success;
+bad_date:
+ printf("Bad date time %s\n", buffer);
+ free(buffer);
+ return;
+
+success:
+ if (c != 'T' && c != ' ')
+ goto bad_date;
+ tm.tm_year = y;
+ tm.tm_mon = m - 1;
+ tm.tm_mday = d;
+ tm.tm_hour = hh;
+ tm.tm_min = mm;
+ tm.tm_sec = ss;
+ *when = utc_mktime(&tm);
+ free(buffer);
+}
+
+static int uddf_dive_match(struct dive *dive, const char *name, int len, char *buf)
+{
+ return MATCH(".datetime", uddf_datetime, &dive->when) ||
+ MATCH(".diveduration", duration, &dive->duration) ||
+ MATCH(".greatestdepth", depth, &dive->maxdepth) ||
+ 0;
+}
+
+static void gps_location(char *buffer, void *_dive)
+{
+ int i;
+ struct dive *dive = _dive;
+ double latitude, longitude;
+
+ i = sscanf(buffer, "%lf %lf", &latitude, &longitude);
+ if (i == 2) {
+ dive->latitude = latitude;
+ dive->longitude = longitude;
+ }
+ free(buffer);