summaryrefslogtreecommitdiffstats
path: root/core/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/parse.c')
-rw-r--r--core/parse.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/core/parse.c b/core/parse.c
index ba440f1b9..228b8d59f 100644
--- a/core/parse.c
+++ b/core/parse.c
@@ -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);
+}
+