[PATCH] Migrate code to for_each_dive and for_each_dc

Anton Lundin glance at acc.umu.se
Tue May 13 15:32:45 PDT 2014


---
 dive.c                 |  5 +----
 divecomputer.cpp       |  5 ++---
 qt-ui/divelistview.cpp |  7 ++-----
 qt-ui/maintab.cpp      |  8 ++++----
 save-xml.c             |  6 +-----
 statistics.c           | 12 +++++-------
 uemis-downloader.c     |  8 ++++----
 7 files changed, 19 insertions(+), 32 deletions(-)

diff --git a/dive.c b/dive.c
index 68f619b..4877109 100644
--- a/dive.c
+++ b/dive.c
@@ -1296,11 +1296,8 @@ static void dc_cylinder_renumber(struct dive *dive, struct divecomputer *dc, int
 static void cylinder_renumber(struct dive *dive, int mapping[])
 {
 	struct divecomputer *dc;
-
-	dc = &dive->dc;
-	do {
+	for_each_dc(dive, dc)
 		dc_cylinder_renumber(dive, dc, mapping);
-	} while ((dc = dc->next) != NULL);
 }
 
 /*
diff --git a/divecomputer.cpp b/divecomputer.cpp
index 6044f1a..c714137 100644
--- a/divecomputer.cpp
+++ b/divecomputer.cpp
@@ -162,9 +162,9 @@ extern "C" void set_dc_nickname(struct dive *dive)
 	if (!dive)
 		return;
 
-	struct divecomputer *dc = &dive->dc;
+	struct divecomputer *dc;
 
-	while (dc) {
+	for_each_dc(dive, dc) {
 		if (dc->model && *dc->model && dc->deviceid &&
 		    !dcList.getExact(dc->model, dc->deviceid)) {
 			// we don't have this one, yet
@@ -181,6 +181,5 @@ extern "C" void set_dc_nickname(struct dive *dive)
 				dcList.addDC(dc->model, dc->deviceid);
 			}
 		}
-		dc = dc->next;
 	}
 }
diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp
index 6fd5dee..95bf3ca 100644
--- a/qt-ui/divelistview.cpp
+++ b/qt-ui/divelistview.cpp
@@ -696,8 +696,7 @@ void DiveListView::deleteDive()
 	// so instead of using the for_each_dive macro I'm using an explicit for loop
 	// to make this easier to understand
 	int lastDiveNr = -1;
-	for (i = 0; i < dive_table.nr; i++) {
-		d = get_dive(i);
+	for_each_dive(i, d) {
 		if (!d->selected)
 			continue;
 		delete_single_dive(i);
@@ -877,8 +876,7 @@ void DiveListView::loadImages()
 		for_each_dive(j, dive) {
 			if (!dive->selected)
 				continue;
-			dc = &(dive->dc);
-			while (dc) {
+			for_each_dc(dive, dc) {
 				when = dc->when ? dc->when : dive->when;
 				duration_s = dc->duration.seconds ? dc->duration.seconds : dive->duration.seconds;
 				if (when - 3600 < imagetime && when + duration_s + 3600 > imagetime) {
@@ -899,7 +897,6 @@ void DiveListView::loadImages()
 					MainWindow::instance()->refreshDisplay();
 					MainWindow::instance()->graphics()->replot();
 				}
-				dc = dc->next;
 			}
 		}
 	}
diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp
index 172e4e2..e7b2efc 100644
--- a/qt-ui/maintab.cpp
+++ b/qt-ui/maintab.cpp
@@ -270,8 +270,8 @@ void MainTab::enableEdition(EditMode newEditMode)
 
 		// We may be editing one or more dives here. backup everything.
 		struct dive *mydive;
-		for (int i = 0; i < dive_table.nr; i++) {
-			mydive = get_dive(i);
+		int i;
+		for_each_dive(i, mydive) {
 			if (!mydive)
 				continue;
 			if (!mydive->selected)
@@ -831,8 +831,8 @@ void MainTab::rejectChanges()
 		}
 
 		struct dive *mydive;
-		for (int i = 0; i < dive_table.nr; i++) {
-			mydive = get_dive(i);
+		int i;
+		for_each_dive(i, mydive) {
 			if (!mydive)
 				continue;
 			if (!mydive->selected)
diff --git a/save-xml.c b/save-xml.c
index ddaa8cd..e52cfdf 100644
--- a/save-xml.c
+++ b/save-xml.c
@@ -392,12 +392,8 @@ void save_one_dive(struct membuffer *b, struct dive *dive)
 	save_weightsystem_info(b, dive);
 	save_dive_temperature(b, dive);
 	/* Save the dive computer data */
-	dc = &dive->dc;
-	do {
+	for_each_dc(dive, dc)
 		save_dc(b, dive, dc);
-		dc = dc->next;
-	} while (dc);
-
 	put_format(b, "</dive>\n");
 }
 
