10 #define FRACTION(n,x) ((unsigned)(n)/(x)),((unsigned)(n)%(x))
12 static void show_temperature(FILE *f, temperature_t temp, const char *pre, const char *post)
15 int mcelsius = temp.mkelvin - 273150;
19 mcelsius = - mcelsius;
21 fprintf(f, "%s%s%u.%03u%s", pre, sign, FRACTION(mcelsius, 1000), post);
25 static void save_overview(FILE *f, struct dive *dive)
27 fprintf(f, " <maxdepth>%u.%03u m</maxdepth>\n", FRACTION(dive->maxdepth.mm, 1000));
28 show_temperature(f, dive->airtemp, " <airtemp>", " C</airtemp>\n");
29 show_temperature(f, dive->watertemp, " <watertemp>", " C</airtemp>\n");
32 static void save_gasmix(FILE *f, struct dive *dive)
36 for (i = 0; i < MAX_MIXES; i++) {
37 gasmix_t *mix = dive->gasmix+i;
38 int o2 = mix->o2.permille, he = mix->he.permille;
39 int n2 = 1000 - o2 - he;
41 if (!mix->o2.permille)
43 fprintf(f, " <gasmix o2='%u.%u%%'", FRACTION(o2, 10));
45 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
46 fprintf(f, " n2='%u.%u%%'></gasmix>\n", FRACTION(n2, 10));
50 static void save_sample(FILE *f, struct sample *sample)
52 fprintf(f, " <sample time='%u:%02u' depth='%u.%03u'",
53 FRACTION(sample->time.seconds,60),
54 FRACTION(sample->depth.mm, 1000));
55 show_temperature(f, sample->temperature, " temp='", " C'");
56 if (sample->tankpressure.mbar) {
57 fprintf(f, " pressure='%u.%03u bar'",
58 FRACTION(sample->tankpressure.mbar, 1000));
60 fprintf(f, "></sample>\n");
63 static void save_dive(FILE *f, struct dive *dive)
66 struct tm *tm = gmtime(&dive->when);
68 fprintf(f, "<dive date='%02u.%02u.%u' time='%02u:%02u:%02u'>\n",
69 tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900,
70 tm->tm_hour, tm->tm_min, tm->tm_sec);
71 save_overview(f, dive);
73 for (i = 0; i < dive->samples; i++)
74 save_sample(f, dive->sample+i);
75 fprintf(f, "</dive>\n");
80 void save_dives(const char *filename)
83 FILE *f = fopen(filename, "w");
87 fprintf(f, "<dives>\n<program name='diveclog' version='%d'></program>\n", VERSION);
88 for (i = 0; i < dive_table.nr; i++)
89 save_dive(f, get_dive(i));
90 fprintf(f, "</dives>\n");