]> git.tdb.fi Git - ext/subsurface.git/blob - dive.h
Merge branch 'open-files' of git://github.com/nathansamson/diveclog
[ext/subsurface.git] / dive.h
1 #ifndef DIVE_H
2 #define DIVE_H
3
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include <glib.h>
8
9 /*
10  * Some silly typedefs to make our units very explicit.
11  *
12  * Also, the units are chosen so that values can be expressible as
13  * integers, so that we never have FP rounding issues. And they
14  * are small enough that converting to/from imperial units doesn't
15  * really matter.
16  *
17  * We also strive to make '0' a meaningless number saying "not
18  * initialized", since many values are things that may not have
19  * been reported (eg cylinder pressure or temperature from dive
20  * computers that don't support them). But sometimes -1 is an even
21  * more explicit way of saying "not there".
22  *
23  * Thus "millibar" for pressure, for example, or "millikelvin" for
24  * temperatures. Doing temperatures in celsius or fahrenheit would
25  * make for loss of precision when converting from one to the other,
26  * and using millikelvin is SI-like but also means that a temperature
27  * of '0' is clearly just a missing temperature or cylinder pressure.
28  *
29  * Also strive to use units that can not possibly be mistaken for a
30  * valid value in a "normal" system without conversion. If the max
31  * depth of a dive is '20000', you probably didn't convert from mm on
32  * output, or if the max depth gets reported as "0.2ft" it was either
33  * a really boring dive, or there was some missing input conversion,
34  * and a 60-ft dive got recorded as 60mm.
35  *
36  * Doing these as "structs containing value" means that we always
37  * have to explicitly write out those units in order to get at the
38  * actual value. So there is hopefully little fear of using a value
39  * in millikelvin as Fahrenheit by mistake.
40  *
41  * We don't actually use these all yet, so maybe they'll change, but
42  * I made a number of types as guidelines.
43  */
44 typedef struct {
45         int seconds;
46 } duration_t;
47
48 typedef struct {
49         int mm;
50 } depth_t;
51
52 typedef struct {
53         int mbar;
54 } pressure_t;
55
56 typedef struct {
57         int mkelvin;
58 } temperature_t;
59
60 typedef struct {
61         int mliter;
62 } volume_t;
63
64 typedef struct {
65         int permille;
66 } fraction_t;
67
68 typedef struct {
69         int grams;
70 } weight_t;
71
72 typedef struct {
73         fraction_t o2;
74         fraction_t he;
75 } gasmix_t;
76
77 typedef struct {
78         volume_t size;
79         pressure_t workingpressure;
80         const char *description;        /* "LP85", "AL72", "AL80", "HP100+" or whatever */
81 } cylinder_type_t;
82
83 typedef struct {
84         cylinder_type_t type;
85         gasmix_t gasmix;
86         pressure_t start, end;
87 } cylinder_t;
88
89 static inline int to_feet(depth_t depth)
90 {
91         return depth.mm * 0.00328084 + 0.5;
92 }
93
94 static inline int to_C(temperature_t temp)
95 {
96         if (!temp.mkelvin)
97                 return 0;
98         return (temp.mkelvin - 273150) / 1000;
99 }
100
101 static inline int to_PSI(pressure_t pressure)
102 {
103         return pressure.mbar * 0.0145037738 + 0.5;
104 }
105
106 struct sample {
107         duration_t time;
108         depth_t depth;
109         temperature_t temperature;
110         pressure_t cylinderpressure;
111         int cylinderindex;
112 };
113
114 #define MAX_CYLINDERS (8)
115
116 struct dive {
117         time_t when;
118         char *location;
119         char *notes;
120         depth_t maxdepth, meandepth;
121         duration_t duration, surfacetime;
122         depth_t visibility;
123         temperature_t airtemp, watertemp;
124         cylinder_t cylinder[MAX_CYLINDERS];
125         int samples;
126         struct sample sample[];
127 };
128
129 extern int verbose;
130
131 struct dive_table {
132         int nr, allocated;
133         struct dive **dives;
134 };
135
136 extern struct dive_table dive_table;
137
138 static inline struct dive *get_dive(unsigned int nr)
139 {
140         if (nr >= dive_table.nr)
141                 return NULL;
142         return dive_table.dives[nr];
143 }
144
145 extern void parse_xml_init(void);
146 extern void parse_xml_file(const char *filename, GError **error);
147
148 extern void flush_dive_info_changes(void);
149 extern void save_dives(const char *filename);
150
151 static inline unsigned int dive_size(int samples)
152 {
153         return sizeof(struct dive) + samples*sizeof(struct sample);
154 }
155
156 extern struct dive *fixup_dive(struct dive *dive);
157 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
158
159 #define DIVE_ERROR_PARSE 1
160
161 #endif /* DIVE_H */