]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Fix up small details in input/output
[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%s", pre, sign, FRACTION(mcelsius, 1000), post);
22         }
23 }
24
25 static void save_overview(FILE *f, struct dive *dive)
26 {
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");
30 }
31
32 static void save_gasmix(FILE *f, struct dive *dive)
33 {
34         int i;
35
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;
40
41                 if (!mix->o2.permille)
42                         return;
43                 fprintf(f, "  <gasmix o2='%u.%u%%'", FRACTION(o2, 10));
44                 if (mix->he.permille)
45                         fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
46                 fprintf(f, " n2='%u.%u%%'></gasmix>\n", FRACTION(n2, 10));
47         }
48 }
49
50 static void save_sample(FILE *f, struct sample *sample)
51 {
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));
59         }
60         fprintf(f, "></sample>\n");
61 }
62
63 static void save_dive(FILE *f, struct dive *dive)
64 {
65         int i;
66         struct tm *tm = gmtime(&dive->when);
67
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);
72         save_gasmix(f, dive);
73         for (i = 0; i < dive->samples; i++)
74                 save_sample(f, dive->sample+i);
75         fprintf(f, "</dive>\n");
76 }
77
78 #define VERSION 1
79
80 void save_dives(const char *filename)
81 {
82         int i;
83         FILE *f = fopen(filename, "w");
84
85         if (!f)
86                 return;
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");
91 }