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