From 661756c9237a26562cae102f53b12aa0b403fde1 Mon Sep 17 00:00:00 2001 From: nirav Date: Sat, 8 Sep 2018 01:28:21 +0530 Subject: Added dynamic file type checking --- file.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- image.c | 22 ---------------------- image.h | 1 - 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/file.c b/file.c index 1477f39..1ff7f99 100644 --- a/file.c +++ b/file.c @@ -1,4 +1,5 @@ #include "file.h" +#include "image.h" #include #include #include @@ -7,7 +8,9 @@ #include #include -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); diff --git a/image.c b/image.c index 1e8874b..d4765fd 100644 --- a/image.c +++ b/image.c @@ -95,25 +95,3 @@ void zoom(int type) update_pixbuf(); } - -void print_supported_formats() -{ - GSList *list, *it; - GdkPixbufFormat *format; - gchar **extensions; - gchar *ex; - - list = gdk_pixbuf_get_formats(); - if (list != NULL) { - for (it = list; it->next != NULL; it = it->next) { - format = (GdkPixbufFormat *)it->data; - printf("%s:", gdk_pixbuf_format_get_description(format)); - extensions = gdk_pixbuf_format_get_extensions(format); - for (ex = *extensions; *ex; ex++) { - printf(" %s", ex); - } - printf("\n"); - } - g_slist_free(list); - } -} diff --git a/image.h b/image.h index e58afd7..dfbd9ff 100644 --- a/image.h +++ b/image.h @@ -21,6 +21,5 @@ GtkWidget *new_image(); int load_image(char *file_name); void fit_image(); void zoom(int type); -void print_supported_formats(); #endif -- cgit v1.2.3