diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2012-12-30 20:27:01 -0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2012-12-30 20:27:01 -0800 |
commit | df0ea072920668de4517ad85c742c7169bd24f22 (patch) | |
tree | 27d5a5bfa96fb7bc7155b5dea988e048ae0ad27e | |
parent | 96db56f89c76ffc2ede8605f4b0febb0c2261c5a (diff) | |
download | subsurface-df0ea072920668de4517ad85c742c7169bd24f22.tar.gz |
Fix nickname saving in XML file to deal with utf8 characters
This makes the whole code much cleaner and simpler.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | dive.h | 3 | ||||
-rw-r--r-- | gtk-gui.c | 24 | ||||
-rw-r--r-- | save-xml.c | 35 |
3 files changed, 39 insertions, 23 deletions
@@ -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 @@ -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, "</trip>\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; - - 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; - } + return; - len += strlen(dc_xml) + strlen(nickname) + sizeof("<divecomputerid nickname=''/>\n"); - tmp = malloc(len); - snprintf(tmp, len, "%s<divecomputerid%s nickname='%s'/>\n", dc_xml, pattern, nickname); - free(pattern); - free(dc_xml); - return tmp; + fprintf(f, "<divecomputerid model='%s' deviceid='%08x'", dc->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, "</settings>\n<dives>\n"); for (trip = dive_trip_list; trip != NULL; trip = trip->next) |