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, write to the
24 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
32 #include "xdgmimeparent.h"
33 #include "xdgmimeint.h"
48 typedef struct XdgMimeParents XdgMimeParents
;
59 struct XdgMimeParents
*parents
;
64 _xdg_mime_parent_list_new (void)
68 list
= malloc (sizeof (XdgParentList
));
77 _xdg_mime_parent_list_free (XdgParentList
*list
)
84 for (i
= 0; i
< list
->n_mimes
; i
++)
86 for (p
= list
->parents
[i
].parents
; *p
; p
++)
89 free (list
->parents
[i
].parents
);
90 free (list
->parents
[i
].mime
);
98 parent_entry_cmp (const void *v1
, const void *v2
)
100 return strcmp (((XdgMimeParents
*)v1
)->mime
, ((XdgMimeParents
*)v2
)->mime
);
104 _xdg_mime_parent_list_lookup (XdgParentList
*list
,
107 XdgMimeParents
*entry
;
110 if (list
->n_mimes
> 0)
112 key
.mime
= (char *)mime
;
115 entry
= bsearch (&key
, list
->parents
, list
->n_mimes
,
116 sizeof (XdgMimeParents
), &parent_entry_cmp
);
118 return (const char **)entry
->parents
;
125 _xdg_mime_parent_read_from_file (XdgParentList
*list
,
126 const char *file_name
)
131 XdgMimeParents
*entry
;
133 file
= fopen (file_name
, "r");
138 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
140 alloc
= list
->n_mimes
+ 16;
141 list
->parents
= realloc (list
->parents
, alloc
* sizeof (XdgMimeParents
));
142 while (fgets (line
, 255, file
) != NULL
)
148 sep
= strchr (line
, ' ');
152 sep
[strlen (sep
) -1] = '\000';
154 for (i
= 0; i
< list
->n_mimes
; i
++)
156 if (strcmp (list
->parents
[i
].mime
, line
) == 0)
158 entry
= &(list
->parents
[i
]);
165 if (list
->n_mimes
== alloc
)
168 list
->parents
= realloc (list
->parents
,
169 alloc
* sizeof (XdgMimeParents
));
171 list
->parents
[list
->n_mimes
].mime
= strdup (line
);
172 list
->parents
[list
->n_mimes
].parents
= NULL
;
173 entry
= &(list
->parents
[list
->n_mimes
]);
179 entry
->n_parents
= 1;
180 entry
->parents
= malloc ((entry
->n_parents
+ 1) * sizeof (char *));
184 entry
->n_parents
+= 1;
185 entry
->parents
= realloc (entry
->parents
,
186 (entry
->n_parents
+ 2) * sizeof (char *));
188 entry
->parents
[entry
->n_parents
- 1] = strdup (sep
);
189 entry
->parents
[entry
->n_parents
] = NULL
;
192 list
->parents
= realloc (list
->parents
,
193 list
->n_mimes
* sizeof (XdgMimeParents
));
197 if (list
->n_mimes
> 1)
198 qsort (list
->parents
, list
->n_mimes
,
199 sizeof (XdgMimeParents
), &parent_entry_cmp
);
204 _xdg_mime_parent_list_dump (XdgParentList
*list
)
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
);