From 7545e542038464a6cc20444b38dadad9fef32042 Mon Sep 17 00:00:00 2001 From: Sebastian Dransfeld Date: Tue, 9 Nov 2010 20:15:55 +0000 Subject: [PATCH] Let efreet icon return const We return a pointer to an internal cache, so no need to do strdup. I we implement a cache, the pointer will be a return from a mmap'ed file, so no need to do strdup. A bit late to do an API break, but must be done. SVN revision: 54372 --- legacy/efreet/src/lib/Efreet_Mime.h | 2 +- legacy/efreet/src/lib/efreet_icon.c | 88 +++++++++---------- legacy/efreet/src/lib/efreet_icon.h | 4 +- legacy/efreet/src/lib/efreet_mime.c | 4 +- .../efreet/src/tests/compare/efreet_alloc.c | 3 +- legacy/efreet/src/tests/ef_icon_theme.c | 20 +++-- 6 files changed, 61 insertions(+), 60 deletions(-) diff --git a/legacy/efreet/src/lib/Efreet_Mime.h b/legacy/efreet/src/lib/Efreet_Mime.h index 3bab016aa0..9258f73e2b 100644 --- a/legacy/efreet/src/lib/Efreet_Mime.h +++ b/legacy/efreet/src/lib/Efreet_Mime.h @@ -52,7 +52,7 @@ EAPI const char *efreet_mime_globs_type_get(const char *file); EAPI const char *efreet_mime_special_type_get(const char *file); EAPI const char *efreet_mime_fallback_type_get(const char *file); -EAPI char *efreet_mime_type_icon_get(const char *mime, const char *theme, +EAPI const char *efreet_mime_type_icon_get(const char *mime, const char *theme, unsigned int size); EAPI void efreet_mime_type_cache_clear(void); diff --git a/legacy/efreet/src/lib/efreet_icon.c b/legacy/efreet/src/lib/efreet_icon.c index 3530e22c7c..c31427a948 100644 --- a/legacy/efreet/src/lib/efreet_icon.c +++ b/legacy/efreet/src/lib/efreet_icon.c @@ -35,23 +35,23 @@ static char *efreet_icon_remove_extension(const char *icon); static Efreet_Icon_Theme *efreet_icon_find_theme_check(const char *theme_name); -static char *efreet_icon_find_fallback(Efreet_Icon_Theme *theme, +static const char *efreet_icon_find_fallback(Efreet_Icon_Theme *theme, const char *icon, unsigned int size); -static char *efreet_icon_list_find_fallback(Efreet_Icon_Theme *theme, +static const char *efreet_icon_list_find_fallback(Efreet_Icon_Theme *theme, Eina_List *icons, unsigned int size); -static char *efreet_icon_find_helper(Efreet_Icon_Theme *theme, +static const char *efreet_icon_find_helper(Efreet_Icon_Theme *theme, const char *icon, unsigned int size); -static char *efreet_icon_list_find_helper(Efreet_Icon_Theme *theme, +static const char *efreet_icon_list_find_helper(Efreet_Icon_Theme *theme, Eina_List *icons, unsigned int size); -static char *efreet_icon_lookup_icon(Efreet_Icon_Theme *theme, +static const char *efreet_icon_lookup_icon(Efreet_Icon_Theme *theme, const char *icon_name, unsigned int size); -static char *efreet_icon_fallback_icon(const char *icon_name); -static char *efreet_icon_fallback_dir_scan(const char *dir, - const char *icon_name); +static const char *efreet_icon_fallback_icon(const char *icon_name); +static const char *efreet_icon_fallback_dir_scan(const char *dir, + const char *icon_name); -static char *efreet_icon_lookup_directory(Efreet_Icon_Theme *theme, +static const char *efreet_icon_lookup_directory(Efreet_Icon_Theme *theme, Efreet_Icon_Theme_Directory *dir, const char *icon_name); static double efreet_icon_directory_size_distance(Efreet_Icon_Theme_Directory *dir, @@ -62,7 +62,7 @@ static int efreet_icon_directory_size_match(Efreet_Icon_Theme_Directory *dir, static Efreet_Icon *efreet_icon_new(const char *path); static void efreet_icon_populate(Efreet_Icon *icon, const char *file); -static char *efreet_icon_lookup_directory_helper(Efreet_Icon_Theme_Directory *dir, +static const char *efreet_icon_lookup_directory_helper(Efreet_Icon_Theme_Directory *dir, const char *path, const char *icon_name); static Efreet_Icon_Theme *efreet_icon_theme_new(void); @@ -85,7 +85,7 @@ static int efreet_icon_theme_cache_check_dir(Efreet_Icon_Theme *theme, const char *dir); static void efreet_icon_cache_free(Efreet_Icon_Cache *value); -static char *efreet_icon_cache_check(Efreet_Icon_Theme *theme, const char *icon, unsigned int size); +static const char *efreet_icon_cache_check(Efreet_Icon_Theme *theme, const char *icon, unsigned int size); static void efreet_icon_cache_add(Efreet_Icon_Theme *theme, const char *icon, unsigned int size, const char *value); /** @@ -348,10 +348,10 @@ efreet_icon_find_theme_check(const char *theme_name) * @return Returns the path to the given icon or NULL if none found * @brief Retrives the path to the given icon. */ -EAPI char * +EAPI const char * efreet_icon_path_find(const char *theme_name, const char *icon, unsigned int size) { - char *value = NULL; + const char *value = NULL; Efreet_Icon_Theme *theme; theme = efreet_icon_find_theme_check(theme_name); @@ -392,13 +392,13 @@ efreet_icon_path_find(const char *theme_name, const char *icon, unsigned int siz * @note This function will search the given theme for all icons before falling * back. This is useful when searching for mimetype icons. */ -EAPI char * +EAPI const char * efreet_icon_list_find(const char *theme_name, Eina_List *icons, unsigned int size) { Eina_List *l; const char *icon = NULL; - char *value = NULL; + const char *value = NULL; char *data; Efreet_Icon_Theme *theme; @@ -454,7 +454,7 @@ efreet_icon_list_find(const char *theme_name, Eina_List *icons, EAPI Efreet_Icon * efreet_icon_find(const char *theme_name, const char *icon, unsigned int size) { - char *path; + const char *path; path = efreet_icon_path_find(theme_name, icon, size); if (path) @@ -462,7 +462,6 @@ efreet_icon_find(const char *theme_name, const char *icon, unsigned int size) Efreet_Icon *ic; ic = efreet_icon_new(path); - free(path); return ic; } @@ -478,13 +477,13 @@ efreet_icon_find(const char *theme_name, const char *icon, unsigned int size) * icon found * @brief Scans inheriting themes for the given icon */ -static char * +static const char * efreet_icon_find_fallback(Efreet_Icon_Theme *theme, const char *icon, unsigned int size) { Eina_List *l; - char *parent = NULL; - char *value = NULL; + const char *parent = NULL; + const char *value = NULL; if (theme->inherits) { @@ -522,11 +521,11 @@ efreet_icon_find_fallback(Efreet_Icon_Theme *theme, * icon found * @brief Scans the theme and any inheriting themes for the given icon */ -static char * +static const char * efreet_icon_find_helper(Efreet_Icon_Theme *theme, const char *icon, unsigned int size) { - char *value; + const char *value; static int recurse = 0; efreet_icon_theme_cache_check(theme); @@ -559,13 +558,13 @@ efreet_icon_find_helper(Efreet_Icon_Theme *theme, * icon found * @brief Scans inheriting themes for the given icons */ -static char * +static const char * efreet_icon_list_find_fallback(Efreet_Icon_Theme *theme, Eina_List *icons, unsigned int size) { Eina_List *l; - char *parent = NULL; - char *value = NULL; + const char *parent = NULL; + const char *value = NULL; if (theme->inherits) { @@ -607,12 +606,12 @@ efreet_icon_list_find_fallback(Efreet_Icon_Theme *theme, * icon found * @brief Scans the theme and any inheriting themes for the given icons */ -static char * +static const char * efreet_icon_list_find_helper(Efreet_Icon_Theme *theme, Eina_List *icons, unsigned int size) { Eina_List *l; - char *value = NULL; + const char *value = NULL; const char *icon = NULL; static int recurse = 0; @@ -649,12 +648,12 @@ efreet_icon_list_find_helper(Efreet_Icon_Theme *theme, * none found * @brief Looks for the @a icon in the @a theme for the @a size given. */ -static char * +static const char * efreet_icon_lookup_icon(Efreet_Icon_Theme *theme, const char *icon_name, unsigned int size) { Eina_List *l; - char *icon = NULL, *tmp = NULL; + const char *icon = NULL, *tmp = NULL; Efreet_Icon_Theme_Directory *dir; double minimal_distance = INT_MAX; unsigned int ret_size = 0; @@ -692,7 +691,6 @@ efreet_icon_lookup_icon(Efreet_Icon_Theme *theme, const char *icon_name, icon_name); if (tmp) { - FREE(icon); icon = tmp; minimal_distance = distance; ret_size = size; @@ -713,13 +711,13 @@ efreet_icon_lookup_icon(Efreet_Icon_Theme *theme, const char *icon_name, * none found * @brief Tries to find the file closest matching the given icon */ -static char * +static const char * efreet_icon_lookup_directory(Efreet_Icon_Theme *theme, Efreet_Icon_Theme_Directory *dir, const char *icon_name) { Eina_List *l; - char *icon = NULL; + const char *icon = NULL; const char *path; const char *tmp; @@ -797,10 +795,10 @@ efreet_icon_directory_size_distance(Efreet_Icon_Theme_Directory *dir, * @return Returns the Efreet_Icon for the given name or NULL if none found * @brief Looks for the un-themed icon in the base directories */ -static char * +static const char * efreet_icon_fallback_icon(const char *icon_name) { - char *icon; + const char *icon; if (!icon_name) return NULL; icon = efreet_icon_cache_check(NULL, icon_name, 0); @@ -830,7 +828,7 @@ efreet_icon_fallback_icon(const char *icon_name) EINA_LIST_FOREACH(xdg_dirs, l, dir) { snprintf(path, sizeof(path), "%s/icons", dir); - icon = efreet_icon_fallback_dir_scan(path, icon_name); + efreet_icon_fallback_dir_scan(path, icon_name); if (icon) { efreet_icon_cache_add(NULL, icon_name, 0, icon); @@ -875,11 +873,11 @@ efreet_icon_fallback_icon(const char *icon_name) * @brief Scans the given @a dir for the given @a icon_name returning the * Efreet_icon if found, NULL otherwise. */ -static char * +static const char * efreet_icon_fallback_dir_scan(const char *dir, const char *icon_name) { Eina_List *l; - char *icon = NULL; + const char *icon = NULL; char path[PATH_MAX], *ext; const char *icon_path[] = { dir, "/", icon_name, NULL }; size_t size; @@ -893,7 +891,7 @@ efreet_icon_fallback_dir_scan(const char *dir, const char *icon_name) if (ecore_file_exists(path)) { - icon = strdup(path); + icon = eina_stringshare_add(path); if (icon) break; } *(path + size) = '\0'; @@ -904,7 +902,7 @@ efreet_icon_fallback_dir_scan(const char *dir, const char *icon_name) { if ((ecore_file_exists(path)) && (!ecore_file_is_dir(path))) { - icon = strdup(path); + icon = eina_stringshare_add(path); #ifdef STRICT_SPEC if (icon) WRN("[Efreet]: Found an icon that already has an extension: %s", path); @@ -925,12 +923,12 @@ efreet_icon_fallback_dir_scan(const char *dir, const char *icon_name) * @brief Caches the icons in the given theme directory path at the given * size */ -static char * +static const char * efreet_icon_lookup_directory_helper(Efreet_Icon_Theme_Directory *dir, const char *path, const char *icon_name) { Eina_List *l; - char *icon = NULL; + const char *icon = NULL; char file_path[PATH_MAX]; const char *ext; size_t len; @@ -951,7 +949,7 @@ efreet_icon_lookup_directory_helper(Efreet_Icon_Theme_Directory *dir, if (ecore_file_exists(file_path)) { - icon = strdup(file_path); + icon = eina_stringshare_add(file_path); break; } } @@ -1603,7 +1601,7 @@ efreet_icon_cache_free(Efreet_Icon_Cache *value) free(value); } -static char * +static const char * efreet_icon_cache_check(Efreet_Icon_Theme *theme, const char *icon, unsigned int size) { Efreet_Icon_Cache *cache; @@ -1621,7 +1619,7 @@ efreet_icon_cache_check(Efreet_Icon_Theme *theme, const char *icon, unsigned int if (!cache->path) return NON_EXISTING; else if (!stat(cache->path, &st) && st.st_mtime == cache->lasttime) - return strdup(cache->path); + return cache->path; eina_hash_del_by_key(efreet_icon_cache, key); } return NULL; @@ -1643,7 +1641,7 @@ efreet_icon_cache_add(Efreet_Icon_Theme *theme, const char *icon, unsigned int s if ((value) && !stat(value, &st)) { - cache->path = eina_stringshare_add(value); + cache->path = value; cache->lasttime = st.st_mtime; } else diff --git a/legacy/efreet/src/lib/efreet_icon.h b/legacy/efreet/src/lib/efreet_icon.h index 2ed4f74cbd..31bef004d7 100644 --- a/legacy/efreet/src/lib/efreet_icon.h +++ b/legacy/efreet/src/lib/efreet_icon.h @@ -150,10 +150,10 @@ EAPI Efreet_Icon_Theme *efreet_icon_theme_find(const char *theme_name); EAPI Efreet_Icon *efreet_icon_find(const char *theme_name, const char *icon, unsigned int size); -EAPI char *efreet_icon_list_find(const char *theme_name, +EAPI const char *efreet_icon_list_find(const char *theme_name, Eina_List *icons, unsigned int size); -EAPI char *efreet_icon_path_find(const char *theme_name, +EAPI const char *efreet_icon_path_find(const char *theme_name, const char *icon, unsigned int size); EAPI void efreet_icon_free(Efreet_Icon *icon); diff --git a/legacy/efreet/src/lib/efreet_mime.c b/legacy/efreet/src/lib/efreet_mime.c index 50a69a183e..a5cd63fe55 100644 --- a/legacy/efreet/src/lib/efreet_mime.c +++ b/legacy/efreet/src/lib/efreet_mime.c @@ -330,10 +330,10 @@ efreet_mime_type_get(const char *file) * @return Returns mime type icon path as a string * @brief Retreive the mime type icon for a file */ -EAPI char * +EAPI const char * efreet_mime_type_icon_get(const char *mime, const char *theme, unsigned int size) { - char *icon = NULL; + const char *icon = NULL; char *data; Eina_List *icons = NULL; const char *env = NULL; diff --git a/legacy/efreet/src/tests/compare/efreet_alloc.c b/legacy/efreet/src/tests/compare/efreet_alloc.c index a6582e1399..65b0c883d2 100644 --- a/legacy/efreet/src/tests/compare/efreet_alloc.c +++ b/legacy/efreet/src/tests/compare/efreet_alloc.c @@ -7,7 +7,7 @@ int main(void) { int i = 0, k; - char *path; + const char *path; efreet_init(); @@ -17,7 +17,6 @@ main(void) { path = efreet_icon_path_find(THEME, icons[i], SIZE); // printf("%s: %s\n", icons[i], (path ? path : "NOT FOUND")); - if (path) free(path); } } diff --git a/legacy/efreet/src/tests/ef_icon_theme.c b/legacy/efreet/src/tests/ef_icon_theme.c index bb66698885..d40a2c4775 100644 --- a/legacy/efreet/src/tests/ef_icon_theme.c +++ b/legacy/efreet/src/tests/ef_icon_theme.c @@ -480,7 +480,8 @@ ef_cb_efreet_icon_match(void) double start = ecore_time_get(); for (i = 0; system_icons[i]; i++) { - char *path, *s; + const char *path; + char *p, *s; path = efreet_icon_path_find(THEME, system_icons[i], SIZE); @@ -500,9 +501,10 @@ ef_cb_efreet_icon_match(void) printf("Found icon not in hash: %s\n", system_icons[i]); } - s = strrchr(path, '.'); + p = strdup(path); + s = strrchr(p, '.'); if (s) *s = '\0'; - s = strrchr(path, '/'); + s = strrchr(p, '/'); if (s) s++; if (s && strcmp(s, system_icons[i])) @@ -510,7 +512,7 @@ ef_cb_efreet_icon_match(void) printf("Name mismatch name (%s) vs ef (%s)\n", system_icons[i], s); ret = 0; } - free(path); + free(p); } printf("Time: %f\n", (ecore_time_get() - start)); eina_hash_free(icon_hash); @@ -518,15 +520,17 @@ ef_cb_efreet_icon_match(void) start = ecore_time_get(); for (i = 0; system_icons[i]; i++) { - char *path, *s; + const char *path; + char *p, *s; path = efreet_icon_path_find(THEME, system_icons[i], SIZE); if (!path) continue; + p = strdup(path); - s = strrchr(path, '.'); + s = strrchr(p, '.'); if (s) *s = '\0'; - s = strrchr(path, '/'); + s = strrchr(p, '/'); if (s) s++; if (s && strcmp(s, system_icons[i])) @@ -534,7 +538,7 @@ ef_cb_efreet_icon_match(void) printf("Name mismatch name (%s) vs ef (%s)\n", system_icons[i], s); ret = 0; } - free(path); + free(p); } printf("Time: %f\n", (ecore_time_get() - start));