From 68f9a08ee41b24770222d755773c141470afa198 Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Thu, 29 Dec 2022 18:37:44 +0100 Subject: [PATCH] Use babl utility to iterate over directory contents --- babl/babl-extension.c | 66 ++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/babl/babl-extension.c b/babl/babl-extension.c index 8f06fcd..0d36eef 100644 --- a/babl/babl-extension.c +++ b/babl/babl-extension.c @@ -260,45 +260,53 @@ babl_extension_load (const char *path) } } +struct dir_foreach_ctx +{ + const char **exclusion_patterns; +}; + static void -babl_extension_load_dir (const char *base_path, - const char **exclusion_patterns) +dir_foreach (const char *base_path, + const char *entry, + void *user_data) { - DIR *dir; + struct dir_foreach_ctx *ctx = (struct dir_foreach_ctx*) user_data; - if ((dir = opendir (base_path))) + if (entry[0] != '.') { - struct dirent *dentry; + char *path = NULL; + char *extension; - while ((dentry = readdir (dir)) != NULL) + path = babl_strcat (path, base_path); + path = babl_strcat (path, BABL_DIR_SEPARATOR); + path = babl_strcat (path, entry); + + if ((extension = strrchr (entry, '.')) != NULL && + !strcmp (extension, SHREXT)) { - if (dentry->d_name[0] != '.') - { - char *path = NULL; - char *extension; - - path = babl_strcat (path, base_path); - path = babl_strcat (path, BABL_DIR_SEPARATOR); - path = babl_strcat (path, dentry->d_name); - - if ((extension = strrchr (dentry->d_name, '.')) != NULL && - !strcmp (extension, SHREXT)) - { - int excluded = 0; - for (int i = 0; exclusion_patterns[i]; i++) - if (strstr (path, exclusion_patterns[i])) - excluded = 1; - if (!excluded) - babl_extension_load (path); - } - - babl_free (path); - } + int excluded = 0; + for (int i = 0; ctx->exclusion_patterns[i]; i++) + if (strstr (path, ctx->exclusion_patterns[i])) + excluded = 1; + if (!excluded) + babl_extension_load (path); } - closedir (dir); + + babl_free (path); } } +static void +babl_extension_load_dir (const char *base_path, + const char **exclusion_patterns) +{ + struct dir_foreach_ctx ctx; + + ctx.exclusion_patterns = exclusion_patterns; + + _babl_dir_foreach (base_path, dir_foreach, &ctx); +} + static char * expand_path (char *path) { -- 2.30.2