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"
|
2015-09-01 00:45:31 +00:00
|
|
|
#include "divelist.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "divelog.h"
|
2017-02-19 22:11:37 +00:00
|
|
|
#include "membuffer.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "subsurface-string.h"
|
2019-03-10 21:28:14 +00:00
|
|
|
#include "table.h"
|
2019-04-05 19:33:27 +00:00
|
|
|
#include "sha1.h"
|
2015-02-12 19:19:05 +00:00
|
|
|
|
2015-06-13 09:54:33 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
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-03-10 15:03:39 +00:00
|
|
|
// TODO: keep table sorted by UUID and do a binary search?
|
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
|
|
|
{
|
2019-04-30 13:53:31 +00:00
|
|
|
double lat1_r = udeg_to_radians(loc1->lat.udeg);
|
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) +
|
2019-04-30 13:53:31 +00:00
|
|
|
cos(lat1_r) * cos(lat2_r) * sin(lon_d_r/2) * sin(lon_d_r/2);
|
|
|
|
if (a < 0.0) a = 0.0;
|
|
|
|
if (a > 1.0) a = 1.0;
|
2015-06-13 09:54:33 +00:00
|
|
|
double c = 2 * atan2(sqrt(a), sqrt(1.0 - a));
|
|
|
|
|
2019-04-30 13:53:31 +00:00
|
|
|
// Earth radius 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-11 23:25:31 +00:00
|
|
|
int register_dive_site(struct dive_site *ds)
|
2015-02-12 19:19:05 +00:00
|
|
|
{
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
return add_dive_site_to_table(ds, divelog.sites);
|
2019-03-03 14:12:22 +00:00
|
|
|
}
|
2017-02-19 22:11:37 +00:00
|
|
|
|
2019-03-10 21:28:14 +00:00
|
|
|
static int compare_sites(const struct dive_site *a, const struct dive_site *b)
|
|
|
|
{
|
|
|
|
return a->uuid > b->uuid ? 1 : a->uuid == b->uuid ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int site_less_than(const struct dive_site *a, const struct dive_site *b)
|
2019-03-03 14:12:22 +00:00
|
|
|
{
|
2019-03-10 21:28:14 +00:00
|
|
|
return compare_sites(a, b) < 0;
|
|
|
|
}
|
2015-02-12 19:19:05 +00:00
|
|
|
|
2019-03-10 21:28:14 +00:00
|
|
|
static MAKE_GROW_TABLE(dive_site_table, struct dive_site *, dive_sites)
|
|
|
|
static MAKE_GET_INSERTION_INDEX(dive_site_table, struct dive_site *, dive_sites, site_less_than)
|
|
|
|
static MAKE_ADD_TO(dive_site_table, struct dive_site *, dive_sites)
|
|
|
|
static MAKE_REMOVE_FROM(dive_site_table, dive_sites)
|
|
|
|
static MAKE_GET_IDX(dive_site_table, struct dive_site *, dive_sites)
|
|
|
|
MAKE_SORT(dive_site_table, struct dive_site *, dive_sites, compare_sites)
|
|
|
|
static MAKE_REMOVE(dive_site_table, struct dive_site *, dive_site)
|
2019-06-04 18:59:08 +00:00
|
|
|
MAKE_CLEAR_TABLE(dive_site_table, dive_sites, dive_site)
|
2019-09-25 18:17:41 +00:00
|
|
|
MAKE_MOVE_TABLE(dive_site_table, dive_sites)
|
2019-03-10 21:28:14 +00:00
|
|
|
|
2019-03-11 23:25:31 +00:00
|
|
|
int add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_table)
|
2019-03-10 21:28:14 +00:00
|
|
|
{
|
2019-04-05 19:33:27 +00:00
|
|
|
/* If the site doesn't yet have an UUID, create a new one.
|
|
|
|
* Make this deterministic for testing. */
|
|
|
|
if (!ds->uuid) {
|
|
|
|
SHA_CTX ctx;
|
|
|
|
uint32_t csum[5];
|
|
|
|
|
|
|
|
SHA1_Init(&ctx);
|
|
|
|
if (ds->name)
|
|
|
|
SHA1_Update(&ctx, ds->name, strlen(ds->name));
|
|
|
|
if (ds->description)
|
|
|
|
SHA1_Update(&ctx, ds->description, strlen(ds->description));
|
|
|
|
if (ds->notes)
|
|
|
|
SHA1_Update(&ctx, ds->notes, strlen(ds->notes));
|
|
|
|
SHA1_Final((unsigned char *)csum, &ctx);
|
|
|
|
ds->uuid = csum[0];
|
|
|
|
}
|
|
|
|
|
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 */
|
2019-04-05 19:33:27 +00:00
|
|
|
while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, ds_table) != NULL)
|
|
|
|
++ds->uuid;
|
2019-03-08 18:37:27 +00:00
|
|
|
|
2019-03-10 21:28:14 +00:00
|
|
|
int idx = dive_site_table_get_insertion_index(ds_table, ds);
|
|
|
|
add_to_dive_site_table(ds_table, idx, ds);
|
2019-03-11 23:25:31 +00:00
|
|
|
return idx;
|
2019-03-03 14:12:22 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-17 20:26:57 +00:00
|
|
|
struct dive_site *alloc_dive_site_with_name(const char *name)
|
|
|
|
{
|
|
|
|
struct dive_site *ds = alloc_dive_site();
|
|
|
|
ds->name = copy_string(name);
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dive_site *alloc_dive_site_with_gps(const char *name, const location_t *loc)
|
|
|
|
{
|
|
|
|
struct dive_site *ds = alloc_dive_site_with_name(name);
|
|
|
|
ds->location = *loc;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-07 08:01:18 +00:00
|
|
|
int nr_of_dives_at_dive_site(struct dive_site *ds)
|
2015-07-18 20:34:05 +00:00
|
|
|
{
|
2019-03-07 08:01:18 +00:00
|
|
|
return ds->dives.nr;
|
2015-07-18 20:34:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 11:41:45 +00:00
|
|
|
bool is_dive_site_selected(struct dive_site *ds)
|
2015-07-16 04:25:26 +00:00
|
|
|
{
|
2019-03-06 18:26:19 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ds->dives.nr; i++) {
|
|
|
|
if (ds->dives.dives[i]->selected)
|
|
|
|
return true;
|
2015-07-16 04:25:26 +00:00
|
|
|
}
|
2019-03-06 18:26:19 +00:00
|
|
|
return false;
|
2015-07-16 04:25:26 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2019-04-14 21:13:57 +00:00
|
|
|
free(ds->dives.dives);
|
2018-10-21 19:48:47 +00:00
|
|
|
free_taxonomy(&ds->taxonomy);
|
|
|
|
free(ds);
|
|
|
|
}
|
2018-10-14 21:30:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 23:25:31 +00:00
|
|
|
int unregister_dive_site(struct dive_site *ds)
|
2019-03-03 14:12:22 +00:00
|
|
|
{
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
return remove_dive_site(ds, divelog.sites);
|
2015-02-13 15:02:26 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2019-03-10 21:28:14 +00:00
|
|
|
remove_dive_site(ds, ds_table);
|
2019-03-03 14:12:22 +00:00
|
|
|
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
|
|
|
{
|
2020-03-17 20:26:57 +00:00
|
|
|
struct dive_site *ds = alloc_dive_site_with_name(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
|
|
|
{
|
2020-03-17 20:26:57 +00:00
|
|
|
struct dive_site *ds = alloc_dive_site_with_gps(name, loc);
|
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 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;
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
for_each_dive_site (i, ds, divelog.sites)
|
2019-03-03 14:12:22 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2019-03-05 21:58:47 +00:00
|
|
|
unregister_dive_from_dive_site(d);
|
2019-01-01 09:45:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:20:29 +00:00
|
|
|
void add_dive_to_dive_site(struct dive *d, struct dive_site *ds)
|
|
|
|
{
|
|
|
|
int idx;
|
2022-02-25 23:11:16 +00:00
|
|
|
if (!d) {
|
|
|
|
fprintf(stderr, "Warning: add_dive_to_dive_site called with NULL dive\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!ds) {
|
|
|
|
fprintf(stderr, "Warning: add_dive_to_dive_site called with NULL dive site\n");
|
|
|
|
return;
|
|
|
|
}
|
2019-03-04 22:20:29 +00:00
|
|
|
if (d->dive_site == ds)
|
|
|
|
return;
|
2019-03-05 21:58:47 +00:00
|
|
|
if (d->dive_site) {
|
2019-03-04 22:20:29 +00:00
|
|
|
fprintf(stderr, "Warning: adding dive that already belongs to a dive site to a different site\n");
|
2019-03-05 21:58:47 +00:00
|
|
|
unregister_dive_from_dive_site(d);
|
|
|
|
}
|
2019-03-04 22:20:29 +00:00
|
|
|
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;
|
2019-03-10 19:04:47 +00:00
|
|
|
remove_dive(d, &ds->dives);
|
2019-03-04 22:20:29 +00:00
|
|
|
d->dive_site = NULL;
|
|
|
|
return ds;
|
|
|
|
}
|