10 #define FRACTION(n,x) ((unsigned)(n)/(x)),((unsigned)(n)%(x))
12 static void show_milli(FILE *f, const char *pre, int value, const char *unit, const char *post)
24 for (i = 2; i >= 0; i--) {
25 buf[i] = (v % 10) + '0';
35 fprintf(f, "%u.%s%s%s", v, buf, unit, post);
38 static void show_temperature(FILE *f, temperature_t temp, const char *pre, const char *post)
41 show_milli(f, pre, temp.mkelvin - 273150, " C", post);
44 static void show_depth(FILE *f, depth_t depth, const char *pre, const char *post)
47 show_milli(f, pre, depth.mm, " m", post);
50 static void show_duration(FILE *f, duration_t duration, const char *pre, const char *post)
53 fprintf(f, "%s%u:%02u min%s", pre, FRACTION(duration.seconds, 60), post);
56 static void show_pressure(FILE *f, pressure_t pressure, const char *pre, const char *post)
59 show_milli(f, pre, pressure.mbar, " bar", post);
63 * We're outputting utf8 in xml.
64 * We need to quote the characters <, >, &.
66 * Technically I don't think we'd necessarily need to quote the control
67 * characters, but at least libxml2 doesn't like them. It doesn't even
68 * allow them quoted. So we just skip them and replace them with '?'.
70 * Nothing else (and if we ever do this using attributes, we'd need to
71 * quote the quotes we use too).
73 static void quote(FILE *f, const char *text)
101 fwrite(text, (p - text - 1), 1, f);
109 static void show_utf8(FILE *f, const char *text, const char *pre, const char *post)
115 while (isspace(*text))
120 while (len && isspace(text[len-1]))
122 /* FIXME! Quoting! */
128 static void save_overview(FILE *f, struct dive *dive)
130 show_depth(f, dive->maxdepth, " <maxdepth>", "</maxdepth>\n");
131 show_depth(f, dive->meandepth, " <meandepth>", "</meandepth>\n");
132 show_temperature(f, dive->airtemp, " <airtemp>", "</airtemp>\n");
133 show_temperature(f, dive->watertemp, " <watertemp>", "</watertemp>\n");
134 show_duration(f, dive->duration, " <duration>", "</duration>\n");
135 show_duration(f, dive->surfacetime, " <surfacetime>", "</surfacetime>\n");
136 show_pressure(f, dive->beginning_pressure, " <cylinderstartpressure>", "</cylinderstartpressure>\n");
137 show_pressure(f, dive->end_pressure, " <cylinderendpressure>", "</cylinderendpressure>\n");
138 show_utf8(f, dive->location, " <location>","</location>\n");
139 show_utf8(f, dive->notes, " <notes>","</notes>\n");
142 static void save_cylinder_info(FILE *f, struct dive *dive)
146 for (i = 0; i < MAX_CYLINDERS; i++) {
147 cylinder_t *cylinder = dive->cylinder+i;
148 int volume = cylinder->type.size.mliter;
149 const char *description = cylinder->type.description;
150 int o2 = cylinder->gasmix.o2.permille;
151 int he = cylinder->gasmix.he.permille;
153 /* No cylinder information at all? */
156 fprintf(f, " <cylinder");
158 fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
160 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
163 show_milli(f, " size='", volume, " l", "'");
165 fprintf(f, " description='%s'", description);
170 static void save_sample(FILE *f, struct sample *sample)
172 fprintf(f, " <sample time='%u:%02u min'", FRACTION(sample->time.seconds,60));
173 show_milli(f, " depth='", sample->depth.mm, " m", "'");
174 show_temperature(f, sample->temperature, " temp='", "'");
175 show_pressure(f, sample->cylinderpressure, " pressure='", "'");
176 if (sample->cylinderindex)
177 fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
181 static void save_dive(FILE *f, struct dive *dive)
184 struct tm *tm = gmtime(&dive->when);
186 fprintf(f, "<dive date='%04u-%02u-%02u' time='%02u:%02u:%02u'>\n",
187 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
188 tm->tm_hour, tm->tm_min, tm->tm_sec);
189 save_overview(f, dive);
190 save_cylinder_info(f, dive);
191 for (i = 0; i < dive->samples; i++)
192 save_sample(f, dive->sample+i);
193 fprintf(f, "</dive>\n");
198 void save_dives(const char *filename)
201 FILE *f = fopen(filename, "w");
206 /* Flush any edits of current dives back to the dives! */
207 flush_dive_info_changes();
209 fprintf(f, "<dives>\n<program name='diveclog' version='%d'></program>\n", VERSION);
210 for (i = 0; i < dive_table.nr; i++)
211 save_dive(f, get_dive(i));
212 fprintf(f, "</dives>\n");