From 5dd814773ad841255d16ffb1f6c468b75235b657 Mon Sep 17 00:00:00 2001 From: o9000 Date: Thu, 21 Dec 2017 11:57:49 +0100 Subject: [PATCH] A bit of refactoring --- src/launcher/icon-theme-common.c | 37 ++++++++++++++++++++++---------- src/util/common.c | 8 +++---- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/launcher/icon-theme-common.c b/src/launcher/icon-theme-common.c index acd184a..e7ebe6d 100644 --- a/src/launcher/icon-theme-common.c +++ b/src/launcher/icon-theme-common.c @@ -535,18 +535,33 @@ gint compare_theme_directories(gconstpointer a, gconstpointer b, gpointer size_q return abs(da->size - size) - abs(db->size - size); } +Bool is_full_path(const char *s) +{ + if (!s) + return FALSE; + return s[0] == '/'; +} + +Bool file_exists(const char *path) +{ + return g_file_test(path, G_FILE_TEST_EXISTS); +} + +char *icon_path_from_full_path(const char *s) +{ + if (is_full_path(s) && file_exists(s)) + return strdup(s); + return NULL; +} + char *get_icon_path_helper(GSList *themes, const char *icon_name, int size) { - if (icon_name == NULL) + if (!icon_name) return NULL; - // If the icon_name is already a path and the file exists, return it - if (strstr(icon_name, "/") == icon_name) { - if (g_file_test(icon_name, G_FILE_TEST_EXISTS)) - return strdup(icon_name); - else - return NULL; - } + char *result = icon_path_from_full_path(icon_name); + if (result) + return result; const GSList *basenames = get_icon_locations(); GSList *extensions = NULL; @@ -679,10 +694,10 @@ char *get_icon_path_helper(GSList *themes, const char *icon_name, int size) for (GSList *ext = extensions; ext; ext = g_slist_next(ext)) { char *base_name = (char *)base->data; char *extension = (char *)ext->data; - size_t file_name_size = strlen(base_name) + strlen(icon_name) + strlen(extension) + 100; - file_name = calloc(file_name_size, 1); + size_t file_name_size2 = strlen(base_name) + strlen(icon_name) + strlen(extension) + 100; + file_name = calloc(file_name_size2, 1); // filename = directory/iconname.extension - snprintf(file_name, file_name_size, "%s/%s%s", base_name, icon_name, extension); + snprintf(file_name, file_name_size2, "%s/%s%s", base_name, icon_name, extension); if (debug_icons) fprintf(stderr, "tint2: Checking %s\n", file_name); if (g_file_test(file_name, G_FILE_TEST_EXISTS)) { diff --git a/src/util/common.c b/src/util/common.c index ff9c845..cf1d963 100644 --- a/src/util/common.c +++ b/src/util/common.c @@ -483,10 +483,10 @@ char *contract_tilde(const char *s) strlcat(home_slash, "/", buf_size); if ((strcmp(s, home) == 0 || strstr(s, home_slash) == s)) { - size_t buf_size = strlen(s) - strlen(home) + 2; - char *result = calloc(buf_size, 1); - strlcat(result, "~", buf_size); - strlcat(result, s + strlen(home), buf_size); + size_t buf_size2 = strlen(s) - strlen(home) + 2; + char *result = calloc(buf_size2, 1); + strlcat(result, "~", buf_size2); + strlcat(result, s + strlen(home), buf_size2); free(home_slash); return result; } else {