Dive site: return pointer to dive_site in get_dive_site_*()

As a first step in removing dive-site uuids, change the interface
of the get_dive_site_*() functions to return pointers instead
of uuids. This makes code a bit more complicated in places where
the uuid is extracted afterwards (needed NULL check). Nevertheless,
these places should disappear once pointers instead of uuids are
stored in the dive-structures.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-10-23 12:42:01 +02:00 committed by Dirk Hohndel
parent 4cea7b4901
commit 68961a169e
9 changed files with 68 additions and 77 deletions

View file

@ -156,6 +156,7 @@ unsigned char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive)
*compl_buffer, *compl_buffer,
*membuf = runner; *membuf = runner;
char buffer[1024]; char buffer[1024];
struct dive_site *ds;
device_data_t *devdata = calloc(1, sizeof(device_data_t)); device_data_t *devdata = calloc(1, sizeof(device_data_t));
/* /*
@ -200,7 +201,8 @@ unsigned char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive)
* Locality and Dive points. * Locality and Dive points.
*/ */
snprintf(buffer, sizeof(buffer), "%s, %s", locality, dive_point); snprintf(buffer, sizeof(buffer), "%s, %s", locality, dive_point);
dt_dive->dive_site_uuid = get_dive_site_uuid_by_name(buffer, NULL); ds = get_dive_site_by_name(buffer);
dt_dive->dive_site_uuid = ds ? ds->uuid : 0;
if (dt_dive->dive_site_uuid == 0) if (dt_dive->dive_site_uuid == 0)
dt_dive->dive_site_uuid = create_dive_site(buffer, dt_dive->when); dt_dive->dive_site_uuid = create_dive_site(buffer, dt_dive->when);
free(locality); free(locality);

View file

@ -11,47 +11,41 @@
struct dive_site_table dive_site_table; struct dive_site_table dive_site_table;
/* there could be multiple sites of the same name - return the first one */ /* there could be multiple sites of the same name - return the first one */
uint32_t get_dive_site_uuid_by_name(const char *name, struct dive_site **dsp) struct dive_site *get_dive_site_by_name(const char *name)
{ {
int i; int i;
struct dive_site *ds; struct dive_site *ds;
for_each_dive_site (i, ds) { for_each_dive_site (i, ds) {
if (same_string(ds->name, name)) { if (same_string(ds->name, name))
if (dsp) return ds;
*dsp = ds;
return ds->uuid;
}
} }
return 0; return NULL;
} }
/* there could be multiple sites at the same GPS fix - return the first one */ /* there could be multiple sites at the same GPS fix - return the first one */
uint32_t get_dive_site_uuid_by_gps(const location_t *loc, struct dive_site **dsp) struct dive_site *get_dive_site_by_gps(const location_t *loc)
{ {
int i; int i;
struct dive_site *ds; struct dive_site *ds;
for_each_dive_site (i, ds) { for_each_dive_site (i, ds) {
if (same_location(loc, &ds->location)) { if (same_location(loc, &ds->location))
if (dsp) return ds;
*dsp = ds;
return ds->uuid;
}
} }
return 0; return NULL;
} }
/* to avoid a bug where we have two dive sites with different name and the same GPS coordinates /* 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, * 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 */ * this function allows us to verify if a very specific name/GPS combination already exists */
uint32_t get_dive_site_uuid_by_gps_and_name(char *name, const location_t *loc) struct dive_site *get_dive_site_by_gps_and_name(char *name, const location_t *loc)
{ {
int i; int i;
struct dive_site *ds; struct dive_site *ds;
for_each_dive_site (i, ds) { for_each_dive_site (i, ds) {
if (same_location(loc, &ds->location) && same_string(ds->name, name)) if (same_location(loc, &ds->location) && same_string(ds->name, name))
return ds->uuid; return ds;
} }
return 0; return NULL;
} }
// Calculate the distance in meters between two coordinates. // Calculate the distance in meters between two coordinates.
@ -70,22 +64,19 @@ unsigned int get_distance(const location_t *loc1, const location_t *loc2)
} }
/* find the closest one, no more than distance meters away - if more than one at same distance, pick the first */ /* find the closest one, no more than distance meters away - if more than one at same distance, pick the first */
uint32_t get_dive_site_uuid_by_gps_proximity(const location_t *loc, int distance, struct dive_site **dsp) struct dive_site *get_dive_site_by_gps_proximity(const location_t *loc, int distance)
{ {
int i; int i;
int uuid = 0; struct dive_site *ds, *res = NULL;
struct dive_site *ds;
unsigned int cur_distance, min_distance = distance; unsigned int cur_distance, min_distance = distance;
for_each_dive_site (i, ds) { for_each_dive_site (i, ds) {
if (dive_site_has_gps_location(ds) && if (dive_site_has_gps_location(ds) &&
(cur_distance = get_distance(&ds->location, loc)) < min_distance) { (cur_distance = get_distance(&ds->location, loc)) < min_distance) {
min_distance = cur_distance; min_distance = cur_distance;
uuid = ds->uuid; res = ds;
if (dsp)
*dsp = ds;
} }
} }
return uuid; return res;
} }
/* try to create a uniqe ID - fingers crossed */ /* try to create a uniqe ID - fingers crossed */

