]> git.proxmox.com Git - libxdgmime-perl.git/blame - src/xdgmimeicon.c
Squashed 'xdgmime-source/' changes from 28b70c4..3e7ee2d
[libxdgmime-perl.git] / src / xdgmimeicon.c
CommitLineData
cf31d981
SI
1/* -*- mode: C; c-file-style: "gnu" -*- */
2/* xdgmimeicon.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) 2008 Red Hat, Inc.
7 *
73d5a30a 8 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
cf31d981
SI
9 */
10
11#ifdef HAVE_CONFIG_H
12#include <config.h>
13#endif
14
15#include "xdgmimeicon.h"
16#include "xdgmimeint.h"
17#include <stdlib.h>
18#include <stdio.h>
19#include <assert.h>
20#include <string.h>
21#include <fnmatch.h>
22
23#ifndef FALSE
24#define FALSE (0)
25#endif
26
27#ifndef TRUE
28#define TRUE (!FALSE)
29#endif
30
31typedef struct XdgIcon XdgIcon;
32
33struct XdgIcon
34{
35 char *mime_type;
36 char *icon_name;
37};
38
39struct XdgIconList
40{
41 struct XdgIcon *icons;
42 int n_icons;
43};
44
45XdgIconList *
46_xdg_mime_icon_list_new (void)
47{
48 XdgIconList *list;
49
50 list = malloc (sizeof (XdgIconList));
51
52 list->icons = NULL;
53 list->n_icons = 0;
54
55 return list;
56}
57
58void
59_xdg_mime_icon_list_free (XdgIconList *list)
60{
61 int i;
62
63 if (list->icons)
64 {
65 for (i = 0; i < list->n_icons; i++)
66 {
67 free (list->icons[i].mime_type);
68 free (list->icons[i].icon_name);
69 }
70 free (list->icons);
71 }
72 free (list);
73}
74
75static int
76icon_entry_cmp (const void *v1, const void *v2)
77{
78 return strcmp (((XdgIcon *)v1)->mime_type, ((XdgIcon *)v2)->mime_type);
79}
80
81const char *
82_xdg_mime_icon_list_lookup (XdgIconList *list,
83 const char *mime_type)
84{
85 XdgIcon *entry;
86 XdgIcon key;
87
88 if (list->n_icons > 0)
89 {
90 key.mime_type = (char *)mime_type;
91 key.icon_name = NULL;
92
93 entry = bsearch (&key, list->icons, list->n_icons,
94 sizeof (XdgIcon), icon_entry_cmp);
95 if (entry)
96 return entry->icon_name;
97 }
98
99 return NULL;
100}
101
102void
103_xdg_mime_icon_read_from_file (XdgIconList *list,
104 const char *file_name)
105{
106 FILE *file;
107 char line[255];
108 int alloc;
109
110 file = fopen (file_name, "r");
111
112 if (file == NULL)
113 return;
114
115 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
116 * Blah */
117 alloc = list->n_icons + 16;
118 list->icons = realloc (list->icons, alloc * sizeof (XdgIcon));
119 while (fgets (line, 255, file) != NULL)
120 {
121 char *sep;
122 if (line[0] == '#')
123 continue;
124
125 sep = strchr (line, ':');
126 if (sep == NULL)
127 continue;
128 *(sep++) = '\000';
129 sep[strlen (sep) -1] = '\000';
130 if (list->n_icons == alloc)
131 {
132 alloc <<= 1;
133 list->icons = realloc (list->icons,
134 alloc * sizeof (XdgIcon));
135 }
136 list->icons[list->n_icons].mime_type = strdup (line);
137 list->icons[list->n_icons].icon_name = strdup (sep);
138 list->n_icons++;
139 }
140 list->icons = realloc (list->icons,
141 list->n_icons * sizeof (XdgIcon));
142
143 fclose (file);
144
145 if (list->n_icons > 1)
146 qsort (list->icons, list->n_icons,
147 sizeof (XdgIcon), icon_entry_cmp);
148}
149
150
151void
152_xdg_mime_icon_list_dump (XdgIconList *list)
153{
154 int i;
155
156 if (list->icons)
157 {
158 for (i = 0; i < list->n_icons; i++)
159 {
160 printf ("%s %s\n",
161 list->icons[i].mime_type,
162 list->icons[i].icon_name);
163 }
164 }
165}
166
167