Skip to content

Commit

Permalink
Make utf8_validate_or_convert() to always allocate a new string.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Monin committed May 30, 2008
1 parent ab16da5 commit 639e668
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 45 deletions.
36 changes: 23 additions & 13 deletions src/bar_exif.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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"));
Expand All @@ -216,20 +219,22 @@ static void bar_exif_update(ExifBar *eb)
while (list && i < EXIF_BAR_CUSTOM_COUNT)
{
gchar *text;
gchar *utf8_text;
gchar *name;
gchar *buf;

name = list->data;
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]);
Expand Down Expand Up @@ -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);
}
Expand Down
30 changes: 20 additions & 10 deletions src/bar_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/logwindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
12 changes: 3 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,17 @@ 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;

if (!text) return NULL;

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 */
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 17 additions & 11 deletions src/pan-view.c
Original file line number Diff line number Diff line change
Expand Up @@ -1444,40 +1444,46 @@ 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"));
if (work) pan_text_alignment_add(ta, "---", NULL);
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);
Expand Down

0 comments on commit 639e668

Please sign in to comment.