Back to index

libcitadel  8.12
xdgmimealias.c
Go to the documentation of this file.
00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* xdgmimealias.c: Private file.  Datastructure for storing the aliases.
00003  *
00004  * More info can be found at http://www.freedesktop.org/standards/
00005  *
00006  * Copyright (C) 2004  Red Hat, Inc.
00007  * Copyright (C) 2004  Matthias Clasen <mclasen@redhat.com>
00008  *
00009  * Licensed under the Academic Free License version 2.0
00010  * Or under the following terms:
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with this library; if not, write to the
00024  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00025  * Boston, MA 02111-1307, USA.
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include "xdgmimealias.h"
00033 #include "xdgmimeint.h"
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <assert.h>
00037 #include <string.h>
00038 #include <fnmatch.h>
00039 
00040 #ifndef       FALSE
00041 #define       FALSE  (0)
00042 #endif
00043 
00044 #ifndef       TRUE
00045 #define       TRUE   (!FALSE)
00046 #endif
00047 
00048 typedef struct XdgAlias XdgAlias;
00049 
00050 struct XdgAlias 
00051 {
00052   char *alias;
00053   char *mime_type;
00054 };
00055 
00056 struct XdgAliasList
00057 {
00058   struct XdgAlias *aliases;
00059   int n_aliases;
00060 };
00061 
00062 XdgAliasList *
00063 _xdg_mime_alias_list_new (void)
00064 {
00065   XdgAliasList *list;
00066 
00067   list = malloc (sizeof (XdgAliasList));
00068 
00069   list->aliases = NULL;
00070   list->n_aliases = 0;
00071 
00072   return list;
00073 }
00074 
00075 void         
00076 _xdg_mime_alias_list_free (XdgAliasList *list)
00077 {
00078   int i;
00079 
00080   if (list->aliases)
00081     {
00082       for (i = 0; i < list->n_aliases; i++)
00083        {
00084          free (list->aliases[i].alias);
00085          free (list->aliases[i].mime_type);
00086        }
00087       free (list->aliases);
00088     }
00089   free (list);
00090 }
00091 
00092 static int
00093 alias_entry_cmp (const void *v1, const void *v2)
00094 {
00095   return strcmp (((XdgAlias *)v1)->alias, ((XdgAlias *)v2)->alias);
00096 }
00097 
00098 const char  *
00099 _xdg_mime_alias_list_lookup (XdgAliasList *list,
00100                           const char   *alias)
00101 {
00102   XdgAlias *entry;
00103   XdgAlias key;
00104 
00105   if (list->n_aliases > 0)
00106     {
00107       key.alias = (char *)alias;
00108       key.mime_type = NULL;
00109 
00110       entry = bsearch (&key, list->aliases, list->n_aliases,
00111                      sizeof (XdgAlias), alias_entry_cmp);
00112       if (entry)
00113         return entry->mime_type;
00114     }
00115 
00116   return NULL;
00117 }
00118 
00119 void
00120 _xdg_mime_alias_read_from_file (XdgAliasList *list,
00121                             const char   *file_name)
00122 {
00123   FILE *file;
00124   char line[255];
00125   int alloc;
00126 
00127   file = fopen (file_name, "r");
00128 
00129   if (file == NULL)
00130     return;
00131 
00132   /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
00133    * Blah */
00134   alloc = list->n_aliases + 16;
00135   list->aliases = realloc (list->aliases, alloc * sizeof (XdgAlias));
00136   while (fgets (line, 255, file) != NULL)
00137     {
00138       char *sep;
00139       if (line[0] == '#')
00140        continue;
00141 
00142       sep = strchr (line, ' ');
00143       if (sep == NULL)
00144        continue;
00145       *(sep++) = '\000';
00146       sep[strlen (sep) -1] = '\000';
00147       if (list->n_aliases == alloc)
00148        {
00149          alloc <<= 1;
00150          list->aliases = realloc (list->aliases, 
00151                                alloc * sizeof (XdgAlias));
00152        }
00153       list->aliases[list->n_aliases].alias = strdup (line);
00154       list->aliases[list->n_aliases].mime_type = strdup (sep);
00155       list->n_aliases++;
00156     }
00157   list->aliases = realloc (list->aliases, 
00158                         list->n_aliases * sizeof (XdgAlias));
00159 
00160   fclose (file);  
00161   
00162   if (list->n_aliases > 1)
00163     qsort (list->aliases, list->n_aliases, 
00164            sizeof (XdgAlias), alias_entry_cmp);
00165 }
00166 
00167 
00168 void
00169 _xdg_mime_alias_list_dump (XdgAliasList *list)
00170 {
00171   int i;
00172 
00173   if (list->aliases)
00174     {
00175       for (i = 0; i < list->n_aliases; i++)
00176        {
00177          printf ("%s %s\n", 
00178                 list->aliases[i].alias,
00179                 list->aliases[i].mime_type);
00180        }
00181     }
00182 }
00183 
00184