2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-02-11 19:22:00 +00:00
|
|
|
/* divesite.c */
|
|
|
|
#include "divesite.h"
|
2015-02-12 19:19:05 +00:00
|
|
|
#include "dive.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "subsurface-string.h"
|
2015-09-01 00:45:31 +00:00
|
|
|
#include "divelist.h"
|
2017-02-19 22:11:37 +00:00
|
|
|
#include "membuffer.h"
|
2015-02-12 19:19:05 +00:00
|
|
|
|
2015-06-13 09:54:33 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2015-02-12 19:19:05 +00:00
|
|
|
struct dive_site_table dive_site_table;
|
|
|
|
|
2019-02-26 21:26:11 +00:00
|
|
|
int get_divesite_idx(const struct dive_site *ds, struct dive_site_table *ds_table)
|
2019-02-26 20:14:48 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const struct dive_site *d;
|
2019-03-03 14:18:00 +00:00
|
|
|
// tempting as it may be, don't die when called with ds=NULL
|
2019-02-26 20:14:48 +00:00
|
|
|
if (ds)
|
2019-02-26 21:26:11 +00:00
|
|
|
for_each_dive_site(i, d, ds_table) {
|
2019-03-03 14:18:00 +00:00
|
|
|
if (d == ds)
|
2019-02-26 20:14:48 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-26 21:26:11 +00:00
|
|
|
struct dive_site *get_dive_site_by_uuid(uint32_t uuid, struct dive_site_table *ds_table)
|
2019-02-26 10:03:57 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive_site *ds;
|
2019-02-26 21:26:11 +00:00
|
|
|
for_each_dive_site (i, ds, ds_table)
|
2019-02-26 10:03:57 +00:00
|
|
|
if (ds->uuid == uuid)
|
2019-02-26 21:26:11 +00:00
|
|
|
return get_dive_site(i, ds_table);
|
2019-02-26 10:03:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-13 09:14:33 +00:00
|
|
|
/* there could be multiple sites of the same name - return the first one */
|
2019-02-26 21:26:11 +00:00
|
|
|
struct dive_site *get_dive_site_by_name(const char *name, struct dive_site_table *ds_table)
|
2015-02-13 09:14:33 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive_site *ds;
|
2019-02-26 21:26:11 +00:00
|
|
|
for_each_dive_site (i, ds, ds_table) {
|
2018-10-23 10:42:01 +00:00
|
|
|
if (same_string(ds->name, name))
|
|
|
|
return ds;
|
2015-02-13 09:14:33 +00:00
|
|
|
}
|
2018-10-23 10:42:01 +00:00
|
|
|
return NULL;
|
2015-02-13 09:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* there could be multiple sites at the same GPS fix - return the first one */
|
2019-02-26 21:26:11 +00:00
|
|
|
struct dive_site *get_dive_site_by_gps(const location_t *loc, struct dive_site_table *ds_table)
|
2015-02-13 09:14:33 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive_site *ds;
|
2019-02-26 21:26:11 +00:00
|
|
|
for_each_dive_site (i, ds, ds_table) {
|
2018-10-23 10:42:01 +00:00
|
|
|
if (same_location(loc, &ds->location))
|
|
|
|
return ds;
|
2015-02-13 09:14:33 +00:00
|
|
|
}
|
2018-10-23 10:42:01 +00:00
|
|
|
return NULL;
|
2015-02-13 09:14:33 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 17:10:07 +00:00
|
|
|
/* to avoid a bug where we have two dive sites with different name and the same GPS coordinates
|
|
|
|
* and first get the gps coordinates (reading a V2 file) and happen to get back "the other" name,
|
|
|
|
* this function allows us to verify if a very specific name/GPS combination already exists */
|
2019-02-26 21:26:11 +00:00
|
|
|
struct dive_site *get_dive_site_by_gps_and_name(char *name, const location_t *loc, struct dive_site_table *ds_table)
|
2015-08-30 17:10:07 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive_site *ds;
|
2019-02-26 21:26:11 +00:00
|
|
|
for_each_dive_site (i, ds, ds_table) {
|
2018-10-20 18:12:15 +00:00
|
|
|
if (same_location(loc, &ds->location) && same_string(ds->name, name))
|
2018-10-23 10:42:01 +00:00
|
|
|
return ds;
|
2015-08-30 17:10:07 +00:00
|
|
|
}
|
2018-10-23 10:42:01 +00:00
|
|
|
return NULL;
|
2015-08-30 17:10:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 09:54:33 +00:00
|
|
|
// Calculate the distance in meters between two coordinates.
|
2018-10-20 18:12:15 +00:00
|
|
|
unsigned int get_distance(const location_t *loc1, const location_t *loc2)
|
2015-06-13 09:54:33 +00:00
|
|
|
{
|
2018-10-20 18:12:15 +00:00
|
|
|
double lat2_r = udeg_to_radians(loc2->lat.udeg);
|
|
|
|
double lat_d_r = udeg_to_radians(loc2->lat.udeg - loc1->lat.udeg);
|
|
|
|
double lon_d_r = udeg_to_radians(loc2->lon.udeg - loc1->lon.udeg);
|
2015-06-13 09:54:33 +00:00
|
|
|
|
|
|
|
double a = sin(lat_d_r/2) * sin(lat_d_r/2) +
|
|
|
|
cos(lat2_r) * cos(lat2_r) * sin(lon_d_r/2) * sin(lon_d_r/2);
|
|
|
|
double c = 2 * atan2(sqrt(a), sqrt(1.0 - a));
|
|
|
|
|
|
|
|
// Earth radious in metres
|
2017-03-09 16:07:30 +00:00
|
|
|
return lrint(6371000 * c);
|
2015-06-13 09:54:33 +00:00
|
|
|
}
|
2015-06-10 18:45:34 +00:00
|
|
|
|
|
|
|
/* find the closest one, no more than distance meters away - if more than one at same distance, pick the first */
|
2019-02-26 21:26:11 +00:00
|
|
|
struct dive_site *get_dive_site_by_gps_proximity(const location_t *loc, int distance, struct dive_site_table *ds_table)
|
2015-06-10 18:45:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
2018-10-23 10:42:01 +00:00
|
|
|
struct dive_site *ds, *res = NULL;
|
2015-06-13 09:54:33 +00:00
|
|
|
unsigned int cur_distance, min_distance = distance;
|
2019-02-26 21:26:11 +00:00
|
|
|
for_each_dive_site (i, ds, ds_table) {
|
2015-06-10 18:45:34 +00:00
|
|
|
if (dive_site_has_gps_location(ds) &&
|
2018-10-20 18:12:15 +00:00
|
|
|
(cur_distance = get_distance(&ds->location, loc)) < min_distance) {
|
2015-06-10 18:45:34 +00:00
|
|
|
min_distance = cur_distance;
|
2018-10-23 10:42:01 +00:00
|
|
|
res = ds;
|
2015-06-10 18:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 10:42:01 +00:00
|
|
|
return res;
|
2015-06-10 18:45:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
void register_dive_site(struct dive_site *ds)
|
2015-02-12 19:19:05 +00:00
|
|
|
{
|
2019-03-03 14:12:22 +00:00
|
|
|
add_dive_site_to_table(ds, &dive_site_table);
|
|
|
|
}
|
2017-02-19 22:11:37 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
void add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_table)
|
|
|
|
{
|
2019-02-26 21:26:11 +00:00
|
|
|
int nr = ds_table->nr;
|
|
|
|
int allocated = ds_table->allocated;
|
|
|
|
struct dive_site **sites = ds_table->dive_sites;
|
2015-02-12 19:19:05 +00:00
|
|
|
|
2019-03-08 18:37:27 +00:00
|
|
|
/* Take care to never have the same uuid twice. This could happen on
|
|
|
|
* reimport of a log where the dive sites have diverged */
|
|
|
|
while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, ds_table) != NULL) {
|
|
|
|
ds->uuid = rand() & 0xff;
|
|
|
|
ds->uuid |= (rand() & 0xff) << 8;
|
|
|
|
ds->uuid |= (rand() & 0xff) << 16;
|
|
|
|
ds->uuid |= (rand() & 0xff) << 24;
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:19:05 +00:00
|
|
|
if (nr >= allocated) {
|
|
|
|
allocated = (nr + 32) * 3 / 2;
|
|
|
|
sites = realloc(sites, allocated * sizeof(struct dive_site *));
|
|
|
|
if (!sites)
|
|
|
|
exit(1);
|
2019-02-26 21:26:11 +00:00
|
|
|
ds_table->dive_sites = sites;
|
|
|
|
ds_table->allocated = allocated;
|
2015-02-12 19:19:05 +00:00
|
|
|
}
|
2019-03-03 14:12:22 +00:00
|
|
|
sites[nr] = ds;
|
|
|
|
ds_table->nr = nr + 1;
|
|
|
|
}
|
|
|
|
|
2019-03-03 16:10:09 +00:00
|
|
|
struct dive_site *alloc_dive_site()
|
|
|
|
{
|
|
|
|
struct dive_site *ds;
|
|
|
|
ds = calloc(1, sizeof(*ds));
|
|
|
|
if (!ds)
|
|
|
|
exit(1);
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2019-03-03 17:39:12 +00:00
|
|
|
/* when parsing, dive sites are identified by uuid */
|
2019-03-03 14:12:22 +00:00
|
|
|
struct dive_site *alloc_or_get_dive_site(uint32_t uuid, struct dive_site_table *ds_table)
|
|
|
|
{
|
|
|
|
struct dive_site *ds;
|
|
|
|
|
|
|
|
if (uuid && (ds = get_dive_site_by_uuid(uuid, ds_table)) != NULL)
|
|
|
|
return ds;
|
|
|
|
|
2019-03-03 16:10:09 +00:00
|
|
|
ds = alloc_dive_site();
|
2019-03-08 18:37:27 +00:00
|
|
|
ds->uuid = uuid;
|
2019-03-03 16:10:09 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
add_dive_site_to_table(ds, ds_table);
|
|
|
|
|
2015-02-12 19:19:05 +00:00
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2018-10-23 16:31:39 +00:00
|
|
|
int nr_of_dives_at_dive_site(struct dive_site *ds, bool select_only)
|
2015-07-18 20:34:05 +00:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
int nr = 0;
|
|
|
|
struct dive *d;
|
2018-10-23 16:31:39 +00:00
|
|
|
if (!ds)
|
|
|
|
return 0;
|
2015-07-18 20:34:05 +00:00
|
|
|
for_each_dive(j, d) {
|
2018-10-26 15:03:54 +00:00
|
|
|
if (d->dive_site == ds && (!select_only || d->selected)) {
|
2015-07-18 20:34:05 +00:00
|
|
|
nr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2018-10-23 16:55:42 +00:00
|
|
|
bool is_dive_site_used(struct dive_site *ds, bool select_only)
|
2015-07-16 04:25:26 +00:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
bool found = false;
|
|
|
|
struct dive *d;
|
2018-10-23 16:55:42 +00:00
|
|
|
if (!ds)
|
|
|
|
return false;
|
2015-07-16 04:25:26 +00:00
|
|
|
for_each_dive(j, d) {
|
2018-10-26 15:03:54 +00:00
|
|
|
if (d->dive_site == ds && (!select_only || d->selected)) {
|
2015-07-16 04:25:26 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:30:31 +00:00
|
|
|
void free_dive_site(struct dive_site *ds)
|
|
|
|
{
|
2018-10-21 19:48:47 +00:00
|
|
|
if (ds) {
|
|
|
|
free(ds->name);
|
|
|
|
free(ds->notes);
|
|
|
|
free(ds->description);
|
|
|
|
free_taxonomy(&ds->taxonomy);
|
|
|
|
free(ds);
|
|
|
|
}
|
2018-10-14 21:30:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
void unregister_dive_site(struct dive_site *ds)
|
|
|
|
{
|
|
|
|
remove_dive_site_from_table(ds, &dive_site_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_dive_site_from_table(struct dive_site *ds, struct dive_site_table *ds_table)
|
2015-02-13 15:02:26 +00:00
|
|
|
{
|
2019-02-26 21:26:11 +00:00
|
|
|
int nr = ds_table->nr;
|
2015-02-13 15:02:26 +00:00
|
|
|
for (int i = 0; i < nr; i++) {
|
2019-02-26 21:26:11 +00:00
|
|
|
if (ds == get_dive_site(i, ds_table)) {
|
2015-02-13 15:02:26 +00:00
|
|
|
if (nr - 1 > i)
|
2019-02-26 21:26:11 +00:00
|
|
|
memmove(&ds_table->dive_sites[i],
|
|
|
|
&ds_table->dive_sites[i+1],
|
|
|
|
(nr - 1 - i) * sizeof(ds_table->dive_sites[0]));
|
|
|
|
ds_table->nr = nr - 1;
|
2015-02-13 15:02:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
void delete_dive_site(struct dive_site *ds, struct dive_site_table *ds_table)
|
|
|
|
{
|
|
|
|
if (!ds)
|
|
|
|
return;
|
|
|
|
remove_dive_site_from_table(ds, ds_table);
|
|
|
|
free_dive_site(ds);
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:19:05 +00:00
|
|
|
/* allocate a new site and add it to the table */
|
2019-03-03 17:39:12 +00:00
|
|
|
struct dive_site *create_dive_site(const char *name, struct dive_site_table *ds_table)
|
2015-02-12 09:59:16 +00:00
|
|
|
{
|
2019-03-03 17:39:12 +00:00
|
|
|
struct dive_site *ds = alloc_dive_site();
|
2015-02-12 09:59:16 +00:00
|
|
|
ds->name = copy_string(name);
|
2019-03-03 17:39:12 +00:00
|
|
|
add_dive_site_to_table(ds, ds_table);
|
2018-10-23 11:29:04 +00:00
|
|
|
return ds;
|
2015-02-12 09:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* same as before, but with GPS data */
|
2019-03-03 17:39:12 +00:00
|
|
|
struct dive_site *create_dive_site_with_gps(const char *name, const location_t *loc, struct dive_site_table *ds_table)
|
2015-02-12 19:19:05 +00:00
|
|
|
{
|
2019-03-03 17:39:12 +00:00
|
|
|
struct dive_site *ds = alloc_dive_site();
|
2015-02-12 19:19:05 +00:00
|
|
|
ds->name = copy_string(name);
|
2018-10-20 18:12:15 +00:00
|
|
|
ds->location = *loc;
|
2019-03-03 17:39:12 +00:00
|
|
|
add_dive_site_to_table(ds, ds_table);
|
2015-02-12 19:19:05 +00:00
|
|
|
|
2018-10-23 11:29:04 +00:00
|
|
|
return ds;
|
2015-02-12 19:19:05 +00:00
|
|
|
}
|
2015-02-14 06:53:03 +00:00
|
|
|
|
2019-03-03 17:39:12 +00:00
|
|
|
/* if all fields are empty, the dive site is pointless */
|
2015-02-14 06:53:03 +00:00
|
|
|
bool dive_site_is_empty(struct dive_site *ds)
|
|
|
|
{
|
2018-07-07 05:38:01 +00:00
|
|
|
return !ds ||
|
2018-07-11 06:46:02 +00:00
|
|
|
(empty_string(ds->name) &&
|
2018-01-07 10:12:48 +00:00
|
|
|
empty_string(ds->description) &&
|
|
|
|
empty_string(ds->notes) &&
|
2018-10-20 18:12:15 +00:00
|
|
|
!has_location(&ds->location));
|
2015-02-14 06:53:03 +00:00
|
|
|
}
|
2015-06-26 17:40:12 +00:00
|
|
|
|
2017-10-03 05:57:26 +00:00
|
|
|
void copy_dive_site(struct dive_site *orig, struct dive_site *copy)
|
|
|
|
{
|
|
|
|
free(copy->name);
|
|
|
|
free(copy->notes);
|
|
|
|
free(copy->description);
|
|
|
|
|
2018-10-20 18:12:15 +00:00
|
|
|
copy->location = orig->location;
|
2017-10-03 05:57:26 +00:00
|
|
|
copy->name = copy_string(orig->name);
|
|
|
|
copy->notes = copy_string(orig->notes);
|
|
|
|
copy->description = copy_string(orig->description);
|
2018-10-13 09:52:59 +00:00
|
|
|
copy_taxonomy(&orig->taxonomy, ©->taxonomy);
|
2017-10-03 05:57:26 +00:00
|
|
|
}
|
2015-06-26 18:03:34 +00:00
|
|
|
|
2017-02-19 22:11:37 +00:00
|
|
|
static void merge_string(char **a, char **b)
|
|
|
|
{
|
|
|
|
char *s1 = *a, *s2 = *b;
|
|
|
|
|
2017-02-20 01:20:09 +00:00
|
|
|
if (!s2)
|
|
|
|
return;
|
|
|
|
|
2017-02-19 22:11:37 +00:00
|
|
|
if (same_string(s1, s2))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!s1) {
|
|
|
|
*a = strdup(s2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*a = format_string("(%s) or (%s)", s1, s2);
|
|
|
|
free(s1);
|
|
|
|
}
|
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
/* Used to check on import if two dive sites are equivalent.
|
|
|
|
* Since currently no merging is performed, be very conservative
|
|
|
|
* and only consider equal dive sites that are exactly the same.
|
|
|
|
* Taxonomy is not compared, as no taxonomy is generated on
|
|
|
|
* import.
|
|
|
|
*/
|
|
|
|
static bool same_dive_site(const struct dive_site *a, const struct dive_site *b)
|
|
|
|
{
|
|
|
|
return same_string(a->name, b->name)
|
|
|
|
&& same_location(&a->location, &b->location)
|
|
|
|
&& same_string(a->description, b->description)
|
|
|
|
&& same_string(a->notes, b->notes);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dive_site *get_same_dive_site(const struct dive_site *site)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive_site *ds;
|
|
|
|
for_each_dive_site (i, ds, &dive_site_table)
|
|
|
|
if (same_dive_site(ds, site))
|
|
|
|
return ds;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-19 22:11:37 +00:00
|
|
|
void merge_dive_site(struct dive_site *a, struct dive_site *b)
|
|
|
|
{
|
2018-10-20 18:12:15 +00:00
|
|
|
if (!has_location(&a->location)) a->location = b->location;
|
2017-02-19 22:11:37 +00:00
|
|
|
merge_string(&a->name, &b->name);
|
|
|
|
merge_string(&a->notes, &b->notes);
|
|
|
|
merge_string(&a->description, &b->description);
|
|
|
|
|
|
|
|
if (!a->taxonomy.category) {
|
|
|
|
a->taxonomy = b->taxonomy;
|
|
|
|
memset(&b->taxonomy, 0, sizeof(b->taxonomy));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 17:40:41 +00:00
|
|
|
void merge_dive_sites(struct dive_site *ref, struct dive_site *dive_sites[], int count)
|
2015-09-01 00:35:17 +00:00
|
|
|
{
|
2015-09-01 00:45:31 +00:00
|
|
|
int curr_dive, i;
|
|
|
|
struct dive *d;
|
|
|
|
for(i = 0; i < count; i++){
|
2018-10-23 17:40:41 +00:00
|
|
|
if (dive_sites[i] == ref)
|
2015-09-01 00:45:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for_each_dive(curr_dive, d) {
|
2018-10-26 15:03:54 +00:00
|
|
|
if (d->dive_site != dive_sites[i] )
|
2015-09-01 00:45:31 +00:00
|
|
|
continue;
|
2018-10-26 15:03:54 +00:00
|
|
|
d->dive_site = ref;
|
2017-12-19 12:13:06 +00:00
|
|
|
invalidate_dive_cache(d);
|
2015-09-01 00:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 00:35:17 +00:00
|
|
|
|
2015-09-01 01:01:25 +00:00
|
|
|
for(i = 0; i < count; i++) {
|
2018-10-23 17:40:41 +00:00
|
|
|
if (dive_sites[i] == ref)
|
2015-09-01 00:45:31 +00:00
|
|
|
continue;
|
2019-02-26 21:26:11 +00:00
|
|
|
delete_dive_site(dive_sites[i], &dive_site_table);
|
2015-09-01 00:45:31 +00:00
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
2015-09-01 00:35:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 17:39:12 +00:00
|
|
|
struct dive_site *find_or_create_dive_site_with_name(const char *name, struct dive_site_table *ds_table)
|
2015-07-13 18:13:48 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive_site *ds;
|
2019-02-26 21:26:11 +00:00
|
|
|
for_each_dive_site(i,ds, ds_table) {
|
2015-09-23 14:38:38 +00:00
|
|
|
if (same_string(name, ds->name))
|
2015-07-13 18:13:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ds)
|
2018-10-23 11:29:04 +00:00
|
|
|
return ds;
|
2019-03-03 17:39:12 +00:00
|
|
|
return create_dive_site(name, ds_table);
|
2015-07-13 18:13:48 +00:00
|
|
|
}
|
2015-08-24 18:07:57 +00:00
|
|
|
|
2019-02-26 21:26:11 +00:00
|
|
|
void purge_empty_dive_sites(struct dive_site_table *ds_table)
|
2019-01-01 09:45:26 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
struct dive *d;
|
|
|
|
struct dive_site *ds;
|
|
|
|
|
2019-02-26 21:26:11 +00:00
|
|
|
for (i = 0; i < ds_table->nr; i++) {
|
|
|
|
ds = get_dive_site(i, ds_table);
|
2019-01-01 09:45:26 +00:00
|
|
|
if (!dive_site_is_empty(ds))
|
|
|
|
continue;
|
|
|
|
for_each_dive(j, d) {
|
|
|
|
if (d->dive_site == ds)
|
|
|
|
d->dive_site = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:20:29 +00:00
|
|
|
void add_dive_to_dive_site(struct dive *d, struct dive_site *ds)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
if (d->dive_site == ds)
|
|
|
|
return;
|
|
|
|
if (d->dive_site)
|
|
|
|
fprintf(stderr, "Warning: adding dive that already belongs to a dive site to a different site\n");
|
|
|
|
idx = dive_table_get_insertion_index(&ds->dives, d);
|
|
|
|
add_to_dive_table(&ds->dives, idx, d);
|
|
|
|
d->dive_site = ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dive_site *unregister_dive_from_dive_site(struct dive *d)
|
|
|
|
{
|
|
|
|
struct dive_site *ds = d->dive_site;
|
|
|
|
if (!ds)
|
|
|
|
return NULL;
|
|
|
|
remove_dive(&ds->dives, d);
|
|
|
|
d->dive_site = NULL;
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assign arbitrary UUIDs to dive sites. This is called by before writing the dive log to XML or git. */
|
2015-08-24 18:07:57 +00:00
|
|
|
static int compare_sites(const void *_a, const void *_b)
|
|
|
|
{
|
|
|
|
const struct dive_site *a = (const struct dive_site *)*(void **)_a;
|
|
|
|
const struct dive_site *b = (const struct dive_site *)*(void **)_b;
|
|
|
|
return a->uuid > b->uuid ? 1 : a->uuid == b->uuid ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2019-02-26 21:26:11 +00:00
|
|
|
void dive_site_table_sort(struct dive_site_table *ds_table)
|
2015-08-24 18:07:57 +00:00
|
|
|
{
|
2019-02-26 21:26:11 +00:00
|
|
|
qsort(ds_table->dive_sites, ds_table->nr, sizeof(struct dive_site *), compare_sites);
|
2015-08-24 18:07:57 +00:00
|
|
|
}
|