diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-08-17 20:59:46 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-08-17 13:21:49 -0700 |
commit | f24fe107655acaed8a0765d70100d351e9e38706 (patch) | |
tree | 9bbb88229743782f8b58d36062a82dc4b4574eb0 /core/divelist.c | |
parent | 9ae2a8bf7a0896f8c55e9c9a87b2b48265be62d9 (diff) | |
download | subsurface-f24fe107655acaed8a0765d70100d351e9e38706.tar.gz |
core: fix dive renumbering logic on import
0249e12 split up the dive import logic in multiple steps. Thereby,
the one of the conditions for renumbering the imported dives (is
the last old dive numbered) got messed up: The first number of the
new dive was compared to the total number of old dives, which makes
no sense.
- Simply check for the number of the last existing dive (if any).
- Don't remember the number of old dives - the original table is
not modified anyway.
Fixes #2731
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/divelist.c')
-rw-r--r-- | core/divelist.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/core/divelist.c b/core/divelist.c index f224b3414..a5eaffe06 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -1126,9 +1126,9 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table * { int i, j, nr, start_renumbering_at = 0; struct dive_trip *trip_import, *new_trip; - int preexisting; bool sequence_changed = false; bool new_dive_has_number = false; + bool last_old_dive_is_numbered; /* If the caller didn't pass an import_trip_table because all * dives are tripless, provide a local table. This may be @@ -1176,8 +1176,6 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table * if (!(flags & IMPORT_ADD_TO_NEW_TRIP)) autogroup_dives(import_table, import_trip_table); - preexisting = dive_table.nr; /* Remember old size for renumbering */ - /* If dive sites already exist, use the existing versions. */ for (i = 0; i < import_sites_table->nr; i++) { struct dive_site *new_ds = import_sites_table->dive_sites[i]; @@ -1264,14 +1262,16 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table * /* If new dives were only added at the end, renumber the added dives. * But only if - * - The last dive in the old dive table had a number itself. + * - The last dive in the old dive table had a number itself (if there is a last dive). * - None of the new dives has a number. */ - nr = dive_table.nr > 0 ? dive_table.dives[dive_table.nr - 1]->number : 0; + last_old_dive_is_numbered = dive_table.nr == 0 || dive_table.dives[dive_table.nr - 1]->number > 0; + /* We counted the number of merged dives that were added to dives_to_add. * Skip those. Since sequence_changed is false all added dives are *after* * all merged dives. */ - if (!sequence_changed && nr >= preexisting && !new_dive_has_number) { + if (!sequence_changed && last_old_dive_is_numbered && !new_dive_has_number) { + nr = dive_table.nr > 0 ? dive_table.dives[dive_table.nr - 1]->number : 0; for (i = start_renumbering_at; i < dives_to_add->nr; i++) dives_to_add->dives[i]->number = ++nr; } |