Coverage Report

Created: 2026-02-26 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/irssi/subprojects/glib-2.74.7/gio/xdgmime/xdgmimeicon.c
Line
Count
Source
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
 *
8
 * Licensed under the Academic Free License version 2.0
9
 * Or under the following terms:
10
 *
11
 * This library is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU Lesser General Public
13
 * License as published by the Free Software Foundation; either
14
 * version 2.1 of the License, or (at your option) any later version.
15
 *
16
 * This library is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 * Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Lesser General Public
22
 * License along with this library; if not, write to the
23
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24
 * Boston, MA 02111-1307, USA.
25
 */
26
27
#ifdef HAVE_CONFIG_H
28
#include <config.h>
29
#endif
30
31
#include "xdgmimeicon.h"
32
#include "xdgmimeint.h"
33
#include <stdlib.h>
34
#include <stdio.h>
35
#include <assert.h>
36
#include <string.h>
37
#include <fnmatch.h>
38
39
#ifndef FALSE
40
#define FALSE (0)
41
#endif
42
43
#ifndef TRUE
44
#define TRUE  (!FALSE)
45
#endif
46
47
typedef struct XdgIcon XdgIcon;
48
49
struct XdgIcon 
50
{
51
  char *mime_type;
52
  char *icon_name;
53
};
54
55
struct XdgIconList
56
{
57
  struct XdgIcon *icons;
58
  int n_icons;
59
};
60
61
XdgIconList *
62
_xdg_mime_icon_list_new (void)
63
0
{
64
0
  XdgIconList *list;
65
66
0
  list = malloc (sizeof (XdgIconList));
67
68
0
  list->icons = NULL;
69
0
  list->n_icons = 0;
70
71
0
  return list;
72
0
}
73
74
void         
75
_xdg_mime_icon_list_free (XdgIconList *list)
76
0
{
77
0
  int i;
78
79
0
  if (list->icons)
80
0
    {
81
0
      for (i = 0; i < list->n_icons; i++)
82
0
  {
83
0
    free (list->icons[i].mime_type);
84
0
    free (list->icons[i].icon_name);
85
0
  }
86
0
      free (list->icons);
87
0
    }
88
0
  free (list);
89
0
}
90
91
static int
92
icon_entry_cmp (const void *v1, const void *v2)
93
0
{
94
0
  return strcmp (((XdgIcon *)v1)->mime_type, ((XdgIcon *)v2)->mime_type);
95
0
}
96
97
const char  *
98
_xdg_mime_icon_list_lookup (XdgIconList *list,
99
          const char  *mime_type)
100
0
{
101
0
  XdgIcon *entry;
102
0
  XdgIcon key;
103
104
0
  if (list->n_icons > 0)
105
0
    {
106
0
      key.mime_type = (char *)mime_type;
107
0
      key.icon_name = NULL;
108
109
0
      entry = bsearch (&key, list->icons, list->n_icons,
110
0
           sizeof (XdgIcon), icon_entry_cmp);
111
0
      if (entry)
112
0
        return entry->icon_name;
113
0
    }
114
115
0
  return NULL;
116
0
}
117
118
void
119
_xdg_mime_icon_read_from_file (XdgIconList *list,
120
             const char   *file_name)
121
0
{
122
0
  FILE *file;
123
0
  char line[255];
124
0
  int alloc;
125
126
0
  file = fopen (file_name, "r");
127
128
0
  if (file == NULL)
129
0
    return;
130
131
  /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
132
   * Blah */
133
0
  alloc = list->n_icons + 16;
134
0
  list->icons = realloc (list->icons, alloc * sizeof (XdgIcon));
135
0
  while (fgets (line, 255, file) != NULL)
136
0
    {
137
0
      char *sep;
138
0
      if (line[0] == '#')
139
0
  continue;
140
141
0
      sep = strchr (line, ':');
142
0
      if (sep == NULL)
143
0
  continue;
144
0
      *(sep++) = '\000';
145
0
      sep[strlen (sep) -1] = '\000';
146
0
      if (list->n_icons == alloc)
147
0
  {
148
0
    alloc <<= 1;
149
0
    list->icons = realloc (list->icons, 
150
0
           alloc * sizeof (XdgIcon));
151
0
  }
152
0
      list->icons[list->n_icons].mime_type = strdup (line);
153
0
      list->icons[list->n_icons].icon_name = strdup (sep);
154
0
      list->n_icons++;
155
0
    }
156
0
  list->icons = realloc (list->icons, 
157
0
         list->n_icons * sizeof (XdgIcon));
158
159
0
  fclose (file);  
160
  
161
0
  if (list->n_icons > 1)
162
0
    qsort (list->icons, list->n_icons, 
163
0
           sizeof (XdgIcon), icon_entry_cmp);
164
0
}
165
166
167
void
168
_xdg_mime_icon_list_dump (XdgIconList *list)
169
0
{
170
0
  int i;
171
172
0
  if (list->icons)
173
0
    {
174
0
      for (i = 0; i < list->n_icons; i++)
175
0
  {
176
0
    printf ("%s %s\n", 
177
0
      list->icons[i].mime_type,
178
0
      list->icons[i].icon_name);
179
0
  }
180
0
    }
181
0
}
182
183