#include "dive.h"
+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++;
+}
+
/*
* So when we re-calculate maxdepth and meandepth, we will
* not override the old numbers if they are close to the
#define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
#define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
-static int alloc_samples;
-
static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
{
- int nr = dive->samples;
- struct sample *d;
+ struct sample *p = prepare_sample(&dive);
- if (nr >= alloc_samples) {
- alloc_samples = (alloc_samples + 64) * 3 / 2;
- dive = realloc(dive, dive_size(alloc_samples));
- if (!dive)
- return NULL;
- }
- dive->samples = nr+1;
- d = dive->sample + nr;
-
- *d = *sample;
- d->time.seconds = time;
+ if (!p)
+ return NULL;
+ *p = *sample;
+ p->time.seconds = time;
+ finish_sample(dive, p);
return dive;
}
if (a->when != b->when)
return NULL;
- alloc_samples = 5;
- res = malloc(dive_size(alloc_samples));
- if (!res)
- return NULL;
- memset(res, 0, dive_size(alloc_samples));
+ res = alloc_dive();
res->when = a->when;
res->location = merge_text(a->location, b->location);
res->notes = merge_text(a->notes, b->notes);
+ MERGE_MAX(res, a, b, number);
MERGE_MAX(res, a, b, maxdepth.mm);
res->meandepth.mm = 0;
MERGE_MAX(res, a, b, duration.seconds);
depth_t visibility;
temperature_t airtemp, watertemp;
cylinder_t cylinder[MAX_CYLINDERS];
- int samples;
+ int samples, alloc_samples;
struct sample sample[];
};
return sizeof(struct dive) + samples*sizeof(struct sample);
}
+extern struct dive *alloc_dive(void);
+extern void record_dive(struct dive *dive);
+
+extern struct sample *prepare_sample(struct dive **divep);
+extern void finish_sample(struct dive *dive, struct sample *sample);
+
extern struct dive *fixup_dive(struct dive *dive);
extern struct dive *try_to_merge(struct dive *a, struct dive *b);
/*
* Add a dive into the dive_table array
*/
-static void record_dive(struct dive *dive)
+void record_dive(struct dive *dive)
{
int nr = dive_table.nr, allocated = dive_table.allocated;
struct dive **dives = dive_table.dives;
/*
* Dive info as it is being built up..
*/
-static int alloc_samples;
static struct dive *dive;
static struct sample *sample;
static struct tm tm;
*/
static void dive_start(void)
{
- unsigned int size;
-
if (dive)
return;
-
- alloc_samples = 5;
- size = dive_size(alloc_samples);
- dive = malloc(size);
- if (!dive)
- exit(1);
- memset(dive, 0, size);
+ dive = alloc_dive();
memset(&tm, 0, sizeof(tm));
}
static void sample_start(void)
{
- int nr;
-
- if (!dive)
- return;
- nr = dive->samples;
- 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;
- }
- sample = dive->sample + nr;
- memset(sample, 0, sizeof(*sample));
+ sample = prepare_sample(&dive);
event_index = 0;
}
if (!dive)
return;
+ finish_sample(dive, sample);
sample = NULL;
- dive->samples++;
}
static void entry(const char *name, int size, const char *raw)