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/>.
30 #include "xdgmimeparent.h"
31 #include "xdgmimeint.h"
46 typedef struct XdgMimeParents XdgMimeParents
;
57 struct XdgMimeParents
*parents
;
62 _xdg_mime_parent_list_new (void)
66 list
= malloc (sizeof (XdgParentList
));
75 _xdg_mime_parent_list_free (XdgParentList
*list
)
82 for (i
= 0; i
< list
->n_mimes
; i
++)
84 for (p
= list
->parents
[i
].parents
; *p
; p
++)
87 free (list
->parents
[i
].parents
);
88 free (list
->parents
[i
].mime
);
96 parent_entry_cmp (const void *v1
, const void *v2
)
98 return strcmp (((XdgMimeParents
*)v1
)->mime
, ((XdgMimeParents
*)v2
)->mime
);
102 _xdg_mime_parent_list_lookup (XdgParentList
*list
,
105 XdgMimeParents
*entry
;
108 if (list
->n_mimes
> 0)
110 key
.mime
= (char *)mime
;
114 entry
= bsearch (&key
, list
->parents
, list
->n_mimes
,
115 sizeof (XdgMimeParents
), &parent_entry_cmp
);
117 return (const char **)entry
->parents
;
124 _xdg_mime_parent_read_from_file (XdgParentList
*list
,
125 const char *file_name
)
130 XdgMimeParents
*entry
;
132 file
= fopen (file_name
, "r");
137 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
139 alloc
= list
->n_mimes
+ 16;
140 list
->parents
= realloc (list
->parents
, alloc
* sizeof (XdgMimeParents
));
141 while (fgets (line
, 255, file
) != NULL
)
147 sep
= strchr (line
, ' ');
151 sep
[strlen (sep
) -1] = '\000';
153 for (i
= 0; i
< list
->n_mimes
; i
++)
155 if (strcmp (list
->parents
[i
].mime
, line
) == 0)
157 entry
= &(list
->parents
[i
]);
164 if (list
->n_mimes
== alloc
)
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
]);
178 entry
->n_parents
= 1;
179 entry
->parents
= malloc ((entry
->n_parents
+ 1) * sizeof (char *));
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
));
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
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
);