mirror of
				https://github.com/subsurface/subsurface.git
				synced 2025-02-19 22:16:15 +00:00 
			
		
		
		
	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>
This commit is contained in:
		
							parent
							
								
									96db56f89c
								
							
						
					
					
						commit
						df0ea07292
					
				
					 3 changed files with 39 additions and 23 deletions
				
			
		
							
								
								
									
										35
									
								
								save-xml.c
									
										
									
									
									
								
							
							
						
						
									
										35
									
								
								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;
 | 
			
		||||
		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("<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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue