+void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name)
+{
+ struct event *ev, **p;
+ unsigned int size, len = strlen(name);
+
+ size = sizeof(*ev) + len + 1;
+ ev = malloc(size);
+ if (!ev)
+ return;
+ memset(ev, 0, size);
+ memcpy(ev->name, name, len);
+ ev->time.seconds = time;
+ ev->type = type;
+ ev->flags = flags;
+ ev->value = value;
+ ev->next = NULL;
+
+ p = &dive->events;
+ while (*p)
+ p = &(*p)->next;
+ *p = ev;
+}
+
+double get_depth_units(unsigned int mm, int *frac, const char **units)
+{
+ int decimals;
+ double d;
+ const char *unit;
+
+ switch (output_units.length) {
+ case METERS:
+ d = mm / 1000.0;
+ unit = "m";
+ decimals = d < 20;
+ break;
+ case FEET:
+ d = mm_to_feet(mm);
+ unit = "ft";
+ decimals = 0;
+ break;
+ }
+ if (frac)
+ *frac = decimals;
+ if (units)
+ *units = unit;
+ return d;
+}
+
+struct dive *alloc_dive(void)
+{
+ const int initial_samples = 5;
+ unsigned int size;
+ struct dive *dive;
+
+ size = dive_size(initial_samples);
+ dive = malloc(size);
+ if (!dive)
+ exit(1);
+ memset(dive, 0, size);
+ dive->alloc_samples = initial_samples;
+ return dive;
+}
+
+struct sample *prepare_sample(struct dive **divep)
+{
+ struct dive *dive = *divep;
+ if (dive) {
+ int nr = dive->samples;
+ int alloc_samples = dive->alloc_samples;
+ struct sample *sample;
+ if (nr >= alloc_samples) {
+ unsigned int size;
+
+ alloc_samples = (alloc_samples * 3)/2 + 10;
+ size = dive_size(alloc_samples);
+ dive = realloc(dive, size);
+ if (!dive)
+ return NULL;
+ dive->alloc_samples = alloc_samples;
+ *divep = dive;
+ }
+ sample = dive->sample + nr;
+ memset(sample, 0, sizeof(*sample));
+ return sample;
+ }
+ return NULL;
+}
+
+void finish_sample(struct dive *dive, struct sample *sample)
+{
+ dive->samples++;
+}
+