/src/rauc/subprojects/glib-2.76.5/gio/xdgmime/xdgmimealias.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: C; c-file-style: "gnu" -*- */ |
2 | | /* xdgmimealias.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) 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 "xdgmimealias.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 XdgAlias XdgAlias; |
49 | | |
50 | | struct XdgAlias |
51 | | { |
52 | | char *alias; |
53 | | char *mime_type; |
54 | | }; |
55 | | |
56 | | struct XdgAliasList |
57 | | { |
58 | | struct XdgAlias *aliases; |
59 | | int n_aliases; |
60 | | }; |
61 | | |
62 | | XdgAliasList * |
63 | | _xdg_mime_alias_list_new (void) |
64 | 0 | { |
65 | 0 | XdgAliasList *list; |
66 | |
|
67 | 0 | list = malloc (sizeof (XdgAliasList)); |
68 | |
|
69 | 0 | list->aliases = NULL; |
70 | 0 | list->n_aliases = 0; |
71 | |
|
72 | 0 | return list; |
73 | 0 | } |
74 | | |
75 | | void |
76 | | _xdg_mime_alias_list_free (XdgAliasList *list) |
77 | 0 | { |
78 | 0 | int i; |
79 | |
|
80 | 0 | if (list->aliases) |
81 | 0 | { |
82 | 0 | for (i = 0; i < list->n_aliases; i++) |
83 | 0 | { |
84 | 0 | free (list->aliases[i].alias); |
85 | 0 | free (list->aliases[i].mime_type); |
86 | 0 | } |
87 | 0 | free (list->aliases); |
88 | 0 | } |
89 | 0 | free (list); |
90 | 0 | } |
91 | | |
92 | | static int |
93 | | alias_entry_cmp (const void *v1, const void *v2) |
94 | 0 | { |
95 | 0 | return strcmp (((XdgAlias *)v1)->alias, ((XdgAlias *)v2)->alias); |
96 | 0 | } |
97 | | |
98 | | const char * |
99 | | _xdg_mime_alias_list_lookup (XdgAliasList *list, |
100 | | const char *alias) |
101 | 0 | { |
102 | 0 | XdgAlias *entry; |
103 | 0 | XdgAlias key; |
104 | |
|
105 | 0 | if (list->n_aliases > 0) |
106 | 0 | { |
107 | 0 | key.alias = (char *)alias; |
108 | 0 | key.mime_type = NULL; |
109 | |
|
110 | 0 | entry = bsearch (&key, list->aliases, list->n_aliases, |
111 | 0 | sizeof (XdgAlias), alias_entry_cmp); |
112 | 0 | if (entry) |
113 | 0 | return entry->mime_type; |
114 | 0 | } |
115 | | |
116 | 0 | return NULL; |
117 | 0 | } |
118 | | |
119 | | void |
120 | | _xdg_mime_alias_read_from_file (XdgAliasList *list, |
121 | | const char *file_name) |
122 | 0 | { |
123 | 0 | FILE *file; |
124 | 0 | char line[255]; |
125 | 0 | int alloc; |
126 | |
|
127 | 0 | file = fopen (file_name, "r"); |
128 | |
|
129 | 0 | if (file == NULL) |
130 | 0 | return; |
131 | | |
132 | | /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. |
133 | | * Blah */ |
134 | 0 | alloc = list->n_aliases + 16; |
135 | 0 | list->aliases = realloc (list->aliases, alloc * sizeof (XdgAlias)); |
136 | 0 | while (fgets (line, 255, file) != NULL) |
137 | 0 | { |
138 | 0 | char *sep; |
139 | 0 | if (line[0] == '#') |
140 | 0 | continue; |
141 | | |
142 | 0 | sep = strchr (line, ' '); |
143 | 0 | if (sep == NULL) |
144 | 0 | continue; |
145 | 0 | *(sep++) = '\000'; |
146 | 0 | sep[strlen (sep) -1] = '\000'; |
147 | 0 | if (list->n_aliases == alloc) |
148 | 0 | { |
149 | 0 | alloc <<= 1; |
150 | 0 | list->aliases = realloc (list->aliases, |
151 | 0 | alloc * sizeof (XdgAlias)); |
152 | 0 | } |
153 | 0 | list->aliases[list->n_aliases].alias = strdup (line); |
154 | 0 | list->aliases[list->n_aliases].mime_type = strdup (sep); |
155 | 0 | list->n_aliases++; |
156 | 0 | } |
157 | 0 | list->aliases = realloc (list->aliases, |
158 | 0 | list->n_aliases * sizeof (XdgAlias)); |
159 | |
|
160 | 0 | fclose (file); |
161 | | |
162 | 0 | if (list->n_aliases > 1) |
163 | 0 | qsort (list->aliases, list->n_aliases, |
164 | 0 | sizeof (XdgAlias), alias_entry_cmp); |
165 | 0 | } |
166 | | |
167 | | |
168 | | void |
169 | | _xdg_mime_alias_list_dump (XdgAliasList *list) |
170 | 0 | { |
171 | 0 | int i; |
172 | |
|
173 | 0 | if (list->aliases) |
174 | 0 | { |
175 | 0 | for (i = 0; i < list->n_aliases; i++) |
176 | 0 | { |
177 | 0 | printf ("%s %s\n", |
178 | 0 | list->aliases[i].alias, |
179 | 0 | list->aliases[i].mime_type); |
180 | 0 | } |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | |