Back to index

libcitadel  8.12
xdgmimeparent.c
Go to the documentation of this file.
00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* xdgmimealias.c: Private file.  Datastructure for storing the hierarchy.
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 "xdgmimeparent.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 XdgMimeParents XdgMimeParents;
00049 
00050 struct XdgMimeParents
00051 {
00052   char *mime;
00053   char **parents;
00054   int n_parents;
00055 };
00056 
00057 struct XdgParentList
00058 {
00059   struct XdgMimeParents *parents;
00060   int n_mimes;
00061 };
00062 
00063 XdgParentList *
00064 _xdg_mime_parent_list_new (void)
00065 {
00066   XdgParentList *list;
00067 
00068   list = malloc (sizeof (XdgParentList));
00069 
00070   list->parents = NULL;
00071   list->n_mimes = 0;
00072 
00073   return list;
00074 }
00075 
00076 void         
00077 _xdg_mime_parent_list_free (XdgParentList *list)
00078 {
00079   int i;
00080   char **p;
00081 
00082   if (list->parents)
00083     {
00084       for (i = 0; i < list->n_mimes; i++)
00085        {
00086          for (p = list->parents[i].parents; *p; p++)
00087            free (*p);
00088 
00089          free (list->parents[i].parents);
00090          free (list->parents[i].mime);
00091        }
00092       free (list->parents);
00093     }
00094   free (list);
00095 }
00096 
00097 static int
00098 parent_entry_cmp (const void *v1, const void *v2)
00099 {
00100   return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime);
00101 }
00102 
00103 const char **
00104 _xdg_mime_parent_list_lookup (XdgParentList *list,
00105                            const char    *mime)
00106 {
00107   XdgMimeParents *entry;
00108   XdgMimeParents key;
00109 
00110   if (list->n_mimes > 0)
00111     {
00112       key.mime = (char *)mime;
00113       key.parents = NULL;
00114 
00115       entry = bsearch (&key, list->parents, list->n_mimes,
00116                      sizeof (XdgMimeParents), &parent_entry_cmp);
00117       if (entry)
00118         return (const char **)entry->parents;
00119     }
00120 
00121   return NULL;
00122 }
00123 
00124 void
00125 _xdg_mime_parent_read_from_file (XdgParentList *list,
00126                              const char    *file_name)
00127 {
00128   FILE *file;
00129   char line[255];
00130   int i, alloc;
00131   XdgMimeParents *entry;
00132 
00133   file = fopen (file_name, "r");
00134 
00135   if (file == NULL)
00136     return;
00137 
00138   /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
00139    * Blah */
00140   alloc = list->n_mimes + 16;
00141   list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents));
00142   while (fgets (line, 255, file) != NULL)
00143     {
00144       char *sep;
00145       if (line[0] == '#')
00146        continue;
00147 
00148       sep = strchr (line, ' ');
00149       if (sep == NULL)
00150        continue;
00151       *(sep++) = '\000';
00152       sep[strlen (sep) -1] = '\000';
00153       entry = NULL;
00154       for (i = 0; i < list->n_mimes; i++)
00155        {
00156          if (strcmp (list->parents[i].mime, line) == 0)
00157            {
00158              entry = &(list->parents[i]);
00159              break;
00160            }
00161        }
00162       
00163       if (!entry)
00164        {
00165          if (list->n_mimes == alloc)
00166            {
00167              alloc <<= 1;
00168              list->parents = realloc (list->parents, 
00169                                    alloc * sizeof (XdgMimeParents));
00170            }
00171          list->parents[list->n_mimes].mime = strdup (line);
00172          list->parents[list->n_mimes].parents = NULL;
00173          entry = &(list->parents[list->n_mimes]);
00174          list->n_mimes++;
00175        }
00176 
00177       if (!entry->parents)
00178        {
00179          entry->n_parents = 1;
00180          entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *));
00181        }
00182       else
00183        {
00184          entry->n_parents += 1;
00185          entry->parents = realloc (entry->parents, 
00186                                 (entry->n_parents + 2) * sizeof (char *));
00187        }
00188       entry->parents[entry->n_parents - 1] = strdup (sep);
00189       entry->parents[entry->n_parents] = NULL;
00190     }
00191 
00192   list->parents = realloc (list->parents, 
00193                         list->n_mimes * sizeof (XdgMimeParents));
00194 
00195   fclose (file);  
00196   
00197   if (list->n_mimes > 1)
00198     qsort (list->parents, list->n_mimes, 
00199            sizeof (XdgMimeParents), &parent_entry_cmp);
00200 }
00201 
00202 
00203 void         
00204 _xdg_mime_parent_list_dump (XdgParentList *list)
00205 {
00206   int i;
00207   char **p;
00208 
00209   if (list->parents)
00210     {
00211       for (i = 0; i < list->n_mimes; i++)
00212        {
00213          for (p = list->parents[i].parents; *p; p++)
00214            printf ("%s %s\n", list->parents[i].mime, *p);
00215        }
00216     }
00217 }
00218 
00219