10 #define FRACTION(n,x) ((unsigned)(n)/(x)),((unsigned)(n)%(x))
12 static void show_milli(FILE *f, const char *pre, int value, const char *unit, const char *post)
24 for (i = 2; i >= 0; i--) {
25 buf[i] = (v % 10) + '0';
35 fprintf(f, "%u.%s%s%s", v, buf, unit, post);
38 static void show_temperature(FILE *f, temperature_t temp, const char *pre, const char *post)
41 show_milli(f, pre, temp.mkelvin - 273150, " C", post);
44 static void show_depth(FILE *f, depth_t depth, const char *pre, const char *post)
47 show_milli(f, pre, depth.mm, " m", post);
50 static void show_duration(FILE *f, duration_t duration, const char *pre, const char *post)
53 fprintf(f, "%s%u:%02u min%s", pre, FRACTION(duration.seconds, 60), post);
56 static void show_pressure(FILE *f, pressure_t pressure, const char *pre, const char *post)
59 show_milli(f, pre, pressure.mbar, " bar", post);
63 * We're outputting utf8 in xml.
64 * We need to quote the characters <, >, &.
66 * Technically I don't think we'd necessarily need to quote the control
67 * characters, but at least libxml2 doesn't like them. It doesn't even
68 * allow them quoted. So we just skip them and replace them with '?'.
70 * Nothing else (and if we ever do this using attributes, we'd need to
71 * quote the quotes we use too).
73 static void quote(FILE *f, const char *text)
101 fwrite(text, (p - text - 1), 1, f);
109 static void show_utf8(FILE *f, const char *text, const char *pre, const char *post)
115 while (isspace(*text))
120 while (len && isspace(text[len-1]))
122 /* FIXME! Quoting! */
128 static void save_depths(FILE *f, struct dive *dive)
130 /* What's the point of this dive entry again? */
131 if (!dive->maxdepth.mm && !dive->meandepth.mm)
135 show_depth(f, dive->maxdepth, " max='", "'");
136 show_depth(f, dive->meandepth, " mean='", "'");
140 static void save_temperatures(FILE *f, struct dive *dive)
142 if (!dive->airtemp.mkelvin && !dive->watertemp.mkelvin)
144 fputs(" <temperature", f);
145 show_temperature(f, dive->airtemp, " air='", "'");
146 show_temperature(f, dive->watertemp, " water='", "'");
150 static void show_location(FILE *f, struct dive *dive)
153 const char *prefix = " <location>";
154 double latitude = dive->latitude;
155 double longitude = dive->longitude;
158 * Ok, theoretically I guess you could dive at
159 * exactly 0,0. But we don't support that. So
160 * if you do, just fudge it a bit, and say that
161 * you dove a few meters away.
163 if (latitude || longitude) {
164 int len = snprintf(buffer, sizeof(buffer)-4,
165 " <location gps='%.12g %.12g'>",
166 latitude, longitude);
167 if (!dive->location) {
168 memcpy(&buffer[len-1], "/>\n", 4);
174 show_utf8(f, dive->location, prefix,"</location>\n");
177 static void save_overview(FILE *f, struct dive *dive)
179 save_depths(f, dive);
180 save_temperatures(f, dive);
181 show_duration(f, dive->surfacetime, " <surfacetime>", "</surfacetime>\n");
182 show_location(f, dive);
183 show_utf8(f, dive->divemaster, " <divemaster>","</divemaster>\n");
184 show_utf8(f, dive->buddy, " <buddy>","</buddy>\n");
185 show_utf8(f, dive->notes, " <notes>","</notes>\n");
188 static void save_cylinder_info(FILE *f, struct dive *dive)
192 for (i = 0; i < MAX_CYLINDERS; i++) {
193 cylinder_t *cylinder = dive->cylinder+i;
194 int volume = cylinder->type.size.mliter;
195 const char *description = cylinder->type.description;
196 int o2 = cylinder->gasmix.o2.permille;
197 int he = cylinder->gasmix.he.permille;
198 int start = cylinder->start.mbar;
199 int end = cylinder->end.mbar;
201 /* No cylinder information at all? */
202 if (!o2 && !volume && !start && !end)
204 fprintf(f, " <cylinder");
206 show_milli(f, " size='", volume, " l", "'");
207 show_pressure(f, cylinder->type.workingpressure, " workpressure='", "'");
208 if (description && *description)
209 fprintf(f, " description='%s'", description);
211 fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
213 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
215 show_pressure(f, cylinder->start, " start='", "'");
216 show_pressure(f, cylinder->end, " end='", "'");
221 static void show_index(FILE *f, int value, const char *pre, const char *post)
224 fprintf(f, " %s%d%s", pre, value, post);
227 static void save_sample(FILE *f, struct sample *sample)
229 fprintf(f, " <sample time='%u:%02u min'", FRACTION(sample->time.seconds,60));
230 show_milli(f, " depth='", sample->depth.mm, " m", "'");
231 show_temperature(f, sample->temperature, " temp='", "'");
232 show_pressure(f, sample->cylinderpressure, " pressure='", "'");
233 if (sample->cylinderindex)
234 fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
238 static void save_one_event(FILE *f, struct event *ev)
240 fprintf(f, " <event time='%d:%02d min'", FRACTION(ev->time.seconds,60));
241 show_index(f, ev->type, "type='", "'");
242 show_index(f, ev->flags, "flags='", "'");
243 show_index(f, ev->value, "value='", "'");
244 show_utf8(f, ev->name, " name='", "'");
249 static void save_events(FILE *f, struct event *ev)
252 save_one_event(f, ev);
257 static void save_dive(FILE *f, struct dive *dive)
260 struct tm *tm = gmtime(&dive->when);
264 fprintf(f, " number='%d'", dive->number);
265 fprintf(f, " date='%04u-%02u-%02u'",
266 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
267 fprintf(f, " time='%02u:%02u:%02u'",
268 tm->tm_hour, tm->tm_min, tm->tm_sec);
269 fprintf(f, " duration='%u:%02u min'>\n",
270 FRACTION(dive->duration.seconds, 60));
271 save_overview(f, dive);
272 save_cylinder_info(f, dive);
273 save_events(f, dive->events);
274 for (i = 0; i < dive->samples; i++)
275 save_sample(f, dive->sample+i);
276 fprintf(f, "</dive>\n");
281 void save_dives(const char *filename)
284 FILE *f = fopen(filename, "w");
289 /* Flush any edits of current dives back to the dives! */
290 update_dive(current_dive);
292 fprintf(f, "<dives>\n<program name='subsurface' version='%d'></program>\n", VERSION);
293 for (i = 0; i < dive_table.nr; i++)
294 save_dive(f, get_dive(i));
295 fprintf(f, "</dives>\n");