summaryrefslogtreecommitdiffstats
path: root/info.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2012-09-24 12:03:58 -0700
committerGravatar Linus Torvalds <torvalds@linux-foundation.org>2012-09-24 12:03:58 -0700
commit6ae67f96f8e2252b6d32b42708324ab77f8ff0fa (patch)
tree7a72d5ba5bd3b25d50e373622902497893d59d9f /info.c
parent37760d830f516fe6813aabce82b6158249a691ad (diff)
downloadsubsurface-6ae67f96f8e2252b6d32b42708324ab77f8ff0fa.tar.gz
Update cylinder info properly
The "cylinders_equal()/copy_cylinders()" functions were buggered, and only checked (and copied) the cylinder type. That was on purpose, since you do want to be able to change the type of a cylinder without changing the gasmix of the cylinder. HOWEVER, the reverse is also true: you may want to change the gasmix of a cylinder without changing the type. So it's not that the type of the cylinder is special - it's that the type and the gasmix should be considered separately. Do that properly for the equipment editing case. Reported-by: Ďoďo <dodo.sk@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'info.c')
-rw-r--r--info.c85
1 files changed, 81 insertions, 4 deletions
diff --git a/info.c b/info.c
index 96e270459..ce7240c39 100644
--- a/info.c
+++ b/info.c
@@ -519,18 +519,95 @@ static void save_equipment_data(struct dive *dive)
}
}
+/* Empty and NULL compare equal */
+static int same_string(const char *a, const char *b)
+{
+ /* Both NULL or same */
+ if (a == b)
+ return 1;
+ /* Both non-NULL: strcmp */
+ if (a && b)
+ return !strcmp(a, b);
+ /* One non-NULL? Is that one empty? */
+ return !*(a ? a : b);
+}
+
+static int same_type(cylinder_t *dst, cylinder_t *src)
+{
+ return dst->type.size.mliter == src->type.size.mliter &&
+ dst->type.workingpressure.mbar == src->type.workingpressure.mbar &&
+ same_string(dst->type.description, src->type.description);
+}
+
+static void copy_type(cylinder_t *dst, cylinder_t *src)
+{
+ dst->type.size = src->type.size;
+ dst->type.workingpressure = src->type.workingpressure;
+ if (dst->type.description)
+ free((void *)dst->type.description);
+ if (!src->type.description || !*src->type.description)
+ dst->type.description = NULL;
+ else
+ dst->type.description = strdup((char *)src->type.description);
+}
+
+static int same_gasmix(cylinder_t *dst, cylinder_t *src)
+{
+ return !memcmp(&dst->gasmix, &src->gasmix, sizeof(dst->gasmix));
+}
+
+static void copy_gasmix(cylinder_t *dst, cylinder_t *src)
+{
+ memcpy(&dst->gasmix, &src->gasmix, sizeof(dst->gasmix));
+}
+
+static int same_press(cylinder_t *dst, cylinder_t *src)
+{
+ return dst->start.mbar == src->start.mbar &&
+ dst->end.mbar == src->end.mbar;
+}
+
+static void copy_press(cylinder_t *dst, cylinder_t *src)
+{
+ dst->start = src->start;
+ dst->end = src->end;
+}
+
+/*
+ * When we update the cylinder information, we do it individually
+ * by type/gasmix/pressure, so that you can change them separately.
+ *
+ * The rule is: the destination has to be the same as the original
+ * field, and the source has to have changed. If so, we change the
+ * destination field.
+ */
+static void update_cylinder(cylinder_t *dst, cylinder_t *src, cylinder_t *orig)
+{
+ /* Destination type same? Change it */
+ if (same_type(dst, orig) && !same_type(src, orig))
+ copy_type(dst, src);
+
+ /* Destination gasmix same? Change it */
+ if (same_gasmix(dst, orig) && !same_gasmix(src, orig))
+ copy_gasmix(dst, src);
+
+ /* Destination pressures the same? */
+ if (same_press(dst, orig) && !same_press(src, orig))
+ copy_press(dst, src);
+}
+
/* the editing happens on the master dive; we copy the equipment
data if it has changed in the master dive and the other dive
either has no entries for the equipment or the same entries
as the master dive had before it was edited */
void update_equipment_data(struct dive *dive, struct dive *master)
{
+ int i;
+
if (dive == master)
return;
- if ( ! cylinders_equal(remember_cyl, master->cylinder) &&
- (no_cylinders(dive->cylinder) ||
- cylinders_equal(dive->cylinder, remember_cyl)))
- copy_cylinders(master->cylinder, dive->cylinder);
+ for (i = 0; i < MAX_CYLINDERS; i++)
+ update_cylinder(dive->cylinder+i, master->cylinder+i, remember_cyl+i);
if (! weightsystems_equal(remember_ws, master->weightsystem) &&
(no_weightsystems(dive->weightsystem) ||
weightsystems_equal(dive->weightsystem, remember_ws)))