]>
Commit | Line | Data |
---|---|---|
cf31d981 SI |
1 | /* -*- mode: C; c-file-style: "gnu" -*- */ |
2 | /* xdgmimealias.c: Private file. Datastructure for storing the hierarchy. | |
3 | * | |
4 | * More info can be found at http://www.freedesktop.org/standards/ | |
5 | * | |
6 | * Copyright (C) 2004 Red Hat, Inc. | |
7 | * Copyright (C) 2004 Matthias Clasen <mclasen@redhat.com> | |
8 | * | |
73d5a30a | 9 | * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0 |
cf31d981 SI |
10 | */ |
11 | ||
12 | #ifdef HAVE_CONFIG_H | |
13 | #include <config.h> | |
14 | #endif | |
15 | ||
16 | #include "xdgmimeparent.h" | |
17 | #include "xdgmimeint.h" | |
18 | #include <stdlib.h> | |
19 | #include <stdio.h> | |
20 | #include <assert.h> | |
21 | #include <string.h> | |
22 | #include <fnmatch.h> | |
23 | ||
24 | #ifndef FALSE | |
25 | #define FALSE (0) | |
26 | #endif | |
27 | ||
28 | #ifndef TRUE | |
29 | #define TRUE (!FALSE) | |
30 | #endif | |
31 | ||
32 | typedef struct XdgMimeParents XdgMimeParents; | |
33 | ||
34 | struct XdgMimeParents | |
35 | { | |
36 | char *mime; | |
37 | char **parents; | |
38 | int n_parents; | |
39 | }; | |
40 | ||
41 | struct XdgParentList | |
42 | { | |
43 | struct XdgMimeParents *parents; | |
44 | int n_mimes; | |
45 | }; | |
46 | ||
47 | XdgParentList * | |
48 | _xdg_mime_parent_list_new (void) | |
49 | { | |
50 | XdgParentList *list; | |
51 | ||
52 | list = malloc (sizeof (XdgParentList)); | |
53 | ||
54 | list->parents = NULL; | |
55 | list->n_mimes = 0; | |
56 | ||
57 | return list; | |
58 | } | |
59 | ||
60 | void | |
61 | _xdg_mime_parent_list_free (XdgParentList *list) | |
62 | { | |
63 | int i; | |
64 | char **p; | |
65 | ||
66 | if (list->parents) | |
67 | { | |
68 | for (i = 0; i < list->n_mimes; i++) | |
69 | { | |
70 | for (p = list->parents[i].parents; *p; p++) | |
71 | free (*p); | |
72 | ||
73 | free (list->parents[i].parents); | |
74 | free (list->parents[i].mime); | |
75 | } | |
76 | free (list->parents); | |
77 | } | |
78 | free (list); | |
79 | } | |
80 | ||
81 | static int | |
82 | parent_entry_cmp (const void *v1, const void *v2) | |
83 | { | |
84 | return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime); | |
85 | } | |
86 | ||
87 | const char ** | |
88 | _xdg_mime_parent_list_lookup (XdgParentList *list, | |
89 | const char *mime) | |
90 | { | |
91 | XdgMimeParents *entry; | |
92 | XdgMimeParents key; | |
93 | ||
94 | if (list->n_mimes > 0) | |
95 | { | |
96 | key.mime = (char *)mime; | |
97 | key.parents = NULL; | |
73d5a30a | 98 | key.n_parents = 0; |
cf31d981 SI |
99 | |
100 | entry = bsearch (&key, list->parents, list->n_mimes, | |
101 | sizeof (XdgMimeParents), &parent_entry_cmp); | |
102 | if (entry) | |
103 | return (const char **)entry->parents; | |
104 | } | |
105 | ||
106 | return NULL; | |
107 | } | |
108 | ||
109 | void | |
110 | _xdg_mime_parent_read_from_file (XdgParentList *list, | |
111 | const char *file_name) | |
112 | { | |
113 | FILE *file; | |
114 | char line[255]; | |
115 | int i, alloc; | |
116 | XdgMimeParents *entry; | |
117 | ||
118 | file = fopen (file_name, "r"); | |
119 | ||
120 | if (file == NULL) | |
121 | return; | |
122 | ||
123 | /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. | |
124 | * Blah */ | |
125 | alloc = list->n_mimes + 16; | |
126 | list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents)); | |
127 | while (fgets (line, 255, file) != NULL) | |
128 | { | |
129 | char *sep; | |
130 | if (line[0] == '#') | |
131 | continue; | |
132 | ||
133 | sep = strchr (line, ' '); | |
134 | if (sep == NULL) | |
135 | continue; | |
136 | *(sep++) = '\000'; | |
137 | sep[strlen (sep) -1] = '\000'; | |
138 | entry = NULL; | |
139 | for (i = 0; i < list->n_mimes; i++) | |
140 | { | |
141 | if (strcmp (list->parents[i].mime, line) == 0) | |
142 | { | |
143 | entry = &(list->parents[i]); | |
144 | break; | |
145 | } | |
146 | } | |
147 | ||
148 | if (!entry) | |
149 | { | |
150 | if (list->n_mimes == alloc) | |
151 | { | |
152 | alloc <<= 1; | |
153 | list->parents = realloc (list->parents, | |
154 | alloc * sizeof (XdgMimeParents)); | |
155 | } | |
156 | list->parents[list->n_mimes].mime = strdup (line); | |
157 | list->parents[list->n_mimes].parents = NULL; | |
158 | entry = &(list->parents[list->n_mimes]); | |
159 | list->n_mimes++; | |
160 | } | |
161 | ||
162 | if (!entry->parents) | |
163 | { | |
164 | entry->n_parents = 1; | |
165 | entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *)); | |
166 | } | |
167 | else | |
168 | { | |
169 | entry->n_parents += 1; | |
170 | entry->parents = realloc (entry->parents, | |
171 | (entry->n_parents + 2) * sizeof (char *)); | |
172 | } | |
173 | entry->parents[entry->n_parents - 1] = strdup (sep); | |
174 | entry->parents[entry->n_parents] = NULL; | |
175 | } | |
176 | ||
177 | list->parents = realloc (list->parents, | |
178 | list->n_mimes * sizeof (XdgMimeParents)); | |
179 | ||
180 | fclose (file); | |
181 | ||
182 | if (list->n_mimes > 1) | |
183 | qsort (list->parents, list->n_mimes, | |
184 | sizeof (XdgMimeParents), &parent_entry_cmp); | |
185 | } | |
186 | ||
187 | ||
188 | void | |
189 | _xdg_mime_parent_list_dump (XdgParentList *list) | |
190 | { | |
191 | int i; | |
192 | char **p; | |
193 | ||
194 | if (list->parents) | |
195 | { | |
196 | for (i = 0; i < list->n_mimes; i++) | |
197 | { | |
198 | for (p = list->parents[i].parents; *p; p++) | |
199 | printf ("%s %s\n", list->parents[i].mime, *p); | |
200 | } | |
201 | } | |
202 | } | |
203 | ||
204 |