]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Merge branch 'compiler-warning' of https://github.com/nathansamson/diveclog
[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_temperature(FILE *f, temperature_t temp, const char *pre, const char *post)
13 {
14         if (temp.mkelvin) {
15                 int mcelsius = temp.mkelvin - 273150;
16                 const char *sign ="";
17                 if (mcelsius < 0) {
18                         sign = "-";
19                         mcelsius = - mcelsius;
20                 }
21                 fprintf(f, "%s%s%u.%03u C%s", pre, sign, FRACTION(mcelsius, 1000), post);
22         }
23 }
24
25 static void show_depth(FILE *f, depth_t depth, const char *pre, const char *post)
26 {
27         if (depth.mm)
28                 fprintf(f, "%s%u.%03u m%s", pre, FRACTION(depth.mm, 1000), post);
29 }
30
31 static void show_duration(FILE *f, duration_t duration, const char *pre, const char *post)
32 {
33         if (duration.seconds)
34                 fprintf(f, "%s%u:%02u min%s", pre, FRACTION(duration.seconds, 60), post);
35 }
36
37 static void show_pressure(FILE *f, pressure_t pressure, const char *pre, const char *post)
38 {
39         if (pressure.mbar)
40                 fprintf(f, "%s%u.%03u bar%s", pre, FRACTION(pressure.mbar, 1000), post);
41 }
42
43 /*
44  * We're outputting utf8 in xml.
45  * We need to quote the characters <, >, &.
46  *
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 '?'.
50  *
51  * Nothing else (and if we ever do this using attributes, we'd need to
52  * quote the quotes we use too).
53  */
54 static void quote(FILE *f, const char *text)
55 {
56         const char *p = text;
57
58         for (;;) {
59                 const char *escape;
60
61                 switch (*p++) {
62                 default:
63                         continue;
64                 case 0:
65                         escape = NULL;
66                         break;
67                 case 1 ... 8:
68                 case 11: case 12:
69                 case 14 ... 31:
70                         escape = "?";
71                         break;
72                 case '<':
73                         escape = "&lt;";
74                         break;
75                 case '>':
76                         escape = "&gt;";
77                         break;
78                 case '&':
79                         escape = "&amp;";
80                         break;
81                 }
82                 fwrite(text, (p - text - 1), 1, f);
83                 if (!escape)
84                         break;
85                 fputs(escape, f);
86                 text = p;
87         }
88 }
89
90 static void show_utf8(FILE *f, const char *text, const char *pre, const char *post)
91 {
92         int len;
93
94         if (!text)
95                 return;
96         while (isspace(*text))
97                 text++;
98         len = strlen(text);
99         if (!len)
100                 return;
101         while (len && isspace(text[len-1]))
102                 len--;
103         /* FIXME! Quoting! */
104         fputs(pre, f);
105         quote(f, text);
106         fputs(post, f);
107 }
108
109 static void save_overview(FILE *f, struct dive *dive)
110 {
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");
121 }
122
123 static void save_cylinder_info(FILE *f, struct dive *dive)
124 {
125         int i;
126
127         for (i = 0; i < MAX_CYLINDERS; i++) {
128                 cylinder_t *cylinder = dive->cylinder+i;
129                 int volume = cylinder->type.size.mliter;
130                 int o2 = cylinder->gasmix.o2.permille;
131                 int he = cylinder->gasmix.he.permille;
132
133                 /* No cylinder information at all? */
134                 if (!o2 && !volume)
135                         return;
136                 fprintf(f, "  <cylinder");
137                 if (o2) {
138                         fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
139                         if (he)
140                                 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
141                 }
142                 if (volume)
143                         fprintf(f, " size='%u.%03u l'", FRACTION(volume, 1000));
144                 fprintf(f, " />\n");
145         }
146 }
147
148 static void save_sample(FILE *f, struct sample *sample)
149 {
150         fprintf(f, "  <sample time='%u:%02u min' depth='%u.%03u m'",
151                 FRACTION(sample->time.seconds,60),
152                 FRACTION(sample->depth.mm, 1000));
153         show_temperature(f, sample->temperature, " temp='", "'");
154         show_pressure(f, sample->cylinderpressure, " pressure='", "'");
155         if (sample->cylinderindex)
156                 fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
157         fprintf(f, " />\n");
158 }
159
160 static void save_dive(FILE *f, struct dive *dive)
161 {
162         int i;
163         struct tm *tm = gmtime(&dive->when);
164
165         fprintf(f, "<dive date='%04u-%02u-%02u' time='%02u:%02u:%02u'>\n",
166                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
167                 tm->tm_hour, tm->tm_min, tm->tm_sec);
168         save_overview(f, dive);
169         save_cylinder_info(f, dive);
170         for (i = 0; i < dive->samples; i++)
171                 save_sample(f, dive->sample+i);
172         fprintf(f, "</dive>\n");
173 }
174
175 #define VERSION 1
176
177 void save_dives(const char *filename)
178 {
179         int i;
180         FILE *f = fopen(filename, "w");
181
182         if (!f)
183                 return;
184
185         /* Flush any edits of current dives back to the dives! */
186         flush_dive_info_changes();
187
188         fprintf(f, "<dives>\n<program name='diveclog' version='%d'></program>\n", VERSION);
189         for (i = 0; i < dive_table.nr; i++)
190                 save_dive(f, get_dive(i));
191         fprintf(f, "</dives>\n");
192         fclose(f);
193 }