]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Divide the panes evenly in view_three
[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  * Nothing else (and if we ever do this using attributes, we'd need to
71  * quote the quotes we use too).
72  */
73 static void quote(FILE *f, const char *text)
74 {
75         const char *p = text;
76
77         for (;;) {
78                 const char *escape;
79
80                 switch (*p++) {
81                 default:
82                         continue;
83                 case 0:
84                         escape = NULL;
85                         break;
86                 case 1 ... 8:
87                 case 11: case 12:
88                 case 14 ... 31:
89                         escape = "?";
90                         break;
91                 case '<':
92                         escape = "&lt;";
93                         break;
94                 case '>':
95                         escape = "&gt;";
96                         break;
97                 case '&':
98                         escape = "&amp;";
99                         break;
100                 }
101                 fwrite(text, (p - text - 1), 1, f);
102                 if (!escape)
103                         break;
104                 fputs(escape, f);
105                 text = p;
106         }
107 }
108
109 static void show_utf8(FILE *f, const char *text, const char *pre, const char *post)
110 {
111         int len;
112
113         if (!text)
114                 return;
115         while (isspace(*text))
116                 text++;
117         len = strlen(text);
118         if (!len)
119                 return;
120         while (len && isspace(text[len-1]))
121                 len--;
122         /* FIXME! Quoting! */
123         fputs(pre, f);
124         quote(f, text);
125         fputs(post, f);
126 }
127
128 static void save_depths(FILE *f, struct dive *dive)
129 {
130         /* What's the point of this dive entry again? */
131         if (!dive->maxdepth.mm && !dive->meandepth.mm)
132                 return;
133
134         fputs("  <depth", f);
135         show_depth(f, dive->maxdepth, " max='", "'");
136         show_depth(f, dive->meandepth, " mean='", "'");
137         fputs(" />\n", f);
138 }
139
140 static void save_temperatures(FILE *f, struct dive *dive)
141 {
142         if (!dive->airtemp.mkelvin && !dive->watertemp.mkelvin)
143                 return;
144         fputs("  <temperature", f);
145         show_temperature(f, dive->airtemp, " air='", "'");
146         show_temperature(f, dive->watertemp, " water='", "'");
147         fputs(" />\n", f);
148 }
149
150 static void show_location(FILE *f, struct dive *dive)
151 {
152         char buffer[80];
153         const char *prefix = "  <location>";
154         double latitude = dive->latitude;
155         double longitude = dive->longitude;
156
157         /*
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.
162          */
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);
169                         fputs(buffer, f);
170                         return;
171                 }
172                 prefix = buffer;
173         }
174         show_utf8(f, dive->location, prefix,"</location>\n");
175 }
176
177 static void save_overview(FILE *f, struct dive *dive)
178 {
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");
186 }
187
188 static void save_cylinder_info(FILE *f, struct dive *dive)
189 {
190         int i;
191
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;
200
201                 /* No cylinder information at all? */
202                 if (!o2 && !volume && !start && !end)
203                         return;
204                 fprintf(f, "  <cylinder");
205                 if (volume)
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);
210                 if (o2) {
211                         fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
212                         if (he)
213                                 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
214                 }
215                 show_pressure(f, cylinder->start, " start='", "'");
216                 show_pressure(f, cylinder->end, " end='", "'");
217                 fprintf(f, " />\n");
218         }
219 }
220
221 static void save_weightsystem_info(FILE *f, struct dive *dive)
222 {
223         int i;
224
225         for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
226                 weightsystem_t *ws = dive->weightsystem+i;
227                 int grams = ws->weight.grams;
228                 const char *description = ws->description;
229
230                 /* No weight information at all? */
231                 if (grams == 0)
232                         return;
233                 fprintf(f, "  <weightsystem");
234                 show_milli(f, " weight='", grams, " kg", "'");
235                 if (description && *description)
236                         fprintf(f, " description='%s'", description);
237                 fprintf(f, " />\n");
238         }
239 }
240
241 static void show_index(FILE *f, int value, const char *pre, const char *post)
242 {
243         if (value)
244                 fprintf(f, " %s%d%s", pre, value, post);
245 }
246
247 static void save_sample(FILE *f, struct sample *sample)
248 {
249         fprintf(f, "  <sample time='%u:%02u min'", FRACTION(sample->time.seconds,60));
250         show_milli(f, " depth='", sample->depth.mm, " m", "'");
251         show_temperature(f, sample->temperature, " temp='", "'");
252         show_pressure(f, sample->cylinderpressure, " pressure='", "'");
253         if (sample->cylinderindex)
254                 fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
255         fprintf(f, " />\n");
256 }
257
258 static void save_one_event(FILE *f, struct event *ev)
259 {
260         fprintf(f, "  <event time='%d:%02d min'", FRACTION(ev->time.seconds,60));
261         show_index(f, ev->type, "type='", "'");
262         show_index(f, ev->flags, "flags='", "'");
263         show_index(f, ev->value, "value='", "'");
264         show_utf8(f, ev->name, " name='", "'");
265         fprintf(f, " />\n");
266 }
267
268
269 static void save_events(FILE *f, struct event *ev)
270 {
271         while (ev) {
272                 save_one_event(f, ev);
273                 ev = ev->next;
274         }
275 }
276
277 static void save_dive(FILE *f, struct dive *dive)
278 {
279         int i;
280         struct tm *tm = gmtime(&dive->when);
281
282         fputs("<dive", f);
283         if (dive->number)
284                 fprintf(f, " number='%d'", dive->number);
285         if (dive->rating)
286                 fprintf(f, " rating='%d'", dive->rating);
287         fprintf(f, " date='%04u-%02u-%02u'",
288                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
289         fprintf(f, " time='%02u:%02u:%02u'",
290                 tm->tm_hour, tm->tm_min, tm->tm_sec);
291         fprintf(f, " duration='%u:%02u min'>\n",
292                 FRACTION(dive->duration.seconds, 60));
293         save_overview(f, dive);
294         save_cylinder_info(f, dive);
295         save_weightsystem_info(f, dive);
296         save_events(f, dive->events);
297         for (i = 0; i < dive->samples; i++)
298                 save_sample(f, dive->sample+i);
299         fprintf(f, "</dive>\n");
300 }
301
302 #define VERSION 1
303
304 void save_dives(const char *filename)
305 {
306         int i;
307         FILE *f = fopen(filename, "w");
308
309         if (!f)
310                 return;
311
312         /* Flush any edits of current dives back to the dives! */
313         update_dive(current_dive);
314
315         fprintf(f, "<dives>\n<program name='subsurface' version='%d'></program>\n", VERSION);
316         for (i = 0; i < dive_table.nr; i++)
317                 save_dive(f, get_dive(i));
318         fprintf(f, "</dives>\n");
319         fclose(f);
320 }