+ show_milli(f, pre, pressure.mbar, " bar", post);
+}
+
+/*
+ * We're outputting utf8 in xml.
+ * We need to quote the characters <, >, &.
+ *
+ * Technically I don't think we'd necessarily need to quote the control
+ * characters, but at least libxml2 doesn't like them. It doesn't even
+ * allow them quoted. So we just skip them and replace them with '?'.
+ *
+ * Nothing else (and if we ever do this using attributes, we'd need to
+ * quote the quotes we use too).
+ */
+static void quote(FILE *f, const char *text)
+{
+ const char *p = text;
+
+ for (;;) {
+ const char *escape;
+
+ switch (*p++) {
+ default:
+ continue;
+ case 0:
+ escape = NULL;
+ break;
+ case 1 ... 8:
+ case 11: case 12:
+ case 14 ... 31:
+ escape = "?";
+ break;
+ case '<':
+ escape = "<";
+ break;
+ case '>':
+ escape = ">";
+ break;
+ case '&':
+ escape = "&";
+ break;
+ }
+ fwrite(text, (p - text - 1), 1, f);
+ if (!escape)
+ break;
+ fputs(escape, f);
+ text = p;
+ }
+}
+
+static void show_utf8(FILE *f, const char *text, const char *pre, const char *post)
+{
+ int len;
+
+ if (!text)
+ return;
+ while (isspace(*text))
+ text++;
+ len = strlen(text);
+ if (!len)
+ return;
+ while (len && isspace(text[len-1]))
+ len--;
+ /* FIXME! Quoting! */
+ fputs(pre, f);
+ quote(f, text);
+ fputs(post, f);
+}
+
+static void save_depths(FILE *f, struct dive *dive)
+{
+ /* What's the point of this dive entry again? */
+ if (!dive->maxdepth.mm && !dive->meandepth.mm)
+ return;
+
+ fputs(" <depth", f);
+ show_depth(f, dive->maxdepth, " max='", "'");
+ show_depth(f, dive->meandepth, " mean='", "'");
+ fputs(" />\n", f);
+}
+
+static void save_temperatures(FILE *f, struct dive *dive)
+{
+ if (!dive->airtemp.mkelvin && !dive->watertemp.mkelvin)
+ return;
+ fputs(" <temperature", f);
+ show_temperature(f, dive->airtemp, " air='", "'");
+ show_temperature(f, dive->watertemp, " water='", "'");
+ fputs(" />\n", f);
+}
+
+static void show_location(FILE *f, struct dive *dive)
+{
+ char buffer[80];
+ const char *prefix = " <location>";
+ double latitude = dive->latitude;
+ double longitude = dive->longitude;
+
+ /*
+ * Ok, theoretically I guess you could dive at
+ * exactly 0,0. But we don't support that. So
+ * if you do, just fudge it a bit, and say that
+ * you dove a few meters away.
+ */
+ if (latitude || longitude) {
+ int len = snprintf(buffer, sizeof(buffer)-4,
+ " <location gps='%.12g %.12g'>",
+ latitude, longitude);
+ if (!dive->location) {
+ memcpy(&buffer[len-1], "/>\n", 4);
+ fputs(buffer, f);
+ return;
+ }
+ prefix = buffer;
+ }
+ show_utf8(f, dive->location, prefix,"</location>\n");