summaryrefslogtreecommitdiff
path: root/src/icon.c
blob: 9e02f996b6e666ee88608b97002fcf7a349daaa2 (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
#include "fileserv.h"
#include <fnmatch.h>
#include <string.h>

typedef TAILQ_HEAD(icon_list,icon) ICON_LIST;

static void
add_icon(ICON_LIST *lst, char const *type, char const *src, char const *alt)
{
	ICON *icon;

	icon = xcalloc(1, sizeof(icon[0]));
	icon->name = xstrdup(type);
	icon->src = xstrdup(src);
	icon->alt = alt ? xstrdup(alt) : NULL;

	TAILQ_INSERT_TAIL(lst, icon, link);
}

static ICON_LIST mime_icon_list = TAILQ_HEAD_INITIALIZER(mime_icon_list);

ICON const *
icon_by_mime(char const *type)
{
	ICON *icon;
	
	TAILQ_FOREACH(icon, &mime_icon_list, link) {
		if (fnmatch(icon->name, type, 0) == 0)
			return icon;
	}
	return NULL;
}

void
add_icon_by_mime(char const *type, char const *src, char const *alt)
{
	return add_icon(&mime_icon_list, type, src, alt);
}

static ICON_LIST suf_icon_list = TAILQ_HEAD_INITIALIZER(suf_icon_list);

void
add_icon_by_name(char const *name, char const *src, char const *alt)
{
	ICON *icon, *cur;
	size_t len, i;
	
	icon = xcalloc(1, sizeof(icon[0]));
	len = strlen(name);
	icon->name = xmalloc(len + 1);
	for (i = 0; i < len; i++)
		icon->name[i] = name[len-i-1];
	icon->name[i] = 0;
	icon->src = xstrdup(src);
	icon->alt = alt ? xstrdup(alt) : NULL;

	TAILQ_FOREACH(cur, &suf_icon_list, link) {
		if (strlen(cur->name) < len) {
			TAILQ_INSERT_BEFORE(cur, icon, link);
			return;
		}
	}
	TAILQ_INSERT_TAIL(&suf_icon_list, icon, link);
}

static int
sufcmp(char const *suf, char const *end, char const *beg)
{
	while (*suf) {
		if (end == beg || *suf++ != *--end)
			return 1;
	}
	return 0;
}

ICON const *
icon_by_name(char const *name)
{
	ICON *icon;
	char const *end = name + strlen(name);
	
	TAILQ_FOREACH(icon, &suf_icon_list, link) {
		if (sufcmp(icon->name, end, name) == 0)
			return icon;
	}
	return NULL;
}

static ICON_LIST type_icon_list = TAILQ_HEAD_INITIALIZER(type_icon_list);

ICON const *
icon_by_type(char const *type)
{
	ICON *icon;
	
	TAILQ_FOREACH(icon, &type_icon_list, link) {
		if (strcmp(icon->name, type) == 0)
			return icon;
	}
	return NULL;
}

void
add_icon_by_type(char const *name, char const *src, char const *alt)
{
	return add_icon(&type_icon_list, name, src, alt);
}

Return to:

Send suggestions and report system problems to the System administrator.