]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Save and parse notes and locations
[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  * Nothing else (and if we ever do this using attributes, we'd need to
48  * quote the quotes we use too).
49  */
50 static void quote(FILE *f, const char *text)
51 {
52         const char *p = text;
53
54         for (;;) {
55                 const char *escape;
56
57                 switch (*p++) {
58                 default:
59                         continue;
60                 case 0:
61                         escape = NULL;
62                         break;
63                 case '<':
64                         escape = "&lt;";
65                         break;
66                 case '>':
67                         escape = "&gt;";
68                         break;
69                 case '&':
70                         escape = "&amp;";
71                         break;
72                 }
73                 fwrite(text, (p - text - 1), 1, f);
74                 if (!escape)
75                         break;
76                 fputs(escape, f);
77                 text = p;
78         }
79 }
80
81 static void show_utf8(FILE *f, const char *text, const char *pre, const char *post)
82 {
83         int len;
84
85         if (!text)
86                 return;
87         while (isspace(*text))
88                 text++;
89         len = strlen(text);
90         if (!len)
91                 return;
92         while (len && isspace(text[len-1]))
93                 len--;
94         /* FIXME! Quoting! */
95         fputs(pre, f);
96         quote(f, text);
97         fputs(post, f);
98 }
99
100 static void save_overview(FILE *f, struct dive *dive)
101 {
102         show_depth(f, dive->maxdepth, "  <maxdepth>", "</maxdepth>\n");
103         show_depth(f, dive->meandepth, "  <meandepth>", "</meandepth>\n");
104         show_temperature(f, dive->airtemp, "  <airtemp>", "</airtemp>\n");
105         show_temperature(f, dive->watertemp, "  <watertemp>", "</airtemp>\n");
106         show_duration(f, dive->duration, "  <duration>", "</duration>\n");
107         show_duration(f, dive->surfacetime, "  <surfacetime>", "</surfacetime>\n");
108         show_pressure(f, dive->beginning_pressure, "  <cylinderstartpressure>", "</cylinderstartpressure>\n");
109         show_pressure(f, dive->end_pressure, "  <cylinderendpressure>", "</cylinderendpressure>\n");
110         show_utf8(f, dive->location, "  <location>","</location>\n");
111         show_utf8(f, dive->notes, "  <notes>","</notes>\n");
112 }
113
114 static void save_gasmix(FILE *f, struct dive *dive)
115 {
116         int i;
117
118         for (i = 0; i < MAX_MIXES; i++) {
119                 gasmix_t *mix = dive->gasmix+i;
120                 int o2 = mix->o2.permille, he = mix->he.permille;
121                 int n2 = 1000 - o2 - he;
122
123                 if (!mix->o2.permille)
124                         return;
125                 fprintf(f, "  <gasmix o2='%u.%u%%'", FRACTION(o2, 10));
126                 if (mix->he.permille)
127                         fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
128                 fprintf(f, " n2='%u.%u%%' />\n", FRACTION(n2, 10));
129         }
130 }
131
132 static void save_sample(FILE *f, struct sample *sample)
133 {
134         fprintf(f, "  <sample time='%u:%02u min' depth='%u.%03u m'",
135                 FRACTION(sample->time.seconds,60),
136                 FRACTION(sample->depth.mm, 1000));
137         show_temperature(f, sample->temperature, " temp='", "'");
138         show_pressure(f, sample->tankpressure, " pressure='", "'");
139         if (sample->tankindex)
140                 fprintf(f, " tankindex='%d'", sample->tankindex);
141         fprintf(f, " />\n");
142 }
143
144 static void save_dive(FILE *f, struct dive *dive)
145 {
146         int i;
147         struct tm *tm = gmtime(&dive->when);
148
149         fprintf(f, "<dive date='%04u-%02u-%02u' time='%02u:%02u:%02u'>\n",
150                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
151                 tm->tm_hour, tm->tm_min, tm->tm_sec);
152         save_overview(f, dive);
153         save_gasmix(f, dive);
154         for (i = 0; i < dive->samples; i++)
155                 save_sample(f, dive->sample+i);
156         fprintf(f, "</dive>\n");
157 }
158
159 #define VERSION 1
160
161 void save_dives(const char *filename)
162 {
163         int i;
164         FILE *f = fopen(filename, "w");
165
166         if (!f)
167                 return;
168
169         /* Flush any edits of current dives back to the dives! */
170         flush_dive_info_changes();
171
172         fprintf(f, "<dives>\n<program name='diveclog' version='%d'></program>\n", VERSION);
173         for (i = 0; i < dive_table.nr; i++)
174                 save_dive(f, get_dive(i));
175         fprintf(f, "</dives>\n");
176 }