[PATCH 2/2] Replace cylinder_is_used with is_cylinder_used

Anton Lundin glance at acc.umu.se
Thu Jul 17 08:19:19 PDT 2014


is_cylinder_used uses get_cylinder_index as underlaying function that
does the right thing with with respect on how to find the closest
matching cylinder, and handles both types of gaschange events correctly.

Signed-off-by: Anton Lundin <glance at acc.umu.se>
---
 dive.c           |  2 +-
 dive.h           |  2 +-
 divelist.c       |  2 +-
 equipment.c      | 25 -------------------------
 qt-ui/models.cpp |  6 +++---
 statistics.c     |  2 +-
 6 files changed, 7 insertions(+), 32 deletions(-)

diff --git a/dive.c b/dive.c
index b39634d..acab8ff 100644
--- a/dive.c
+++ b/dive.c
@@ -448,7 +448,7 @@ void copy_cylinders(struct dive *s, struct dive *d, bool used_only)
 	if (!s || !d)
 		return;
 	for (i = 0; i < MAX_CYLINDERS; i++)
-		if (!used_only || cylinder_is_used(s, &s->cylinder[i]))
+		if (!used_only || is_cylinder_used(s, i))
 			d->cylinder[i] = s->cylinder[i];
 		else
 			memset(&d->cylinder[i], 0, sizeof(cylinder_t));
diff --git a/dive.h b/dive.h
index da97e01..204cb2f 100644
--- a/dive.h
+++ b/dive.h
@@ -623,7 +623,7 @@ extern void renumber_dives(int start_nr, bool selected_only);
 extern void copy_events(struct divecomputer *s, struct divecomputer *d);
 extern void copy_cylinders(struct dive *s, struct dive *d, bool used_only);
 extern void copy_samples(struct divecomputer *s, struct divecomputer *d);
-extern bool cylinder_is_used(struct dive *d, cylinder_t *cyl);
+extern bool is_cylinder_used(struct dive *dive, int idx);
 extern void fill_default_cylinder(cylinder_t *cyl);
 extern void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int time, int idx);
 extern void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name);
diff --git a/divelist.c b/divelist.c
index 00e9647..929fc42 100644
--- a/divelist.c
+++ b/divelist.c
@@ -117,7 +117,7 @@ void get_dive_gas(struct dive *dive, int *o2_p, int *he_p, int *o2low_p)
 		int o2 = get_o2(&cyl->gasmix);
 		int he = get_he(&cyl->gasmix);
 
-		if (!cylinder_is_used(dive, cyl))
+		if (!is_cylinder_used(dive, i))
 			continue;
 		if (cylinder_none(cyl))
 			continue;
diff --git a/equipment.c b/equipment.c
index 14d02df..8ca9f09 100644
--- a/equipment.c
+++ b/equipment.c
@@ -73,31 +73,6 @@ bool cylinder_none(void *_data)
 	return cylinder_nodata(cyl) && cylinder_nosamples(cyl);
 }
 
-/* look at all dive computers and figure out if this cylinder is used anywhere
- * d has to be a valid dive (test before calling)
- * cyl does not have to be a cylinder that is part of this dive structure */
-bool cylinder_is_used(struct dive *d, cylinder_t *cyl)
-{
-	struct divecomputer *dc = &d->dc;
-	bool same_as_first = gasmix_distance(&cyl->gasmix, &d->cylinder[0].gasmix) < 200;
-	while (dc) {
-		struct event *ev = get_next_event(dc->events, "gaschange");
-		if (same_as_first && (!ev || ev->time.seconds > 30)) {
-			// unless there is a gas change in the first 30 seconds we can
-			// always mark the first cylinder as used
-			return true;
-		}
-		while (ev) {
-			if (gasmix_distance(&cyl->gasmix, get_gasmix_from_event(ev)) < 200)
-				return true;
-
-			ev = get_next_event(ev->next, "gaschange");
-		}
-		dc = dc->next;
-	}
-	return false;
-}
-
 void get_gas_string(const struct gasmix *gasmix, char *text, int len)
 {
 	if (gasmix_is_air(gasmix))
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index b098659..9d43da6 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -308,7 +308,7 @@ void CylindersModel::updateDive()
 	for (int i = 0; i < MAX_CYLINDERS; i++) {
 		if (!cylinder_none(&displayed_dive.cylinder[i]) &&
 		    (prefs.display_unused_tanks ||
-		     cylinder_is_used(&displayed_dive, &displayed_dive.cylinder[i]) ||
+		     is_cylinder_used(&displayed_dive, i) ||
 		     displayed_dive.cylinder[i].manually_added))
 			rows = i + 1;
 	}
@@ -324,7 +324,7 @@ void CylindersModel::copyFromDive(dive *d)
 		return;
 	rows = 0;
 	for (int i = 0; i < MAX_CYLINDERS; i++) {
-		if (!cylinder_none(&d->cylinder[i]) && cylinder_is_used(d, &d->cylinder[i])) {
+		if (!cylinder_none(&d->cylinder[i]) && is_cylinder_used(d, i)) {
 			rows = i + 1;
 		}
 	}
@@ -360,7 +360,7 @@ void CylindersModel::remove(const QModelIndex &index)
 	    ((DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING &&
 	      DivePlannerPointsModel::instance()->tankInUse(cyl->gasmix)) ||
 	     (DivePlannerPointsModel::instance()->currentMode() == DivePlannerPointsModel::NOTHING &&
-	      (cyl->manually_added || cylinder_is_used(&displayed_dive, cyl))))) {
+	      (cyl->manually_added || is_cylinder_used(&displayed_dive, index.row()))))) {
 		QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(
 									tr("Cylinder cannot be removed"),
 									tr("This gas is in use. Only cylinders that are not used in the dive can be removed.")),
diff --git a/statistics.c b/statistics.c
index 68b9cdd..21b386b 100644
--- a/statistics.c
+++ b/statistics.c
@@ -292,7 +292,7 @@ void get_selected_dives_text(char *buffer, int size)
 	}
 }
 
-static bool is_cylinder_used(struct dive *dive, int idx)
+bool is_cylinder_used(struct dive *dive, int idx)
 {
 	struct divecomputer *dc;
 	bool firstGasExplicit = false;
-- 
1.9.1



More information about the subsurface mailing list