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