/src/rauc/subprojects/glib-2.76.5/gio/xdgmime/xdgmimeparent.c
Line | Count | Source |
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 | | * |
9 | | * Licensed under the Academic Free License version 2.0 |
10 | | * Or under the following terms: |
11 | | * |
12 | | * This library is free software; you can redistribute it and/or |
13 | | * modify it under the terms of the GNU Lesser General Public |
14 | | * License as published by the Free Software Foundation; either |
15 | | * version 2.1 of the License, or (at your option) any later version. |
16 | | * |
17 | | * This library is distributed in the hope that it will be useful, |
18 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | | * Lesser General Public License for more details. |
21 | | * |
22 | | * You should have received a copy of the GNU Lesser General Public |
23 | | * License along with this library; if not, write to the |
24 | | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
25 | | * Boston, MA 02111-1307, USA. |
26 | | */ |
27 | | |
28 | | #ifdef HAVE_CONFIG_H |
29 | | #include <config.h> |
30 | | #endif |
31 | | |
32 | | #include "xdgmimeparent.h" |
33 | | #include "xdgmimeint.h" |
34 | | #include <stdlib.h> |
35 | | #include <stdio.h> |
36 | | #include <assert.h> |
37 | | #include <string.h> |
38 | | #include <fnmatch.h> |
39 | | |
40 | | #ifndef FALSE |
41 | | #define FALSE (0) |
42 | | #endif |
43 | | |
44 | | #ifndef TRUE |
45 | | #define TRUE (!FALSE) |
46 | | #endif |
47 | | |
48 | | typedef struct XdgMimeParents XdgMimeParents; |
49 | | |
50 | | struct XdgMimeParents |
51 | | { |
52 | | char *mime; |
53 | | char **parents; |
54 | | int n_parents; |
55 | | }; |
56 | | |
57 | | struct XdgParentList |
58 | | { |
59 | | struct XdgMimeParents *parents; |
60 | | int n_mimes; |
61 | | }; |
62 | | |
63 | | XdgParentList * |
64 | | _xdg_mime_parent_list_new (void) |
65 | 0 | { |
66 | 0 | XdgParentList *list; |
67 | |
|
68 | 0 | list = malloc (sizeof (XdgParentList)); |
69 | |
|
70 | 0 | list->parents = NULL; |
71 | 0 | list->n_mimes = 0; |
72 | |
|
73 | 0 | return list; |
74 | 0 | } |
75 | | |
76 | | void |
77 | | _xdg_mime_parent_list_free (XdgParentList *list) |
78 | 0 | { |
79 | 0 | int i; |
80 | 0 | char **p; |
81 | |
|
82 | 0 | if (list->parents) |
83 | 0 | { |
84 | 0 | for (i = 0; i < list->n_mimes; i++) |
85 | 0 | { |
86 | 0 | for (p = list->parents[i].parents; *p; p++) |
87 | 0 | free (*p); |
88 | |
|
89 | 0 | free (list->parents[i].parents); |
90 | 0 | free (list->parents[i].mime); |
91 | 0 | } |
92 | 0 | free (list->parents); |
93 | 0 | } |
94 | 0 | free (list); |
95 | 0 | } |
96 | | |
97 | | static int |
98 | | parent_entry_cmp (const void *v1, const void *v2) |
99 | 0 | { |
100 | 0 | return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime); |
101 | 0 | } |
102 | | |
103 | | const char ** |
104 | | _xdg_mime_parent_list_lookup (XdgParentList *list, |
105 | | const char *mime) |
106 | 0 | { |
107 | 0 | XdgMimeParents *entry; |
108 | 0 | XdgMimeParents key; |
109 | |
|
110 | 0 | if (list->n_mimes > 0) |
111 | 0 | { |
112 | 0 | key.mime = (char *)mime; |
113 | 0 | key.parents = NULL; |
114 | 0 | key.n_parents = 0; |
115 | |
|
116 | 0 | entry = bsearch (&key, list->parents, list->n_mimes, |
117 | 0 | sizeof (XdgMimeParents), &parent_entry_cmp); |
118 | 0 | if (entry) |
119 | 0 | return (const char **)entry->parents; |
120 | 0 | } |
121 | | |
122 | 0 | return NULL; |
123 | 0 | } |
124 | | |
125 | | void |
126 | | _xdg_mime_parent_read_from_file (XdgParentList *list, |
127 | | const char *file_name) |
128 | 0 | { |
129 | 0 | FILE *file; |
130 | 0 | char line[255]; |
131 | 0 | int i, alloc; |
132 | 0 | XdgMimeParents *entry; |
133 | |
|
134 | 0 | file = fopen (file_name, "r"); |
135 | |
|
136 | 0 | if (file == NULL) |
137 | 0 | return; |
138 | | |
139 | | /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. |
140 | | * Blah */ |
141 | 0 | alloc = list->n_mimes + 16; |
142 | 0 | list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents)); |
143 | 0 | while (fgets (line, 255, file) != NULL) |
144 | 0 | { |
145 | 0 | char *sep; |
146 | 0 | if (line[0] == '#') |
147 | 0 | continue; |
148 | | |
149 | 0 | sep = strchr (line, ' '); |
150 | 0 | if (sep == NULL) |
151 | 0 | continue; |
152 | 0 | *(sep++) = '\000'; |
153 | 0 | sep[strlen (sep) -1] = '\000'; |
154 | 0 | entry = NULL; |
155 | 0 | for (i = 0; i < list->n_mimes; i++) |
156 | 0 | { |
157 | 0 | if (strcmp (list->parents[i].mime, line) == 0) |
158 | 0 | { |
159 | 0 | entry = &(list->parents[i]); |
160 | 0 | break; |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | 0 | if (!entry) |
165 | 0 | { |
166 | 0 | if (list->n_mimes == alloc) |
167 | 0 | { |
168 | 0 | alloc <<= 1; |
169 | 0 | list->parents = realloc (list->parents, |
170 | 0 | alloc * sizeof (XdgMimeParents)); |
171 | 0 | } |
172 | 0 | list->parents[list->n_mimes].mime = strdup (line); |
173 | 0 | list->parents[list->n_mimes].parents = NULL; |
174 | 0 | entry = &(list->parents[list->n_mimes]); |
175 | 0 | list->n_mimes++; |
176 | 0 | } |
177 | |
|
178 | 0 | if (!entry->parents) |
179 | 0 | { |
180 | 0 | entry->n_parents = 1; |
181 | 0 | entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *)); |
182 | 0 | } |
183 | 0 | else |
184 | 0 | { |
185 | 0 | entry->n_parents += 1; |
186 | 0 | entry->parents = realloc (entry->parents, |
187 | 0 | (entry->n_parents + 2) * sizeof (char *)); |
188 | 0 | } |
189 | 0 | entry->parents[entry->n_parents - 1] = strdup (sep); |
190 | 0 | entry->parents[entry->n_parents] = NULL; |
191 | 0 | } |
192 | |
|
193 | 0 | list->parents = realloc (list->parents, |
194 | 0 | list->n_mimes * sizeof (XdgMimeParents)); |
195 | |
|
196 | 0 | fclose (file); |
197 | | |
198 | 0 | if (list->n_mimes > 1) |
199 | 0 | qsort (list->parents, list->n_mimes, |
200 | 0 | sizeof (XdgMimeParents), &parent_entry_cmp); |
201 | 0 | } |
202 | | |
203 | | |
204 | | void |
205 | | _xdg_mime_parent_list_dump (XdgParentList *list) |
206 | 0 | { |
207 | 0 | int i; |
208 | 0 | char **p; |
209 | |
|
210 | 0 | if (list->parents) |
211 | 0 | { |
212 | 0 | for (i = 0; i < list->n_mimes; i++) |
213 | 0 | { |
214 | 0 | for (p = list->parents[i].parents; *p; p++) |
215 | 0 | printf ("%s %s\n", list->parents[i].mime, *p); |
216 | 0 | } |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | | |