aboutsummaryrefslogtreecommitdiff
path: root/src/file.c
blob: 22067c20ebfcbc730ece1730878d7420016f46f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#define _DEFAULT_SOURCE
#include "file.h"
#include "image.h"
#include <dirent.h>
#include <gtk-3.0/gtk/gtk.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

static int curr_file_index, file_list_count;
static char **file_list;
static char **extensions = NULL;

char *get_filename_ext(const char *file_name)
{
    char *dot = strrchr(file_name, '.');
    if (!dot || dot == file_name)
        return "";
    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)
{
    if (dir->d_type == DT_REG && is_file_format_supported(dir->d_name)) {
        return 1;
    }
    return 0;
}

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);
    if (file_list_count < 0) {
        return -1;
    }

    file_list = malloc(file_list_count * sizeof(char *));
    char *file_basename = basename(strdup(file_name));
    int i = 0;
    while (i < file_list_count) {
        if (!strcmp(file_basename, name_list[i]->d_name))
            curr_file_index = i;
        file_list[i] = g_build_filename(dir_name, name_list[i]->d_name, NULL);
        free(name_list[i]);
        i++;
    }
    free(name_list);

    return 0;
}

void next_file()
{
    if (curr_file_index >= file_list_count - 1)
        return;
    if (!load_image(file_list[curr_file_index + 1]))
        curr_file_index++;
}

void prev_file()
{
    if (curr_file_index <= 0)
        return;
    if (!load_image(file_list[curr_file_index - 1]))
        curr_file_index--;
}

void first_file()
{
    if (curr_file_index == 0)
        return;
    if (!load_image(file_list[0]))
        curr_file_index = 0;
}

void last_file()
{
    if (curr_file_index == file_list_count - 1)
        return;
    if (!load_image(file_list[file_list_count - 1]))
        curr_file_index = file_list_count - 1;
}

void clean()
{
    for (int i = 0; i < file_list_count; ++i) {
        g_free(file_list[i]);
    }
    g_free(file_list);
}