]> Pileus Git - ~andy/gtk/blob - gtk/xdgmime/xdgmimealias.c
Sync from upstream
[~andy/gtk] / gtk / xdgmime / xdgmimealias.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimealias.c: Private file.  Datastructure for storing the aliases.
3  *
4  * More info can be found at http://www.freedesktop.org/standards/
5  *
6  * Copyright (C) 2004  Red Hat, Inc.
7  * Copyright (C) 2004  Matthias Clasen <mclasen@redhat.com>
8  *
9  * Licensed under the Academic Free License version 2.0
10  * Or under the following terms:
11  *
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.
16  *
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.
21  *
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.
26  */
27
28 #include <config.h>
29 #include "xdgmimealias.h"
30 #include "xdgmimeint.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <assert.h>
34 #include <string.h>
35 #include <fnmatch.h>
36
37 #ifndef FALSE
38 #define FALSE   (0)
39 #endif
40
41 #ifndef TRUE
42 #define TRUE    (!FALSE)
43 #endif
44
45 typedef struct XdgAlias XdgAlias;
46
47 struct XdgAlias 
48 {
49   char *alias;
50   char *mime_type;
51 };
52
53 struct XdgAliasList
54 {
55   struct XdgAlias *aliases;
56   int n_aliases;
57 };
58
59 XdgAliasList *
60 _xdg_mime_alias_list_new (void)
61 {
62   XdgAliasList *list;
63
64   list = malloc (sizeof (XdgAliasList));
65
66   list->aliases = NULL;
67   list->n_aliases = 0;
68
69   return list;
70 }
71
72 void         
73 _xdg_mime_alias_list_free (XdgAliasList *list)
74 {
75   int i;
76
77   if (list->aliases)
78     {
79       for (i = 0; i < list->n_aliases; i++)
80         {
81           free (list->aliases[i].alias);
82           free (list->aliases[i].mime_type);
83         }
84       free (list->aliases);
85     }
86   free (list);
87 }
88
89 static int
90 alias_entry_cmp (const void *v1, const void *v2)
91 {
92   return strcmp (((XdgAlias *)v1)->alias, ((XdgAlias *)v2)->alias);
93 }
94
95 const char  *
96 _xdg_mime_alias_list_lookup (XdgAliasList *list,
97                              const char   *alias)
98 {
99   XdgAlias *entry;
100   XdgAlias key;
101
102   key.alias = (char *)alias;
103   key.mime_type = 0;
104
105   entry = bsearch (&key, list->aliases, list->n_aliases,
106                    sizeof (XdgAlias), alias_entry_cmp);
107   if (entry)
108     return entry->mime_type;
109
110   return NULL;
111 }
112
113 void
114 _xdg_mime_alias_read_from_file (XdgAliasList *list,
115                                 const char   *file_name)
116 {
117   FILE *file;
118   char line[255];
119   int alloc;
120
121   file = fopen (file_name, "r");
122
123   if (file == NULL)
124     return;
125
126   /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
127    * Blah */
128   alloc = 16;
129   list->aliases = malloc (alloc * sizeof (XdgAlias));
130   while (fgets (line, 255, file) != NULL)
131     {
132       char *sep;
133       if (line[0] == '#')
134         continue;
135
136       sep = strchr (line, ' ');
137       if (sep == NULL)
138         continue;
139       *(sep++) = '\000';
140       sep[strlen (sep) -1] = '\000';
141       if (list->n_aliases == alloc)
142         {
143           alloc <<= 1;
144           list->aliases = realloc (list->aliases, 
145                                    alloc * sizeof (XdgAlias));
146         }
147       list->aliases[list->n_aliases].alias = strdup (line);
148       list->aliases[list->n_aliases].mime_type = strdup (sep);
149       list->n_aliases++;
150     }
151   list->aliases = realloc (list->aliases, 
152                            list->n_aliases * sizeof (XdgAlias));
153
154   fclose (file);  
155   
156   qsort (list->aliases, list->n_aliases, 
157          sizeof (XdgAlias), alias_entry_cmp);
158 }
159
160
161 void
162 _xdg_mime_alias_list_dump (XdgAliasList *list)
163 {
164   int i;
165
166   if (list->aliases)
167     {
168       for (i = 0; i < list->n_aliases; i++)
169         {
170           printf ("%s %s\n", 
171                   list->aliases[i].alias,
172                   list->aliases[i].mime_type);
173         }
174     }
175 }
176
177