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 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 "xdgmimealias.h"
33 #include "xdgmimeint.h"
48 typedef struct XdgAlias XdgAlias
;
58 struct XdgAlias
*aliases
;
63 _xdg_mime_alias_list_new (void)
67 list
= malloc (sizeof (XdgAliasList
));
76 _xdg_mime_alias_list_free (XdgAliasList
*list
)
82 for (i
= 0; i
< list
->n_aliases
; i
++)
84 free (list
->aliases
[i
].alias
);
85 free (list
->aliases
[i
].mime_type
);
93 alias_entry_cmp (const void *v1
, const void *v2
)
95 return strcmp (((XdgAlias
*)v1
)->alias
, ((XdgAlias
*)v2
)->alias
);
99 _xdg_mime_alias_list_lookup (XdgAliasList
*list
,
105 if (list
->n_aliases
> 0)
107 key
.alias
= (char *)alias
;
110 entry
= bsearch (&key
, list
->aliases
, list
->n_aliases
,
111 sizeof (XdgAlias
), alias_entry_cmp
);
113 return entry
->mime_type
;
120 _xdg_mime_alias_read_from_file (XdgAliasList
*list
,
121 const char *file_name
)
127 file
= fopen (file_name
, "r");
132 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
134 alloc
= list
->n_aliases
+ 16;
135 list
->aliases
= realloc (list
->aliases
, alloc
* sizeof (XdgAlias
));
136 while (fgets (line
, 255, file
) != NULL
)
142 sep
= strchr (line
, ' ');
146 sep
[strlen (sep
) -1] = '\000';
147 if (list
->n_aliases
== alloc
)
150 list
->aliases
= realloc (list
->aliases
,
151 alloc
* sizeof (XdgAlias
));
153 list
->aliases
[list
->n_aliases
].alias
= strdup (line
);
154 list
->aliases
[list
->n_aliases
].mime_type
= strdup (sep
);
157 list
->aliases
= realloc (list
->aliases
,
158 list
->n_aliases
* sizeof (XdgAlias
));
162 if (list
->n_aliases
> 1)
163 qsort (list
->aliases
, list
->n_aliases
,
164 sizeof (XdgAlias
), alias_entry_cmp
);
169 _xdg_mime_alias_list_dump (XdgAliasList
*list
)
175 for (i
= 0; i
< list
->n_aliases
; i
++)
178 list
->aliases
[i
].alias
,
179 list
->aliases
[i
].mime_type
);