Dive site: replace dive->dive_site_uuid by dive_site

Replace the UUID reference of struct dive by a pointer to dive_site.
This commit is rather large in lines, but nevertheless quite simple
since most of the UUID->pointer work was done in previous commits.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-10-26 17:03:54 +02:00 committed by Dirk Hohndel
parent acd44467c1
commit 724055f0af
28 changed files with 181 additions and 169 deletions

View file

@ -202,9 +202,9 @@ unsigned char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive)
*/
snprintf(buffer, sizeof(buffer), "%s, %s", locality, dive_point);
ds = get_dive_site_by_name(buffer);
dt_dive->dive_site_uuid = ds ? ds->uuid : 0;
if (dt_dive->dive_site_uuid == 0)
dt_dive->dive_site_uuid = create_dive_site(buffer, dt_dive->when)->uuid;
dt_dive->dive_site = ds;
if (!dt_dive->dive_site)
dt_dive->dive_site = create_dive_site(buffer, dt_dive->when);
free(locality);
locality = NULL;
free(dive_point);

View file

@ -650,7 +650,7 @@ void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_compo
if (what.visibility)
d->visibility = s->visibility;
if (what.divesite)
d->dive_site_uuid = s->dive_site_uuid;
d->dive_site = s->dive_site;
if (what.tags)
STRUCTURED_LIST_COPY(struct tag_entry, s->tag_list, d->tag_list, copy_tl);
if (what.cylinders)
@ -3503,10 +3503,10 @@ struct dive *merge_dives(const struct dive *a, const struct dive *b, int offset,
join_dive_computers(res, &res->dc, &a->dc, &b->dc, cylinders_map_a, cylinders_map_b, 0);
/* we take the first dive site, unless it's empty */
if (a->dive_site_uuid && !dive_site_is_empty(get_dive_site_by_uuid(a->dive_site_uuid)))
res->dive_site_uuid = a->dive_site_uuid;
if (a->dive_site && !dive_site_is_empty(a->dive_site))
res->dive_site = a->dive_site;
else
res->dive_site_uuid = b->dive_site_uuid;
res->dive_site = b->dive_site;
fixup_dive(res);
return res;
}
@ -4075,12 +4075,12 @@ unsigned int dive_get_picture_count(struct dive *dive)
void dive_set_geodata_from_picture(struct dive *dive, struct picture *picture)
{
struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
struct dive_site *ds = dive->dive_site;
if (!dive_site_has_gps_location(ds) && has_location(&picture->location)) {
if (ds) {
ds->location = picture->location;
} else {
dive->dive_site_uuid = create_dive_site_with_gps("", &picture->location, dive->when)->uuid;
dive->dive_site = create_dive_site_with_gps("", &picture->location, dive->when);
invalidate_dive_cache(dive);
}
}
@ -4333,14 +4333,12 @@ struct dive *get_dive_from_table(int nr, struct dive_table *dt)
struct dive_site *get_dive_site_for_dive(const struct dive *dive)
{
if (dive)
return get_dive_site_by_uuid(dive->dive_site_uuid);
return NULL;
return dive->dive_site;
}
const char *get_dive_country(const struct dive *dive)
{
struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
struct dive_site *ds = dive->dive_site;
if (ds) {
int idx = taxonomy_index_for_category(&ds->taxonomy, TC_COUNTRY);
if (idx >= 0)
@ -4351,7 +4349,7 @@ const char *get_dive_country(const struct dive *dive)
const char *get_dive_location(const struct dive *dive)
{
const struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
const struct dive_site *ds = dive->dive_site;
if (ds && ds->name)
return ds->name;
return NULL;
@ -4432,7 +4430,7 @@ int dive_has_gps_location(const struct dive *dive)
{
if (!dive)
return false;
return dive_site_has_gps_location(get_dive_site_by_uuid(dive->dive_site_uuid));
return dive_site_has_gps_location(dive->dive_site);
}
struct gasmix get_gasmix(const struct dive *dive, const struct divecomputer *dc, int time, const struct event **evp, struct gasmix gasmix)

View file

@ -310,7 +310,7 @@ struct dive {
bool selected;
bool hidden_by_filter;
timestamp_t when;
uint32_t dive_site_uuid;
struct dive_site *dive_site;
char *notes;
char *divemaster, *buddy;
int rating;

View file

@ -138,7 +138,7 @@ int nr_of_dives_at_dive_site(struct dive_site *ds, bool select_only)
if (!ds)
return 0;
for_each_dive(j, d) {
if (d->dive_site_uuid == ds->uuid && (!select_only || d->selected)) {
if (d->dive_site == ds && (!select_only || d->selected)) {
nr++;
}
}
@ -153,7 +153,7 @@ bool is_dive_site_used(struct dive_site *ds, bool select_only)
if (!ds)
return false;
for_each_dive(j, d) {
if (d->dive_site_uuid == ds->uuid && (!select_only || d->selected)) {
if (d->dive_site == ds && (!select_only || d->selected)) {
found = true;
break;
}
@ -318,9 +318,9 @@ void merge_dive_sites(struct dive_site *ref, struct dive_site *dive_sites[], int
continue;
for_each_dive(curr_dive, d) {
if (d->dive_site_uuid != dive_sites[i]->uuid )
if (d->dive_site != dive_sites[i] )
continue;
d->dive_site_uuid = ref->uuid;
d->dive_site = ref;
invalidate_dive_cache(d);
}
}

View file

@ -209,10 +209,10 @@ int GpsLocation::getGpsNum() const
static void copy_gps_location(struct gpsTracker &gps, struct dive *d)
{
struct dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
struct dive_site *ds = d->dive_site;
if (!ds) {
ds = create_dive_site(qPrintable(gps.name), gps.when);
d->dive_site_uuid = ds->uuid;
d->dive_site = ds;
}
ds->location = gps.location;
}

View file

@ -197,7 +197,7 @@ static int cobalt_dive(void *param, int columns, char **data, char **column)
return 1;
}
sprintf(tmp, "%s / %s", location, location_site);
state->cur_dive->dive_site_uuid = find_or_create_dive_site_with_name(tmp, state->cur_dive->when)->uuid;
state->cur_dive->dive_site = find_or_create_dive_site_with_name(tmp, state->cur_dive->when);
free(tmp);
}
free(location);

View file

@ -287,7 +287,7 @@ static int divinglog_dive(void *param, int columns, char **data, char **column)
state->cur_dive->when = (time_t)(atol(data[1]));
if (data[2])
state->cur_dive->dive_site_uuid = find_or_create_dive_site_with_name(data[2], state->cur_dive->when)->uuid;
state->cur_dive->dive_site = find_or_create_dive_site_with_name(data[2], state->cur_dive->when);
if (data[3])
utf8_string(data[3], &state->cur_dive->buddy);

View file

@ -596,7 +596,7 @@ static void parse_string_field(struct dive *dive, dc_field_string_t *str)
parse_location(line, &location);
if (location.lat.udeg && location.lon.udeg)
dive->dive_site_uuid = create_dive_site_with_gps(str->value, &location, time(NULL))->uuid;
dive->dive_site = create_dive_site_with_gps(str->value, &location, time(NULL));
}
}
#endif

View file

@ -227,7 +227,7 @@ static void parse_dives (int log_version, const unsigned char *buf, unsigned int
// now that we have the dive time we can store the divesite
// (we need the dive time to create deterministic uuids)
if (found_divesite) {
dive->dive_site_uuid = find_or_create_dive_site_with_name(location, dive->when)->uuid;
dive->dive_site = find_or_create_dive_site_with_name(location, dive->when);
free(location);
}
//unsigned int end_time = array_uint32_le(buf + ptr);

View file

@ -159,9 +159,9 @@ static void parse_dive_gps(char *line, struct membuffer *str, void *_dive)
if (!ds) {
ds = get_dive_site_by_gps(&location);
if (!ds)
dive->dive_site_uuid = create_dive_site_with_gps("", &location, dive->when)->uuid;
dive->dive_site = create_dive_site_with_gps("", &location, dive->when);
else
dive->dive_site_uuid = ds->uuid;
dive->dive_site = ds;
} else {
if (dive_site_has_gps_location(ds) && !same_location(&ds->location, &location)) {
const char *coords = printGPSCoords(&location);
@ -183,9 +183,9 @@ static void parse_dive_location(char *line, struct membuffer *str, void *_dive)
if (!ds) {
ds = get_dive_site_by_name(name);
if (!ds)
dive->dive_site_uuid = create_dive_site(name, dive->when)->uuid;
dive->dive_site = create_dive_site(name, dive->when);
else
dive->dive_site_uuid = ds->uuid;
dive->dive_site = ds;
} else {
// we already had a dive site linked to the dive
if (empty_string(ds->name)) {
@ -212,7 +212,7 @@ static void parse_dive_notes(char *line, struct membuffer *str, void *_dive)
{ UNUSED(line); struct dive *dive = _dive; dive->notes = get_utf8(str); }
static void parse_dive_divesiteid(char *line, struct membuffer *str, void *_dive)
{ UNUSED(str); struct dive *dive = _dive; dive->dive_site_uuid = get_hex(line); }
{ UNUSED(str); struct dive *dive = _dive; dive->dive_site = get_dive_site_by_uuid(get_hex(line)); }
/*
* We can have multiple tags in the membuffer. They are separated by

View file

@ -558,6 +558,13 @@ static void hex_value(char *buffer, uint32_t *i)
*i = strtoul(buffer, NULL, 16);
}
static void dive_site(char *buffer, struct dive_site **ds)
{
uint32_t uuid;
hex_value(buffer, &uuid);
*ds = get_dive_site_by_uuid(uuid);
}
static void get_tripflag(char *buffer, tripflag_t *tf)
{
*tf = strcmp(buffer, "NOTRIP") ? TF_NONE : NO_TRIP;
@ -965,10 +972,9 @@ static void try_to_fill_sample(struct sample *sample, const char *name, char *bu
nonmatch("sample", name, buf);
}
static void divinglog_place(char *place, uint32_t *uuid, struct parser_state *state)
static void divinglog_place(char *place, struct dive_site **ds, struct parser_state *state)
{
char buffer[1024];
struct dive_site *ds;
snprintf(buffer, sizeof(buffer),
"%s%s%s%s%s",
@ -977,11 +983,9 @@ static void divinglog_place(char *place, uint32_t *uuid, struct parser_state *st
state->city ? state->city : "",
state->country ? ", " : "",
state->country ? state->country : "");
ds = get_dive_site_by_name(buffer);
if (ds)
*uuid = ds->uuid;
else
*uuid = create_dive_site(buffer, state->cur_dive->when)->uuid;
*ds = get_dive_site_by_name(buffer);
if (!*ds)
*ds = create_dive_site(buffer, state->cur_dive->when);
// TODO: capture the country / city info in the taxonomy instead
free(state->city);
@ -1006,7 +1010,7 @@ static int divinglog_dive_match(struct dive *dive, const char *name, char *buf,
MATCH("names.buddy", utf8_string, &dive->buddy) ||
MATCH("name.country", utf8_string, &state->country) ||
MATCH("name.city", utf8_string, &state->city) ||
MATCH_STATE("name.place", divinglog_place, &dive->dive_site_uuid) ||
MATCH_STATE("name.place", divinglog_place, &dive->dive_site) ||
0;
}
@ -1133,7 +1137,7 @@ static void gps_lat(char *buffer, struct dive *dive)
location.lat = parse_degrees(buffer, &end);
if (!ds) {
dive->dive_site_uuid = create_dive_site_with_gps(NULL, &location, dive->when)->uuid;
dive->dive_site = create_dive_site_with_gps(NULL, &location, dive->when);
} else {
if (ds->location.lat.udeg && ds->location.lat.udeg != location.lat.udeg)
fprintf(stderr, "Oops, changing the latitude of existing dive site id %8x name %s; not good\n", ds->uuid, ds->name ?: "(unknown)");
@ -1149,7 +1153,7 @@ static void gps_long(char *buffer, struct dive *dive)
location.lon = parse_degrees(buffer, &end);
if (!ds) {
dive->dive_site_uuid = create_dive_site_with_gps(NULL, &location, dive->when)->uuid;
dive->dive_site = create_dive_site_with_gps(NULL, &location, dive->when);
} else {
if (ds->location.lon.udeg && ds->location.lon.udeg != location.lon.udeg)
fprintf(stderr, "Oops, changing the longitude of existing dive site id %8x name %s; not good\n", ds->uuid, ds->name ?: "(unknown)");
@ -1174,12 +1178,11 @@ static void gps_location(char *buffer, struct dive_site *ds)
static void gps_in_dive(char *buffer, struct dive *dive, struct parser_state *state)
{
struct dive_site *ds = NULL;
struct dive_site *ds = dive->dive_site;
location_t location;
uint32_t uuid = dive->dive_site_uuid;
parse_location(buffer, &location);
if (uuid == 0) {
if (!ds) {
// check if we have a dive site within 20 meters of that gps fix
ds = get_dive_site_by_gps_proximity(&location, 20);
@ -1187,12 +1190,11 @@ static void gps_in_dive(char *buffer, struct dive *dive, struct parser_state *st
// 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
state->cur_location = location;
dive->dive_site_uuid = ds->uuid;
dive->dive_site = ds;
} else {
dive->dive_site_uuid = create_dive_site_with_gps("", &location, dive->when)->uuid;
dive->dive_site = create_dive_site_with_gps("", &location, dive->when);
}
} else {
ds = get_dive_site_by_uuid(uuid);
if (dive_site_has_gps_location(ds) &&
has_location(&location) && !same_location(&ds->location, &location)) {
// Houston, we have a problem
@ -1233,7 +1235,7 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf, str
default:
break;
}
if (MATCH("divesiteid", hex_value, &dive->dive_site_uuid))
if (MATCH("divesiteid", dive_site, &dive->dive_site))
return;
if (MATCH("number", get_index, &dive->number))
return;
@ -2120,7 +2122,7 @@ int parse_dlf_buffer(unsigned char *buffer, size_t size, struct dive_table *tabl
/* Measure GPS */
state.cur_location.lat.udeg = (int)((ptr[7] << 24) + (ptr[6] << 16) + (ptr[5] << 8) + (ptr[4] << 0));
state.cur_location.lon.udeg = (int)((ptr[11] << 24) + (ptr[10] << 16) + (ptr[9] << 8) + (ptr[8] << 0));
state.cur_dive->dive_site_uuid = create_dive_site_with_gps("DLF imported", &state.cur_location, state.cur_dive->when)->uuid;
state.cur_dive->dive_site = create_dive_site_with_gps("DLF imported", &state.cur_location, state.cur_dive->when);
break;
default:
break;

View file

@ -163,7 +163,7 @@ void event_end(struct parser_state *state)
bool is_dive(struct parser_state *state)
{
return state->cur_dive &&
(state->cur_dive->dive_site_uuid || state->cur_dive->when || state->cur_dive->dc.samples);
(state->cur_dive->dive_site || state->cur_dive->when || state->cur_dive->dc.samples);
}
void reset_dc_info(struct divecomputer *dc, struct parser_state *state)
@ -417,7 +417,7 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state)
char *to_free = NULL;
int size = trimspace(buffer);
if(size) {
struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
struct dive_site *ds = dive->dive_site;
if (!ds) {
// if the dive doesn't have a uuid, check if there's already a dive site by this name
ds = get_dive_site_by_name(buffer);
@ -433,10 +433,10 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state)
// way around
struct dive_site *exact_match = get_dive_site_by_gps_and_name(buffer, &ds->location);
if (exact_match) {
dive->dive_site_uuid = exact_match->uuid;
dive->dive_site = exact_match;
} else {
struct dive_site *newds = create_dive_site(buffer, dive->when);
dive->dive_site_uuid = newds->uuid;
dive->dive_site = newds;
if (has_location(&state->cur_location)) {
// we started this uuid with GPS data, so lets use those
newds->location = state->cur_location;
@ -447,10 +447,10 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state)
}
} else {
// add the existing dive site to the current dive
dive->dive_site_uuid = ds->uuid;
dive->dive_site = ds;
}
} else {
dive->dive_site_uuid = create_dive_site(buffer, dive->when)->uuid;
dive->dive_site = create_dive_site(buffer, dive->when);
}
}
free(to_free);

View file

@ -431,17 +431,15 @@ static void create_dive_buffer(struct dive *dive, struct membuffer *b)
SAVE("visibility", visibility);
cond_put_format(dive->tripflag == NO_TRIP, b, "notrip\n");
save_tags(b, dive->tag_list);
cond_put_format(dive->dive_site_uuid && get_dive_site_by_uuid(dive->dive_site_uuid),
b, "divesiteid %08x\n", dive->dive_site_uuid);
if (verbose && dive->dive_site_uuid && !get_dive_site_by_uuid(dive->dive_site_uuid))
fprintf(stderr, "removed reference to non-existant dive site with uuid %08x\n", dive->dive_site_uuid);
cond_put_format(!!dive->dive_site, b, "divesiteid %08x\n", dive->dive_site->uuid);
if (verbose && dive->dive_site)
fprintf(stderr, "removed reference to non-existant dive site with uuid %08x\n", dive->dive_site->uuid);
save_overview(b, dive);
save_cylinder_info(b, dive);
save_weightsystem_info(b, dive);
save_dive_temperature(b, dive);
}
/*
* libgit2 has a "git_treebuilder" concept, but it's broken, and can not
* be used to do a flat tree (like the git "index") nor a recursive tree.
@ -887,8 +885,8 @@ static void save_divesites(git_repository *repo, struct dir *tree)
int j;
struct dive *d;
for_each_dive(j, d) {
if (d->dive_site_uuid == ds->uuid)
d->dive_site_uuid = 0;
if (d->dive_site == ds)
d->dive_site = NULL;
}
delete_dive_site(ds);
i--; // since we just deleted that one

View file

@ -483,11 +483,8 @@ void save_one_dive_to_mb(struct membuffer *b, struct dive *dive, bool anonymize)
if (dive->visibility)
put_format(b, " visibility='%d'", dive->visibility);
save_tags(b, dive->tag_list);
if (dive->dive_site_uuid) {
if (get_dive_site_by_uuid(dive->dive_site_uuid) != NULL)
put_format(b, " divesiteid='%8x'", dive->dive_site_uuid);
else if (verbose)
fprintf(stderr, "removed reference to non-existant dive site with uuid %08x\n", dive->dive_site_uuid);
if (dive->dive_site) {
put_format(b, " divesiteid='%8x'", dive->dive_site->uuid);
}
show_date(b, dive->when);
if (dive->dc.duration.seconds > 0)
@ -602,8 +599,8 @@ void save_dives_buffer(struct membuffer *b, const bool select_only, bool anonymi
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_uuid == ds->uuid)
d->dive_site_uuid = 0;
if (d->dive_site == ds)
d->dive_site = NULL;
}
delete_dive_site(ds);
i--; // since we just deleted that one

View file

@ -112,7 +112,7 @@ QString DiveObjectHelper::location() const
QString DiveObjectHelper::gps() const
{
struct dive_site *ds = get_dive_site_by_uuid(m_dive->dive_site_uuid);
struct dive_site *ds = m_dive->dive_site;
return ds ? QString(printGPSCoords(&ds->location)) : QString();
}
@ -129,7 +129,7 @@ QString DiveObjectHelper::gps_decimal() const
QVariant DiveObjectHelper::dive_site() const
{
return QVariant::fromValue((uintptr_t)get_dive_site_by_uuid(m_dive->dive_site_uuid));
return QVariant::fromValue((uintptr_t)m_dive->dive_site);
}
QString DiveObjectHelper::duration() const

View file

@ -993,11 +993,11 @@ static bool process_raw_buffer(device_data_t *devdata, uint32_t deviceid, char *
int divespot_id = atoi(val);
if (divespot_id != -1) {
struct dive_site *ds = create_dive_site("from Uemis", dive->when);
dive->dive_site_uuid = ds->uuid;
dive->dive_site = ds;
uemis_mark_divelocation(dive->dc.diveid, divespot_id, ds);
}
#if UEMIS_DEBUG & 2
fprintf(debugfile, "Created divesite %d for diveid : %d\n", dive->dive_site_uuid, dive->dc.diveid);
fprintf(debugfile, "Created divesite %d for diveid : %d\n", dive->dive_site->uuid, dive->dc.diveid);
#endif
} else if (dive) {
parse_tag(dive, tag, val);
@ -1175,11 +1175,11 @@ static bool load_uemis_divespot(const char *mountpath, int divespot_id)
static void get_uemis_divespot(const char *mountpath, int divespot_id, struct dive *dive)
{
struct dive_site *nds = get_dive_site_by_uuid(dive->dive_site_uuid);
struct dive_site *nds = dive->dive_site;
if (is_divespot_mappable(divespot_id)) {
struct dive_site *ds = get_dive_site_by_divespot_id(divespot_id);
dive->dive_site_uuid = ds ? ds->uuid : 0;
dive->dive_site = ds;
} else if (nds && nds->name && strstr(nds->name,"from Uemis")) {
if (load_uemis_divespot(mountpath, divespot_id)) {
/* get the divesite based on the diveid, this should give us
@ -1195,15 +1195,15 @@ static void get_uemis_divespot(const char *mountpath, int divespot_id, struct di
/* if the uuid's are the same, the new site is a duplicate and can be deleted */
if (nds->uuid != ods->uuid) {
delete_dive_site(nds);
dive->dive_site_uuid = ods->uuid;
dive->dive_site = ods;
}
}
add_to_divespot_mapping(divespot_id, get_dive_site_by_uuid(dive->dive_site_uuid));
add_to_divespot_mapping(divespot_id, dive->dive_site);
} else {
/* if we can't load the dive site details, delete the site we
* created in process_raw_buffer
*/
delete_dive_site(get_dive_site_by_uuid(dive->dive_site_uuid));
delete_dive_site(dive->dive_site);
}
}
}