[PATCH 3/6] Match newly downloaded dives against dive computer information

Linus Torvalds torvalds at linux-foundation.org
Sun Nov 25 11:54:28 PST 2012


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Sat, 24 Nov 2012 17:06:06 -1000
Subject: [PATCH 3/6] 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 <torvalds at linux-foundation.org>
---

This is better, but subsequent patches will actually add things like 
device ID and dive ID fields that we could also use. 

 libdivecomputer.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/libdivecomputer.c b/libdivecomputer.c
index 48680c7ce49c..82b1ba3cd88d 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;
-- 
1.8.0.dirty



More information about the subsurface mailing list