diff options
-rw-r--r-- | core/divesite.c | 17 | ||||
-rw-r--r-- | core/divesite.h | 1 | ||||
-rw-r--r-- | core/save-git.c | 12 | ||||
-rw-r--r-- | core/save-xml.c | 10 |
4 files changed, 24 insertions, 16 deletions
diff --git a/core/divesite.c b/core/divesite.c index 726dec42d..d09597190 100644 --- a/core/divesite.c +++ b/core/divesite.c @@ -346,6 +346,23 @@ struct dive_site *find_or_create_dive_site_with_name(const char *name, timestamp return create_dive_site(name, divetime); } +void purge_empty_dive_sites() +{ + int i, j; + struct dive *d; + struct dive_site *ds; + + for (i = 0; i < dive_site_table.nr; i++) { + ds = get_dive_site(i); + if (!dive_site_is_empty(ds)) + continue; + for_each_dive(j, d) { + if (d->dive_site == ds) + d->dive_site = NULL; + } + } +} + static int compare_sites(const void *_a, const void *_b) { const struct dive_site *a = (const struct dive_site *)*(void **)_a; diff --git a/core/divesite.h b/core/divesite.h index 3ce87128a..1cb0cbeff 100644 --- a/core/divesite.h +++ b/core/divesite.h @@ -73,6 +73,7 @@ void clear_dive_site(struct dive_site *ds); unsigned int get_distance(const location_t *loc1, const location_t *loc2); struct dive_site *find_or_create_dive_site_with_name(const char *name, timestamp_t divetime); void merge_dive_sites(struct dive_site *ref, struct dive_site *dive_sites[], int count); +void purge_empty_dive_sites(); #ifdef __cplusplus } diff --git a/core/save-git.c b/core/save-git.c index 7b903a5e9..cdc0ca980 100644 --- a/core/save-git.c +++ b/core/save-git.c @@ -879,18 +879,12 @@ static void save_divesites(git_repository *repo, struct dir *tree) subdir = new_directory(repo, tree, &dirname); free_buffer(&dirname); + purge_empty_dive_sites(); for (int i = 0; i < dive_site_table.nr; i++) { struct membuffer b = { 0 }; struct dive_site *ds = get_dive_site(i); - if (dive_site_is_empty(ds) || !is_dive_site_used(ds, false)) { - int j; - struct dive *d; - for_each_dive(j, d) { - if (d->dive_site == ds) - d->dive_site = NULL; - } - delete_dive_site(ds); - i--; // since we just deleted that one + if (!is_dive_site_used(ds, false)) { + /* Only write used dive sites */ continue; } else if (ds->name && (strncmp(ds->name, "Auto-created dive", 17) == 0 || diff --git a/core/save-xml.c b/core/save-xml.c index baebb0ad9..e49e186f4 100644 --- a/core/save-xml.c +++ b/core/save-xml.c @@ -592,18 +592,14 @@ void save_dives_buffer(struct membuffer *b, const bool select_only, bool anonymi /* save the dive sites - to make the output consistent let's sort the table, first */ dive_site_table_sort(); + purge_empty_dive_sites(); put_format(b, "<divesites>\n"); for (i = 0; i < dive_site_table.nr; i++) { int j; struct dive *d; struct dive_site *ds = get_dive_site(i); - if (dive_site_is_empty(ds) || !is_dive_site_used(ds, false)) { - for_each_dive(j, d) { - if (d->dive_site == ds) - d->dive_site = NULL; - } - delete_dive_site(ds); - i--; // since we just deleted that one + if (!is_dive_site_used(ds, false)) { + /* Only write used dive sites */ continue; } else if (ds->name && (strncmp(ds->name, "Auto-created dive", 17) == 0 || |