gosxappinfo: fix typo in url_escape_hostname
[glib.git] / gio / xdgmime / xdgmimeparent.c
blobb29cfd58ae448e2ad2cd8c5fb7aeaf3bc270ac8e
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimealias.c: Private file. Datastructure for storing the hierarchy.
4 * More info can be found at http://www.freedesktop.org/standards/
6 * Copyright (C) 2004 Red Hat, Inc.
7 * Copyright (C) 2004 Matthias Clasen <mclasen@redhat.com>
9 * Licensed under the Academic Free License version 2.0
10 * Or under the following terms:
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.
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.
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/>.
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "xdgmimeparent.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>
38 #ifndef FALSE
39 #define FALSE (0)
40 #endif
42 #ifndef TRUE
43 #define TRUE (!FALSE)
44 #endif
46 typedef struct XdgMimeParents XdgMimeParents;
48 struct XdgMimeParents
50 char *mime;
51 char **parents;
52 int n_parents;
55 struct XdgParentList
57 struct XdgMimeParents *parents;
58 int n_mimes;
61 XdgParentList *
62 _xdg_mime_parent_list_new (void)
64 XdgParentList *list;
66 list = malloc (sizeof (XdgParentList));
68 list->parents = NULL;
69 list->n_mimes = 0;
71 return list;
74 void
75 _xdg_mime_parent_list_free (XdgParentList *list)
77 int i;
78 char **p;
80 if (list->parents)
82 for (i = 0; i < list->n_mimes; i++)
84 for (p = list->parents[i].parents; *p; p++)
85 free (*p);
87 free (list->parents[i].parents);
88 free (list->parents[i].mime);
90 free (list->parents);
92 free (list);
95 static int
96 parent_entry_cmp (const void *v1, const void *v2)
98 return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime);
101 const char **
102 _xdg_mime_parent_list_lookup (XdgParentList *list,
103 const char *mime)
105 XdgMimeParents *entry;
106 XdgMimeParents key;
108 if (list->n_mimes > 0)
110 key.mime = (char *)mime;
111 key.parents = NULL;
112 key.n_parents = 0;
114 entry = bsearch (&key, list->parents, list->n_mimes,
115 sizeof (XdgMimeParents), &parent_entry_cmp);
116 if (entry)
117 return (const char **)entry->parents;
120 return NULL;
123 void
124 _xdg_mime_parent_read_from_file (XdgParentList *list,
125 const char *file_name)
127 FILE *file;
128 char line[255];
129 int i, alloc;
130 XdgMimeParents *entry;
132 file = fopen (file_name, "r");
134 if (file == NULL)
135 return;
137 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
138 * Blah */
139 alloc = list->n_mimes + 16;
140 list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents));
141 while (fgets (line, 255, file) != NULL)
143 char *sep;
144 if (line[0] == '#')
145 continue;
147 sep = strchr (line, ' ');
148 if (sep == NULL)
149 continue;
150 *(sep++) = '\000';
151 sep[strlen (sep) -1] = '\000';
152 entry = NULL;
153 for (i = 0; i < list->n_mimes; i++)
155 if (strcmp (list->parents[i].mime, line) == 0)
157 entry = &(list->parents[i]);
158 break;
162 if (!entry)
164 if (list->n_mimes == alloc)
166 alloc <<= 1;
167 list->parents = realloc (list->parents,
168 alloc * sizeof (XdgMimeParents));
170 list->parents[list->n_mimes].mime = strdup (line);
171 list->parents[list->n_mimes].parents = NULL;
172 entry = &(list->parents[list->n_mimes]);
173 list->n_mimes++;
176 if (!entry->parents)
178 entry->n_parents = 1;
179 entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *));
181 else
183 entry->n_parents += 1;
184 entry->parents = realloc (entry->parents,
185 (entry->n_parents + 2) * sizeof (char *));
187 entry->parents[entry->n_parents - 1] = strdup (sep);
188 entry->parents[entry->n_parents] = NULL;
191 list->parents = realloc (list->parents,
192 list->n_mimes * sizeof (XdgMimeParents));
194 fclose (file);
196 if (list->n_mimes > 1)
197 qsort (list->parents, list->n_mimes,
198 sizeof (XdgMimeParents), &parent_entry_cmp);
201 #ifdef NOT_USED_IN_GIO
203 void
204 _xdg_mime_parent_list_dump (XdgParentList *list)
206 int i;
207 char **p;
209 if (list->parents)
211 for (i = 0; i < list->n_mimes; i++)
213 for (p = list->parents[i].parents; *p; p++)
214 printf ("%s %s\n", list->parents[i].mime, *p);
219 #endif