]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Don't bother saving n2 percentage
[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 pressure = cylinder->type.workingpressure.mbar;
131                 int o2 = cylinder->gasmix.o2.permille;
132                 int he = cylinder->gasmix.he.permille;
133
134                 /* No cylinder information at all? */
135                 if (!o2 && !volume)
136                         return;
137                 fprintf(f, "  <cylinder");
138                 if (o2) {
139                         fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
140                         if (he)
141                                 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
142                 }
143                 if (volume) {
144                         fprintf(f, " size='%u.%03u l'", FRACTION(volume, 1000));
145                         if (pressure)
146                                 fprintf(f, " workpressure='%u.%03u bar'", FRACTION(pressure, 1000));
147                 }
148                 fprintf(f, " />\n");
149         }
150 }
151
152 static void save_sample(FILE *f, struct sample *sample)
153 {
154         fprintf(f, "  <sample time='%u:%02u min' depth='%u.%03u m'",
155                 FRACTION(sample->time.seconds,60),
156                 FRACTION(sample->depth.mm, 1000));
157         show_temperature(f, sample->temperature, " temp='", "'");
158         show_pressure(f, sample->cylinderpressure, " pressure='", "'");
159         if (sample->cylinderindex)
160                 fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
161         fprintf(f, " />\n");
162 }
163
164 static void save_dive(FILE *f, struct dive *dive)
165 {
166         int i;
167         struct tm *tm = gmtime(&dive->when);
168
169         fprintf(f, "<dive date='%04u-%02u-%02u' time='%02u:%02u:%02u'>\n",
170                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
171                 tm->tm_hour, tm->tm_min, tm->tm_sec);
172         save_overview(f, dive);
173         save_cylinder_info(f, dive);
174         for (i = 0; i < dive->samples; i++)
175                 save_sample(f, dive->sample+i);
176         fprintf(f, "</dive>\n");
177 }
178
179 #define VERSION 1
180
181 void save_dives(const char *filename)
182 {
183         int i;
184         FILE *f = fopen(filename, "w");
185
186         if (!f)
187                 return;
188
189         /* Flush any edits of current dives back to the dives! */
190         flush_dive_info_changes();
191
192         fprintf(f, "<dives>\n<program name='diveclog' version='%d'></program>\n", VERSION);
193         for (i = 0; i < dive_table.nr; i++)
194                 save_dive(f, get_dive(i));
195         fprintf(f, "</dives>\n");
196 }