diff --git a/file.h b/file.h index 69ec514bd6..308f74a127 100644 --- a/file.h +++ b/file.h @@ -61,6 +61,7 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_ void dir_list_sort(struct string_list *list, bool dir_first); void dir_list_free(struct string_list *list); bool string_list_find_elem(const struct string_list *list, const char *elem); +bool string_list_find_elem_prefix(const struct string_list *list, const char *prefix, const char *elem); struct string_list *string_split(const char *str, const char *delim); bool path_is_directory(const char *path); diff --git a/file_path.c b/file_path.c index 60a395df22..c7dbdd08e9 100644 --- a/file_path.c +++ b/file_path.c @@ -152,6 +152,24 @@ bool string_list_find_elem(const struct string_list *list, const char *elem) return false; } +bool string_list_find_elem_prefix(const struct string_list *list, const char *prefix, const char *elem) +{ + if (!list) + return false; + + char prefixed[PATH_MAX]; + snprintf(prefixed, sizeof(prefixed), "%s%s", prefix, elem); + + for (size_t i = 0; i < list->size; i++) + { + if (strcmp(list->elems[i].data, elem) == 0 || + strcmp(list->elems[i].data, prefixed) == 0) + return true; + } + + return false; +} + const char *path_get_extension(const char *path) { const char *ext = strrchr(path, '.'); @@ -222,7 +240,7 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_ if (!include_dirs && is_dir) continue; - if (!is_dir && ext_list && !string_list_find_elem(ext_list, file_ext)) + if (!is_dir && ext_list && !string_list_find_elem_prefix(ext_list, ".", file_ext)) continue; char file_path[PATH_MAX]; @@ -279,7 +297,7 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_ if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue; - if (!is_dir && ext_list && !string_list_find_elem(ext_list, file_ext)) + if (!is_dir && ext_list && !string_list_find_elem_prefix(ext_list, ".", file_ext)) continue; char file_path[PATH_MAX];