summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2011-10-05 08:31:31 -0700
committerGravatar Linus Torvalds <torvalds@linux-foundation.org>2011-10-05 08:31:31 -0700
commitd6c2236b8a9e20d22d78de7432f7017a18235619 (patch)
treeaa2c73f2afd72c0d49ffa585dc53d0e4748f4635 /main.c
parentf4820455e2019c1a9deba7e90ed7f06111a4cb39 (diff)
downloadsubsurface-d6c2236b8a9e20d22d78de7432f7017a18235619.tar.gz
Automatically renumber new dives when they are "obvious".
When importing (or reading xml from files) new dives, we now renumber them based on preexisting dive data, *if* such re-numbering is obvious. NOTE! In order to be "obvious", there can be no overlap between old and new dives: all the new dives have to come at the end. That's what happens with a normal libdivecomputer import, since we cut the import short when we find a preexisting dive. But if any of the new dives overlap the old dives in any way, or already have been numbered separately, the automatic renumbering is not done, and you need to do a manual renumber. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'main.c')
-rw-r--r--main.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/main.c b/main.c
index d259b3888..be52dbd97 100644
--- a/main.c
+++ b/main.c
@@ -45,12 +45,70 @@ const char *monthname(int mon)
}
/*
+ * When adding dives to the dive table, we try to renumber
+ * the new dives based on any old dives in the dive table.
+ *
+ * But we only do it if:
+ *
+ * - the last dive in the old dive table was numbered
+ *
+ * - all the new dives are strictly at the end (so the
+ * "last dive" is at the same location in the dive table
+ * after re-sorting the dives.
+ *
+ * - none of the new dives have any numbers
+ *
+ * This catches the common case of importing new dives from
+ * a dive computer, and gives them proper numbers based on
+ * your old dive list. But it tries to be very conservative
+ * and not give numbers if there is *any* question about
+ * what the numbers should be - in which case you need to do
+ * a manual re-numbering.
+ */
+static void try_to_renumber(struct dive *last, int preexisting)
+{
+ int i, nr;
+
+ /*
+ * If the new dives aren't all strictly at the end,
+ * we're going to expect the user to do a manual
+ * renumbering.
+ */
+ if (get_dive(preexisting-1) != last)
+ return;
+
+ /*
+ * If any of the new dives already had a number,
+ * we'll have to do a manual renumbering.
+ */
+ for (i = preexisting; i < dive_table.nr; i++) {
+ struct dive *dive = get_dive(i);
+ if (dive->number)
+ return;
+ }
+
+ /*
+ * Ok, renumber..
+ */
+ nr = last->number;
+ for (i = preexisting; i < dive_table.nr; i++) {
+ struct dive *dive = get_dive(i);
+ dive->number = ++nr;
+ }
+}
+
+/*
* This doesn't really report anything at all. We just sort the
* dives, the GUI does the reporting
*/
void report_dives(void)
{
int i;
+ int preexisting = dive_table.preexisting;
+ struct dive *last;
+
+ /* This does the right thing for -1: NULL */
+ last = get_dive(preexisting-1);
qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
@@ -77,6 +135,11 @@ void report_dives(void)
i--;
}
+ /* Was the previous dive table state numbered? */
+ if (last && last->number)
+ try_to_renumber(last, preexisting);
+
+ dive_table.preexisting = dive_table.nr;
dive_list_update_dives();
}