summaryrefslogtreecommitdiffstats
path: root/parse-xml.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2012-04-02 22:00:29 -0700
committerGravatar Linus Torvalds <torvalds@linux-foundation.org>2012-04-02 22:00:29 -0700
commit9470f713d05b489fec32457a4d2eba759bc64d7a (patch)
tree9eb10198ed2f90a695f01c7ae7e2029ca7decf31 /parse-xml.c
parent1cbe2444cc6c0c8da9e730561914986506d83770 (diff)
downloadsubsurface-9470f713d05b489fec32457a4d2eba759bc64d7a.tar.gz
Renumber dives when deleting a dive
... but only do it if the numbering of subsequent dives was consecutive to begin with. Note that we do accept unnumbered dives (and will stop the sequence check if we find one), but in order to renumber dives on delete, we require that starting with the dive we delete, the subsequent numbered dives have to be a nice incrementing series. If that is the case, then we fix up that numbering as we delete the dive. Put another way: if the dive numbering was an incrementing sequence before the delete, then it will be a sane incrementing sequence after it too. But if you had missing dives before the delete, we will turn the delete into just another missing dive. The basic rule is that we never renumber any dives unless that renumbering is "obviously correct". It's better to leave old numbers as-is (and expect that the user is going to do an explicit re-numbering operation) than it is to change dive numbers in a sequence that we don't understand. I do suspect that we should possibly check the dive number "backwards" too, but this doesn't do that. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'parse-xml.c')
-rw-r--r--parse-xml.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/parse-xml.c b/parse-xml.c
index 552786b7a..494023387 100644
--- a/parse-xml.c
+++ b/parse-xml.c
@@ -39,6 +39,40 @@ void record_dive(struct dive *dive)
dive_table.nr = nr+1;
}
+static void delete_dive_renumber(struct dive **dives, int i, int nr)
+{
+ struct dive *dive = dives[i];
+ int number = dive->number, j;
+
+ if (!number)
+ return;
+
+ /*
+ * Check that all numbered dives after the deleted
+ * ones are consecutive, return without renumbering
+ * if that is not the case.
+ */
+ for (j = i+1; j < nr; j++) {
+ struct dive *next = dives[j];
+ if (!next->number)
+ break;
+ number++;
+ if (next->number != number)
+ return;
+ }
+
+ /*
+ * Ok, we hit the end of the dives or a unnumbered
+ * dive - renumber.
+ */
+ for (j = i+1 ; j < nr; j++) {
+ struct dive *next = dives[j];
+ if (!next->number)
+ break;
+ next->number--;
+ }
+}
+
/*
* Remove a dive from the dive_table array
*/
@@ -55,6 +89,8 @@ void delete_dive(struct dive *dive)
struct dive *d = dives[i];
if (d != dive)
continue;
+ /* should we re-number? */
+ delete_dive_renumber(dives, i, nr);
memmove(dives+i, dives+i+1, sizeof(struct dive *)*(nr-i-1));
dives[nr] = NULL;
dive_table.nr = nr-1;