diff options
author | nirav <nirav@teisuu.com> | 2018-09-08 01:28:21 +0530 |
---|---|---|
committer | Dandelion <nirav@teisuu.com> | 2018-09-08 01:30:09 +0530 |
commit | 661756c9237a26562cae102f53b12aa0b403fde1 (patch) | |
tree | e674391fb8aa39efef9822ddaa94653c4abf9b9f /file.c | |
parent | b4a6aa115dfd2bbef29af2439661999f8b827fba (diff) | |
download | im-661756c9237a26562cae102f53b12aa0b403fde1.tar.gz im-661756c9237a26562cae102f53b12aa0b403fde1.zip |
Added dynamic file type checking
Diffstat (limited to 'file.c')
-rw-r--r-- | file.c | 49 |
1 files changed, 46 insertions, 3 deletions
@@ -1,4 +1,5 @@ #include "file.h" +#include "image.h" #include <dirent.h> #include <gtk-3.0/gtk/gtk.h> #include <libgen.h> @@ -7,7 +8,9 @@ #include <string.h> #include <sys/types.h> -char *get_filename_ext(char *file_name) +static char **extensions = NULL; + +char *get_filename_ext(const char *file_name) { char *dot = strrchr(file_name, '.'); if (!dot || dot == file_name) @@ -15,10 +18,49 @@ char *get_filename_ext(char *file_name) return dot + 1; } +int is_file_format_supported(const char *file_name) +{ + char *file_ext = get_filename_ext(file_name); + char **it = extensions; + while (*it != NULL) { + if (!strcmp(*it++, file_ext)) { + return 1; + } + } + return 0; +} + +void scan_supported_formats() +{ + GSList *list, *it; + GdkPixbufFormat *format; + gchar **exts, **ex_it; + int i = 0; + + list = gdk_pixbuf_get_formats(); + if (list != NULL) { + for (it = list; it->next != NULL; it = it->next) { + format = (GdkPixbufFormat *)it->data; + exts = gdk_pixbuf_format_get_extensions(format); + if (exts != NULL) { + ex_it = exts; + while (*ex_it != NULL) { + extensions = realloc(extensions, sizeof(char *) * (i + 1)); + extensions[i++] = strdup(*ex_it); + g_free(*ex_it); + ex_it++; + } + g_free(exts); + } + } + g_slist_free(list); + extensions[i] = NULL; + } +} + int image_filter(const struct dirent *dir) { - char *ext = get_filename_ext(strdup(dir->d_name)); - if (dir->d_type == DT_REG && (!strcmp(ext, "png") || !strcmp(ext, "jpg"))) { + if (dir->d_type == DT_REG && is_file_format_supported(dir->d_name)) { return 1; } return 0; @@ -26,6 +68,7 @@ int image_filter(const struct dirent *dir) int scan(const char *file_name) { + scan_supported_formats(); struct dirent **name_list; char *dir_name = dirname(strdup(file_name)); file_list_count = scandir(dir_name, &name_list, image_filter, alphasort); |