Make utf8_validate_or_convert() to always allocate a new string.
authorLaurent Monin <geeqie@norz.org>
Fri, 30 May 2008 08:36:23 +0000 (08:36 +0000)
committerLaurent Monin <geeqie@norz.org>
Fri, 30 May 2008 08:36:23 +0000 (08:36 +0000)
src/bar_exif.c
src/bar_info.c
src/logwindow.c
src/main.c
src/main.h
src/pan-view.c

index 04d964c..0a44997 100644 (file)
@@ -181,6 +181,7 @@ static void bar_exif_update(ExifBar *eb)
                for (i = 0; ExifUIList[i].key; i++)
                        {
                        gchar *text;
+                       gchar *utf8_text;
 
                        if (ExifUIList[i].current == EXIF_UI_OFF)
                                {
@@ -189,18 +190,20 @@ static void bar_exif_update(ExifBar *eb)
                                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 @@ static void bar_exif_update(ExifBar *eb)
                while (list && i < EXIF_BAR_CUSTOM_COUNT)
                        {
                        gchar *text;
+                       gchar *utf8_text;
                        gchar *name;
                        gchar *buf;
 
@@ -223,13 +227,14 @@ static void bar_exif_update(ExifBar *eb)
                        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 @@ static void bar_exif_update(ExifBar *eb)
                        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);
                        }
index d21ae6e..1a1891a 100644 (file)
@@ -143,7 +143,7 @@ static gint comment_file_read(gchar *path, GList **keywords, gchar **comment)
        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 @@ static gint comment_file_read(gchar *path, GList **keywords, gchar **comment)
                                *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 @@ static gint comment_file_read(gchar *path, GList **keywords, gchar **comment)
                        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 @@ static gint comment_xmp_read(FileData *fd, GList **keywords, gchar **comment)
 
        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 @@ static gint comment_xmp_read(FileData *fd, GList **keywords, gchar **comment)
                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 @@ static gint comment_xmp_read(FileData *fd, GList **keywords, gchar **comment)
                                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);
                                }
index 793f4fb..55325ae 100644 (file)
@@ -198,8 +198,9 @@ static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
 
        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);
 }
 
 
index 13b9afd..5073c81 100644 (file)
@@ -52,7 +52,7 @@ gdouble get_zoom_increment(void)
        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 @@ gchar *utf8_validate_or_convert(gchar *text)
        
        len = strlen(text);
        if (!g_utf8_validate(text, len, NULL))
-               {
-               gchar *conv_text;
-
-               conv_text = g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
-               g_free(text);
-               text = conv_text;
-               }
+               return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
 
-       return text;
+       return g_strdup(text);
 }
 
 /* Borrowed from gtkfilesystemunix.c */
index 0800302..fb95d68 100644 (file)
  */
 
 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);
index 248f890..876ec49 100644 (file)
@@ -1444,21 +1444,23 @@ static void pan_info_add_exif(PanTextAlignment *ta, FileData *fd)
                {
                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);
-               g_free(label);
+               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);
                }
 
        work = g_list_last(history_list_get_by_key("exif_extras"));
@@ -1466,18 +1468,22 @@ static void pan_info_add_exif(PanTextAlignment *ta, FileData *fd)
        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);