Coverage Report

Created: 2025-07-01 07:09

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