[PATCH] Make 'get_dive_by_diveid()' work even for non-primary dive computers

Linus Torvalds torvalds at linux-foundation.org
Tue Jan 29 17:12:23 PST 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Wed, 30 Jan 2013 09:25:09 +1100
Subject: [PATCH] Make 'get_dive_by_diveid()' work even for non-primary dive computers

It's only used by the Uemis importer, and Dirk always seems to import
his Uemis data first, so it wasn't very noticeable.  But if the Uemis
data wasn't the first dive computer, it would not find the dive.

Side note: just comparing deviceid is not correct.  We should pass in
the device model too.  But again, that will realistically never really
matter, since non-Uemis importers will generate complex SHA1 hashes of
the dive data for the dive ID, so a collision with the Uemis numbers is
very unlikely.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

Dirk, maybe you would want this to actually return the particular dive 
computer structure pointer, not the dive pointer. But whatever. This just 
makes it iterate over the dive computers.

 dive.h | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/dive.h b/dive.h
index 120393b6bb1f..4132d0c30234 100644
--- a/dive.h
+++ b/dive.h
@@ -440,19 +440,6 @@ static inline struct dive *get_dive(int nr)
 	return dive_table.dives[nr];
 }
 
-static inline struct dive *get_dive_by_diveid(int diveid, int deviceid)
-{
-	int i;
-	for (i = 0; i < dive_table.nr; i++)
-		if (dive_table.dives[i]->dc.diveid == diveid &&
-		    dive_table.dives[i]->dc.deviceid == deviceid)
-			return dive_table.dives[i];
-	return NULL;
-}
-
-/* Check if two dive computer entries are the exact same dive (-1=no/0=maybe/1=yes) */
-extern int match_one_dc(struct divecomputer *a, struct divecomputer *b);
-
 /*
  * Iterate over each dive, with the first parameter being the index
  * iterator variable, and the second one being the dive one.
@@ -463,6 +450,24 @@ extern int match_one_dc(struct divecomputer *a, struct divecomputer *b);
 #define for_each_dive(_i,_x) \
 	for ((_i) = 0; ((_x) = get_dive(_i)) != NULL; (_i)++)
 
+static inline struct dive *get_dive_by_diveid(int diveid, int deviceid)
+{
+	int i;
+	struct dive *dive;
+
+	for_each_dive(i, dive) {
+		struct divecomputer *dc = &dive->dc;
+		do {
+			if (dc->diveid == diveid && dc->deviceid == deviceid)
+				return dive;
+		} while ((dc = dc->next) != NULL);
+	}
+	return NULL;
+}
+
+/* Check if two dive computer entries are the exact same dive (-1=no/0=maybe/1=yes) */
+extern int match_one_dc(struct divecomputer *a, struct divecomputer *b);
+
 extern void parse_xml_init(void);
 extern void parse_xml_buffer(const char *url, const char *buf, int size, GError **error);
 extern void parse_xml_exit(void);
-- 
1.8.1.2.422.g08c0e7f



More information about the subsurface mailing list