]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Some simple test dives for the trips code
[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                 case '\'':
101                         escape = "&apos;";
102                         break;
103                 case '\"':
104                         escape = "&quot;";
105                         break;
106                 }
107                 fwrite(text, (p - text - 1), 1, f);
108                 if (!escape)
109                         break;
110                 fputs(escape, f);
111                 text = p;
112         }
113 }
114
115 static void show_utf8(FILE *f, const char *text, const char *pre, const char *post)
116 {
117         int len;
118
119         if (!text)
120                 return;
121         while (isspace(*text))
122                 text++;
123         len = strlen(text);
124         if (!len)
125                 return;
126         while (len && isspace(text[len-1]))
127                 len--;
128         /* FIXME! Quoting! */
129         fputs(pre, f);
130         quote(f, text);
131         fputs(post, f);
132 }
133
134 static void save_depths(FILE *f, struct dive *dive)
135 {
136         /* What's the point of this dive entry again? */
137         if (!dive->maxdepth.mm && !dive->meandepth.mm)
138                 return;
139
140         fputs("  <depth", f);
141         show_depth(f, dive->maxdepth, " max='", "'");
142         show_depth(f, dive->meandepth, " mean='", "'");
143         fputs(" />\n", f);
144 }
145
146 static void save_temperatures(FILE *f, struct dive *dive)
147 {
148         if (!dive->airtemp.mkelvin && !dive->watertemp.mkelvin)
149                 return;
150         fputs("  <temperature", f);
151         show_temperature(f, dive->airtemp, " air='", "'");
152         show_temperature(f, dive->watertemp, " water='", "'");
153         fputs(" />\n", f);
154 }
155
156 static void show_location(FILE *f, struct dive *dive)
157 {
158         char buffer[80];
159         const char *prefix = "  <location>";
160         double latitude = dive->latitude;
161         double longitude = dive->longitude;
162
163         /*
164          * Ok, theoretically I guess you could dive at
165          * exactly 0,0. But we don't support that. So
166          * if you do, just fudge it a bit, and say that
167          * you dove a few meters away.
168          */
169         if (latitude || longitude) {
170                 int len = snprintf(buffer, sizeof(buffer)-4,
171                         "  <location gps='%.12g %.12g'>",
172                         latitude, longitude);
173                 if (!dive->location) {
174                         memcpy(&buffer[len-1], "/>\n", 4);
175                         fputs(buffer, f);
176                         return;
177                 }
178                 prefix = buffer;
179         }
180         show_utf8(f, dive->location, prefix,"</location>\n");
181 }
182
183 static void save_overview(FILE *f, struct dive *dive)
184 {
185         save_depths(f, dive);
186         save_temperatures(f, dive);
187         show_duration(f, dive->surfacetime, "  <surfacetime>", "</surfacetime>\n");
188         show_location(f, dive);
189         show_utf8(f, dive->divemaster, "  <divemaster>","</divemaster>\n");
190         show_utf8(f, dive->buddy, "  <buddy>","</buddy>\n");
191         show_utf8(f, dive->notes, "  <notes>","</notes>\n");
192         show_utf8(f, dive->suit, "  <suit>","</suit>\n");
193 }
194
195 static void save_cylinder_info(FILE *f, struct dive *dive)
196 {
197         int i;
198
199         for (i = 0; i < MAX_CYLINDERS; i++) {
200                 cylinder_t *cylinder = dive->cylinder+i;
201                 int volume = cylinder->type.size.mliter;
202                 const char *description = cylinder->type.description;
203                 int o2 = cylinder->gasmix.o2.permille;
204                 int he = cylinder->gasmix.he.permille;
205                 int start = cylinder->start.mbar;
206                 int end = cylinder->end.mbar;
207
208                 /* No cylinder information at all? */
209                 if (!o2 && !volume && !start && !end)
210                         return;
211                 fprintf(f, "  <cylinder");
212                 if (volume)
213                         show_milli(f, " size='", volume, " l", "'");
214                 show_pressure(f, cylinder->type.workingpressure, " workpressure='", "'");
215                 if (description && *description)
216                         fprintf(f, " description='%s'", description);
217                 if (o2) {
218                         fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
219                         if (he)
220                                 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
221                 }
222                 show_pressure(f, cylinder->start, " start='", "'");
223                 show_pressure(f, cylinder->end, " end='", "'");
224                 fprintf(f, " />\n");
225         }
226 }
227
228 static void save_weightsystem_info(FILE *f, struct dive *dive)
229 {
230         int i;
231
232         for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
233                 weightsystem_t *ws = dive->weightsystem+i;
234                 int grams = ws->weight.grams;
235                 const char *description = ws->description;
236
237                 /* No weight information at all? */
238                 if (grams == 0)
239                         return;
240                 fprintf(f, "  <weightsystem");
241                 show_milli(f, " weight='", grams, " kg", "'");
242                 if (description && *description)
243                         fprintf(f, " description='%s'", description);
244                 fprintf(f, " />\n");
245         }
246 }
247
248 static void show_index(FILE *f, int value, const char *pre, const char *post)
249 {
250         if (value)
251                 fprintf(f, " %s%d%s", pre, value, post);
252 }
253
254 static void save_sample(FILE *f, struct sample *sample)
255 {
256         fprintf(f, "  <sample time='%u:%02u min'", FRACTION(sample->time.seconds,60));
257         show_milli(f, " depth='", sample->depth.mm, " m", "'");
258         show_temperature(f, sample->temperature, " temp='", "'");
259         show_pressure(f, sample->cylinderpressure, " pressure='", "'");
260         if (sample->cylinderindex)
261                 fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
262         fprintf(f, " />\n");
263 }
264
265 static void save_one_event(FILE *f, struct event *ev)
266 {
267         fprintf(f, "  <event time='%d:%02d min'", FRACTION(ev->time.seconds,60));
268         show_index(f, ev->type, "type='", "'");
269         show_index(f, ev->flags, "flags='", "'");
270         show_index(f, ev->value, "value='", "'");
271         show_utf8(f, ev->name, " name='", "'");
272         fprintf(f, " />\n");
273 }
274
275
276 static void save_events(FILE *f, struct event *ev)
277 {
278         while (ev) {
279                 save_one_event(f, ev);
280                 ev = ev->next;
281         }
282 }
283
284 static void save_trip(FILE *f, struct dive *trip)
285 {
286         struct tm *tm = gmtime(&trip->when);
287
288         fprintf(f, "<trip");
289         fprintf(f, " date='%04u-%02u-%02u'",
290                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
291         if (trip->location)
292                 show_utf8(f, trip->location, " location=\'","\'");
293         fprintf(f, " />\n");
294 }
295
296 static void save_dive(FILE *f, struct dive *dive)
297 {
298         int i;
299         struct tm *tm = gmtime(&dive->when);
300
301         fputs("<dive", f);
302         if (dive->number)
303                 fprintf(f, " number='%d'", dive->number);
304         if (dive->tripflag != TF_NONE)
305                 fprintf(f, " tripflag='%s'", tripflag_names[dive->tripflag]);
306         if (dive->rating)
307                 fprintf(f, " rating='%d'", dive->rating);
308         fprintf(f, " date='%04u-%02u-%02u'",
309                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
310         fprintf(f, " time='%02u:%02u:%02u'",
311                 tm->tm_hour, tm->tm_min, tm->tm_sec);
312         fprintf(f, " duration='%u:%02u min'>\n",
313                 FRACTION(dive->duration.seconds, 60));
314         save_overview(f, dive);
315         save_cylinder_info(f, dive);
316         save_weightsystem_info(f, dive);
317         save_events(f, dive->events);
318         for (i = 0; i < dive->samples; i++)
319                 save_sample(f, dive->sample+i);
320         fprintf(f, "</dive>\n");
321 }
322
323 #define VERSION 1
324
325 void save_dives(const char *filename)
326 {
327         int i;
328         GList *trip = NULL;
329
330         FILE *f = fopen(filename, "w");
331
332         if (!f)
333                 return;
334
335         /* Flush any edits of current dives back to the dives! */
336         update_dive(current_dive);
337
338         fprintf(f, "<dives>\n<program name='subsurface' version='%d'></program>\n", VERSION);
339
340         /* save the trips */
341         while ((trip = NEXT_TRIP(trip, dive_trip_list)) != 0)
342                 save_trip(f, trip->data);
343
344         /* save the dives */
345         for (i = 0; i < dive_table.nr; i++)
346                 save_dive(f, get_dive(i));
347         fprintf(f, "</dives>\n");
348         fclose(f);
349 }