X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=dive.c;h=67a78cc8ad309189dc14419501e1209b1997e637;hb=3a77eb85101a5fb1dc186b88a3a02d2ae27690c7;hp=32ea2ffe19a72f240df0638eafb1a9be3770a7d8;hpb=aa416e3c96dfa53db5ae277e72f6a03821c45cac;p=ext%2Fsubsurface.git diff --git a/dive.c b/dive.c index 32ea2ff..67a78cc 100644 --- a/dive.c +++ b/dive.c @@ -1,8 +1,58 @@ +/* dive.c */ +/* maintains the internal dive list structure */ #include #include #include "dive.h" +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; @@ -117,7 +167,6 @@ struct dive *fixup_dive(struct dive *dive) int maxdepth = 0, mintemp = 0; int lastdepth = 0; int lasttemp = 0; - temperature_t *redundant_temp = NULL; for (i = 0; i < dive->samples; i++) { struct sample *sample = dive->sample + i; @@ -141,17 +190,12 @@ struct dive *fixup_dive(struct dive *dive) /* * If we have consecutive identical * temperature readings, throw away - * the redundant ones. We care about - * the "edges" only. + * the redundant ones. */ - if (lasttemp == temp) { - if (redundant_temp) - redundant_temp->mkelvin = 0; - redundant_temp = &sample->temperature; - } else { - redundant_temp = NULL; + if (lasttemp == temp) + sample->temperature.mkelvin = 0; + else lasttemp = temp; - } if (!mintemp || temp < mintemp) mintemp = temp; @@ -177,6 +221,7 @@ struct dive *fixup_dive(struct dive *dive) /* Don't pick a zero for MERGE_MIN() */ #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) +#define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n) static struct dive *add_sample(struct sample *sample, int time, struct dive *dive) { @@ -313,8 +358,10 @@ struct dive *try_to_merge(struct dive *a, struct dive *b) res = alloc_dive(); res->when = a->when; - res->location = merge_text(a->location, b->location); - res->notes = merge_text(a->notes, b->notes); + MERGE_TXT(res, a, b, location); + MERGE_TXT(res, a, b, notes); + MERGE_TXT(res, a, b, buddy); + MERGE_TXT(res, a, b, divemaster); MERGE_MAX(res, a, b, number); MERGE_MAX(res, a, b, maxdepth.mm); res->meandepth.mm = 0;