1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimealias.c: Private file. Datastructure for storing the aliases.
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 "xdgmimealias.h"
29 #include "xdgmimeint.h"
44 typedef struct XdgAlias XdgAlias
;
54 struct XdgAlias
*aliases
;
59 _xdg_mime_alias_list_new (void)
63 list
= malloc (sizeof (XdgAliasList
));
72 _xdg_mime_alias_list_free (XdgAliasList
*list
)
78 for (i
= 0; i
< list
->n_aliases
; i
++)
80 free (list
->aliases
[i
].alias
);
81 free (list
->aliases
[i
].mime_type
);
89 alias_entry_cmp (const void *v1
, const void *v2
)
91 return strcmp (((XdgAlias
*)v1
)->alias
, ((XdgAlias
*)v2
)->alias
);
95 _xdg_mime_alias_list_lookup (XdgAliasList
*list
,
101 if (list
->n_aliases
> 0)
103 key
.alias
= (char *)alias
;
104 key
.mime_type
= NULL
;
106 entry
= bsearch (&key
, list
->aliases
, list
->n_aliases
,
107 sizeof (XdgAlias
), alias_entry_cmp
);
109 return entry
->mime_type
;
116 _xdg_mime_alias_read_from_file (XdgAliasList
*list
,
117 const char *file_name
)
123 file
= fopen (file_name
, "r");
128 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
130 alloc
= list
->n_aliases
+ 16;
131 list
->aliases
= realloc (list
->aliases
, alloc
* sizeof (XdgAlias
));
132 while (fgets (line
, 255, file
) != NULL
)
138 sep
= strchr (line
, ' ');
142 sep
[strlen (sep
) -1] = '\000';
143 if (list
->n_aliases
== alloc
)
146 list
->aliases
= realloc (list
->aliases
,
147 alloc
* sizeof (XdgAlias
));
149 list
->aliases
[list
->n_aliases
].alias
= strdup (line
);
150 list
->aliases
[list
->n_aliases
].mime_type
= strdup (sep
);
153 list
->aliases
= realloc (list
->aliases
,
154 list
->n_aliases
* sizeof (XdgAlias
));
158 if (list
->n_aliases
> 1)
159 qsort (list
->aliases
, list
->n_aliases
,
160 sizeof (XdgAlias
), alias_entry_cmp
);
164 #ifdef NOT_USED_IN_GIO
167 _xdg_mime_alias_list_dump (XdgAliasList
*list
)
173 for (i
= 0; i
< list
->n_aliases
; i
++)
176 list
->aliases
[i
].alias
,
177 list
->aliases
[i
].mime_type
);