Coverage Report

Created: 2025-06-13 06:55

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