From 0d78fe45c31e2856a68d63235ad6acc8f50312f1 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 13 Dec 2019 06:56:07 +0100 Subject: Core: consider invalid flag when adding dives Adding dives uses the number of the last dive to create a new dive number. Ignore invalid dives. Signed-off-by: Berthold Stoeger --- core/divelist.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/divelist.c b/core/divelist.c index df28bba69..a16563310 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -1268,6 +1268,16 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table * } } +static struct dive *get_last_valid_dive() +{ + int i; + for (i = dive_table.nr - 1; i >= 0; i--) { + if (!dive_table.dives[i]->invalid) + return dive_table.dives[i]; + } + return NULL; +} + /* return the number a dive gets when inserted at the given index. * this function is supposed to be called *before* a dive was added. * this returns: @@ -1277,13 +1287,10 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table * */ int get_dive_nr_at_idx(int idx) { - if (dive_table.nr == 0) + struct dive *last_dive = get_last_valid_dive(dive_table.nr - 1); + if (!last_dive) return 1; - if (idx >= dive_table.nr) { - struct dive *last_dive = get_dive(dive_table.nr - 1); - return last_dive->number ? last_dive->number + 1 : 0; - } - return 0; + return last_dive->number ? last_dive->number + 1 : 0; } void set_dive_nr_for_current_dive() -- cgit v1.2.3-70-g09d2