summaryrefslogtreecommitdiffstats
path: root/dive.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2014-06-11 13:56:33 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-06-11 13:56:33 -0700
commit2b59765da3882f1564e5ae034a35c0ce35441108 (patch)
treef4d37700f4ae3ab36b4a60e3195a539e79a7344a /dive.c
parentc4aa1f542ccbbbdc78dd3d44fe08f6410b9a5d2c (diff)
downloadsubsurface-2b59765da3882f1564e5ae034a35c0ce35441108.tar.gz
Allow the user to delete a dive computer from a dive
This can't be the only dive computer, of course. Goes nicely with the ability to reprder them. Fixes #551 Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'dive.c')
-rw-r--r--dive.c80
1 files changed, 60 insertions, 20 deletions
diff --git a/dive.c b/dive.c
index 68b811217..c60f3c819 100644
--- a/dive.c
+++ b/dive.c
@@ -20,26 +20,6 @@ static const char *default_tags[] = {
QT_TRANSLATE_NOOP("gettextFromC", "deco")
};
-void make_first_dc()
-{
- struct divecomputer *dc = &current_dive->dc;
- struct divecomputer *newdc = malloc(sizeof(*newdc));
- struct divecomputer *cur_dc = current_dc; /* needs to be in a local variable so the macro isn't re-executed */
-
- /* skip the current DC in the linked list */
- while (dc && dc->next != cur_dc)
- dc = dc->next;
- if (!dc) {
- fprintf(stderr, "data inconsistent: can't find the current DC");
- return;
- }
- dc->next = cur_dc->next;
- *newdc = current_dive->dc;
- current_dive->dc = *cur_dc;
- current_dive->dc.next = newdc;
- free(cur_dc);
-}
-
void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name)
{
struct event *ev, **p;
@@ -2326,3 +2306,63 @@ void dive_remove_picture(struct dive *d, struct picture *p)
{
}
+
+/* this always acts on the current divecomputer of the current dive */
+void make_first_dc()
+{
+ struct divecomputer *dc = &current_dive->dc;
+ struct divecomputer *newdc = malloc(sizeof(*newdc));
+ struct divecomputer *cur_dc = current_dc; /* needs to be in a local variable so the macro isn't re-executed */
+
+ /* skip the current DC in the linked list */
+ while (dc && dc->next != cur_dc)
+ dc = dc->next;
+ if (!dc) {
+ fprintf(stderr, "data inconsistent: can't find the current DC");
+ return;
+ }
+ dc->next = cur_dc->next;
+ *newdc = current_dive->dc;
+ current_dive->dc = *cur_dc;
+ current_dive->dc.next = newdc;
+ free(cur_dc);
+}
+
+/* always acts on the current dive */
+int count_divecomputers(void)
+{
+ int ret = 1;
+ struct divecomputer *dc = current_dive->dc.next;
+ while (dc) {
+ ret++;
+ dc = dc->next;
+ }
+ return ret;
+}
+
+/* always acts on the current dive */
+void delete_current_divecomputer(void)
+{
+ struct divecomputer *dc = current_dc;
+
+ if (dc == &current_dive->dc) {
+ /* remove the first one, so copy the second one in place of the first and free the second one
+ * be careful about freeing the no longer needed structures - since we copy things around we can't use free_dc()*/
+ struct divecomputer *fdc = dc->next;
+ free(dc->sample);
+ free((void *)dc->model);
+ free_events(dc->events);
+ memcpy(dc, fdc, sizeof(struct divecomputer));
+ free(fdc);
+ } else {
+ struct divecomputer *pdc = &current_dive->dc;
+ while (pdc->next != dc && pdc->next)
+ pdc = pdc->next;
+ if (pdc->next == dc) {
+ pdc->next = dc->next;
+ free_dc(dc);
+ }
+ }
+ if (dc_number == count_divecomputers())
+ dc_number--;
+}