8 #include <libxml/tree.h>
11 * Some silly typedefs to make our units very explicit.
13 * Also, the units are chosen so that values can be expressible as
14 * integers, so that we never have FP rounding issues. And they
15 * are small enough that converting to/from imperial units doesn't
18 * We also strive to make '0' a meaningless number saying "not
19 * initialized", since many values are things that may not have
20 * been reported (eg cylinder pressure or temperature from dive
21 * computers that don't support them). But sometimes -1 is an even
22 * more explicit way of saying "not there".
24 * Thus "millibar" for pressure, for example, or "millikelvin" for
25 * temperatures. Doing temperatures in celsius or fahrenheit would
26 * make for loss of precision when converting from one to the other,
27 * and using millikelvin is SI-like but also means that a temperature
28 * of '0' is clearly just a missing temperature or cylinder pressure.
30 * Also strive to use units that can not possibly be mistaken for a
31 * valid value in a "normal" system without conversion. If the max
32 * depth of a dive is '20000', you probably didn't convert from mm on
33 * output, or if the max depth gets reported as "0.2ft" it was either
34 * a really boring dive, or there was some missing input conversion,
35 * and a 60-ft dive got recorded as 60mm.
37 * Doing these as "structs containing value" means that we always
38 * have to explicitly write out those units in order to get at the
39 * actual value. So there is hopefully little fear of using a value
40 * in millikelvin as Fahrenheit by mistake.
42 * We don't actually use these all yet, so maybe they'll change, but
43 * I made a number of types as guidelines.
80 pressure_t workingpressure;
81 const char *description; /* "LP85", "AL72", "AL80", "HP100+" or whatever */
87 pressure_t start, end, sample_start, sample_end;
92 const char *description; /* "integrated", "belt", "ankle" */
95 extern gboolean cylinder_none(void *_data);
96 extern gboolean no_cylinders(cylinder_t *cyl);
97 extern gboolean cylinders_equal(cylinder_t *cyl1, cylinder_t *cyl2);
98 extern gboolean no_weightsystems(weightsystem_t *ws);
99 extern gboolean weightsystems_equal(weightsystem_t *ws1, weightsystem_t *ws2);
101 extern int get_pressure_units(unsigned int mb, const char **units);
102 extern double get_depth_units(unsigned int mm, int *frac, const char **units);
103 extern double get_volume_units(unsigned int ml, int *frac, const char **units);
104 extern double get_temp_units(unsigned int mk, const char **units);
105 extern double get_weight_units(unsigned int grams, int *frac, const char **units);
107 static inline double grams_to_lbs(int grams)
109 return grams / 453.6;
112 static inline int lbs_to_grams(double lbs)
114 return lbs * 453.6 + 0.5;
117 static inline double ml_to_cuft(int ml)
119 return ml / 28316.8466;
122 static inline double cuft_to_l(double cuft)
124 return cuft * 28.3168466;
127 static inline double mm_to_feet(int mm)
129 return mm * 0.00328084;
132 static inline unsigned long feet_to_mm(double feet)
134 return feet * 304.8 + 0.5;
137 static inline int to_feet(depth_t depth)
139 return mm_to_feet(depth.mm) + 0.5;
142 static inline double mkelvin_to_C(int mkelvin)
144 return (mkelvin - 273150) / 1000.0;
147 static inline double mkelvin_to_F(int mkelvin)
149 return mkelvin * 9 / 5000.0 - 459.670;
152 static inline unsigned long F_to_mkelvin(double f)
154 return (f-32) * 1000 / 1.8 + 273150.5;
157 static inline int to_C(temperature_t temp)
161 return mkelvin_to_C(temp.mkelvin) + 0.5;
164 static inline int to_F(temperature_t temp)
168 return mkelvin_to_F(temp.mkelvin) + 0.5;
171 static inline int to_K(temperature_t temp)
175 return (temp.mkelvin + 499)/1000;
178 static inline double psi_to_bar(double psi)
180 return psi / 14.5037738;
183 static inline unsigned long psi_to_mbar(double psi)
185 return psi_to_bar(psi)*1000 + 0.5;
188 static inline int to_PSI(pressure_t pressure)
190 return pressure.mbar * 0.0145037738 + 0.5;
193 static inline double bar_to_atm(double bar)
195 return bar / 1.01325;
198 static inline double to_ATM(pressure_t pressure)
200 return pressure.mbar / 1013.25;
203 static inline int mbar_to_PSI(int mbar)
205 pressure_t p = {mbar};
212 temperature_t temperature;
213 pressure_t cylinderpressure;
218 * Events are currently pretty meaningless. This is
219 * just based on the random data that libdivecomputer
220 * gives us. I'm not sure what a real "architected"
221 * event model would actually look like, but right
222 * now you can associate a list of events with a dive,
223 * and we'll do something about it.
228 int type, flags, value;
232 #define MAX_CYLINDERS (8)
233 #define MAX_WEIGHTSYSTEMS (4)
234 #define W_IDX_PRIMARY 0
235 #define W_IDX_SECONDARY 1
237 typedef enum { TF_NONE, NO_TRIP, IN_TRIP, NUM_TRIPFLAGS } tripflag_t;
238 extern const char *tripflag_names[NUM_TRIPFLAGS];
247 char *divemaster, *buddy;
249 double latitude, longitude;
250 depth_t maxdepth, meandepth;
251 duration_t duration, surfacetime;
253 temperature_t airtemp, watertemp;
254 cylinder_t cylinder[MAX_CYLINDERS];
255 weightsystem_t weightsystem[MAX_WEIGHTSYSTEMS];
258 struct event *events;
259 int samples, alloc_samples;
260 struct sample sample[];
263 extern GList *dive_trip_list;
264 extern gboolean autogroup;
265 /* random threashold: three days without diving -> new trip
266 * this works very well for people who usually dive as part of a trip and don't
267 * regularly dive at a local facility; this is why trips are an optional feature */
268 #define TRIP_THRESHOLD 3600*24*3
270 #define UNGROUPED_DIVE(_dive) ((_dive)->tripflag == NO_TRIP)
271 #define DIVE_IN_TRIP(_dive) ((_dive)->tripflag == IN_TRIP)
272 #define NEXT_TRIP(_entry, _list) ((_entry) ? g_list_next(_entry) : (_list))
273 #define PREV_TRIP(_entry, _list) ((_entry) ? g_list_previous(_entry) : g_list_last(_list))
274 #define DIVE_TRIP(_trip) ((struct dive *)(_trip)->data)
275 #define DIVE_FITS_TRIP(_dive, _dive_trip) ((_dive_trip)->when - TRIP_THRESHOLD <= (_dive)->when)
277 static inline int dive_date_cmp(gconstpointer _a, gconstpointer _b) {
278 return ((struct dive *)(_a))->when - ((struct dive *)(_b))->when;
281 #define FIND_TRIP(_trip, _list) g_list_find_custom((_list), (_trip), dive_date_cmp)
284 static void dump_trip_list(void)
288 while ((p = NEXT_TRIP(p, dive_trip_list))) {
289 struct tm *tm = gmtime(&DIVE_TRIP(p)->when);
290 printf("trip %d to \"%s\" on %04u-%02u-%02u\n", ++i, DIVE_TRIP(p)->location,
291 tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday);
297 /* insert the trip into the list - but ensure you don't have two trips
298 * for the same date; but if you have, make sure you don't keep the
299 * one with less information */
300 static inline GList *insert_trip(struct dive *_trip, GList *_list)
302 GList *result = FIND_TRIP(_trip, _list);
304 if (! DIVE_TRIP(result)->location)
305 DIVE_TRIP(result)->location = _trip->location;
307 result = g_list_insert_sorted((_list), (_trip), dive_date_cmp);
316 * We keep our internal data in well-specified units, but
317 * the input and output may come in some random format. This
318 * keeps track of those units.
320 /* turns out in Win32 PASCAL is defined as a calling convention */
325 enum { METERS, FEET } length;
326 enum { LITER, CUFT } volume;
327 enum { BAR, PSI, PASCAL } pressure;
328 enum { CELSIUS, FAHRENHEIT, KELVIN } temperature;
329 enum { KG, LBS } weight;
332 extern const struct units SI_units, IMPERIAL_units;
333 extern struct units input_units, output_units;
338 int nr, allocated, preexisting;
342 extern struct dive_table dive_table;
344 extern int selected_dive;
345 #define current_dive (get_dive(selected_dive))
347 static inline struct dive *get_dive(unsigned int nr)
349 if (nr >= dive_table.nr || nr < 0)
351 return dive_table.dives[nr];
354 extern void parse_xml_init(void);
355 extern void parse_xml_buffer(const char *url, const char *buf, int size, GError **error);
356 extern void set_filename(const char *filename);
358 extern void parse_file(const char *filename, GError **error);
361 extern xmlDoc *test_xslt_transforms(xmlDoc *doc);
364 extern void show_dive_info(struct dive *);
366 extern void show_dive_equipment(struct dive *, int w_idx);
368 extern void show_dive_stats(struct dive *);
370 extern void update_dive(struct dive *new_dive);
371 extern void save_dives(const char *filename);
373 static inline unsigned int dive_size(int samples)
375 return sizeof(struct dive) + samples*sizeof(struct sample);
378 extern time_t utc_mktime(struct tm *tm);
380 extern struct dive *alloc_dive(void);
381 extern void record_dive(struct dive *dive);
382 extern void delete_dive(struct dive *dive);
384 extern struct sample *prepare_sample(struct dive **divep);
385 extern void finish_sample(struct dive *dive);
387 extern void report_dives(gboolean imported);
388 extern struct dive *fixup_dive(struct dive *dive);
389 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
391 extern void renumber_dives(int nr);
393 extern void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name);
395 /* UI related protopypes */
397 extern void init_ui(int *argcp, char ***argvp);
399 extern void run_ui(void);
400 extern void exit_ui(void);
402 extern void report_error(GError* error);
404 extern void add_cylinder_description(cylinder_type_t *);
405 extern void add_weightsystem_description(weightsystem_t *);
406 extern void add_people(const char *string);
407 extern void add_location(const char *string);
408 extern void add_suit(const char *string);
409 extern void remember_event(const char *eventname);
410 extern void evn_foreach(void (*callback)(const char *, int *, void *), void *data);
412 extern int add_new_dive(struct dive *dive);
413 extern int edit_dive_info(struct dive *dive);
414 extern int edit_multi_dive_info(int idx);
415 extern void dive_list_update_dives(void);
416 extern void flush_divelist(struct dive *dive);
418 #define DIVE_ERROR_PARSE 1
420 const char *weekday(int wday);
421 const char *monthname(int mon);
423 #define UTF8_DEGREE "\xc2\xb0"
424 #define UTF8_SUBSCRIPT_2 "\xe2\x82\x82"
425 #define UTF8_WHITESTAR "\xe2\x98\x86"
426 #define UTF8_BLACKSTAR "\xe2\x98\x85"
427 #define ZERO_STARS UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
428 #define ONE_STARS UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
429 #define TWO_STARS UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
430 #define THREE_STARS UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR
431 #define FOUR_STARS UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR
432 #define FIVE_STARS UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR
433 extern const char *star_strings[];
435 #define AIR_PERMILLE 209