Move add_dive_site to parse.c

Signed-off-by: Miika Turkia <miika.turkia@gmail.com>
This commit is contained in:
Miika Turkia 2017-11-27 20:01:19 +02:00 committed by Dirk Hohndel
parent 1c6e189c2b
commit ddb9dba66c
3 changed files with 68 additions and 66 deletions

View file

@ -1140,72 +1140,6 @@ static void gps_in_dive(char *buffer, struct dive *dive)
}
}
static void add_dive_site(char *ds_name, struct dive *dive)
{
static int suffix = 1;
char *buffer = ds_name;
char *to_free = NULL;
int size = trimspace(buffer);
if(size) {
uint32_t uuid = dive->dive_site_uuid;
struct dive_site *ds = get_dive_site_by_uuid(uuid);
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
uuid = get_dive_site_uuid_by_name(buffer, &ds);
if (uuid && import_source == SSRF_WS) {
// when downloading GPS fixes from the Subsurface webservice we will often
// get a lot of dives with identical names (the autogenerated fixes).
// So in this case modify the name to make it unique
int name_size = strlen(buffer) + 10; // 8 digits - enough for 100 million sites
to_free = buffer = malloc(name_size);
do {
suffix++;
snprintf(buffer, name_size, "%s %8d", ds_name, suffix);
} while (get_dive_site_uuid_by_name(buffer, NULL) != 0);
ds = NULL;
}
}
if (ds) {
// we have a uuid, let's hope there isn't a different name
if (same_string(ds->name, "")) {
ds->name = copy_string(buffer);
} else if (!same_string(ds->name, buffer)) {
// if it's not the same name, it's not the same dive site
// 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
// way around
uint32_t exact_match_uuid = get_dive_site_uuid_by_gps_and_name(buffer, ds->latitude, ds->longitude);
if (exact_match_uuid) {
dive->dive_site_uuid = exact_match_uuid;
} else {
dive->dive_site_uuid = create_dive_site(buffer, dive->when);
struct dive_site *newds = get_dive_site_by_uuid(dive->dive_site_uuid);
if (cur_latitude.udeg || cur_longitude.udeg) {
// we started this uuid with GPS data, so lets use those
newds->latitude = cur_latitude;
newds->longitude = cur_longitude;
} else {
newds->latitude = ds->latitude;
newds->longitude = ds->longitude;
}
newds->notes = add_to_string(newds->notes, translate("gettextFromC", "additional name for site: %s\n"), ds->name);
}
} else {
// add the existing dive site to the current dive
dive->dive_site_uuid = uuid;
}
} else {
dive->dive_site_uuid = create_dive_site(buffer, dive->when);
}
}
free(to_free);
}
static void gps_picture_location(char *buffer, struct picture *pic)
{
char *end;

View file

@ -447,3 +447,69 @@ void utf8_string(char *buffer, void *_res)
*res = strdup(buffer);
}
void add_dive_site(char *ds_name, struct dive *dive)
{
static int suffix = 1;
char *buffer = ds_name;
char *to_free = NULL;
int size = trimspace(buffer);
if(size) {
uint32_t uuid = dive->dive_site_uuid;
struct dive_site *ds = get_dive_site_by_uuid(uuid);
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
uuid = get_dive_site_uuid_by_name(buffer, &ds);
if (uuid && import_source == SSRF_WS) {
// when downloading GPS fixes from the Subsurface webservice we will often
// get a lot of dives with identical names (the autogenerated fixes).
// So in this case modify the name to make it unique
int name_size = strlen(buffer) + 10; // 8 digits - enough for 100 million sites
to_free = buffer = malloc(name_size);
do {
suffix++;
snprintf(buffer, name_size, "%s %8d", ds_name, suffix);
} while (get_dive_site_uuid_by_name(buffer, NULL) != 0);
ds = NULL;
}
}
if (ds) {
// we have a uuid, let's hope there isn't a different name
if (same_string(ds->name, "")) {
ds->name = copy_string(buffer);
} else if (!same_string(ds->name, buffer)) {
// if it's not the same name, it's not the same dive site
// 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
// way around
uint32_t exact_match_uuid = get_dive_site_uuid_by_gps_and_name(buffer, ds->latitude, ds->longitude);
if (exact_match_uuid) {
dive->dive_site_uuid = exact_match_uuid;
} else {
dive->dive_site_uuid = create_dive_site(buffer, dive->when);
struct dive_site *newds = get_dive_site_by_uuid(dive->dive_site_uuid);
if (cur_latitude.udeg || cur_longitude.udeg) {
// we started this uuid with GPS data, so lets use those
newds->latitude = cur_latitude;
newds->longitude = cur_longitude;
} else {
newds->latitude = ds->latitude;
newds->longitude = ds->longitude;
}
newds->notes = add_to_string(newds->notes, translate("gettextFromC", "additional name for site: %s\n"), ds->name);
}
} else {
// add the existing dive site to the current dive
dive->dive_site_uuid = uuid;
}
} else {
dive->dive_site_uuid = create_dive_site(buffer, dive->when);
}
}
free(to_free);
}

View file

@ -93,4 +93,6 @@ void userid_start(void);
void userid_stop(void);
void utf8_string(char *buffer, void *_res);
void add_dive_site(char *ds_name, struct dive *dive);
#endif