Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/xdgmime/xdgmimealias.c
blob: 2dd70f1d6987f6c3ce1fb8822e2005c67e80b23c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* -*- mode: C; c-file-style: "gnu" -*- */
/* xdgmimealias.c: Private file.  Datastructure for storing the aliases.
 *
 * More info can be found at http://www.freedesktop.org/standards/
 *
 * Copyright (C) 2004  Red Hat, Inc.
 * Copyright (C) 2004  Matthias Clasen <mclasen@redhat.com>
 *
 * Licensed under the Academic Free License version 2.0
 * Or under the following terms:
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "xdgmimealias.h"
#include "xdgmimeint.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <fnmatch.h>

#ifndef	FALSE
#define	FALSE	(0)
#endif

#ifndef	TRUE
#define	TRUE	(!FALSE)
#endif

typedef struct XdgAlias XdgAlias;

struct XdgAlias 
{
  char *alias;
  char *mime_type;
};

struct XdgAliasList
{
  struct XdgAlias *aliases;
  int n_aliases;
};

XdgAliasList *
_xdg_mime_alias_list_new (void)
{
  XdgAliasList *list;

  list = malloc (sizeof (XdgAliasList));

  list->aliases = NULL;
  list->n_aliases = 0;

  return list;
}

void         
_xdg_mime_alias_list_free (XdgAliasList *list)
{
  int i;

  if (list->aliases)
    {
      for (i = 0; i < list->n_aliases; i++)
	{
	  free (list->aliases[i].alias);
	  free (list->aliases[i].mime_type);
	}
      free (list->aliases);
    }
  free (list);
}

static int
alias_entry_cmp (const void *v1, const void *v2)
{
  return strcmp (((XdgAlias *)v1)->alias, ((XdgAlias *)v2)->alias);
}

const char  *
_xdg_mime_alias_list_lookup (XdgAliasList *list,
			     const char   *alias)
{
  XdgAlias *entry;
  XdgAlias key;

  if (list->n_aliases > 0)
    {
      key.alias = (char *)alias;
      key.mime_type = 0;

      entry = bsearch (&key, list->aliases, list->n_aliases,
		       sizeof (XdgAlias), alias_entry_cmp);
      if (entry)
        return entry->mime_type;
    }

  return NULL;
}

void
_xdg_mime_alias_read_from_file (XdgAliasList *list,
				const char   *file_name)
{
  FILE *file;
  char line[255];
  int alloc;

  file = fopen (file_name, "r");

  if (file == NULL)
    return;

  /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
   * Blah */
  alloc = list->n_aliases + 16;
  list->aliases = realloc (list->aliases, alloc * sizeof (XdgAlias));
  while (fgets (line, 255, file) != NULL)
    {
      char *sep;
      if (line[0] == '#')
	continue;

      sep = strchr (line, ' ');
      if (sep == NULL)
	continue;
      *(sep++) = '\000';
      sep[strlen (sep) -1] = '\000';
      if (list->n_aliases == alloc)
	{
	  alloc <<= 1;
	  list->aliases = realloc (list->aliases, 
				   alloc * sizeof (XdgAlias));
	}
      list->aliases[list->n_aliases].alias = strdup (line);
      list->aliases[list->n_aliases].mime_type = strdup (sep);
      list->n_aliases++;
    }
  list->aliases = realloc (list->aliases, 
			   list->n_aliases * sizeof (XdgAlias));

  fclose (file);  
  
  if (list->n_aliases > 1)
    qsort (list->aliases, list->n_aliases, 
           sizeof (XdgAlias), alias_entry_cmp);
}


void
_xdg_mime_alias_list_dump (XdgAliasList *list)
{
  int i;

  if (list->aliases)
    {
      for (i = 0; i < list->n_aliases; i++)
	{
	  printf ("%s %s\n", 
		  list->aliases[i].alias,
		  list->aliases[i].mime_type);
	}
    }
}