View file

@ -61,10 +61,10 @@ void delete_dive_site(uint32_t id);
uint32_t create_dive_site(const char *name, timestamp_t divetime); uint32_t create_dive_site(const char *name, timestamp_t divetime);
uint32_t create_dive_site_from_current_dive(const char *name); uint32_t create_dive_site_from_current_dive(const char *name);
uint32_t create_dive_site_with_gps(const char *name, const location_t *, timestamp_t divetime); uint32_t create_dive_site_with_gps(const char *name, const location_t *, timestamp_t divetime);
uint32_t get_dive_site_uuid_by_name(const char *name, struct dive_site **dsp); struct dive_site *get_dive_site_by_name(const char *name);
uint32_t get_dive_site_uuid_by_gps(const location_t *, struct dive_site **dsp); struct dive_site *get_dive_site_by_gps(const location_t *);
uint32_t get_dive_site_uuid_by_gps_and_name(char *name, const location_t *); struct dive_site *get_dive_site_by_gps_and_name(char *name, const location_t *);
uint32_t get_dive_site_uuid_by_gps_proximity(const location_t *, int distance, struct dive_site **dsp); struct dive_site *get_dive_site_by_gps_proximity(const location_t *, int distance);
bool dive_site_is_empty(struct dive_site *ds); bool dive_site_is_empty(struct dive_site *ds);
void copy_dive_site_taxonomy(struct dive_site *orig, struct dive_site *copy); void copy_dive_site_taxonomy(struct dive_site *orig, struct dive_site *copy);
void copy_dive_site(struct dive_site *orig, struct dive_site *copy); void copy_dive_site(struct dive_site *orig, struct dive_site *copy);

View file

