Skip to content

Commit

Permalink
Simplify string array related code
Browse files Browse the repository at this point in the history
Use more g_auto(GStrv) and g_strv_* functions.
Deduplicate uri-utils functions.
  • Loading branch information
qarkai authored and caclark committed Dec 31, 2024
1 parent f7edbe1 commit fe17beb
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 171 deletions.
12 changes: 5 additions & 7 deletions src/bar-gps.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ void bar_pane_gps_dnd_receive(GtkWidget *pane, GdkDragContext *,
gdouble latitude;
gdouble longitude;
GString *message;
gchar **latlong;

pgd = static_cast<PaneGPSData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
if (!pgd) return;
Expand Down Expand Up @@ -242,11 +241,10 @@ void bar_pane_gps_dnd_receive(GtkWidget *pane, GdkDragContext *,
g_autofree gchar *location = decode_geo_parameters(reinterpret_cast<const gchar *>(gtk_selection_data_get_data(selection_data)));
if (!(g_strstr_len(location,-1,"Error")))
{
latlong = g_strsplit(location, " ", 2);
g_auto(GStrv) latlong = g_strsplit(location, " ", 2);
champlain_view_center_on(CHAMPLAIN_VIEW(pgd->gps_view),
g_ascii_strtod(latlong[0],nullptr),
g_ascii_strtod(latlong[1],nullptr));
g_strfreev(latlong);
g_ascii_strtod(latlong[0], nullptr),
g_ascii_strtod(latlong[1], nullptr));
}
}
}
Expand Down Expand Up @@ -917,7 +915,6 @@ GtkWidget *bar_pane_gps_new(const gchar *id, const gchar *title, const gchar *ma
ChamplainMarkerLayer *layer;
ChamplainView *view;
const gchar *slider_list[] = {GQ_ICON_ZOOM_IN, GQ_ICON_ZOOM_OUT, nullptr};
const gchar **slider_icons = slider_list;

pgd = g_new0(PaneGPSData, 1);

Expand All @@ -943,9 +940,10 @@ GtkWidget *bar_pane_gps_new(const gchar *id, const gchar *title, const gchar *ma

status = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
#if HAVE_GTK4
const gchar **slider_icons = slider_list;
slider = gtk_scale_button_new(1, 17, 1, slider_icons);
#else
slider = gtk_scale_button_new(GTK_ICON_SIZE_SMALL_TOOLBAR, 1, 17, 1, slider_icons);
slider = gtk_scale_button_new(GTK_ICON_SIZE_SMALL_TOOLBAR, 1, 17, 1, slider_list);
#endif
gtk_widget_set_tooltip_text(slider, _("Zoom"));
gtk_scale_button_set_value(GTK_SCALE_BUTTON(slider), static_cast<gdouble>(zoom));
Expand Down
8 changes: 3 additions & 5 deletions src/bar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -700,14 +700,12 @@ void bar_add(GtkWidget *bar, GtkWidget *pane)

void bar_populate_default(GtkWidget *)
{
const gchar *populate_id[] = {"histogram", "title", "keywords", "comment", "rating", "exif", nullptr};
const gchar **id = populate_id;
const gchar *populate_id[] = {"histogram", "title", "keywords", "comment", "rating", "exif"};

while (*id)
for (const gchar *id : populate_id)
{
const gchar *config = bar_pane_get_default_config(*id);
const gchar *config = bar_pane_get_default_config(id);
if (config) load_config_from_buf(config, strlen(config), FALSE);
id++;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/collect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ CollectionData *collection_from_number(gint n)

if (!list && !info_list) return cd;

GStrv numbers = g_strsplit(data, "\n", -1);
g_auto(GStrv) numbers = g_strsplit(data, "\n", -1);
for (gint i = 1; numbers[i] != nullptr; i++)
{
if (!numbers[i + 1]) break; // numbers[i] is data after last \n, skip it
Expand All @@ -569,7 +569,6 @@ CollectionData *collection_from_number(gint n)
if (info_list) *info_list = g_list_append(*info_list, info);
}

g_strfreev(numbers);
return cd;
}

Expand Down
17 changes: 4 additions & 13 deletions src/command-line-handling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,22 +501,20 @@ void gq_fullscreen(GtkApplication *, GApplicationCommandLine *, GVariantDict *,

void gq_geometry(GtkApplication *, GApplicationCommandLine *, GVariantDict *command_line_options_dict, GList *)
{
gchar **geometry;
gchar *text;

const gchar *text;
g_variant_dict_lookup(command_line_options_dict, "geometry", "&s", &text);

if (text[0] == '+')
{
geometry = g_strsplit_set(text, "+", 3);
g_auto(GStrv) geometry = g_strsplit_set(text, "+", 3);
if (geometry[1] != nullptr && geometry[2] != nullptr )
{
gq_gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
}
}
else
{
geometry = g_strsplit_set(text, "+x", 4);
g_auto(GStrv) geometry = g_strsplit_set(text, "+x", 4);
if (geometry[0] != nullptr && geometry[1] != nullptr)
{
gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
Expand All @@ -527,8 +525,6 @@ void gq_geometry(GtkApplication *, GApplicationCommandLine *, GVariantDict *comm
g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
}
}

g_strfreev(geometry);
}

void gq_get_collection(GtkApplication *, GApplicationCommandLine *app_command_line, GVariantDict *command_line_options_dict, GList *)
Expand Down Expand Up @@ -894,13 +890,10 @@ void gq_log_file(GtkApplication *, GApplicationCommandLine *, GVariantDict *comm
void gq_lua(GtkApplication *, GApplicationCommandLine *app_command_line, GVariantDict *command_line_options_dict, GList *)
{
#if HAVE_LUA
gchar **lua_command;

const gchar *text;
g_variant_dict_lookup(command_line_options_dict, "lua", "&s", &text);

lua_command = g_strsplit(text, ",", 2);

g_auto(GStrv) lua_command = g_strsplit(text, ",", 2);
if (lua_command[0] && lua_command[1])
{
FileData *fd = file_data_new_group(lua_command[0]);
Expand All @@ -918,8 +911,6 @@ void gq_lua(GtkApplication *, GApplicationCommandLine *app_command_line, GVarian
{
g_application_command_line_print(app_command_line, _("lua error: no data\n"));
}

g_strfreev(lua_command);
#else
g_application_command_line_print(app_command_line, _("Lua is not available\n"));
#endif
Expand Down
4 changes: 1 addition & 3 deletions src/dupe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5103,7 +5103,6 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
GList *work;
GtkTreeSelection *selection;
GList *slist;
gchar **rank_split;
GtkTreePath *tpath;
gboolean color_old = FALSE;
gboolean color_new = FALSE;
Expand Down Expand Up @@ -5165,7 +5164,7 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)

g_autofree gchar *rank = nullptr;
gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DUPE_COLUMN_RANK, &rank, -1);
rank_split = g_strsplit_set(rank, " [(", -1);
g_auto(GStrv) rank_split = g_strsplit_set(rank, " [(", -1);
if (rank_split[0] == nullptr)
{
output_string = g_string_append(output_string, "");
Expand All @@ -5175,7 +5174,6 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
output_string = g_string_append(output_string, rank_split[0]);
}
output_string = g_string_append(output_string, sep);
g_strfreev(rank_split);

g_string_append_printf(output_string, "%d", di->second + 1);
output_string = g_string_append(output_string, sep);
Expand Down
53 changes: 12 additions & 41 deletions src/editors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ gboolean editor_read_desktop_file(const gchar *path)
EditorDescription *editor;
gchar *extensions;
const gchar *key = filename_from_path(path);
gchar **categories;
gchar **only_show_in;
gchar **not_show_in;
GtkTreeIter iter;
gboolean category_geeqie = FALSE;

Expand Down Expand Up @@ -227,11 +224,11 @@ gboolean editor_read_desktop_file(const gchar *path)

if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Hidden", nullptr)
|| g_key_file_get_boolean(key_file, DESKTOP_GROUP, "NoDisplay", nullptr))
{
editor->hidden = TRUE;
{
editor->hidden = TRUE;
}

categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", nullptr, nullptr);
g_auto(GStrv) categories = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "Categories", nullptr, nullptr);
if (categories)
{
gboolean found = FALSE;
Expand All @@ -251,41 +248,22 @@ gboolean editor_read_desktop_file(const gchar *path)
}
}
if (!found) editor->ignored = TRUE;
g_strfreev(categories);
}
else
{
editor->ignored = TRUE;
}

only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", nullptr, nullptr);
if (only_show_in)
g_auto(GStrv) only_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "OnlyShowIn", nullptr, nullptr);
if (only_show_in && !g_strv_contains(only_show_in, "X-Geeqie"))
{
gboolean found = FALSE;
gint i;
for (i = 0; only_show_in[i]; i++)
if (strcmp(only_show_in[i], "X-Geeqie") == 0)
{
found = TRUE;
break;
}
if (!found) editor->ignored = TRUE;
g_strfreev(only_show_in);
editor->ignored = TRUE;
}

not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", nullptr, nullptr);
if (not_show_in)
g_auto(GStrv) not_show_in = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "NotShowIn", nullptr, nullptr);
if (not_show_in && g_strv_contains(not_show_in, "X-Geeqie"))
{
gboolean found = FALSE;
gint i;
for (i = 0; not_show_in[i]; i++)
if (strcmp(not_show_in[i], "X-Geeqie") == 0)
{
found = TRUE;
break;
}
if (found) editor->ignored = TRUE;
g_strfreev(not_show_in);
editor->ignored = TRUE;
}


Expand Down Expand Up @@ -341,11 +319,10 @@ gboolean editor_read_desktop_file(const gchar *path)
editor->ext_list = filter_to_list(extensions);
else
{
gchar **mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", nullptr, nullptr);
g_auto(GStrv) mime_types = g_key_file_get_string_list(key_file, DESKTOP_GROUP, "MimeType", nullptr, nullptr);
if (mime_types)
{
editor->ext_list = editor_mime_types_to_extensions(mime_types);
g_strfreev(mime_types);
if (!editor->ext_list) editor->hidden = TRUE;
}
}
Expand Down Expand Up @@ -435,27 +412,21 @@ static GList *editor_add_desktop_dir(GList *list, const gchar *path)

GList *editor_get_desktop_files()
{
gchar **split_dirs;
gint i;
GList *list = nullptr;

const gchar *xdg_data_dirs_env = getenv("XDG_DATA_DIRS");
g_autofree gchar *xdg_data_dirs = (xdg_data_dirs_env && *xdg_data_dirs_env) ? path_to_utf8(xdg_data_dirs_env) : g_strdup("/usr/share");

g_autofree gchar *all_dirs = g_strconcat(get_rc_dir(), ":", gq_appdir, ":", xdg_data_home_get(), ":", xdg_data_dirs, NULL);

split_dirs = g_strsplit(all_dirs, ":", 0);

for (i = 0; split_dirs[i]; i++)
;
g_auto(GStrv) split_dirs = g_strsplit(all_dirs, ":", 0);

for (--i; i >= 0; i--)
for (gint i = g_strv_length(split_dirs) - 1; i >= 0; i--)
{
g_autofree gchar *path = g_build_filename(split_dirs[i], "applications", NULL);
list = editor_add_desktop_dir(list, path);
}

g_strfreev(split_dirs);
return list;
}

Expand Down
17 changes: 9 additions & 8 deletions src/image-load.cc
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,7 @@ static void image_loader_size_cb(gpointer,
gint width, gint height, gpointer data)
{
auto il = static_cast<ImageLoader *>(data);
gchar **mime_types;
gboolean scale = FALSE;
gint n;

g_mutex_lock(il->data_mutex);
il->actual_width = width;
Expand All @@ -584,14 +582,17 @@ static void image_loader_size_cb(gpointer,
if (il->fd->format_class == FORMAT_CLASS_VIDEO)
scale = TRUE;
#endif
mime_types = il->backend->get_format_mime_types();
n = 0;
while (mime_types[n] && !scale)

if (!scale)
{
if (strstr(mime_types[n], "jpeg")) scale = TRUE;
n++;
g_auto(GStrv) mime_types = il->backend->get_format_mime_types();
gint n = 0;
while (mime_types[n] && !scale)
{
if (strstr(mime_types[n], "jpeg")) scale = TRUE;
n++;
}
}
g_strfreev(mime_types);

if (!scale)
{
Expand Down
26 changes: 7 additions & 19 deletions src/layout-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1454,8 +1454,6 @@ static void layout_menu_kbd_map_cb(GtkAction *, gpointer)
g_autofree gchar *tmp_file = nullptr;
GError *error = nullptr;
GIOChannel *channel;
char **pre_key;
char **post_key;
int keymap_index;

fd = g_file_open_tmp("geeqie_keymap_XXXXXX.svg", &tmp_file, &error);
Expand All @@ -1477,8 +1475,8 @@ static void layout_menu_kbd_map_cb(GtkAction *, gpointer)
{
if (g_strrstr(keymap_template[keymap_index], ">key:"))
{
pre_key = g_strsplit(keymap_template[keymap_index],">key:",2);
post_key = g_strsplit(pre_key[1],"<",2);
g_auto(GStrv) pre_key = g_strsplit(keymap_template[keymap_index], ">key:", 2);
g_auto(GStrv) post_key = g_strsplit(pre_key[1], "<", 2);

const gchar *key_name = post_key[0];
const gchar *menu_name = " ";
Expand All @@ -1503,9 +1501,6 @@ static void layout_menu_kbd_map_cb(GtkAction *, gpointer)
g_autofree gchar *converted_line = g_strconcat(pre_key[0], ">", menu_name, "<", post_key[1], "\n", NULL);
g_io_channel_write_chars(channel, converted_line, -1, nullptr, &error);
if (error) {log_printf("Warning: Keyboard Map:%s\n",error->message); g_error_free(error);}

g_strfreev(pre_key);
g_strfreev(post_key);
}
else
{
Expand Down Expand Up @@ -2934,24 +2929,17 @@ static void layout_actions_setup_marks(LayoutWindow *lw)

static GList *layout_actions_editor_menu_path(EditorDescription *editor)
{
gchar **split = g_strsplit(editor->menu_path, "/", 0);
gint i = 0;
GList *ret = nullptr;
g_auto(GStrv) split = g_strsplit(editor->menu_path, "/", 0);

if (split[0] == nullptr)
{
g_strfreev(split);
return nullptr;
}
const guint split_count = g_strv_length(split);
if (split_count == 0) return nullptr;

while (split[i])
GList *ret = nullptr;
for (guint i = 0; i < split_count; i++)
{
ret = g_list_prepend(ret, g_strdup(split[i]));
i++;
}

g_strfreev(split);

ret = g_list_prepend(ret, g_strdup(editor->key));

return g_list_reverse(ret);
Expand Down
Loading

0 comments on commit fe17beb

Please sign in to comment.