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 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
16 #include "xdgmimeparent.h"
17 #include "xdgmimeint.h"
32 typedef struct XdgMimeParents XdgMimeParents;
43 struct XdgMimeParents *parents;
48 _xdg_mime_parent_list_new (void)
52 list = malloc (sizeof (XdgParentList));
61 _xdg_mime_parent_list_free (XdgParentList *list)
68 for (i = 0; i < list->n_mimes; i++)
70 for (p = list->parents[i].parents; *p; p++)
73 free (list->parents[i].parents);
74 free (list->parents[i].mime);
82 parent_entry_cmp (const void *v1, const void *v2)
84 return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime);
88 _xdg_mime_parent_list_lookup (XdgParentList *list,
91 XdgMimeParents *entry;
94 if (list->n_mimes > 0)
96 key.mime = (char *)mime;
100 entry = bsearch (&key, list->parents, list->n_mimes,
101 sizeof (XdgMimeParents), &parent_entry_cmp);
103 return (const char **)entry->parents;
110 _xdg_mime_parent_read_from_file (XdgParentList *list,
111 const char *file_name)
116 XdgMimeParents *entry;
118 file = fopen (file_name, "r");
123 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
125 alloc = list->n_mimes + 16;
126 list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents));
127 while (fgets (line, 255, file) != NULL)
133 sep = strchr (line, ' ');
137 sep[strlen (sep) -1] = '\000';
139 for (i = 0; i < list->n_mimes; i++)
141 if (strcmp (list->parents[i].mime, line) == 0)
143 entry = &(list->parents[i]);
150 if (list->n_mimes == alloc)
153 list->parents = realloc (list->parents,
154 alloc * sizeof (XdgMimeParents));
156 list->parents[list->n_mimes].mime = strdup (line);
157 list->parents[list->n_mimes].parents = NULL;
158 entry = &(list->parents[list->n_mimes]);
164 entry->n_parents = 1;
165 entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *));
169 entry->n_parents += 1;
170 entry->parents = realloc (entry->parents,
171 (entry->n_parents + 2) * sizeof (char *));
173 entry->parents[entry->n_parents - 1] = strdup (sep);
174 entry->parents[entry->n_parents] = NULL;
177 list->parents = realloc (list->parents,
178 list->n_mimes * sizeof (XdgMimeParents));
182 if (list->n_mimes > 1)
183 qsort (list->parents, list->n_mimes,
184 sizeof (XdgMimeParents), &parent_entry_cmp);
189 _xdg_mime_parent_list_dump (XdgParentList *list)
196 for (i = 0; i < list->n_mimes; i++)
198 for (p = list->parents[i].parents; *p; p++)
199 printf ("%s %s\n", list->parents[i].mime, *p);