]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Fix profile and average depth for freedives
[ext/subsurface.git] / save-xml.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <time.h>
7
8 #include "dive.h"
9
10 #define FRACTION(n,x) ((unsigned)(n)/(x)),((unsigned)(n)%(x))
11
12 static void show_milli(FILE *f, const char *pre, int value, const char *unit, const char *post)
13 {
14         int i;
15         char buf[4];
16         unsigned v;
17
18         fputs(pre, f);
19         v = value;
20         if (value < 0) {
21                 putc('-', f);
22                 v = -value;
23         }
24         for (i = 2; i >= 0; i--) {
25                 buf[i] = (v % 10) + '0';
26                 v /= 10;
27         }
28         buf[3] = 0;
29         if (buf[2] == '0') {
30                 buf[2] = 0;
31                 if (buf[1] == '0')
32                         buf[1] = 0;
33         }
34
35         fprintf(f, "%u.%s%s%s", v, buf, unit, post);
36 }
37
38 static void show_temperature(FILE *f, temperature_t temp, const char *pre, const char *post)
39 {
40         if (temp.mkelvin)
41                 show_milli(f, pre, temp.mkelvin - 273150, " C", post);
42 }
43
44 static void show_depth(FILE *f, depth_t depth, const char *pre, const char *post)
45 {
46         if (depth.mm)
47                 show_milli(f, pre, depth.mm, " m", post);
48 }
49
50 static void show_duration(FILE *f, duration_t duration, const char *pre, const char *post)
51 {
52         if (duration.seconds)
53                 fprintf(f, "%s%u:%02u min%s", pre, FRACTION(duration.seconds, 60), post);
54 }
55
56 static void show_pressure(FILE *f, pressure_t pressure, const char *pre, const char *post)
57 {
58         if (pressure.mbar)
59                 show_milli(f, pre, pressure.mbar, " bar", post);
60 }
61
62 /*
63  * We're outputting utf8 in xml.
64  * We need to quote the characters <, >, &.
65  *
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 '?'.
69  *
70  * If we do this for attributes, we need to quote the quotes we use too.
71  */
72 static void quote(FILE *f, const char *text, int is_attribute)
73 {
74         const char *p = text;
75
76         for (;;) {
77                 const char *escape;
78
79                 switch (*p++) {
80                 default:
81                         continue;
82                 case 0:
83                         escape = NULL;
84                         break;
85                 case 1 ... 8:
86                 case 11: case 12:
87                 case 14 ... 31:
88                         escape = "?";
89                         break;
90                 case '<':
91                         escape = "&lt;";
92                         break;
93                 case '>':
94                         escape = "&gt;";
95                         break;
96                 case '&':
97                         escape = "&amp;";
98                         break;
99                 case '\'':
100                         if (!is_attribute)
101                                 continue;
102                         escape = "&apos;";
103                         break;
104                 case '\"':
105                         if (!is_attribute)
106                                 continue;
107                         escape = "&quot;";
108                         break;
109                 }
110                 fwrite(text, (p - text - 1), 1, f);
111                 if (!escape)
112                         break;
113                 fputs(escape, f);
114                 text = p;
115         }
116 }
117
118 static void show_utf8(FILE *f, const char *text, const char *pre, const char *post, int is_attribute)
119 {
120         int len;
121
122         if (!text)
123                 return;
124         while (isspace(*text))
125                 text++;
126         len = strlen(text);
127         if (!len)
128                 return;
129         while (len && isspace(text[len-1]))
130                 len--;
131         /* FIXME! Quoting! */
132         fputs(pre, f);
133         quote(f, text, is_attribute);
134         fputs(post, f);
135 }
136
137 static void save_depths(FILE *f, struct dive *dive)
138 {
139         /* What's the point of this dive entry again? */
140         if (!dive->maxdepth.mm && !dive->meandepth.mm)
141                 return;
142
143         fputs("  <depth", f);
144         show_depth(f, dive->maxdepth, " max='", "'");
145         show_depth(f, dive->meandepth, " mean='", "'");
146         fputs(" />\n", f);
147 }
148
149 static void save_temperatures(FILE *f, struct dive *dive)
150 {
151         if (!dive->airtemp.mkelvin && !dive->watertemp.mkelvin)
152                 return;
153         fputs("  <temperature", f);
154         show_temperature(f, dive->airtemp, " air='", "'");
155         show_temperature(f, dive->watertemp, " water='", "'");
156         fputs(" />\n", f);
157 }
158
159 static void show_location(FILE *f, struct dive *dive)
160 {
161         char buffer[80];
162         const char *prefix = "  <location>";
163         double latitude = dive->latitude;
164         double longitude = dive->longitude;
165
166         /*
167          * Ok, theoretically I guess you could dive at
168          * exactly 0,0. But we don't support that. So
169          * if you do, just fudge it a bit, and say that
170          * you dove a few meters away.
171          */
172         if (latitude || longitude) {
173                 int len = snprintf(buffer, sizeof(buffer)-4,
174                         "  <location gps='%.12g %.12g'>",
175                         latitude, longitude);
176                 if (!dive->location) {
177                         memcpy(&buffer[len-1], "/>\n", 4);
178                         fputs(buffer, f);
179                         return;
180                 }
181                 prefix = buffer;
182         }
183         show_utf8(f, dive->location, prefix,"</location>\n", 0);
184 }
185
186 static void save_overview(FILE *f, struct dive *dive)
187 {
188         save_depths(f, dive);
189         save_temperatures(f, dive);
190         show_duration(f, dive->surfacetime, "  <surfacetime>", "</surfacetime>\n");
191         show_location(f, dive);
192         show_utf8(f, dive->divemaster, "  <divemaster>","</divemaster>\n", 0);
193         show_utf8(f, dive->buddy, "  <buddy>","</buddy>\n", 0);
194         show_utf8(f, dive->notes, "  <notes>","</notes>\n", 0);
195         show_utf8(f, dive->suit, "  <suit>","</suit>\n", 0);
196 }
197
198 static void save_cylinder_info(FILE *f, struct dive *dive)
199 {
200         int i;
201
202         for (i = 0; i < MAX_CYLINDERS; i++) {
203                 cylinder_t *cylinder = dive->cylinder+i;
204                 int volume = cylinder->type.size.mliter;
205                 const char *description = cylinder->type.description;
206                 int o2 = cylinder->gasmix.o2.permille;
207                 int he = cylinder->gasmix.he.permille;
208                 int start = cylinder->start.mbar;
209                 int end = cylinder->end.mbar;
210
211                 /* No cylinder information at all? */
212                 if (!o2 && !volume && !start && !end)
213                         return;
214                 fprintf(f, "  <cylinder");
215                 if (volume)
216                         show_milli(f, " size='", volume, " l", "'");
217                 show_pressure(f, cylinder->type.workingpressure, " workpressure='", "'");
218                 if (description && *description)
219                         fprintf(f, " description='%s'", description);
220                 if (o2) {
221                         fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
222                         if (he)
223                                 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
224                 }
225                 show_pressure(f, cylinder->start, " start='", "'");
226                 show_pressure(f, cylinder->end, " end='", "'");
227                 fprintf(f, " />\n");
228         }
229 }
230
231 static void save_weightsystem_info(FILE *f, struct dive *dive)
232 {
233         int i;
234
235         for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
236                 weightsystem_t *ws = dive->weightsystem+i;
237                 int grams = ws->weight.grams;
238                 const char *description = ws->description;
239
240                 /* No weight information at all? */
241                 if (grams == 0)
242                         return;
243                 fprintf(f, "  <weightsystem");
244                 show_milli(f, " weight='", grams, " kg", "'");
245                 if (description && *description)
246                         fprintf(f, " description='%s'", description);
247                 fprintf(f, " />\n");
248         }
249 }
250
251 static void show_index(FILE *f, int value, const char *pre, const char *post)
252 {
253         if (value)
254                 fprintf(f, " %s%d%s", pre, value, post);
255 }
256
257 static void save_sample(FILE *f, struct sample *sample)
258 {
259         fprintf(f, "  <sample time='%u:%02u min'", FRACTION(sample->time.seconds,60));
260         show_milli(f, " depth='", sample->depth.mm, " m", "'");
261         show_temperature(f, sample->temperature, " temp='", "'");
262         show_pressure(f, sample->cylinderpressure, " pressure='", "'");
263         if (sample->cylinderindex)
264                 fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
265         fprintf(f, " />\n");
266 }
267
268 static void save_one_event(FILE *f, struct event *ev)
269 {
270         fprintf(f, "  <event time='%d:%02d min'", FRACTION(ev->time.seconds,60));
271         show_index(f, ev->type, "type='", "'");
272         show_index(f, ev->flags, "flags='", "'");
273         show_index(f, ev->value, "value='", "'");
274         show_utf8(f, ev->name, " name='", "'", 1);
275         fprintf(f, " />\n");
276 }
277
278
279 static void save_events(FILE *f, struct event *ev)
280 {
281         while (ev) {
282                 save_one_event(f, ev);
283                 ev = ev->next;
284         }
285 }
286
287 static void save_trip(FILE *f, struct dive *trip)
288 {
289         struct tm *tm = gmtime(&trip->when);
290
291         fprintf(f, "<trip");
292         fprintf(f, " date='%04u-%02u-%02u'",
293                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
294         if (trip->location)
295                 show_utf8(f, trip->location, " location=\'","\'", 1);
296         fprintf(f, " />\n");
297 }
298
299 static void save_dive(FILE *f, struct dive *dive)
300 {
301         int i;
302         struct tm *tm = gmtime(&dive->when);
303
304         fputs("<dive", f);
305         if (dive->number)
306                 fprintf(f, " number='%d'", dive->number);
307         if (dive->tripflag != TF_NONE)
308                 fprintf(f, " tripflag='%s'", tripflag_names[dive->tripflag]);
309         if (dive->rating)
310                 fprintf(f, " rating='%d'", dive->rating);
311         fprintf(f, " date='%04u-%02u-%02u'",
312                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
313         fprintf(f, " time='%02u:%02u:%02u'",
314                 tm->tm_hour, tm->tm_min, tm->tm_sec);
315         fprintf(f, " duration='%u:%02u min'>\n",
316                 FRACTION(dive->duration.seconds, 60));
317         save_overview(f, dive);
318         save_cylinder_info(f, dive);
319         save_weightsystem_info(f, dive);
320         save_events(f, dive->events);
321         for (i = 0; i < dive->samples; i++)
322                 save_sample(f, dive->sample+i);
323         fprintf(f, "</dive>\n");
324 }
325
326 #define VERSION 1
327
328 void save_dives(const char *filename)
329 {
330         int i;
331         GList *trip = NULL;
332
333         FILE *f = fopen(filename, "w");
334
335         if (!f)
336                 return;
337
338         /* Flush any edits of current dives back to the dives! */
339         update_dive(current_dive);
340
341         fprintf(f, "<dives>\n<program name='subsurface' version='%d'></program>\n", VERSION);
342
343         /* save the trips */
344         while ((trip = NEXT_TRIP(trip, dive_trip_list)) != 0)
345                 save_trip(f, trip->data);
346
347         /* save the dives */
348         for (i = 0; i < dive_table.nr; i++)
349                 save_dive(f, get_dive(i));
350         fprintf(f, "</dives>\n");
351         fclose(f);
352 }