From 5a4640cf44bc08788eb13ab44a5f84ccf75e4a75 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sat, 24 Nov 2012 17:06:06 -1000 Subject: [PATCH] Match newly downloaded dives against dive computer information Now that we have more complete dive computer information, we can use that to match the dives we download, and stop with the hacky "Would we merge this" check. For XML files without the explicit dive computer information, go back to checking the exact dive time. Signed-off-by: Linus Torvalds Signed-off-by: Dirk Hohndel --- libdivecomputer.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/libdivecomputer.c b/libdivecomputer.c index 48680c7ce..82b1ba3cd 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -182,21 +182,37 @@ 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) +{ + if (a->when != b->when) + return 0; + if (a->vendor && b->vendor && strcasecmp(a->vendor, b->vendor)) + return 0; + if (a->product && b->product && strcasecmp(a->product, b->product)) + return 0; + return 1; +} + /* * Check if this dive already existed before the import */ -static int find_dive(struct dive *dive, device_data_t *devdata) +static int find_dive(struct divecomputer *match) { int i; for (i = 0; i < dive_table.preexisting; i++) { struct dive *old = dive_table.dives[i]; + struct divecomputer *dc = &old->dc; - if (dive->when > old->when + 60) - continue; - if (dive->when + 60 < old->when) - continue; - return 1; + /* Old-style dive with no explicit divecomputer information? */ + if (!dc->when && old->when == match->when) + return 1; + + do { + if (match_dc(dc, match)) + return 1; + dc = dc->next; + } while (dc); } return 0; } @@ -314,7 +330,7 @@ static int dive_cb(const unsigned char *data, unsigned int size, dc_parser_destroy(parser); /* If we already saw this dive, abort. */ - if (!devdata->force_download && find_dive(dive, devdata)) + if (!devdata->force_download && find_dive(&dive->dc)) return 0; dive->downloaded = TRUE;