diff --git a/dive.h b/dive.h index 91694f8c3..1028a6919 100644 --- a/dive.h +++ b/dive.h @@ -532,6 +532,9 @@ extern void flush_divelist(struct dive *dive); extern void set_dc_nickname(struct dive *dive); extern const char *get_dc_nickname(const char *model, uint32_t deviceid); extern void remember_dc(const char *model, uint32_t deviceid, const char *nickname, gboolean change_conf); +extern gboolean dc_was_saved(struct divecomputer *dc); +extern void mark_dc_saved(struct divecomputer *dc); +extern void clear_dc_saved_status(void); #define DIVE_ERROR_PARSE 1 diff --git a/gtk-gui.c b/gtk-gui.c index 60a1f18d3..a6ed46942 100644 --- a/gtk-gui.c +++ b/gtk-gui.c @@ -44,6 +44,7 @@ struct dcnicknamelist { const char *model; uint32_t deviceid; struct dcnicknamelist *next; + gboolean saved; }; static struct dcnicknamelist *nicknamelist; char *nicknamestring; @@ -2078,6 +2079,29 @@ const char *get_dc_nickname(const char *model, uint32_t deviceid) return NULL; } +gboolean dc_was_saved(struct divecomputer *dc) +{ + struct dcnicknamelist *nn_entry = get_dc_nicknameentry(dc->model, dc->deviceid); + return nn_entry && nn_entry->saved; +} + +void mark_dc_saved(struct divecomputer *dc) +{ + struct dcnicknamelist *nn_entry = get_dc_nicknameentry(dc->model, dc->deviceid); + if (nn_entry) + nn_entry->saved = TRUE; +} + +void clear_dc_saved_status() +{ + struct dcnicknamelist *nn_entry = nicknamelist; + + while (nn_entry) { + nn_entry->saved = FALSE; + nn_entry = nn_entry->next; + } +} + /* do we have a DIFFERENT divecomputer of the same model? */ static struct dcnicknamelist *get_different_dc_nicknameentry(const char *model, int deviceid) { diff --git a/save-xml.c b/save-xml.c index 0cfc7ccfd..c85f66408 100644 --- a/save-xml.c +++ b/save-xml.c @@ -461,36 +461,27 @@ static void save_trip(FILE *f, dive_trip_t *trip) fprintf(f, "\n"); } -static char *add_dc_to_string(char *dc_xml, struct divecomputer *dc) +static void save_dc_if_needed(FILE *f, struct divecomputer *dc) { - char *pattern, *tmp; const char *nickname; - int len; /* we have no dc or no model or no deviceid information... nothing to do here */ if (!dc || !dc->model || !*dc->model || !dc->deviceid) - return dc_xml; + return; + if (dc_was_saved(dc)) + return; + + mark_dc_saved(dc); nickname = get_dc_nickname(dc->model, dc->deviceid); /* We have no nickname, or it is the same as the model ID - nothing interesting */ if (!nickname || !*nickname || !strcmp(dc->model, nickname)) - return dc_xml; + return; - len = sizeof(" model='' deviceid=''") + strlen(dc->model) + 8; - pattern = malloc(len); - snprintf(pattern, len, " model='%s' deviceid='%08x'", dc->model, dc->deviceid); - if (dc_xml && strstr(dc_xml, pattern)) { - /* already have that one */ - free(pattern); - return dc_xml; - } - - len += strlen(dc_xml) + strlen(nickname) + sizeof("\n"); - tmp = malloc(len); - snprintf(tmp, len, "%s\n", dc_xml, pattern, nickname); - free(pattern); - free(dc_xml); - return tmp; + fprintf(f, "model, dc->deviceid); + show_utf8(f, nickname, " nickname='", "'", 1); + fprintf(f, "/>\n"); + return; } #define VERSION 2 @@ -500,7 +491,6 @@ void save_dives(const char *filename) int i; struct dive *dive; dive_trip_t *trip; - char *dc_xml = strdup(""); FILE *f = g_fopen(filename, "w"); @@ -514,11 +504,10 @@ void save_dives(const char *filename) for_each_dive(i, dive) { struct divecomputer *dc = &dive->dc; while (dc) { - dc_xml = add_dc_to_string(dc_xml, dc); + save_dc_if_needed(f, dc); dc = dc->next; } } - fprintf(f, dc_xml); fprintf(f, "\n\n"); for (trip = dive_trip_list; trip != NULL; trip = trip->next)