changeset 767:e73d30e0c896

Make utf8_validate_or_convert() to always allocate a new string.
author zas_
date Fri, 30 May 2008 08:36:23 +0000
parents 7148e125bf23
children ff51413f098d
files src/bar_exif.c src/bar_info.c src/logwindow.c src/main.c src/main.h src/pan-view.c
diffstat 6 files changed, 66 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/bar_exif.c	Fri May 30 07:20:25 2008 +0000
+++ b/src/bar_exif.c	Fri May 30 08:36:23 2008 +0000
@@ -181,6 +181,7 @@
 		for (i = 0; ExifUIList[i].key; i++)
 			{
 			gchar *text;
+			gchar *utf8_text;
 
 			if (ExifUIList[i].current == EXIF_UI_OFF)
 				{
@@ -189,18 +190,20 @@
 				continue;
 				}
 			text = exif_get_data_as_text(exif, ExifUIList[i].key);
-			text = utf8_validate_or_convert(text);
+			utf8_text = utf8_validate_or_convert(text);
+			g_free(text);
 			if (ExifUIList[i].current == EXIF_UI_IFSET
-			    && (!text || !*text))
+			    && (!utf8_text || !*utf8_text))
 				{
 				gtk_widget_hide(eb->labels[i]);
 				gtk_widget_hide(eb->keys[i]);
+				g_free(utf8_text);
 				continue;
 				}
 			gtk_widget_show(eb->labels[i]);
 			gtk_widget_show(eb->keys[i]);
-			gtk_label_set_text(GTK_LABEL(eb->labels[i]), text);
-			g_free(text);
+			gtk_label_set_text(GTK_LABEL(eb->labels[i]), utf8_text);
+			g_free(utf8_text);
 			}
 
 		list = g_list_last(history_list_get_by_key("exif_extras"));
@@ -216,6 +219,7 @@
 		while (list && i < EXIF_BAR_CUSTOM_COUNT)
 			{
 			gchar *text;
+			gchar *utf8_text;
 			gchar *name;
 			gchar *buf;
 
@@ -223,13 +227,14 @@
 			list = list->prev;
 
 			text = exif_get_data_as_text(exif, name);
-			text = utf8_validate_or_convert(text);
+			utf8_text = utf8_validate_or_convert(text);
+			g_free(text);
 
 			buf = g_strconcat(name, ":", NULL);
 			gtk_label_set_text(GTK_LABEL(eb->custom_name[i]), buf);
 			g_free(buf);
-			gtk_label_set_text(GTK_LABEL(eb->custom_value[i]), text);
-			g_free(text);
+			gtk_label_set_text(GTK_LABEL(eb->custom_value[i]), utf8_text);
+			g_free(utf8_text);
 
 			gtk_widget_show(eb->custom_name[i]);
 			gtk_widget_show(eb->custom_value[i]);
@@ -259,32 +264,37 @@
 			gchar *tag;
 			gchar *tag_name;
 			gchar *text;
+			gchar *utf8_text;
 			const gchar *format;
 			gchar *elements;
 			gchar *description;
+			gchar *utf8_description;
 
 			tag = g_strdup_printf("0x%04x", exif_item_get_tag_id(item));
 			tag_name = exif_item_get_tag_name(item);
 			format = exif_item_get_format_name(item, TRUE);
 			text = exif_item_get_data_as_text(item);
-			text = utf8_validate_or_convert(text);
+			utf8_text = utf8_validate_or_convert(text);
+			g_free(text);
 			elements = g_strdup_printf("%d", exif_item_get_elements(item));
 			description = exif_item_get_description(item);
 			if (!description) description = g_strdup("");
-			description = utf8_validate_or_convert(description);
+			utf8_description = utf8_validate_or_convert(description);
+			g_free(description);
+
 			gtk_list_store_append(store, &iter);
 			gtk_list_store_set(store, &iter,
 					EXIF_ADVCOL_ENABLED, bar_exif_row_enabled(tag_name),
 					EXIF_ADVCOL_TAG, tag,
 					EXIF_ADVCOL_NAME, tag_name,
-					EXIF_ADVCOL_VALUE, text,
+					EXIF_ADVCOL_VALUE, utf8_text,
 					EXIF_ADVCOL_FORMAT, format,
 					EXIF_ADVCOL_ELEMENTS, elements,
-					EXIF_ADVCOL_DESCRIPTION, description, -1);
+					EXIF_ADVCOL_DESCRIPTION, utf8_description, -1);
 			g_free(tag);
-			g_free(text);
+			g_free(utf8_text);
 			g_free(elements);
-			g_free(description);
+			g_free(utf8_description);
 			g_free(tag_name);
 			item = exif_get_next_item(exif);
 			}
--- a/src/bar_info.c	Fri May 30 07:20:25 2008 +0000
+++ b/src/bar_info.c	Fri May 30 08:36:23 2008 +0000
@@ -143,7 +143,7 @@
 	f = fopen(path, "r");
 	if (!f) return FALSE;
 
-	while (fgets(s_buf,sizeof(s_buf), f))
+	while (fgets(s_buf, sizeof(s_buf), f))
 		{
 		gchar *ptr = s_buf;
 
@@ -176,7 +176,7 @@
 				*ptr = '\0';
 				if (strlen(s_buf) > 0)
 					{
-					gchar *kw = utf8_validate_or_convert(g_strdup(s_buf));
+					gchar *kw = utf8_validate_or_convert(s_buf);
 
 					list = g_list_prepend(list, kw);
 					}
@@ -206,7 +206,10 @@
 			if (ptr[len] == '\n') len++; /* keep the last one */
 			if (len > 0)
 				{
-				*comment = utf8_validate_or_convert(g_strndup(ptr, len));
+				gchar *text = g_strndup(ptr, len);
+
+				*comment = utf8_validate_or_convert(text);
+				g_free(text);
 				}
 			}
 		g_string_free(comment_build, TRUE);
