summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dive.h2
-rw-r--r--libdivecomputer.c3
-rw-r--r--main.c63
3 files changed, 65 insertions, 3 deletions
diff --git a/dive.h b/dive.h
index 40fc5ca2c..5b7f4658b 100644
--- a/dive.h
+++ b/dive.h
@@ -201,7 +201,7 @@ extern struct units input_units, output_units;
extern int verbose;
struct dive_table {
- int nr, allocated;
+ int nr, allocated, preexisting;
struct dive **dives;
};
diff --git a/libdivecomputer.c b/libdivecomputer.c
index 78be2019a..bcf59eeb5 100644
--- a/libdivecomputer.c
+++ b/libdivecomputer.c
@@ -223,7 +223,7 @@ static int find_dive(struct dive *dive, device_data_t *devdata)
{
int i;
- for (i = 0; i < devdata->preexisting; i++) {
+ for (i = 0; i < dive_table.preexisting; i++) {
struct dive *old = dive_table.dives[i];
if (dive->when != old->when)
@@ -333,7 +333,6 @@ static int dive_cb(const unsigned char *data, unsigned int size,
static device_status_t import_device_data(device_t *device, device_data_t *devicedata)
{
- devicedata->preexisting = dive_table.nr;
return device_foreach(device, dive_cb, devicedata);
}
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();
}