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 show_depth(FILE *f, depth_t depth, const char *pre, const char *post)
28 fprintf(f, "%s%u.%03u%s", pre, FRACTION(depth.mm, 1000), post);
31 static void show_duration(FILE *f, duration_t duration, const char *pre, const char *post)
34 fprintf(f, "%s%u:%02u%s", pre, FRACTION(duration.seconds, 60), post);
37 static void show_pressure(FILE *f, pressure_t pressure, const char *pre, const char *post)
40 fprintf(f, "%s%u.%03u%s", pre, FRACTION(pressure.mbar, 1000), post);
43 static void save_overview(FILE *f, struct dive *dive)
45 show_depth(f, dive->maxdepth, " <maxdepth>", " m</maxdepth>\n");
46 show_depth(f, dive->meandepth, " <meandepth>", " m</meandepth>\n");
47 show_temperature(f, dive->airtemp, " <airtemp>", " C</airtemp>\n");
48 show_temperature(f, dive->watertemp, " <watertemp>", " C</airtemp>\n");
49 show_duration(f, dive->duration, " <duration>", " min</duration>\n");
50 show_duration(f, dive->surfacetime, " <surfacetime>", " min</surfacetime>\n");
51 show_pressure(f, dive->beginning_pressure, " <cylinderstartpressure>", " bar</cylinderstartpressure>\n");
52 show_pressure(f, dive->end_pressure, " <cylinderendpressure>", " bar</cylinderendpressure>\n");
55 static void save_gasmix(FILE *f, struct dive *dive)
59 for (i = 0; i < MAX_MIXES; i++) {
60 gasmix_t *mix = dive->gasmix+i;
61 int o2 = mix->o2.permille, he = mix->he.permille;
62 int n2 = 1000 - o2 - he;
64 if (!mix->o2.permille)
66 fprintf(f, " <gasmix o2='%u.%u%%'", FRACTION(o2, 10));
68 fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
69 fprintf(f, " n2='%u.%u%%'></gasmix>\n", FRACTION(n2, 10));
73 static void save_sample(FILE *f, struct sample *sample)
75 fprintf(f, " <sample time='%u:%02u min' depth='%u.%03u m'",
76 FRACTION(sample->time.seconds,60),
77 FRACTION(sample->depth.mm, 1000));
78 show_temperature(f, sample->temperature, " temp='", " C'");
79 show_pressure(f, sample->tankpressure, " pressure='", " bar'");
80 if (sample->tankindex)
81 fprintf(f, " tankindex='%d'", sample->tankindex);
82 fprintf(f, "></sample>\n");
85 static void save_dive(FILE *f, struct dive *dive)
88 struct tm *tm = gmtime(&dive->when);
90 fprintf(f, "<dive date='%04u-%02u-%02u' time='%02u:%02u:%02u'>\n",
91 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
92 tm->tm_hour, tm->tm_min, tm->tm_sec);
93 save_overview(f, dive);
95 for (i = 0; i < dive->samples; i++)
96 save_sample(f, dive->sample+i);
97 fprintf(f, "</dive>\n");
102 void save_dives(const char *filename)
105 FILE *f = fopen(filename, "w");
109 fprintf(f, "<dives>\n<program name='diveclog' version='%d'></program>\n", VERSION);
110 for (i = 0; i < dive_table.nr; i++)
111 save_dive(f, get_dive(i));
112 fprintf(f, "</dives>\n");