@@ -291,10 +294,12 @@
 
 	if (comment)
 		{
+		gchar *text;
 		ExifItem *item = exif_get_item(exif, COMMENT_KEY);
 
-		*comment = exif_item_get_string(item, 0);
-		*comment = utf8_validate_or_convert(*comment);
+		text = exif_item_get_string(item, 0);
+		*comment = utf8_validate_or_convert(text);
+		g_free(text);
 		}
 
 	if (keywords)
@@ -307,10 +312,13 @@
 		for (i = 0; i < exif_item_get_elements(item); i++)
 			{
 			gchar *kw = exif_item_get_string(item, i);
+			gchar *utf8_kw;
 
-			kw = utf8_validate_or_convert(kw);
 			if (!kw) break;
-			*keywords = g_list_append(*keywords, (gpointer) kw);
+
+			utf8_kw = utf8_validate_or_convert(kw);
+			*keywords = g_list_append(*keywords, (gpointer) utf8_kw);
+			g_free(kw);
 			}
 
 		/* FIXME:
@@ -333,12 +341,14 @@
 				if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
 					{
 					gchar *kw;
+					gchar *utf8_kw;
 
 					kw = exif_item_get_data_as_text(item);
-					kw = utf8_validate_or_convert(kw);
-					
 					if (!kw) continue;
-					*keywords = g_list_append(*keywords, (gpointer) kw);
+
+					utf8_kw = utf8_validate_or_convert(kw);
+					*keywords = g_list_append(*keywords, (gpointer) utf8_kw);
+					g_free(kw);
 					}
 				g_free(tag_name);
 				}
--- a/src/logwindow.c	Fri May 30 07:20:25 2008 +0000
+++ b/src/logwindow.c	Fri May 30 08:36:23 2008 +0000
@@ -198,8 +198,9 @@
 
 	if (!text || !*text) return;
 
-	str_utf8 = utf8_validate_or_convert((gchar *)text);
+	str_utf8 = utf8_validate_or_convert(text);
 	gtk_text_buffer_insert_with_tags_by_name(buffer, iter, str_utf8, -1, tag, NULL);
+	g_free(str_utf8);
 }
 
 
--- a/src/main.c	Fri May 30 07:20:25 2008 +0000
+++ b/src/main.c	Fri May 30 08:36:23 2008 +0000
@@ -52,7 +52,7 @@
 	return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
 }
 
-gchar *utf8_validate_or_convert(gchar *text)
+gchar *utf8_validate_or_convert(const gchar *text)
 {
 	gint len;
 
@@ -60,15 +60,9 @@
 	
 	len = strlen(text);
 	if (!g_utf8_validate(text, len, NULL))
-		{
-		gchar *conv_text;
+		return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
 
-		conv_text = g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
-		g_free(text);
-		text = conv_text;
-		}
-
-	return text;
+	return g_strdup(text);
 }
 
 /* Borrowed from gtkfilesystemunix.c */
--- a/src/main.h	Fri May 30 07:20:25 2008 +0000
+++ b/src/main.h	Fri May 30 08:36:23 2008 +0000
@@ -133,7 +133,7 @@
  */
 
 gdouble get_zoom_increment(void);
-gchar *utf8_validate_or_convert(gchar *text);
+gchar *utf8_validate_or_convert(const gchar *text);
 gchar *expand_tilde(const gchar *filename);
 
 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event);
--- a/src/pan-view.c	Fri May 30 07:20:25 2008 +0000
+++ b/src/pan-view.c	Fri May 30 08:36:23 2008 +0000
@@ -1444,21 +1444,23 @@
 		{
 		gchar *label;
 		gchar *text;
+		gchar *utf8_text;
 
 		if (ExifUIList[i].current == EXIF_UI_OFF) continue;
 
 		text = exif_get_data_as_text(exif, ExifUIList[i].key);
-		text = utf8_validate_or_convert(text);
 		if (ExifUIList[i].current == EXIF_UI_IFSET && (!text || !*text))
 			{
-			if (text) g_free(text);
+			g_free(text);
 			continue;
 			}
-
+		
 		label = g_strdup_printf("%s:", exif_get_description_by_key(ExifUIList[i].key));
-		pan_text_alignment_add(ta, label, text);
+		utf8_text = utf8_validate_or_convert(text);
+		g_free(text);
+		pan_text_alignment_add(ta, label, utf8_text);
 		g_free(label);
-		g_free(text);
+		g_free(utf8_text);
 		}
 
 	work = g_list_last(history_list_get_by_key("exif_extras"));
@@ -1466,18 +1468,22 @@
 	while (work)
 		{
 		const gchar *name;
-		gchar *label;
 		gchar *text;
 
 		name = work->data;
 		work = work->prev;
 
-		label = g_strdup_printf("%s:", name);
 		text = exif_get_data_as_text(exif, name);
-		text = utf8_validate_or_convert(text);
-		pan_text_alignment_add(ta, label, text);
-		g_free(label);
-		g_free(text);
+		if (text)
+			{
+			gchar *label = g_strdup_printf("%s:", name);
+			gchar *utf8_text = utf8_validate_or_convert(text);
+
+			g_free(text);
+			pan_text_alignment_add(ta, label, utf8_text);
+			g_free(label);
+			g_free(utf8_text);
+			}
 		}
 
 	exif_free(exif);