10 #define FRACTION(n,x) ((unsigned)(n)/(x)),((unsigned)(n)%(x))
12 static void show_temperature(FILE *f, temperature_t temp, const char *pre, const char *post)
15 int mcelsius = temp.mkelvin - 273150;
19 mcelsius = - mcelsius;
21 fprintf(f, "%s%s%u.%03u C%s", pre, sign, FRACTION(mcelsius, 1000), post);
25 static void show_depth(FILE *f, depth_t depth, const char *pre, const char *post)
28 fprintf(f, "%s%u.%03u m%s", pre, FRACTION(depth.mm, 1000), post);
31 static void show_duration(FILE *f, duration_t duration, const char *pre, const char *post)
34 fprintf(f, "%s%u:%02u min%s", pre, FRACTION(duration.seconds, 60), post);
37 static void show_pressure(FILE *f, pressure_t pressure, const char *pre, const char *post)
40 fprintf(f, "%s%u.%03u bar%s", pre, FRACTION(pressure.mbar, 1000), post);
44 * We're outputting utf8 in xml.
45 * We need to quote the characters <, >, &.
47 * Technically I don't think we'd necessarily need to quote the control
48 * characters, but at least libxml2 doesn't like them. It doesn't even
49 * allow them quoted. So we just skip them and replace them with '?'.
51 * Nothing else (and if we ever do this using attributes, we'd need to
52 * quote the quotes we use too).
54 static void quote(FILE *f, const char *text)
82 fwrite(text, (p - text - 1), 1, f);
90 static void show_utf8(FILE *f, const char *text, const char *pre, const char *post)
96 while (isspace(*text))
101 while (len && isspace(text[len-1]))
103 /* FIXME! Quoting! */
109 static void save_overview(FILE *f, struct dive *dive)
111 show_depth(f, dive->maxdepth, " <maxdepth>", "</maxdepth>\n");
112 show_depth(f, dive->meandepth, " <meandepth>", "</meandepth>\n");
113 show_temperature(f, dive->airtemp, " <airtemp>", "</airtemp>\n");
114 show_temperature(f, dive->watertemp, " <watertemp>", "</watertemp>\n");
115 show_duration(f, dive->duration, " <duration>", "</duration>\n");
116 show_duration(f, dive->surfacetime, " <surfacetime>", "</surfacetime>\n");
117 show_pressure(f, dive->beginning_pressure, " <cylinderstartpressure>", "</cylinderstartpressure>\n");
118 show_pressure(f, dive->end_pressure, " <cylinderendpressure>", "</cylinderendpressure>\n");
119 show_utf8(f, dive->location, " <location>","</location>\n");
120 show_utf8(f, dive->notes, " <notes>","</notes>\n");
123 static void save_cylinder_info(FILE *f, struct dive *dive)
127 for (i = 0; i < MAX_CYLINDERS; i++) {
128 cylinder_t *cylinder = dive->cylinder+i;
129 int volume = cylinder->type.size.mliter;
130 const char *description = cylinder->type.description;
131 int o2 = cylinder->gasmix.o2.permille;
132 int he = cylinder->gasmix.he.permille;
134 /* No cylinder information at all? */
137 fprintf(f, " <cylinder");
139 fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
141 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
144 fprintf(f, " size='%u.%03u l'", FRACTION(volume, 1000));
146 fprintf(f, " name='%s'", description);
151 static void save_sample(FILE *f, struct sample *sample)
153 fprintf(f, " <sample time='%u:%02u min' depth='%u.%03u m'",
154 FRACTION(sample->time.seconds,60),
155 FRACTION(sample->depth.mm, 1000));
156 show_temperature(f, sample->temperature, " temp='", "'");
157 show_pressure(f, sample->cylinderpressure, " pressure='", "'");
158 if (sample->cylinderindex)
159 fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
163 static void save_dive(FILE *f, struct dive *dive)
166 struct tm *tm = gmtime(&dive->when);
168 fprintf(f, "<dive date='%04u-%02u-%02u' time='%02u:%02u:%02u'>\n",
169 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
170 tm->tm_hour, tm->tm_min, tm->tm_sec);
171 save_overview(f, dive);
172 save_cylinder_info(f, dive);
173 for (i = 0; i < dive->samples; i++)
174 save_sample(f, dive->sample+i);
175 fprintf(f, "</dive>\n");
180 void save_dives(const char *filename)
183 FILE *f = fopen(filename, "w");
188 /* Flush any edits of current dives back to the dives! */
189 flush_dive_info_changes();
191 fprintf(f, "<dives>\n<program name='diveclog' version='%d'></program>\n", VERSION);
192 for (i = 0; i < dive_table.nr; i++)
193 save_dive(f, get_dive(i));
194 fprintf(f, "</dives>\n");