/src/glib/gio/xdgmime/xdgmimeicon.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: C; c-file-style: "gnu" -*- */ |
2 | | /* xdgmimeicon.c: Private file. Datastructure for storing the aliases. |
3 | | * |
4 | | * More info can be found at http://www.freedesktop.org/standards/ |
5 | | * |
6 | | * Copyright (C) 2008 Red Hat, Inc. |
7 | | * |
8 | | * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0 |
9 | | */ |
10 | | |
11 | | #ifdef HAVE_CONFIG_H |
12 | | #include <config.h> |
13 | | #endif |
14 | | |
15 | | #include "xdgmimeicon.h" |
16 | | #include "xdgmimeint.h" |
17 | | #include <stdlib.h> |
18 | | #include <stdio.h> |
19 | | #include <assert.h> |
20 | | #include <string.h> |
21 | | #include <fnmatch.h> |
22 | | |
23 | | #ifndef FALSE |
24 | | #define FALSE (0) |
25 | | #endif |
26 | | |
27 | | #ifndef TRUE |
28 | | #define TRUE (!FALSE) |
29 | | #endif |
30 | | |
31 | | typedef struct XdgIcon XdgIcon; |
32 | | |
33 | | struct XdgIcon |
34 | | { |
35 | | char *mime_type; |
36 | | char *icon_name; |
37 | | }; |
38 | | |
39 | | struct XdgIconList |
40 | | { |
41 | | struct XdgIcon *icons; |
42 | | int n_icons; |
43 | | }; |
44 | | |
45 | | XdgIconList * |
46 | | _xdg_mime_icon_list_new (void) |
47 | 0 | { |
48 | 0 | XdgIconList *list; |
49 | |
|
50 | 0 | list = malloc (sizeof (XdgIconList)); |
51 | |
|
52 | 0 | list->icons = NULL; |
53 | 0 | list->n_icons = 0; |
54 | |
|
55 | 0 | return list; |
56 | 0 | } |
57 | | |
58 | | void |
59 | | _xdg_mime_icon_list_free (XdgIconList *list) |
60 | 0 | { |
61 | 0 | int i; |
62 | |
|
63 | 0 | if (list->icons) |
64 | 0 | { |
65 | 0 | for (i = 0; i < list->n_icons; i++) |
66 | 0 | { |
67 | 0 | free (list->icons[i].mime_type); |
68 | 0 | free (list->icons[i].icon_name); |
69 | 0 | } |
70 | 0 | free (list->icons); |
71 | 0 | } |
72 | 0 | free (list); |
73 | 0 | } |
74 | | |
75 | | static int |
76 | | icon_entry_cmp (const void *v1, const void *v2) |
77 | 0 | { |
78 | 0 | return strcmp (((XdgIcon *)v1)->mime_type, ((XdgIcon *)v2)->mime_type); |
79 | 0 | } |
80 | | |
81 | | const char * |
82 | | _xdg_mime_icon_list_lookup (XdgIconList *list, |
83 | | const char *mime_type) |
84 | 0 | { |
85 | 0 | XdgIcon *entry; |
86 | 0 | XdgIcon key; |
87 | |
|
88 | 0 | if (list->n_icons > 0) |
89 | 0 | { |
90 | 0 | key.mime_type = (char *)mime_type; |
91 | 0 | key.icon_name = NULL; |
92 | |
|
93 | 0 | entry = bsearch (&key, list->icons, list->n_icons, |
94 | 0 | sizeof (XdgIcon), icon_entry_cmp); |
95 | 0 | if (entry) |
96 | 0 | return entry->icon_name; |
97 | 0 | } |
98 | | |
99 | 0 | return NULL; |
100 | 0 | } |
101 | | |
102 | | void |
103 | | _xdg_mime_icon_read_from_file (XdgIconList *list, |
104 | | const char *file_name) |
105 | 0 | { |
106 | 0 | FILE *file; |
107 | 0 | char line[255]; |
108 | 0 | int alloc; |
109 | |
|
110 | 0 | file = fopen (file_name, "r"); |
111 | |
|
112 | 0 | if (file == NULL) |
113 | 0 | return; |
114 | | |
115 | | /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. |
116 | | * Blah */ |
117 | 0 | alloc = list->n_icons + 16; |
118 | 0 | list->icons = realloc (list->icons, alloc * sizeof (XdgIcon)); |
119 | 0 | while (fgets (line, 255, file) != NULL) |
120 | 0 | { |
121 | 0 | char *sep; |
122 | 0 | if (line[0] == '#') |
123 | 0 | continue; |
124 | | |
125 | 0 | sep = strchr (line, ':'); |
126 | 0 | if (sep == NULL) |
127 | 0 | continue; |
128 | 0 | *(sep++) = '\000'; |
129 | 0 | sep[strlen (sep) -1] = '\000'; |
130 | 0 | if (list->n_icons == alloc) |
131 | 0 | { |
132 | 0 | alloc <<= 1; |
133 | 0 | list->icons = realloc (list->icons, |
134 | 0 | alloc * sizeof (XdgIcon)); |
135 | 0 | } |
136 | 0 | list->icons[list->n_icons].mime_type = strdup (line); |
137 | 0 | list->icons[list->n_icons].icon_name = strdup (sep); |
138 | 0 | list->n_icons++; |
139 | 0 | } |
140 | 0 | list->icons = realloc (list->icons, |
141 | 0 | list->n_icons * sizeof (XdgIcon)); |
142 | |
|
143 | 0 | fclose (file); |
144 | | |
145 | 0 | if (list->n_icons > 1) |
146 | 0 | qsort (list->icons, list->n_icons, |
147 | 0 | sizeof (XdgIcon), icon_entry_cmp); |
148 | 0 | } |
149 | | |
150 | | |
151 | | void |
152 | | _xdg_mime_icon_list_dump (XdgIconList *list) |
153 | 0 | { |
154 | 0 | int i; |
155 | |
|
156 | 0 | if (list->icons) |
157 | 0 | { |
158 | 0 | for (i = 0; i < list->n_icons; i++) |
159 | 0 | { |
160 | 0 | printf ("%s %s\n", |
161 | 0 | list->icons[i].mime_type, |
162 | 0 | list->icons[i].icon_name); |
163 | 0 | } |
164 | 0 | } |
165 | 0 | } |
166 | | |
167 | | |