From 459696c90c94834b7498a1b20cd6ccb85bfaa9e0 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 16 Dec 2012 10:40:17 -0800 Subject: Use dive ID for matching dives during downloads If we have a dive computer model and dive ID, use that to match newly downloaded dives against the existing dives. Otherwise fall back to "exact date match" again, like we've always done. Signed-off-by: Linus Torvalds Signed-off-by: Dirk Hohndel --- libdivecomputer.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/libdivecomputer.c b/libdivecomputer.c index 51c6a5775..971d70948 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -231,13 +231,32 @@ static int parse_samples(device_data_t *devdata, struct divecomputer *dc, dc_par return dc_parser_samples_foreach(parser, sample_cb, dc); } -static inline int match_dc(struct divecomputer *a, struct divecomputer *b) +static int match_one_dive(struct divecomputer *a, struct dive *dive) { - if (a->when != b->when) - return 0; - if (a->model && b->model && strcasecmp(a->model, b->model)) - return 0; - return 1; + struct divecomputer *b = &dive->dc; + + /* + * Walk the existing dive computer data, + * see if we have a match (or an anti-match: + * the same dive computer but a different + * dive ID). + */ + do { + int match = match_one_dc(a, b); + if (match) + return match > 0; + b = b->next; + } while (b); + + /* Ok, no exact dive computer match. Does the date match? */ + b = &dive->dc; + do { + if (a->when == b->when) + return 1; + b = b->next; + } while (b); + + return 0; } /* @@ -249,17 +268,9 @@ static int find_dive(struct divecomputer *match) for (i = 0; i < dive_table.preexisting; i++) { struct dive *old = dive_table.dives[i]; - struct divecomputer *dc = &old->dc; - /* Old-style dive with no explicit divecomputer information? */ - if (!dc->when && old->when == match->when) + if (match_one_dive(match, old)) return 1; - - do { - if (match_dc(dc, match)) - return 1; - dc = dc->next; - } while (dc); } return 0; } -- cgit v1.2.3-70-g09d2