aboutsummaryrefslogtreecommitdiffstats
path: root/src/icon.c
blob: d131307ece555657548de0c6ef3897ae352fb1c2 (plain) (blame)
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
#include "icon.h"
#include <stdbool.h>
#include <string.h>

const char *
get_file_icon(const char *filename, bool is_dir)
{
	if (is_dir) {
		return DEFAULT_DIR_ICON;
	}

	size_t exact_count =
	    sizeof(exact_match_icons) / sizeof(exact_match_icons[0]);
	for (size_t i = 0; i < exact_count; i++) {
		if (strcmp(filename, exact_match_icons[i].key) == 0) {
			return exact_match_icons[i].icon;
		}
	}

	const char *ext = strrchr(filename, '.');
	if (ext && ext != filename) {
		ext++; // skip "."
		size_t ext_count = sizeof(ext_icons) / sizeof(ext_icons[0]);
		for (size_t i = 0; i < ext_count; i++) {
			if (strcasecmp(ext, ext_icons[i].key) == 0) {
				return ext_icons[i].icon;
			}
		}
	}

	return DEFAULT_FILE_ICON;
}