Dive site: add dive site ref-counting

Instead of setting dive->dive_site directly, call the
add_dive_to_dive_site() and unregister_dive_from_dive_site()
functions. In the parser this turned out to be a bit tricky.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-03-05 22:58:47 +01:00 committed by Dirk Hohndel
parent c22fd9f4fd
commit e2df38d868
19 changed files with 121 additions and 75 deletions

View file

@ -558,11 +558,11 @@ static void hex_value(char *buffer, uint32_t *i)
*i = strtoul(buffer, NULL, 16);
}
static void dive_site(char *buffer, struct dive_site **ds, struct parser_state *state)
static void dive_site(char *buffer, struct dive *d, struct parser_state *state)
{
uint32_t uuid;
hex_value(buffer, &uuid);
*ds = get_dive_site_by_uuid(uuid, state->sites);
add_dive_to_dive_site(d, get_dive_site_by_uuid(uuid, state->sites));
}
static void get_notrip(char *buffer, bool *notrip)
@ -972,9 +972,10 @@ static void try_to_fill_sample(struct sample *sample, const char *name, char *bu
nonmatch("sample", name, buf);
}
static void divinglog_place(char *place, struct dive_site **ds, struct parser_state *state)
static void divinglog_place(char *place, struct dive *d, struct parser_state *state)
{
char buffer[1024];
struct dive_site *ds;
snprintf(buffer, sizeof(buffer),
"%s%s%s%s%s",
@ -983,9 +984,10 @@ static void divinglog_place(char *place, struct dive_site **ds, struct parser_st
state->city ? state->city : "",
state->country ? ", " : "",
state->country ? state->country : "");
*ds = get_dive_site_by_name(buffer, state->sites);
if (!*ds)
*ds = create_dive_site(buffer, state->sites);
ds = get_dive_site_by_name(buffer, state->sites);
if (!ds)
ds = create_dive_site(buffer, state->sites);
add_dive_to_dive_site(d, ds);
// TODO: capture the country / city info in the taxonomy instead
free(state->city);
@ -1010,7 +1012,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) ||
MATCH_STATE("name.place", divinglog_place, dive) ||
0;
}
@ -1137,7 +1139,7 @@ static void gps_lat(char *buffer, struct dive *dive, struct parser_state *state)
location.lat = parse_degrees(buffer, &end);
if (!ds) {
dive->dive_site = create_dive_site_with_gps(NULL, &location, state->sites);
add_dive_to_dive_site(dive, create_dive_site_with_gps(NULL, &location, state->sites));
} 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)");
@ -1153,7 +1155,7 @@ static void gps_long(char *buffer, struct dive *dive, struct parser_state *state
location.lon = parse_degrees(buffer, &end);
if (!ds) {
dive->dive_site = create_dive_site_with_gps(NULL, &location, state->sites);
add_dive_to_dive_site(dive, create_dive_site_with_gps(NULL, &location, state->sites));
} 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)");
@ -1189,10 +1191,10 @@ 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 = ds;
} else {
dive->dive_site = create_dive_site_with_gps("", &location, state->sites);
ds = create_dive_site_with_gps("", &location, state->sites);
}
add_dive_to_dive_site(dive, ds);
} else {
if (dive_site_has_gps_location(ds) &&
has_location(&location) && !same_location(&ds->location, &location)) {
@ -1234,7 +1236,7 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf, str
default:
break;
}
if (MATCH_STATE("divesiteid", dive_site, &dive->dive_site))
if (MATCH_STATE("divesiteid", dive_site, dive))
return;
if (MATCH("number", get_index, &dive->number))
return;
@ -2122,7 +2124,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 = create_dive_site_with_gps("DLF imported", &state.cur_location, state.sites);
add_dive_to_dive_site(state.cur_dive, create_dive_site_with_gps("DLF imported", &state.cur_location, state.sites));
break;
default:
break;