cleanup: break out event-related code into event.[c|h]

In an effort to reduce the size of dive.h and dive.c, break out
the event related functions. Moreover event-names were handled
by the profile-code, collect that also in the new source files.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-10-25 09:14:16 +01:00 committed by Dirk Hohndel
parent d82a7b8b73
commit 8212acc992
26 changed files with 234 additions and 187 deletions

View file

@ -13,6 +13,7 @@
#include "divelist.h"
#include "divesite.h"
#include "errorhelper.h"
#include "event.h"
#include "qthelper.h"
#include "membuffer.h"
#include "picture.h"
@ -21,7 +22,6 @@
#include "structured_list.h"
#include "fulltext.h"
/* one could argue about the best place to have this variable -
* it's used in the UI, but it seems to make the most sense to have it
* here */
@ -123,58 +123,6 @@ int legacy_format_o2pressures(const struct dive *dive, const struct divecomputer
return o2sensor < 0 ? 256 : o2sensor;
}
int event_is_gaschange(const struct event *ev)
{
return ev->type == SAMPLE_EVENT_GASCHANGE ||
ev->type == SAMPLE_EVENT_GASCHANGE2;
}
bool event_is_divemodechange(const struct event *ev)
{
return same_string(ev->name, "modechange");
}
struct event *create_event(unsigned int time, int type, int flags, int value, const char *name)
{
int gas_index = -1;
struct event *ev;
unsigned int size, len = strlen(name);
size = sizeof(*ev) + len + 1;
ev = malloc(size);
if (!ev)
return NULL;
memset(ev, 0, size);
memcpy(ev->name, name, len);
ev->time.seconds = time;
ev->type = type;
ev->flags = flags;
ev->value = value;
/*
* Expand the events into a sane format. Currently
* just gas switches
*/
switch (type) {
case SAMPLE_EVENT_GASCHANGE2:
/* High 16 bits are He percentage */
ev->gas.mix.he.permille = (value >> 16) * 10;
/* Extension to the GASCHANGE2 format: cylinder index in 'flags' */
/* TODO: verify that gas_index < num_cylinders. */
if (flags > 0)
gas_index = flags-1;
/* Fallthrough */
case SAMPLE_EVENT_GASCHANGE:
/* Low 16 bits are O2 percentage */
ev->gas.mix.o2.permille = (value & 0xffff) * 10;
ev->gas.index = gas_index;
break;
}
return ev;
}
/* warning: does not test idx for validity */
struct event *create_gas_switch_event(struct dive *dive, struct divecomputer *dc, int seconds, int idx)
{
@ -195,11 +143,6 @@ struct event *create_gas_switch_event(struct dive *dive, struct divecomputer *dc
return ev;
}
struct event *clone_event_rename(const struct event *ev, const char *name)
{
return create_event(ev->time.seconds, ev->type, ev->flags, ev->value, name);
}
void add_event_to_dc(struct divecomputer *dc, struct event *ev)
{
struct event **p;
@ -253,19 +196,6 @@ void swap_event(struct divecomputer *dc, struct event *from, struct event *to)
}
}
bool same_event(const struct event *a, const struct event *b)
{
if (a->time.seconds != b->time.seconds)
return 0;
if (a->type != b->type)
return 0;
if (a->flags != b->flags)
return 0;
if (a->value != b->value)
return 0;
return !strcmp(a->name, b->name);
}
/* Remove given event from dive computer. Does *not* free the event. */
void remove_event_from_dc(struct divecomputer *dc, struct event *event)
{
@ -546,22 +476,6 @@ void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_compo
}
#undef CONDITIONAL_COPY_STRING
struct event *clone_event(const struct event *src_ev)
{
struct event *ev;
if (!src_ev)
return NULL;
size_t size = sizeof(*src_ev) + strlen(src_ev->name) + 1;
ev = (struct event*) malloc(size);
if (!ev)
exit(1);
memcpy(ev, src_ev, size);
ev->next = NULL;
return ev;
}
/* copies all events in this dive computer */
void copy_events(const struct divecomputer *s, struct divecomputer *d)
{
@ -2811,15 +2725,6 @@ struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded
return res;
}
void free_events(struct event *ev)
{
while (ev) {
struct event *next = ev->next;
free(ev);
ev = next;
}
}
static void free_extra_data(struct extra_data *ed)
{
free((void *)ed->key);