xdgmimeparent.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* -*- mode: C; c-file-style: "gnu" -*- */
  2. /* xdgmimealias.c: Private file. Datastructure for storing the hierarchy.
  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. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include "xdgmimeparent.h"
  31. #include "xdgmimeint.h"
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <assert.h>
  35. #include <string.h>
  36. #include <fnmatch.h>
  37. #ifndef FALSE
  38. #define FALSE (0)
  39. #endif
  40. #ifndef TRUE
  41. #define TRUE (!FALSE)
  42. #endif
  43. typedef struct XdgMimeParents XdgMimeParents;
  44. struct XdgMimeParents
  45. {
  46. char *mime;
  47. char **parents;
  48. int n_parents;
  49. };
  50. struct XdgParentList
  51. {
  52. struct XdgMimeParents *parents;
  53. int n_mimes;
  54. };
  55. XdgParentList *
  56. _xdg_mime_parent_list_new (void)
  57. {
  58. XdgParentList *list;
  59. list = malloc (sizeof (XdgParentList));
  60. list->parents = NULL;
  61. list->n_mimes = 0;
  62. return list;
  63. }
  64. void
  65. _xdg_mime_parent_list_free (XdgParentList *list)
  66. {
  67. int i;
  68. char **p;
  69. if (list->parents)
  70. {
  71. for (i = 0; i < list->n_mimes; i++)
  72. {
  73. for (p = list->parents[i].parents; *p; p++)
  74. free (*p);
  75. free (list->parents[i].parents);
  76. free (list->parents[i].mime);
  77. }
  78. free (list->parents);
  79. }
  80. free (list);
  81. }
  82. static int
  83. parent_entry_cmp (const void *v1, const void *v2)
  84. {
  85. return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime);
  86. }
  87. const char **
  88. _xdg_mime_parent_list_lookup (XdgParentList *list,
  89. const char *mime)
  90. {
  91. XdgMimeParents *entry;
  92. XdgMimeParents key;
  93. if (list->n_mimes > 0)
  94. {
  95. key.mime = (char *)mime;
  96. key.parents = NULL;
  97. entry = bsearch (&key, list->parents, list->n_mimes,
  98. sizeof (XdgMimeParents), &parent_entry_cmp);
  99. if (entry)
  100. return (const char **)entry->parents;
  101. }
  102. return NULL;
  103. }
  104. void
  105. _xdg_mime_parent_read_from_file (XdgParentList *list,
  106. const char *file_name)
  107. {
  108. FILE *file;
  109. char line[255];
  110. int i, alloc;
  111. XdgMimeParents *entry;
  112. file = fopen (file_name, "r");
  113. if (file == NULL)
  114. return;
  115. /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
  116. * Blah */
  117. alloc = list->n_mimes + 16;
  118. list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents));
  119. while (fgets (line, 255, file) != NULL)
  120. {
  121. char *sep;
  122. if (line[0] == '#')
  123. continue;
  124. sep = strchr (line, ' ');
  125. if (sep == NULL)
  126. continue;
  127. *(sep++) = '\000';
  128. sep[strlen (sep) -1] = '\000';
  129. entry = NULL;
  130. for (i = 0; i < list->n_mimes; i++)
  131. {
  132. if (strcmp (list->parents[i].mime, line) == 0)
  133. {
  134. entry = &(list->parents[i]);
  135. break;
  136. }
  137. }
  138. if (!entry)
  139. {
  140. if (list->n_mimes == alloc)
  141. {
  142. alloc <<= 1;
  143. list->parents = realloc (list->parents,
  144. alloc * sizeof (XdgMimeParents));
  145. }
  146. list->parents[list->n_mimes].mime = strdup (line);
  147. list->parents[list->n_mimes].parents = NULL;
  148. entry = &(list->parents[list->n_mimes]);
  149. list->n_mimes++;
  150. }
  151. if (!entry->parents)
  152. {
  153. entry->n_parents = 1;
  154. entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *));
  155. }
  156. else
  157. {
  158. entry->n_parents += 1;
  159. entry->parents = realloc (entry->parents,
  160. (entry->n_parents + 2) * sizeof (char *));
  161. }
  162. entry->parents[entry->n_parents - 1] = strdup (sep);
  163. entry->parents[entry->n_parents] = NULL;
  164. }
  165. list->parents = realloc (list->parents,
  166. list->n_mimes * sizeof (XdgMimeParents));
  167. fclose (file);
  168. if (list->n_mimes > 1)
  169. qsort (list->parents, list->n_mimes,
  170. sizeof (XdgMimeParents), &parent_entry_cmp);
  171. }
  172. void
  173. _xdg_mime_parent_list_dump (XdgParentList *list)
  174. {
  175. int i;
  176. char **p;
  177. if (list->parents)
  178. {
  179. for (i = 0; i < list->n_mimes; i++)
  180. {
  181. for (p = list->parents[i].parents; *p; p++)
  182. printf ("%s %s\n", list->parents[i].mime, *p);
  183. }
  184. }
  185. }