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 /save-xml.c | |
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>
Diffstat (limited to 'save-xml.c')
-rw-r--r-- | save-xml.c | 35 |
1 files changed, 12 insertions, 23 deletions
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) |