]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Fix wrongly nested watertemp xml entry
[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_gasmix(FILE *f, struct dive *dive)
124 {
125         int i;
126
127         for (i = 0; i < MAX_MIXES; i++) {
128                 gasmix_t *mix = dive->gasmix+i;
129                 int o2 = mix->o2.permille, he = mix->he.permille;
130                 int n2 = 1000 - o2 - he;
131
132                 if (!mix->o2.permille)
133                         return;
134                 fprintf(f, "  <gasmix o2='%u.%u%%'", FRACTION(o2, 10));
135                 if (mix->he.permille)
136                         fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
137                 fprintf(f, " n2='%u.%u%%' />\n", FRACTION(n2, 10));
138         }
139 }
140
141 static void save_sample(FILE *f, struct sample *sample)
142 {
143         fprintf(f, "  <sample time='%u:%02u min' depth='%u.%03u m'",
144                 FRACTION(sample->time.seconds,60),
145                 FRACTION(sample->depth.mm, 1000));
146         show_temperature(f, sample->temperature, " temp='", "'");
147         show_pressure(f, sample->tankpressure, " pressure='", "'");
148         if (sample->tankindex)
149                 fprintf(f, " tankindex='%d'", sample->tankindex);
150         fprintf(f, " />\n");
151 }
152
153 static void save_dive(FILE *f, struct dive *dive)
154 {
155         int i;
156         struct tm *tm = gmtime(&dive->when);
157
158         fprintf(f, "<dive date='%04u-%02u-%02u' time='%02u:%02u:%02u'>\n",
159                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
160                 tm->tm_hour, tm->tm_min, tm->tm_sec);
161         save_overview(f, dive);
162         save_gasmix(f, dive);
163         for (i = 0; i < dive->samples; i++)
164                 save_sample(f, dive->sample+i);
165         fprintf(f, "</dive>\n");
166 }
167
168 #define VERSION 1
169
170 void save_dives(const char *filename)
171 {
172         int i;
173         FILE *f = fopen(filename, "w");
174
175         if (!f)
176                 return;
177
178         /* Flush any edits of current dives back to the dives! */
179         flush_dive_info_changes();
180
181         fprintf(f, "<dives>\n<program name='diveclog' version='%d'></program>\n", VERSION);
182         for (i = 0; i < dive_table.nr; i++)
183                 save_dive(f, get_dive(i));
184         fprintf(f, "</dives>\n");
185 }