aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/dive.c43
-rw-r--r--core/dive.h2
2 files changed, 30 insertions, 15 deletions
diff --git a/core/dive.c b/core/dive.c
index ed0c5a757..e6b27e0bb 100644
--- a/core/dive.c
+++ b/core/dive.c
@@ -4151,27 +4151,42 @@ bool dive_remove_picture(struct dive *d, const char *filename)
return false;
}
-/* this always acts on the current divecomputer of the current dive */
-void make_first_dc()
+/* clones a dive and moves given dive computer to front */
+struct dive *make_first_dc(const struct dive *d, int dc_number)
{
- 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 */
+ struct dive *res;
+ struct divecomputer *dc, *newdc, *old_dc;
+
+ /* copy the dive */
+ res = alloc_dive();
+ copy_dive(d, res);
+
+ /* make a new unique id, since we still can't handle two equal ids */
+ res->id = dive_getUniqID();
+ invalidate_dive_cache(res);
+
+ if (dc_number == 0)
+ return res;
+
+ dc = &res->dc;
+ newdc = malloc(sizeof(*newdc));
+ old_dc = get_dive_dc(res, dc_number);
/* skip the current DC in the linked list */
- while (dc && dc->next != cur_dc)
- dc = dc->next;
+ for (dc = &res->dc; dc && dc->next != old_dc; dc = dc->next)
+ ;
if (!dc) {
free(newdc);
fprintf(stderr, "data inconsistent: can't find the current DC");
- return;
+ return res;
}
- dc->next = cur_dc->next;
- *newdc = current_dive->dc;
- current_dive->dc = *cur_dc;
- current_dive->dc.next = newdc;
- free(cur_dc);
- invalidate_dive_cache(current_dive);
+ dc->next = old_dc->next;
+ *newdc = res->dc;
+ res->dc = *old_dc;
+ res->dc.next = newdc;
+ free(old_dc);
+
+ return res;
}
/* always acts on the current dive */
diff --git a/core/dive.h b/core/dive.h
index 7aea050b6..ebc127bd7 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -434,7 +434,7 @@ extern unsigned int number_of_computers(const struct dive *dive);
extern struct divecomputer *get_dive_dc(struct dive *dive, int nr);
extern timestamp_t dive_endtime(const struct dive *dive);
-extern void make_first_dc(void);
+extern struct dive *make_first_dc(const struct dive *d, int dc_number);
extern unsigned int count_divecomputers(void);
extern void delete_current_divecomputer(void);
void split_divecomputer(const struct dive *src, int num, struct dive **out1, struct dive **out2);