@ -151,17 +151,17 @@ static int get_hex(const char *line)
static void parse_dive_gps(char *line, struct membuffer *str, void *_dive) static void parse_dive_gps(char *line, struct membuffer *str, void *_dive)
{ {
UNUSED(str); UNUSED(str);
uint32_t uuid;
location_t location; location_t location;
struct dive *dive = _dive; struct dive *dive = _dive;
struct dive_site *ds = get_dive_site_for_dive(dive); struct dive_site *ds = get_dive_site_for_dive(dive);
parse_location(line, &location); parse_location(line, &location);
if (!ds) { if (!ds) {
uuid = get_dive_site_uuid_by_gps(&location, NULL); ds = get_dive_site_by_gps(&location);
if (!uuid) if (!ds)
uuid = create_dive_site_with_gps("", &location, dive->when); dive->dive_site_uuid = create_dive_site_with_gps("", &location, dive->when);
dive->dive_site_uuid = uuid; else
dive->dive_site_uuid = ds->uuid;
} else { } else {
if (dive_site_has_gps_location(ds) && !same_location(&ds->location, &location)) { if (dive_site_has_gps_location(ds) && !same_location(&ds->location, &location)) {
const char *coords = printGPSCoords(&location); const char *coords = printGPSCoords(&location);
@ -177,15 +177,15 @@ static void parse_dive_gps(char *line, struct membuffer *str, void *_dive)
static void parse_dive_location(char *line, struct membuffer *str, void *_dive) static void parse_dive_location(char *line, struct membuffer *str, void *_dive)
{ {
UNUSED(line); UNUSED(line);
uint32_t uuid;
char *name = get_utf8(str); char *name = get_utf8(str);
struct dive *dive = _dive; struct dive *dive = _dive;
struct dive_site *ds = get_dive_site_for_dive(dive); struct dive_site *ds = get_dive_site_for_dive(dive);
if (!ds) { if (!ds) {
uuid = get_dive_site_uuid_by_name(name, NULL); ds = get_dive_site_by_name(name);
if (!uuid) if (!ds)
uuid = create_dive_site(name, dive->when); dive->dive_site_uuid = create_dive_site(name, dive->when);
dive->dive_site_uuid = uuid; else
dive->dive_site_uuid = ds->uuid;
} else { } else {
// we already had a dive site linked to the dive // we already had a dive site linked to the dive
if (empty_string(ds->name)) { if (empty_string(ds->name)) {

View file

@ -968,6 +968,7 @@ static void try_to_fill_sample(struct sample *sample, const char *name, char *bu
static void divinglog_place(char *place, uint32_t *uuid, struct parser_state *state) static void divinglog_place(char *place, uint32_t *uuid, struct parser_state *state)
{ {
char buffer[1024]; char buffer[1024];
struct dive_site *ds;
snprintf(buffer, sizeof(buffer), snprintf(buffer, sizeof(buffer),
"%s%s%s%s%s", "%s%s%s%s%s",
@ -976,8 +977,10 @@ static void divinglog_place(char *place, uint32_t *uuid, struct parser_state *st
state->city ? state->city : "", state->city ? state->city : "",
state->country ? ", " : "", state->country ? ", " : "",
state->country ? state->country : ""); state->country ? state->country : "");
*uuid = get_dive_site_uuid_by_name(buffer, NULL); ds = get_dive_site_by_name(buffer);
if (*uuid == 0) if (ds)
*uuid = ds->uuid;
else
*uuid = create_dive_site(buffer, state->cur_dive->when); *uuid = create_dive_site(buffer, state->cur_dive->when);
// TODO: capture the country / city info in the taxonomy instead // TODO: capture the country / city info in the taxonomy instead
@ -1178,16 +1181,15 @@ static void gps_in_dive(char *buffer, struct dive *dive, struct parser_state *st
parse_location(buffer, &location); parse_location(buffer, &location);
if (uuid == 0) { if (uuid == 0) {
// check if we have a dive site within 20 meters of that gps fix // check if we have a dive site within 20 meters of that gps fix
uuid = get_dive_site_uuid_by_gps_proximity(&location, 20, &ds); ds = get_dive_site_by_gps_proximity(&location, 20);
if (ds) { if (ds) {
// found a site nearby; in case it turns out this one had a different name let's // found a site nearby; in case it turns out this one had a different name let's
// remember the original coordinates so we can create the correct dive site later // remember the original coordinates so we can create the correct dive site later
state->cur_location = location; state->cur_location = location;
dive->dive_site_uuid = uuid; dive->dive_site_uuid = ds->uuid;
} else { } else {
dive->dive_site_uuid = create_dive_site_with_gps("", &location, dive->when); dive->dive_site_uuid = create_dive_site_with_gps("", &location, dive->when);
ds = get_dive_site_by_uuid(dive->dive_site_uuid);
} }
} else { } else {
ds = get_dive_site_by_uuid(uuid); ds = get_dive_site_by_uuid(uuid);

View file

@ -417,16 +417,10 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state)
char *to_free = NULL; char *to_free = NULL;
int size = trimspace(buffer); int size = trimspace(buffer);
if(size) { if(size) {
uint32_t uuid = dive->dive_site_uuid; struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
struct dive_site *ds = get_dive_site_by_uuid(uuid); if (!ds) {
if (uuid && !ds) {
// that's strange - we have a uuid but it doesn't exist - let's just ignore it
fprintf(stderr, "dive contains a non-existing dive site uuid %x\n", dive->dive_site_uuid);
uuid = 0;
}
if (!uuid) {
// if the dive doesn't have a uuid, check if there's already a dive site by this name // if the dive doesn't have a uuid, check if there's already a dive site by this name
uuid = get_dive_site_uuid_by_name(buffer, &ds); ds = get_dive_site_by_name(buffer);
} }
if (ds) { if (ds) {
// we have a uuid, let's hope there isn't a different name // we have a uuid, let's hope there isn't a different name
@ -437,9 +431,9 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state)
// but wait, we could have gotten this one based on GPS coords and could // but wait, we could have gotten this one based on GPS coords and could
// have had two different names for the same site... so let's search the other // have had two different names for the same site... so let's search the other
// way around // way around
uint32_t exact_match_uuid = get_dive_site_uuid_by_gps_and_name(buffer, &ds->location); struct dive_site *exact_match = get_dive_site_by_gps_and_name(buffer, &ds->location);
if (exact_match_uuid) { if (exact_match) {
dive->dive_site_uuid = exact_match_uuid; dive->dive_site_uuid = exact_match->uuid;
} else { } else {
dive->dive_site_uuid = create_dive_site(buffer, dive->when); dive->dive_site_uuid = create_dive_site(buffer, dive->when);
struct dive_site *newds = get_dive_site_by_uuid(dive->dive_site_uuid); struct dive_site *newds = get_dive_site_by_uuid(dive->dive_site_uuid);
@ -453,7 +447,7 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state)
} }
} else { } else {
// add the existing dive site to the current dive // add the existing dive site to the current dive
dive->dive_site_uuid = uuid; dive->dive_site_uuid = ds->uuid;
} }
} else { } else {
dive->dive_site_uuid = create_dive_site(buffer, dive->when); dive->dive_site_uuid = create_dive_site(buffer, dive->when);

View file

@ -124,15 +124,15 @@ static bool is_divespot_mappable(int divespot_id)
return false; return false;
} }
static uint32_t get_dive_site_uuid_by_divespot_id(int divespot_id) static struct dive_site *get_dive_site_by_divespot_id(int divespot_id)
{ {
struct divespot_mapping *dm = divespot_mapping; struct divespot_mapping *dm = divespot_mapping;
while (dm) { while (dm) {
if (dm->divespot_id == divespot_id) if (dm->divespot_id == divespot_id)
return dm->dive_site_uuid; return get_dive_site_by_uuid(dm->dive_site_uuid);
dm = dm->next; dm = dm->next;
} }
return 0; return NULL;
} }
/* helper function to parse the Uemis data structures */ /* helper function to parse the Uemis data structures */
@ -1177,18 +1177,19 @@ static void get_uemis_divespot(const char *mountpath, int divespot_id, struct di
struct dive_site *nds = get_dive_site_by_uuid(dive->dive_site_uuid); struct dive_site *nds = get_dive_site_by_uuid(dive->dive_site_uuid);
if (is_divespot_mappable(divespot_id)) { if (is_divespot_mappable(divespot_id)) {
dive->dive_site_uuid = get_dive_site_uuid_by_divespot_id(divespot_id); struct dive_site *ds = get_dive_site_by_divespot_id(divespot_id);
dive->dive_site_uuid = ds ? ds->uuid : 0;
} else if (nds && nds->name && strstr(nds->name,"from Uemis")) { } else if (nds && nds->name && strstr(nds->name,"from Uemis")) {
if (load_uemis_divespot(mountpath, divespot_id)) { if (load_uemis_divespot(mountpath, divespot_id)) {
/* get the divesite based on the diveid, this should give us /* get the divesite based on the diveid, this should give us
* the newly created site * the newly created site
*/ */
struct dive_site *ods = NULL; struct dive_site *ods;
/* with the divesite name we got from parse_dive, that is called on load_uemis_divespot /* with the divesite name we got from parse_dive, that is called on load_uemis_divespot
* we search all existing divesites if we have one with the same name already. The function * we search all existing divesites if we have one with the same name already. The function
* returns the first found which is luckily not the newly created. * returns the first found which is luckily not the newly created.
*/ */
(void)get_dive_site_uuid_by_name(nds->name, &ods); ods = get_dive_site_by_name(nds->name);
if (ods) { if (ods) {
/* if the uuid's are the same, the new site is a duplicate and can be deleted */ /* if the uuid's are the same, the new site is a duplicate and can be deleted */
if (nds->uuid != ods->uuid) { if (nds->uuid != ods->uuid) {

View file

@ -872,16 +872,17 @@ parsed:
bool QMLManager::checkLocation(DiveObjectHelper *myDive, struct dive *d, QString location, QString gps) bool QMLManager::checkLocation(DiveObjectHelper *myDive, struct dive *d, QString location, QString gps)
{ {
bool diveChanged = false; bool diveChanged = false;
uint32_t uuid = 0;
struct dive_site *ds = get_dive_site_for_dive(d); struct dive_site *ds = get_dive_site_for_dive(d);
qDebug() << "checkLocation" << location << "gps" << gps << "dive had" << myDive->location() << "gps" << myDive->gas(); qDebug() << "checkLocation" << location << "gps" << gps << "dive had" << myDive->location() << "gps" << myDive->gas();
if (myDive->location() != location) { if (myDive->location() != location) {
diveChanged = true; diveChanged = true;
if (!ds) if (!ds)
uuid = get_dive_site_uuid_by_name(qPrintable(location), NULL); ds = get_dive_site_by_name(qPrintable(location));
if (!uuid && !location.isEmpty()) if (!ds && !location.isEmpty()) {
uuid = create_dive_site(qPrintable(location), d->when); uint32_t uuid = create_dive_site(qPrintable(location), d->when);
d->dive_site_uuid = uuid; ds = get_dive_site_by_uuid(uuid);
}
d->dive_site_uuid = ds ? ds->uuid : 0;
} }
// now make sure that the GPS coordinates match - if the user changed the name but not // now make sure that the GPS coordinates match - if the user changed the name but not
// the GPS coordinates, this still does the right thing as the now new dive site will // the GPS coordinates, this still does the right thing as the now new dive site will
@ -1474,15 +1475,13 @@ QString QMLManager::getVersion() const
} }
QString QMLManager::getGpsFromSiteName(const QString& siteName) QString QMLManager::getGpsFromSiteName(const QString& siteName)
{ uint32_t uuid; {
struct dive_site *ds; struct dive_site *ds;
uuid = get_dive_site_uuid_by_name(qPrintable(siteName), NULL); ds = get_dive_site_by_name(qPrintable(siteName));
if (uuid) { if (ds)
ds = get_dive_site_by_uuid(uuid);
return QString(printGPSCoords(&ds->location)); return QString(printGPSCoords(&ds->location));
} return QString();
return "";
} }
void QMLManager::setNotificationText(QString text) void QMLManager::setNotificationText(QString text)

View file

@ -373,17 +373,19 @@ static void smtk_build_location(MdbHandle *mdb, char *idx, timestamp_t when, uin
str = smtk_concat_str(str, ", ", "%s", col[1]->bind_ptr); // Locality str = smtk_concat_str(str, ", ", "%s", col[1]->bind_ptr); // Locality
str = smtk_concat_str(str, ", ", "%s", site); str = smtk_concat_str(str, ", ", "%s", site);
*location = get_dive_site_uuid_by_name(str, NULL); ds = get_dive_site_by_name(str);
if (*location == 0) { if (!ds) {
if (!has_location(&loc)) if (!has_location(&loc))
*location = create_dive_site(str, when); *location = create_dive_site(str, when);
else else
*location = create_dive_site_with_gps(str, &loc, when); *location = create_dive_site_with_gps(str, &loc, when);
ds = get_dive_site_by_uuid(*location);
} else {
*location = ds->uuid;
} }
smtk_free(bound_values, table->num_cols); smtk_free(bound_values, table->num_cols);
/* Insert site notes */ /* Insert site notes */
ds = get_dive_site_by_uuid(*location);
ds->notes = copy_string(notes); ds->notes = copy_string(notes);
free(notes); free(notes);