+/*
+ * simple structure to track the beginning and end tank pressure as
+ * well as the integral of depth over time spent while we have no
+ * pressure reading from the tank */
+typedef struct pr_track_struct pr_track_t;
+struct pr_track_struct {
+ int start;
+ int end;
+ int t_start;
+ int t_end;
+ double pressure_time;
+ pr_track_t *next;
+};
+
+static pr_track_t *pr_track_alloc(int start, int t_start) {
+ pr_track_t *pt = malloc(sizeof(pr_track_t));
+ pt->start = start;
+ pt->t_start = t_start;
+ pt->end = 0;
+ pt->t_end = 0;
+ pt->pressure_time = 0.0;
+ pt->next = NULL;
+ return pt;
+}
+
+/* poor man's linked list */
+static pr_track_t *list_last(pr_track_t *list)
+{
+ pr_track_t *tail = list;
+ if (!tail)
+ return NULL;
+ while (tail->next) {
+ tail = tail->next;
+ }
+ return tail;
+}
+
+static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
+{
+ pr_track_t *tail = list_last(list);
+ if (!tail)
+ return element;
+ tail->next = element;
+ return list;
+}
+
+static void list_free(pr_track_t *list)
+{
+ if (!list)
+ return;
+ list_free(list->next);
+ free(list);
+}
+
+static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
+ pr_track_t **track_pr)
+{
+ pr_track_t *list = NULL;
+ pr_track_t *nlist = NULL;
+ double pt, magic;
+ int cyl, i;
+ struct plot_data *entry;
+ int cur_pr[MAX_CYLINDERS];
+
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
+ cur_pr[cyl] = track_pr[cyl]->start;
+ }
+
+ /* The first two are "fillers", but in case we don't have a sample
+ * at time 0 we need to process the second of them here */
+ for (i = 1; i < pi->nr; i++) {
+ entry = pi->entry + i;
+ if (SENSOR_PRESSURE(entry)) {
+ cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
+ } else {
+ if(!list || list->t_end < entry->sec) {
+ nlist = track_pr[entry->cylinderindex];
+ list = NULL;
+ while (nlist && nlist->t_start <= entry->sec) {
+ list = nlist;
+ nlist = list->next;
+ }
+ /* there may be multiple segments - so
+ * let's assemble the length */
+ nlist = list;
+ pt = list->pressure_time;
+ while (!nlist->end) {
+ nlist = nlist->next;
+ if (!nlist) {
+ /* oops - we have no end pressure,
+ * so this means this is a tank without
+ * gas consumption information */
+ break;
+ }
+ pt += nlist->pressure_time;
+ }
+ if (!nlist) {
+ /* just continue without calculating
+ * interpolated values */
+ list = NULL;
+ continue;
+ }
+ magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt; }
+ if (pt != 0.0) {
+ double cur_pt = (entry->sec - (entry-1)->sec) *
+ (1 + entry->depth / 10000.0);
+ INTERPOLATED_PRESSURE(entry) =
+ cur_pr[entry->cylinderindex] + cur_pt * magic;
+ cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
+ } else
+ INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
+ }
+ }
+}
+
+static int get_cylinder_index(struct dive *dive, struct event *ev)
+{
+ int i;
+
+ /*
+ * Try to find a cylinder that matches the O2 percentage
+ * in the gas change event 'value' field.
+ *
+ * Crazy suunto gas change events. We really should do
+ * this in libdivecomputer or something.
+ */
+ for (i = 0; i < MAX_CYLINDERS; i++) {
+ cylinder_t *cyl = dive->cylinder+i;
+ int o2 = (cyl->gasmix.o2.permille + 5) / 10;
+ if (o2 == ev->value)
+ return i;
+ }
+
+ return 0;
+}
+
+static struct event *get_next_gaschange(struct event *event)
+{
+ while (event) {
+ if (!strcmp(event->name, "gaschange"))
+ return event;
+ event = event->next;
+ }
+ return event;
+}
+
+static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
+{
+ while (i < pi->nr) {
+ struct plot_data *entry = pi->entry+i;
+ if (entry->sec > end)
+ break;
+ if (entry->cylinderindex != cylinderindex) {
+ entry->cylinderindex = cylinderindex;
+ entry->pressure[0] = 0;
+ }
+ i++;
+ }
+ return i;
+}
+
+static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
+{
+ int i = 0, cylinderindex = 0;
+ struct event *ev = get_next_gaschange(dive->events);
+
+ if (!ev)
+ return;
+
+ do {
+ i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
+ cylinderindex = get_cylinder_index(dive, ev);
+ ev = get_next_gaschange(ev->next);
+ } while (ev);
+ set_cylinder_index(pi, i, cylinderindex, ~0u);
+}
+
+/* for computers that track gas changes through events */
+static int count_gas_change_events(struct dive *dive)
+{
+ int count = 0;
+ struct event *ev = get_next_gaschange(dive->events);
+
+ while (ev) {
+ count++;
+ ev = get_next_gaschange(ev->next);
+ }
+ return count;
+}
+