Allow the user to delete a dive computer from a dive

This can't be the only dive computer, of course. Goes nicely with the
ability to reprder them.

Fixes #551

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-06-11 13:56:33 -07:00
parent c4aa1f542c
commit 2b59765da3
4 changed files with 78 additions and 22 deletions

80
dive.c
View file

@ -20,26 +20,6 @@ static const char *default_tags[] = {
QT_TRANSLATE_NOOP("gettextFromC", "deco")
};
void make_first_dc()
{
struct divecomputer *dc = &current_dive->dc;
struct divecomputer *newdc = malloc(sizeof(*newdc));
struct divecomputer *cur_dc = current_dc; /* needs to be in a local variable so the macro isn't re-executed */
/* skip the current DC in the linked list */
while (dc && dc->next != cur_dc)
dc = dc->next;
if (!dc) {
fprintf(stderr, "data inconsistent: can't find the current DC");
return;
}
dc->next = cur_dc->next;
*newdc = current_dive->dc;
current_dive->dc = *cur_dc;
current_dive->dc.next = newdc;
free(cur_dc);
}
void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name)
{
struct event *ev, **p;
@ -2326,3 +2306,63 @@ void dive_remove_picture(struct dive *d, struct picture *p)
{
}
/* this always acts on the current divecomputer of the current dive */
void make_first_dc()
{
struct divecomputer *dc = &current_dive->dc;
struct divecomputer *newdc = malloc(sizeof(*newdc));
struct divecomputer *cur_dc = current_dc; /* needs to be in a local variable so the macro isn't re-executed */
/* skip the current DC in the linked list */
while (dc && dc->next != cur_dc)
dc = dc->next;
if (!dc) {
fprintf(stderr, "data inconsistent: can't find the current DC");
return;
}
dc->next = cur_dc->next;
*newdc = current_dive->dc;
current_dive->dc = *cur_dc;
current_dive->dc.next = newdc;
free(cur_dc);
}
/* always acts on the current dive */
int count_divecomputers(void)
{
int ret = 1;
struct divecomputer *dc = current_dive->dc.next;
while (dc) {
ret++;
dc = dc->next;
}
return ret;
}
/* always acts on the current dive */
void delete_current_divecomputer(void)
{
struct divecomputer *dc = current_dc;
if (dc == &current_dive->dc) {
/* remove the first one, so copy the second one in place of the first and free the second one
* be careful about freeing the no longer needed structures - since we copy things around we can't use free_dc()*/
struct divecomputer *fdc = dc->next;
free(dc->sample);
free((void *)dc->model);
free_events(dc->events);
memcpy(dc, fdc, sizeof(struct divecomputer));
free(fdc);
} else {
struct divecomputer *pdc = &current_dive->dc;
while (pdc->next != dc && pdc->next)
pdc = pdc->next;
if (pdc->next == dc) {
pdc->next = dc->next;
free_dc(dc);
}
}
if (dc_number == count_divecomputers())
dc_number--;
}