diff --git a/statistics.c b/statistics.c
index 2a96efb..0d418a6 100644
--- a/statistics.c
+++ b/statistics.c
@@ -131,8 +131,7 @@ void process_all_dives(struct dive *dive, struct dive **prev_dive)
 
 	/* this relies on the fact that the dives in the dive_table
 	 * are in chronological order */
-	for (idx = 0; idx < dive_table.nr; idx++) {
-		dp = dive_table.dives[idx];
+	for_each_dive(idx, dp) {
 		if (dive && dp->when == dive->when) {
 			/* that's the one we are showing */
 			if (idx > 0)
@@ -232,8 +231,8 @@ static void get_ranges(char *buffer, int size)
 	int first = -1, last = -1;
 
 	snprintf(buffer, size, "%s", translate("gettextFromC", "for dives #"));
-	for (i = 0; i < dive_table.nr; i++) {
-		struct dive *dive = get_dive(i);
+	struct dive *dive;
+	for_each_dive(i, dive) {
 		if (!dive->selected)
 			continue;
 		if (dive->number < 1) {
@@ -295,12 +294,12 @@ void get_selected_dives_text(char *buffer, int size)
 
 static bool is_gas_used(struct dive *dive, int idx)
 {
-	struct divecomputer *dc = &dive->dc;
+	struct divecomputer *dc;
 	bool firstGasExplicit = false;
 	if (cylinder_none(&dive->cylinder[idx]))
 		return false;
 
-	while (dc) {
+	for_each_dc(dive, dc) {
 		struct event *event = get_next_event(dc->events, "gaschange");
 		while (event) {
 			if (event->time.seconds < 30)
@@ -309,7 +308,6 @@ static bool is_gas_used(struct dive *dive, int idx)
 				return true;
 			event = get_next_event(event->next, "gaschange");
 		}
-		dc = dc->next;
 	}
 	if (idx == 0 && !firstGasExplicit)
 		return true;
diff --git a/uemis-downloader.c b/uemis-downloader.c
index d0c3de1..e98cdab 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -793,14 +793,14 @@ static char *uemis_get_divenr(char *deviceidstr)
 	char divenr[10];
 
 	deviceid = atoi(deviceidstr);
-	for (i = 0; i < dive_table.nr; i++) {
-		struct divecomputer *dc = &dive_table.dives[i]->dc;
-		while (dc) {
+	struct dive *dive;
+	for_each_dive(i, dive) {
+		struct divecomputer *dc;
+		for_each_dc(dive, dc) {
 			if (dc->model && !strcmp(dc->model, "Uemis Zurich") &&
 			    (dc->deviceid == 0 || dc->deviceid == 0x7fffffff || dc->deviceid == deviceid) &&
 			    dc->diveid > maxdiveid)
 				maxdiveid = dc->diveid;
-			dc = dc->next;
 		}
 	}
 	snprintf(divenr, 10, "%d", maxdiveid);
-- 
1.9.1



More information about the subsurface mailing list