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.1 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/>.
28 #include "xdgmimeparent.h"
29 #include "xdgmimeint.h"
44 typedef struct XdgMimeParents XdgMimeParents
;
55 struct XdgMimeParents
*parents
;
60 _xdg_mime_parent_list_new (void)
64 list
= malloc (sizeof (XdgParentList
));
73 _xdg_mime_parent_list_free (XdgParentList
*list
)
80 for (i
= 0; i
< list
->n_mimes
; i
++)
82 for (p
= list
->parents
[i
].parents
; *p
; p
++)
85 free (list
->parents
[i
].parents
);
86 free (list
->parents
[i
].mime
);
94 parent_entry_cmp (const void *v1
, const void *v2
)
96 return strcmp (((XdgMimeParents
*)v1
)->mime
, ((XdgMimeParents
*)v2
)->mime
);
100 _xdg_mime_parent_list_lookup (XdgParentList
*list
,
103 XdgMimeParents
*entry
;
106 if (list
->n_mimes
> 0)
108 key
.mime
= (char *)mime
;
112 entry
= bsearch (&key
, list
->parents
, list
->n_mimes
,
113 sizeof (XdgMimeParents
), &parent_entry_cmp
);
115 return (const char **)entry
->parents
;
122 _xdg_mime_parent_read_from_file (XdgParentList
*list
,
123 const char *file_name
)
128 XdgMimeParents
*entry
;
130 file
= fopen (file_name
, "r");
135 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
137 alloc
= list
->n_mimes
+ 16;
138 list
->parents
= realloc (list
->parents
, alloc
* sizeof (XdgMimeParents
));
139 while (fgets (line
, 255, file
) != NULL
)
145 sep
= strchr (line
, ' ');
149 sep
[strlen (sep
) -1] = '\000';
151 for (i
= 0; i
< list
->n_mimes
; i
++)
153 if (strcmp (list
->parents
[i
].mime
, line
) == 0)
155 entry
= &(list
->parents
[i
]);
162 if (list
->n_mimes
== alloc
)
165 list
->parents
= realloc (list
->parents
,
166 alloc
* sizeof (XdgMimeParents
));
168 list
->parents
[list
->n_mimes
].mime
= strdup (line
);
169 list
->parents
[list
->n_mimes
].parents
= NULL
;
170 entry
= &(list
->parents
[list
->n_mimes
]);
176 entry
->n_parents
= 1;
177 entry
->parents
= malloc ((entry
->n_parents
+ 1) * sizeof (char *));
181 entry
->n_parents
+= 1;
182 entry
->parents
= realloc (entry
->parents
,
183 (entry
->n_parents
+ 2) * sizeof (char *));
185 entry
->parents
[entry
->n_parents
- 1] = strdup (sep
);
186 entry
->parents
[entry
->n_parents
] = NULL
;
189 list
->parents
= realloc (list
->parents
,
190 list
->n_mimes
* sizeof (XdgMimeParents
));
194 if (list
->n_mimes
> 1)
195 qsort (list
->parents
, list
->n_mimes
,
196 sizeof (XdgMimeParents
), &parent_entry_cmp
);
199 #ifdef NOT_USED_IN_GIO
202 _xdg_mime_parent_list_dump (XdgParentList
*list
)
209 for (i
= 0; i
< list
->n_mimes
; i
++)
211 for (p
= list
->parents
[i
].parents
; *p
; p
++)
212 printf ("%s %s\n", list
->parents
[i
].mime
, *p
);