/src/glib/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, see <http://www.gnu.org/licenses/>. |
24 | | */ |
25 | | |
26 | | #include "config.h" |
27 | | |
28 | | #include "xdgmimealias.h" |
29 | | #include "xdgmimeint.h" |
30 | | #include <stdlib.h> |
31 | | #include <stdio.h> |
32 | | #include <assert.h> |
33 | | #include <string.h> |
34 | | #include <fnmatch.h> |
35 | | |
36 | | #ifndef FALSE |
37 | | #define FALSE (0) |
38 | | #endif |
39 | | |
40 | | #ifndef TRUE |
41 | | #define TRUE (!FALSE) |
42 | | #endif |
43 | | |
44 | | typedef struct XdgAlias XdgAlias; |
45 | | |
46 | | struct XdgAlias |
47 | | { |
48 | | char *alias; |
49 | | char *mime_type; |
50 | | }; |
51 | | |
52 | | struct XdgAliasList |
53 | | { |
54 | | struct XdgAlias *aliases; |
55 | | int n_aliases; |
56 | | }; |
57 | | |
58 | | XdgAliasList * |
59 | | _xdg_mime_alias_list_new (void) |
60 | 0 | { |
61 | 0 | XdgAliasList *list; |
62 | |
|
63 | 0 | list = malloc (sizeof (XdgAliasList)); |
64 | |
|
65 | 0 | list->aliases = NULL; |
66 | 0 | list->n_aliases = 0; |
67 | |
|
68 | 0 | return list; |
69 | 0 | } |
70 | | |
71 | | void |
72 | | _xdg_mime_alias_list_free (XdgAliasList *list) |
73 | 0 | { |
74 | 0 | int i; |
75 | |
|
76 | 0 | if (list->aliases) |
77 | 0 | { |
78 | 0 | for (i = 0; i < list->n_aliases; i++) |
79 | 0 | { |
80 | 0 | free (list->aliases[i].alias); |
81 | 0 | free (list->aliases[i].mime_type); |
82 | 0 | } |
83 | 0 | free (list->aliases); |
84 | 0 | } |
85 | 0 | free (list); |
86 | 0 | } |
87 | | |
88 | | static int |
89 | | alias_entry_cmp (const void *v1, const void *v2) |
90 | 0 | { |
91 | 0 | return strcmp (((XdgAlias *)v1)->alias, ((XdgAlias *)v2)->alias); |
92 | 0 | } |
93 | | |
94 | | const char * |
95 | | _xdg_mime_alias_list_lookup (XdgAliasList *list, |
96 | | const char *alias) |
97 | 0 | { |
98 | 0 | XdgAlias *entry; |
99 | 0 | XdgAlias key; |
100 | |
|
101 | 0 | if (list->n_aliases > 0) |
102 | 0 | { |
103 | 0 | key.alias = (char *)alias; |
104 | 0 | key.mime_type = NULL; |
105 | |
|
106 | 0 | entry = bsearch (&key, list->aliases, list->n_aliases, |
107 | 0 | sizeof (XdgAlias), alias_entry_cmp); |
108 | 0 | if (entry) |
109 | 0 | return entry->mime_type; |
110 | 0 | } |
111 | | |
112 | 0 | return NULL; |
113 | 0 | } |
114 | | |
115 | | void |
116 | | _xdg_mime_alias_read_from_file (XdgAliasList *list, |
117 | | const char *file_name) |
118 | 0 | { |
119 | 0 | FILE *file; |
120 | 0 | char line[255]; |
121 | 0 | int alloc; |
122 | |
|
123 | 0 | file = fopen (file_name, "r"); |
124 | |
|
125 | 0 | if (file == NULL) |
126 | 0 | return; |
127 | | |
128 | | /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. |
129 | | * Blah */ |
130 | 0 | alloc = list->n_aliases + 16; |
131 | 0 | list->aliases = realloc (list->aliases, alloc * sizeof (XdgAlias)); |
132 | 0 | while (fgets (line, 255, file) != NULL) |
133 | 0 | { |
134 | 0 | char *sep; |
135 | 0 | if (line[0] == '#') |
136 | 0 | continue; |
137 | | |
138 | 0 | sep = strchr (line, ' '); |
139 | 0 | if (sep == NULL) |
140 | 0 | continue; |
141 | 0 | *(sep++) = '\000'; |
142 | 0 | sep[strlen (sep) -1] = '\000'; |
143 | 0 | if (list->n_aliases == alloc) |
144 | 0 | { |
145 | 0 | alloc <<= 1; |
146 | 0 | list->aliases = realloc (list->aliases, |
147 | 0 | alloc * sizeof (XdgAlias)); |
148 | 0 | } |
149 | 0 | list->aliases[list->n_aliases].alias = strdup (line); |
150 | 0 | list->aliases[list->n_aliases].mime_type = strdup (sep); |
151 | 0 | list->n_aliases++; |
152 | 0 | } |
153 | 0 | list->aliases = realloc (list->aliases, |
154 | 0 | list->n_aliases * sizeof (XdgAlias)); |
155 | |
|
156 | 0 | fclose (file); |
157 | | |
158 | 0 | if (list->n_aliases > 1) |
159 | 0 | qsort (list->aliases, list->n_aliases, |
160 | 0 | sizeof (XdgAlias), alias_entry_cmp); |
161 | 0 | } |
162 | | |
163 | | |
164 | | #ifdef NOT_USED_IN_GIO |
165 | | |
166 | | void |
167 | | _xdg_mime_alias_list_dump (XdgAliasList *list) |
168 | | { |
169 | | int i; |
170 | | |
171 | | if (list->aliases) |
172 | | { |
173 | | for (i = 0; i < list->n_aliases; i++) |
174 | | { |
175 | | printf ("%s %s\n", |
176 | | list->aliases[i].alias, |
177 | | list->aliases[i].mime_type); |
178 | | } |
179 | | } |
180 | | } |
181 | | |
182 | | #endif |