]> git.tdb.fi Git - ext/subsurface.git/blob - save-xml.c
Save everything in our current dives and samples into the xml file
[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 show_depth(FILE *f, depth_t depth, const char *pre, const char *post)
26 {
27         if (depth.mm)
28                 fprintf(f, "%s%u.%03u%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:%03u%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%s", pre, FRACTION(pressure.mbar, 1000), post);
41 }
42
43 static void save_overview(FILE *f, struct dive *dive)
44 {
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");
53 }
54
55 static void save_gasmix(FILE *f, struct dive *dive)
56 {
57         int i;
58
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;
63
64                 if (!mix->o2.permille)
65                         return;
66                 fprintf(f, "  <gasmix o2='%u.%u%%'", FRACTION(o2, 10));
67                 if (mix->he.permille)
68                         fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
69                 fprintf(f, " n2='%u.%u%%'></gasmix>\n", FRACTION(n2, 10));
70         }
71 }
72
73 static void save_sample(FILE *f, struct sample *sample)
74 {
75         fprintf(f, "  <sample time='%u:%02u' depth='%u.%03u'",
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");
83 }
84
85 static void save_dive(FILE *f, struct dive *dive)
86 {
87         int i;
88         struct tm *tm = gmtime(&dive->when);
89
90         fprintf(f, "<dive date='%02u.%02u.%u' time='%02u:%02u:%02u'>\n",
91                 tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900,
92                 tm->tm_hour, tm->tm_min, tm->tm_sec);
93         save_overview(f, dive);
94         save_gasmix(f, dive);
95         for (i = 0; i < dive->samples; i++)
96                 save_sample(f, dive->sample+i);
97         fprintf(f, "</dive>\n");
98 }
99
100 #define VERSION 1
101
102 void save_dives(const char *filename)
103 {
104         int i;
105         FILE *f = fopen(filename, "w");
106
107         if (!f)
108                 return;
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");
113 }