diff options
author | nirav <nirav@teisuu.com> | 2018-09-02 13:07:04 +0530 |
---|---|---|
committer | Dandelion <nirav@teisuu.com> | 2018-09-02 13:07:04 +0530 |
commit | 4283d4074c45d27704b00d9ea80eae7847d66375 (patch) | |
tree | 2b3cdc93496a22feba9af1a50444c7d7ab6cdce4 /dir.c | |
parent | 86031af54187b1815dbf18e0da463dffcb193733 (diff) | |
download | im-4283d4074c45d27704b00d9ea80eae7847d66375.tar.gz im-4283d4074c45d27704b00d9ea80eae7847d66375.zip |
Added support for loading next, prev image
Diffstat (limited to 'dir.c')
-rw-r--r-- | dir.c | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -0,0 +1,54 @@ +#include "dir.h" +#include <dirent.h> +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> + +char *get_filename_ext(char *file_name) +{ + char *dot = strrchr(file_name, '.'); + if (!dot || dot == file_name) + return ""; + return dot + 1; +} + +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"))) { + return 1; + } + return 0; +} + +int scan(const char *file_name, char ***list, int *list_count) +{ + struct dirent **name_list; + char **files; + int curr_index; + char *dir_name = dirname(strdup(file_name)); + size_t dir_name_len = strlen(dir_name); + *list_count = scandir(dir_name, &name_list, image_filter, alphasort); + if (list_count < 0) { + return -1; + } + + files = malloc(*list_count * sizeof(char *)); + char *file_basename = basename(strdup(file_name)); + int i = 0; + while (i < *list_count) { + if (!strcmp(file_basename, name_list[i]->d_name)) + curr_index = i; + files[i] = malloc((strlen(name_list[i]->d_name) + dir_name_len + 2) * + sizeof(char)); + sprintf(files[i], "%s/%s", dir_name, name_list[i]->d_name); + free(name_list[i]); + i++; + } + *list = files; + free(name_list); + + return curr_index; +} |