Import Upstream version 2.72.4
This commit is contained in:
commit
4ef3ff9793
2003 changed files with 1332420 additions and 0 deletions
1
gio/xdgmime/.gitignore
vendored
Normal file
1
gio/xdgmime/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
test-mime
|
||||
17
gio/xdgmime/meson.build
Normal file
17
gio/xdgmime/meson.build
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
xdgmime_sources = files(
|
||||
'xdgmime.c',
|
||||
'xdgmimealias.c',
|
||||
'xdgmimecache.c',
|
||||
'xdgmimeglob.c',
|
||||
'xdgmimeicon.c',
|
||||
'xdgmimeint.c',
|
||||
'xdgmimemagic.c',
|
||||
'xdgmimeparent.c',
|
||||
)
|
||||
|
||||
xdgmime_lib = static_library('xdgmime',
|
||||
sources : xdgmime_sources,
|
||||
include_directories : [configinc],
|
||||
pic : true,
|
||||
c_args : [ '-DHAVE_CONFIG_H',
|
||||
'-DXDG_PREFIX=_gio_xdg' ] + glib_hidden_visibility_args)
|
||||
1012
gio/xdgmime/xdgmime.c
Normal file
1012
gio/xdgmime/xdgmime.c
Normal file
File diff suppressed because it is too large
Load diff
135
gio/xdgmime/xdgmime.h
Normal file
135
gio/xdgmime/xdgmime.h
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmime.h: XDG Mime Spec mime resolver. Based on version 0.11 of the spec.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
|
||||
*
|
||||
* 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.1 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __XDG_MIME_H__
|
||||
#define __XDG_MIME_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define XDG_ENTRY(func) _XDG_ENTRY2(XDG_PREFIX,func)
|
||||
#define _XDG_ENTRY2(prefix,func) _XDG_ENTRY3(prefix,func)
|
||||
#define _XDG_ENTRY3(prefix,func) prefix##_##func
|
||||
|
||||
#define XDG_RESERVED_ENTRY(func) _XDG_RESERVED_ENTRY2(XDG_PREFIX,func)
|
||||
#define _XDG_RESERVED_ENTRY2(prefix,func) _XDG_RESERVED_ENTRY3(prefix,func)
|
||||
#define _XDG_RESERVED_ENTRY3(prefix,func) _##prefix##_##func
|
||||
#endif
|
||||
|
||||
typedef void (*XdgMimeCallback) (void *user_data);
|
||||
typedef void (*XdgMimeDestroy) (void *user_data);
|
||||
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define xdg_mime_get_mime_type_for_data XDG_ENTRY(get_mime_type_for_data)
|
||||
#define xdg_mime_get_mime_type_for_file XDG_ENTRY(get_mime_type_for_file)
|
||||
#define xdg_mime_get_mime_type_from_file_name XDG_ENTRY(get_mime_type_from_file_name)
|
||||
#define xdg_mime_get_mime_types_from_file_name XDG_ENTRY(get_mime_types_from_file_name)
|
||||
#define xdg_mime_is_valid_mime_type XDG_ENTRY(is_valid_mime_type)
|
||||
#define xdg_mime_mime_type_equal XDG_ENTRY(mime_type_equal)
|
||||
#define xdg_mime_media_type_equal XDG_ENTRY(media_type_equal)
|
||||
#define xdg_mime_mime_type_subclass XDG_ENTRY(mime_type_subclass)
|
||||
#define xdg_mime_get_mime_parents XDG_ENTRY(get_mime_parents)
|
||||
#define xdg_mime_list_mime_parents XDG_ENTRY(list_mime_parents)
|
||||
#define xdg_mime_unalias_mime_type XDG_ENTRY(unalias_mime_type)
|
||||
#define xdg_mime_get_max_buffer_extents XDG_ENTRY(get_max_buffer_extents)
|
||||
#define xdg_mime_shutdown XDG_ENTRY(shutdown)
|
||||
#define xdg_mime_dump XDG_ENTRY(dump)
|
||||
#define xdg_mime_register_reload_callback XDG_ENTRY(register_reload_callback)
|
||||
#define xdg_mime_remove_callback XDG_ENTRY(remove_callback)
|
||||
#define xdg_mime_type_unknown XDG_ENTRY(type_unknown)
|
||||
#define xdg_mime_type_empty XDG_ENTRY(type_empty)
|
||||
#define xdg_mime_type_textplain XDG_ENTRY(type_textplain)
|
||||
#define xdg_mime_get_icon XDG_ENTRY(get_icon)
|
||||
#define xdg_mime_get_generic_icon XDG_ENTRY(get_generic_icon)
|
||||
|
||||
#define _xdg_mime_mime_type_equal XDG_RESERVED_ENTRY(mime_type_equal)
|
||||
#define _xdg_mime_mime_type_subclass XDG_RESERVED_ENTRY(mime_type_subclass)
|
||||
#define _xdg_mime_unalias_mime_type XDG_RESERVED_ENTRY(unalias_mime_type)
|
||||
#endif
|
||||
|
||||
extern const char xdg_mime_type_unknown[];
|
||||
extern const char xdg_mime_type_empty[];
|
||||
extern const char xdg_mime_type_textplain[];
|
||||
#define XDG_MIME_TYPE_UNKNOWN xdg_mime_type_unknown
|
||||
#define XDG_MIME_TYPE_EMPTY xdg_mime_type_empty
|
||||
#define XDG_MIME_TYPE_TEXTPLAIN xdg_mime_type_textplain
|
||||
|
||||
const char *xdg_mime_get_mime_type_for_data (const void *data,
|
||||
size_t len,
|
||||
int *result_prio);
|
||||
const char *xdg_mime_get_mime_type_for_file (const char *file_name,
|
||||
struct stat *statbuf);
|
||||
const char *xdg_mime_get_mime_type_from_file_name (const char *file_name);
|
||||
int xdg_mime_get_mime_types_from_file_name(const char *file_name,
|
||||
const char *mime_types[],
|
||||
int n_mime_types);
|
||||
int xdg_mime_is_valid_mime_type (const char *mime_type);
|
||||
int xdg_mime_mime_type_equal (const char *mime_a,
|
||||
const char *mime_b);
|
||||
int xdg_mime_media_type_equal (const char *mime_a,
|
||||
const char *mime_b);
|
||||
int xdg_mime_mime_type_subclass (const char *mime_a,
|
||||
const char *mime_b);
|
||||
/* xdg_mime_get_mime_parents() is deprecated since it does
|
||||
* not work correctly with caches. Use xdg_mime_list_parents()
|
||||
* instead, but notice that that function expects you to free
|
||||
* the array it returns.
|
||||
*/
|
||||
const char **xdg_mime_get_mime_parents (const char *mime);
|
||||
char ** xdg_mime_list_mime_parents (const char *mime);
|
||||
const char *xdg_mime_unalias_mime_type (const char *mime);
|
||||
const char *xdg_mime_get_icon (const char *mime);
|
||||
const char *xdg_mime_get_generic_icon (const char *mime);
|
||||
int xdg_mime_get_max_buffer_extents (void);
|
||||
void xdg_mime_shutdown (void);
|
||||
void xdg_mime_dump (void);
|
||||
int xdg_mime_register_reload_callback (XdgMimeCallback callback,
|
||||
void *data,
|
||||
XdgMimeDestroy destroy);
|
||||
void xdg_mime_remove_callback (int callback_id);
|
||||
|
||||
void xdg_mime_set_dirs (const char * const *dirs);
|
||||
|
||||
/* Private versions of functions that don't call xdg_mime_init () */
|
||||
int _xdg_mime_mime_type_equal (const char *mime_a,
|
||||
const char *mime_b);
|
||||
int _xdg_mime_mime_type_subclass (const char *mime,
|
||||
const char *base);
|
||||
const char *_xdg_mime_unalias_mime_type (const char *mime);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif /* __XDG_MIME_H__ */
|
||||
184
gio/xdgmime/xdgmimealias.c
Normal file
184
gio/xdgmime/xdgmimealias.c
Normal file
|
|
@ -0,0 +1,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.1 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 = NULL;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
51
gio/xdgmime/xdgmimealias.h
Normal file
51
gio/xdgmime/xdgmimealias.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimealias.h: 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) 200 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.1 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.
|
||||
*/
|
||||
|
||||
#ifndef __XDG_MIME_ALIAS_H__
|
||||
#define __XDG_MIME_ALIAS_H__
|
||||
|
||||
#include "xdgmime.h"
|
||||
|
||||
typedef struct XdgAliasList XdgAliasList;
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define _xdg_mime_alias_read_from_file XDG_RESERVED_ENTRY(alias_read_from_file)
|
||||
#define _xdg_mime_alias_list_new XDG_RESERVED_ENTRY(alias_list_new)
|
||||
#define _xdg_mime_alias_list_free XDG_RESERVED_ENTRY(alias_list_free)
|
||||
#define _xdg_mime_alias_list_lookup XDG_RESERVED_ENTRY(alias_list_lookup)
|
||||
#define _xdg_mime_alias_list_dump XDG_RESERVED_ENTRY(alias_list_dump)
|
||||
#endif
|
||||
|
||||
void _xdg_mime_alias_read_from_file (XdgAliasList *list,
|
||||
const char *file_name);
|
||||
XdgAliasList *_xdg_mime_alias_list_new (void);
|
||||
void _xdg_mime_alias_list_free (XdgAliasList *list);
|
||||
const char *_xdg_mime_alias_list_lookup (XdgAliasList *list,
|
||||
const char *alias);
|
||||
void _xdg_mime_alias_list_dump (XdgAliasList *list);
|
||||
|
||||
#endif /* __XDG_MIME_ALIAS_H__ */
|
||||
1165
gio/xdgmime/xdgmimecache.c
Normal file
1165
gio/xdgmime/xdgmimecache.c
Normal file
File diff suppressed because it is too large
Load diff
81
gio/xdgmime/xdgmimecache.h
Normal file
81
gio/xdgmime/xdgmimecache.h
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimecache.h: Private file. Datastructure for mmapped caches.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2005 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.1 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.
|
||||
*/
|
||||
|
||||
#ifndef __XDG_MIME_CACHE_H__
|
||||
#define __XDG_MIME_CACHE_H__
|
||||
|
||||
#include "xdgmime.h"
|
||||
|
||||
typedef struct _XdgMimeCache XdgMimeCache;
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define _xdg_mime_cache_new_from_file XDG_RESERVED_ENTRY(cache_new_from_file)
|
||||
#define _xdg_mime_cache_ref XDG_RESERVED_ENTRY(cache_ref)
|
||||
#define _xdg_mime_cache_unref XDG_RESERVED_ENTRY(cache_unref)
|
||||
#define _xdg_mime_cache_get_max_buffer_extents XDG_RESERVED_ENTRY(cache_get_max_buffer_extents)
|
||||
#define _xdg_mime_cache_get_mime_type_for_data XDG_RESERVED_ENTRY(cache_get_mime_type_for_data)
|
||||
#define _xdg_mime_cache_get_mime_type_for_file XDG_RESERVED_ENTRY(cache_get_mime_type_for_file)
|
||||
#define _xdg_mime_cache_get_mime_type_from_file_name XDG_RESERVED_ENTRY(cache_get_mime_type_from_file_name)
|
||||
#define _xdg_mime_cache_get_mime_types_from_file_name XDG_RESERVED_ENTRY(cache_get_mime_types_from_file_name)
|
||||
#define _xdg_mime_cache_list_mime_parents XDG_RESERVED_ENTRY(cache_list_mime_parents)
|
||||
#define _xdg_mime_cache_mime_type_subclass XDG_RESERVED_ENTRY(cache_mime_type_subclass)
|
||||
#define _xdg_mime_cache_unalias_mime_type XDG_RESERVED_ENTRY(cache_unalias_mime_type)
|
||||
#define _xdg_mime_cache_get_icon XDG_RESERVED_ENTRY(cache_get_icon)
|
||||
#define _xdg_mime_cache_get_generic_icon XDG_RESERVED_ENTRY(cache_get_generic_icon)
|
||||
#define _xdg_mime_cache_glob_dump XDG_RESERVED_ENTRY(cache_glob_dump)
|
||||
#endif
|
||||
|
||||
extern XdgMimeCache **_caches;
|
||||
|
||||
XdgMimeCache *_xdg_mime_cache_new_from_file (const char *file_name);
|
||||
XdgMimeCache *_xdg_mime_cache_ref (XdgMimeCache *cache);
|
||||
void _xdg_mime_cache_unref (XdgMimeCache *cache);
|
||||
|
||||
|
||||
const char *_xdg_mime_cache_get_mime_type_for_data (const void *data,
|
||||
size_t len,
|
||||
int *result_prio);
|
||||
const char *_xdg_mime_cache_get_mime_type_for_file (const char *file_name,
|
||||
struct stat *statbuf);
|
||||
int _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
|
||||
const char *mime_types[],
|
||||
int n_mime_types);
|
||||
const char *_xdg_mime_cache_get_mime_type_from_file_name (const char *file_name);
|
||||
int _xdg_mime_cache_is_valid_mime_type (const char *mime_type);
|
||||
int _xdg_mime_cache_mime_type_equal (const char *mime_a,
|
||||
const char *mime_b);
|
||||
int _xdg_mime_cache_media_type_equal (const char *mime_a,
|
||||
const char *mime_b);
|
||||
int _xdg_mime_cache_mime_type_subclass (const char *mime_a,
|
||||
const char *mime_b);
|
||||
char **_xdg_mime_cache_list_mime_parents (const char *mime);
|
||||
const char *_xdg_mime_cache_unalias_mime_type (const char *mime);
|
||||
int _xdg_mime_cache_get_max_buffer_extents (void);
|
||||
const char *_xdg_mime_cache_get_icon (const char *mime);
|
||||
const char *_xdg_mime_cache_get_generic_icon (const char *mime);
|
||||
void _xdg_mime_cache_glob_dump (void);
|
||||
|
||||
#endif /* __XDG_MIME_CACHE_H__ */
|
||||
725
gio/xdgmime/xdgmimeglob.c
Normal file
725
gio/xdgmime/xdgmimeglob.c
Normal file
|
|
@ -0,0 +1,725 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimeglob.c: Private file. Datastructure for storing the globs.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
|
||||
*
|
||||
* 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.1 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 "xdgmimeglob.h"
|
||||
#include "xdgmimeint.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <fnmatch.h>
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE (0)
|
||||
#endif
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE (!FALSE)
|
||||
#endif
|
||||
|
||||
typedef struct XdgGlobHashNode XdgGlobHashNode;
|
||||
typedef struct XdgGlobList XdgGlobList;
|
||||
|
||||
struct XdgGlobHashNode
|
||||
{
|
||||
xdg_unichar_t character;
|
||||
const char *mime_type;
|
||||
int weight;
|
||||
int case_sensitive;
|
||||
XdgGlobHashNode *next;
|
||||
XdgGlobHashNode *child;
|
||||
};
|
||||
struct XdgGlobList
|
||||
{
|
||||
const char *data;
|
||||
const char *mime_type;
|
||||
int weight;
|
||||
int case_sensitive;
|
||||
XdgGlobList *next;
|
||||
};
|
||||
|
||||
struct XdgGlobHash
|
||||
{
|
||||
XdgGlobList *literal_list;
|
||||
XdgGlobHashNode *simple_node;
|
||||
XdgGlobList *full_list;
|
||||
};
|
||||
|
||||
|
||||
/* XdgGlobList
|
||||
*/
|
||||
static XdgGlobList *
|
||||
_xdg_glob_list_new (void)
|
||||
{
|
||||
XdgGlobList *new_element;
|
||||
|
||||
new_element = calloc (1, sizeof (XdgGlobList));
|
||||
|
||||
return new_element;
|
||||
}
|
||||
|
||||
/* Frees glob_list and all of its children */
|
||||
static void
|
||||
_xdg_glob_list_free (XdgGlobList *glob_list)
|
||||
{
|
||||
XdgGlobList *ptr, *next;
|
||||
|
||||
ptr = glob_list;
|
||||
|
||||
while (ptr != NULL)
|
||||
{
|
||||
next = ptr->next;
|
||||
|
||||
if (ptr->data)
|
||||
free ((void *) ptr->data);
|
||||
if (ptr->mime_type)
|
||||
free ((void *) ptr->mime_type);
|
||||
free (ptr);
|
||||
|
||||
ptr = next;
|
||||
}
|
||||
}
|
||||
|
||||
static XdgGlobList *
|
||||
_xdg_glob_list_append (XdgGlobList *glob_list,
|
||||
void *data,
|
||||
const char *mime_type,
|
||||
int weight,
|
||||
int case_sensitive)
|
||||
{
|
||||
XdgGlobList *new_element;
|
||||
XdgGlobList *tmp_element;
|
||||
|
||||
tmp_element = glob_list;
|
||||
while (tmp_element != NULL)
|
||||
{
|
||||
if (strcmp (tmp_element->data, data) == 0 &&
|
||||
strcmp (tmp_element->mime_type, mime_type) == 0)
|
||||
return glob_list;
|
||||
|
||||
tmp_element = tmp_element->next;
|
||||
}
|
||||
|
||||
new_element = _xdg_glob_list_new ();
|
||||
new_element->data = data;
|
||||
new_element->mime_type = mime_type;
|
||||
new_element->weight = weight;
|
||||
new_element->case_sensitive = case_sensitive;
|
||||
if (glob_list == NULL)
|
||||
return new_element;
|
||||
|
||||
tmp_element = glob_list;
|
||||
while (tmp_element->next != NULL)
|
||||
tmp_element = tmp_element->next;
|
||||
|
||||
tmp_element->next = new_element;
|
||||
|
||||
return glob_list;
|
||||
}
|
||||
|
||||
/* XdgGlobHashNode
|
||||
*/
|
||||
|
||||
static XdgGlobHashNode *
|
||||
_xdg_glob_hash_node_new (void)
|
||||
{
|
||||
XdgGlobHashNode *glob_hash_node;
|
||||
|
||||
glob_hash_node = calloc (1, sizeof (XdgGlobHashNode));
|
||||
|
||||
return glob_hash_node;
|
||||
}
|
||||
|
||||
static void
|
||||
_xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node,
|
||||
int depth)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < depth; i++)
|
||||
printf (" ");
|
||||
|
||||
printf ("%c", (char)glob_hash_node->character);
|
||||
if (glob_hash_node->mime_type)
|
||||
printf (" - %s %d\n", glob_hash_node->mime_type, glob_hash_node->weight);
|
||||
else
|
||||
printf ("\n");
|
||||
if (glob_hash_node->child)
|
||||
_xdg_glob_hash_node_dump (glob_hash_node->child, depth + 1);
|
||||
if (glob_hash_node->next)
|
||||
_xdg_glob_hash_node_dump (glob_hash_node->next, depth);
|
||||
}
|
||||
|
||||
static XdgGlobHashNode *
|
||||
_xdg_glob_hash_insert_ucs4 (XdgGlobHashNode *glob_hash_node,
|
||||
xdg_unichar_t *text,
|
||||
const char *mime_type,
|
||||
int weight,
|
||||
int case_sensitive)
|
||||
{
|
||||
XdgGlobHashNode *node;
|
||||
xdg_unichar_t character;
|
||||
|
||||
character = text[0];
|
||||
|
||||
if ((glob_hash_node == NULL) ||
|
||||
(character < glob_hash_node->character))
|
||||
{
|
||||
node = _xdg_glob_hash_node_new ();
|
||||
node->character = character;
|
||||
node->next = glob_hash_node;
|
||||
glob_hash_node = node;
|
||||
}
|
||||
else if (character == glob_hash_node->character)
|
||||
{
|
||||
node = glob_hash_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
XdgGlobHashNode *prev_node;
|
||||
int found_node = FALSE;
|
||||
|
||||
/* Look for the first character of text in glob_hash_node, and insert it if we
|
||||
* have to.*/
|
||||
prev_node = glob_hash_node;
|
||||
node = prev_node->next;
|
||||
|
||||
while (node != NULL)
|
||||
{
|
||||
if (character < node->character)
|
||||
{
|
||||
node = _xdg_glob_hash_node_new ();
|
||||
node->character = character;
|
||||
node->next = prev_node->next;
|
||||
prev_node->next = node;
|
||||
|
||||
found_node = TRUE;
|
||||
break;
|
||||
}
|
||||
else if (character == node->character)
|
||||
{
|
||||
found_node = TRUE;
|
||||
break;
|
||||
}
|
||||
prev_node = node;
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
if (! found_node)
|
||||
{
|
||||
node = _xdg_glob_hash_node_new ();
|
||||
node->character = character;
|
||||
node->next = prev_node->next;
|
||||
prev_node->next = node;
|
||||
}
|
||||
}
|
||||
|
||||
text++;
|
||||
if (*text == 0)
|
||||
{
|
||||
if (node->mime_type)
|
||||
{
|
||||
if (strcmp (node->mime_type, mime_type) != 0)
|
||||
{
|
||||
XdgGlobHashNode *child;
|
||||
int found_node = FALSE;
|
||||
|
||||
child = node->child;
|
||||
while (child && child->character == 0)
|
||||
{
|
||||
if (strcmp (child->mime_type, mime_type) == 0)
|
||||
{
|
||||
found_node = TRUE;
|
||||
break;
|
||||
}
|
||||
child = child->next;
|
||||
}
|
||||
|
||||
if (!found_node)
|
||||
{
|
||||
child = _xdg_glob_hash_node_new ();
|
||||
child->character = 0;
|
||||
child->mime_type = strdup (mime_type);
|
||||
child->weight = weight;
|
||||
child->case_sensitive = case_sensitive;
|
||||
child->child = NULL;
|
||||
child->next = node->child;
|
||||
node->child = child;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
node->mime_type = strdup (mime_type);
|
||||
node->weight = weight;
|
||||
node->case_sensitive = case_sensitive;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
node->child = _xdg_glob_hash_insert_ucs4 (node->child, text, mime_type, weight, case_sensitive);
|
||||
}
|
||||
return glob_hash_node;
|
||||
}
|
||||
|
||||
/* glob must be valid UTF-8 */
|
||||
static XdgGlobHashNode *
|
||||
_xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
|
||||
const char *text,
|
||||
const char *mime_type,
|
||||
int weight,
|
||||
int case_sensitive)
|
||||
{
|
||||
XdgGlobHashNode *node;
|
||||
xdg_unichar_t *unitext;
|
||||
int len;
|
||||
|
||||
unitext = _xdg_convert_to_ucs4 (text, &len);
|
||||
_xdg_reverse_ucs4 (unitext, len);
|
||||
node = _xdg_glob_hash_insert_ucs4 (glob_hash_node, unitext, mime_type, weight, case_sensitive);
|
||||
free (unitext);
|
||||
return node;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
const char *mime;
|
||||
int weight;
|
||||
} MimeWeight;
|
||||
|
||||
static int
|
||||
_xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node,
|
||||
const char *file_name,
|
||||
int len,
|
||||
int case_sensitive_check,
|
||||
MimeWeight mime_types[],
|
||||
int n_mime_types)
|
||||
{
|
||||
int n;
|
||||
XdgGlobHashNode *node;
|
||||
xdg_unichar_t character;
|
||||
|
||||
if (glob_hash_node == NULL)
|
||||
return 0;
|
||||
|
||||
character = file_name[len - 1];
|
||||
|
||||
for (node = glob_hash_node; node && character >= node->character; node = node->next)
|
||||
{
|
||||
if (character == node->character)
|
||||
{
|
||||
len--;
|
||||
n = 0;
|
||||
if (len > 0)
|
||||
{
|
||||
n = _xdg_glob_hash_node_lookup_file_name (node->child,
|
||||
file_name,
|
||||
len,
|
||||
case_sensitive_check,
|
||||
mime_types,
|
||||
n_mime_types);
|
||||
}
|
||||
if (n == 0)
|
||||
{
|
||||
if (node->mime_type &&
|
||||
(case_sensitive_check ||
|
||||
!node->case_sensitive))
|
||||
{
|
||||
mime_types[n].mime = node->mime_type;
|
||||
mime_types[n].weight = node->weight;
|
||||
n++;
|
||||
}
|
||||
node = node->child;
|
||||
while (n < n_mime_types && node && node->character == 0)
|
||||
{
|
||||
if (node->mime_type &&
|
||||
(case_sensitive_check ||
|
||||
!node->case_sensitive))
|
||||
{
|
||||
mime_types[n].mime = node->mime_type;
|
||||
mime_types[n].weight = node->weight;
|
||||
n++;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int compare_mime_weight (const void *a, const void *b)
|
||||
{
|
||||
const MimeWeight *aa = (const MimeWeight *)a;
|
||||
const MimeWeight *bb = (const MimeWeight *)b;
|
||||
|
||||
return bb->weight - aa->weight;
|
||||
}
|
||||
|
||||
#define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
|
||||
static char *
|
||||
ascii_tolower (const char *str)
|
||||
{
|
||||
char *p, *lower;
|
||||
|
||||
lower = strdup (str);
|
||||
p = lower;
|
||||
while (*p != 0)
|
||||
{
|
||||
char c = *p;
|
||||
*p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
|
||||
}
|
||||
return lower;
|
||||
}
|
||||
|
||||
static int
|
||||
filter_out_dupes (MimeWeight mimes[], int n_mimes)
|
||||
{
|
||||
int last;
|
||||
int i, j;
|
||||
|
||||
last = n_mimes;
|
||||
|
||||
for (i = 0; i < last; i++)
|
||||
{
|
||||
j = i + 1;
|
||||
while (j < last)
|
||||
{
|
||||
if (strcmp (mimes[i].mime, mimes[j].mime) == 0)
|
||||
{
|
||||
mimes[i].weight = MAX (mimes[i].weight, mimes[j].weight);
|
||||
last--;
|
||||
mimes[j].mime = mimes[last].mime;
|
||||
mimes[j].weight = mimes[last].weight;
|
||||
}
|
||||
else
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
int
|
||||
_xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
|
||||
const char *file_name,
|
||||
const char *mime_types[],
|
||||
int n_mime_types)
|
||||
{
|
||||
XdgGlobList *list;
|
||||
int i, n;
|
||||
MimeWeight mimes[10];
|
||||
int n_mimes = 10;
|
||||
int len;
|
||||
char *lower_case;
|
||||
|
||||
/* First, check the literals */
|
||||
|
||||
assert (file_name != NULL && n_mime_types > 0);
|
||||
|
||||
n = 0;
|
||||
|
||||
lower_case = ascii_tolower (file_name);
|
||||
|
||||
for (list = glob_hash->literal_list; list; list = list->next)
|
||||
{
|
||||
if (strcmp ((const char *)list->data, file_name) == 0)
|
||||
{
|
||||
mime_types[0] = list->mime_type;
|
||||
free (lower_case);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (list = glob_hash->literal_list; list; list = list->next)
|
||||
{
|
||||
if (!list->case_sensitive &&
|
||||
strcmp ((const char *)list->data, lower_case) == 0)
|
||||
{
|
||||
mime_types[0] = list->mime_type;
|
||||
free (lower_case);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
len = strlen (file_name);
|
||||
n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, lower_case, len, FALSE,
|
||||
mimes, n_mimes);
|
||||
if (n < 2)
|
||||
n += _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, file_name, len, TRUE,
|
||||
mimes + n, n_mimes - n);
|
||||
|
||||
if (n < 2)
|
||||
{
|
||||
for (list = glob_hash->full_list; list && n < n_mime_types; list = list->next)
|
||||
{
|
||||
if (fnmatch ((const char *)list->data, file_name, 0) == 0)
|
||||
{
|
||||
mimes[n].mime = list->mime_type;
|
||||
mimes[n].weight = list->weight;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
free (lower_case);
|
||||
|
||||
n = filter_out_dupes (mimes, n);
|
||||
|
||||
qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
|
||||
|
||||
if (n_mime_types < n)
|
||||
n = n_mime_types;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
mime_types[i] = mimes[i].mime;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* XdgGlobHash
|
||||
*/
|
||||
|
||||
XdgGlobHash *
|
||||
_xdg_glob_hash_new (void)
|
||||
{
|
||||
XdgGlobHash *glob_hash;
|
||||
|
||||
glob_hash = calloc (1, sizeof (XdgGlobHash));
|
||||
|
||||
return glob_hash;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
_xdg_glob_hash_free_nodes (XdgGlobHashNode *node)
|
||||
{
|
||||
if (node)
|
||||
{
|
||||
if (node->child)
|
||||
_xdg_glob_hash_free_nodes (node->child);
|
||||
if (node->next)
|
||||
_xdg_glob_hash_free_nodes (node->next);
|
||||
if (node->mime_type)
|
||||
free ((void *) node->mime_type);
|
||||
free (node);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_glob_hash_free (XdgGlobHash *glob_hash)
|
||||
{
|
||||
_xdg_glob_list_free (glob_hash->literal_list);
|
||||
_xdg_glob_list_free (glob_hash->full_list);
|
||||
_xdg_glob_hash_free_nodes (glob_hash->simple_node);
|
||||
free (glob_hash);
|
||||
}
|
||||
|
||||
XdgGlobType
|
||||
_xdg_glob_determine_type (const char *glob)
|
||||
{
|
||||
const char *ptr;
|
||||
int maybe_in_simple_glob = FALSE;
|
||||
int first_char = TRUE;
|
||||
|
||||
ptr = glob;
|
||||
|
||||
while (*ptr != '\0')
|
||||
{
|
||||
if (*ptr == '*' && first_char)
|
||||
maybe_in_simple_glob = TRUE;
|
||||
else if (*ptr == '\\' || *ptr == '[' || *ptr == '?' || *ptr == '*')
|
||||
return XDG_GLOB_FULL;
|
||||
|
||||
first_char = FALSE;
|
||||
ptr = _xdg_utf8_next_char (ptr);
|
||||
}
|
||||
if (maybe_in_simple_glob)
|
||||
return XDG_GLOB_SIMPLE;
|
||||
else
|
||||
return XDG_GLOB_LITERAL;
|
||||
}
|
||||
|
||||
/* glob must be valid UTF-8 */
|
||||
void
|
||||
_xdg_glob_hash_append_glob (XdgGlobHash *glob_hash,
|
||||
const char *glob,
|
||||
const char *mime_type,
|
||||
int weight,
|
||||
int case_sensitive)
|
||||
{
|
||||
XdgGlobType type;
|
||||
|
||||
assert (glob_hash != NULL);
|
||||
assert (glob != NULL);
|
||||
|
||||
type = _xdg_glob_determine_type (glob);
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case XDG_GLOB_LITERAL:
|
||||
glob_hash->literal_list = _xdg_glob_list_append (glob_hash->literal_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
|
||||
break;
|
||||
case XDG_GLOB_SIMPLE:
|
||||
glob_hash->simple_node = _xdg_glob_hash_insert_text (glob_hash->simple_node, glob + 1, mime_type, weight, case_sensitive);
|
||||
break;
|
||||
case XDG_GLOB_FULL:
|
||||
glob_hash->full_list = _xdg_glob_list_append (glob_hash->full_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_glob_hash_dump (XdgGlobHash *glob_hash)
|
||||
{
|
||||
XdgGlobList *list;
|
||||
printf ("LITERAL STRINGS\n");
|
||||
if (!glob_hash || glob_hash->literal_list == NULL)
|
||||
{
|
||||
printf (" None\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
for (list = glob_hash->literal_list; list; list = list->next)
|
||||
printf (" %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
|
||||
}
|
||||
printf ("\nSIMPLE GLOBS\n");
|
||||
if (!glob_hash || glob_hash->simple_node == NULL)
|
||||
{
|
||||
printf (" None\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
_xdg_glob_hash_node_dump (glob_hash->simple_node, 4);
|
||||
}
|
||||
|
||||
printf ("\nFULL GLOBS\n");
|
||||
if (!glob_hash || glob_hash->full_list == NULL)
|
||||
{
|
||||
printf (" None\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
for (list = glob_hash->full_list; list; list = list->next)
|
||||
printf (" %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
_xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,
|
||||
const char *file_name,
|
||||
int version_two)
|
||||
{
|
||||
FILE *glob_file;
|
||||
char line[255];
|
||||
char *p;
|
||||
|
||||
glob_file = fopen (file_name, "r");
|
||||
|
||||
if (glob_file == NULL)
|
||||
return;
|
||||
|
||||
/* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
|
||||
* Blah */
|
||||
while (fgets (line, 255, glob_file) != NULL)
|
||||
{
|
||||
char *colon;
|
||||
char *mimetype, *glob, *end;
|
||||
int weight;
|
||||
int case_sensitive;
|
||||
|
||||
if (line[0] == '#' || line[0] == 0)
|
||||
continue;
|
||||
|
||||
end = line + strlen(line) - 1;
|
||||
if (*end == '\n')
|
||||
*end = 0;
|
||||
|
||||
p = line;
|
||||
if (version_two)
|
||||
{
|
||||
colon = strchr (p, ':');
|
||||
if (colon == NULL)
|
||||
continue;
|
||||
*colon = 0;
|
||||
weight = atoi (p);
|
||||
p = colon + 1;
|
||||
}
|
||||
else
|
||||
weight = 50;
|
||||
|
||||
colon = strchr (p, ':');
|
||||
if (colon == NULL)
|
||||
continue;
|
||||
*colon = 0;
|
||||
|
||||
mimetype = p;
|
||||
p = colon + 1;
|
||||
glob = p;
|
||||
case_sensitive = FALSE;
|
||||
|
||||
colon = strchr (p, ':');
|
||||
if (version_two && colon != NULL)
|
||||
{
|
||||
char *flag;
|
||||
|
||||
/* We got flags */
|
||||
*colon = 0;
|
||||
p = colon + 1;
|
||||
|
||||
/* Flags end at next colon */
|
||||
colon = strchr (p, ':');
|
||||
if (colon != NULL)
|
||||
*colon = 0;
|
||||
|
||||
flag = strstr (p, "cs");
|
||||
if (flag != NULL &&
|
||||
/* Start or after comma */
|
||||
(flag == p ||
|
||||
flag[-1] == ',') &&
|
||||
/* ends with comma or end of string */
|
||||
(flag[2] == 0 ||
|
||||
flag[2] == ','))
|
||||
case_sensitive = TRUE;
|
||||
}
|
||||
|
||||
_xdg_glob_hash_append_glob (glob_hash, glob, mimetype, weight, case_sensitive);
|
||||
}
|
||||
|
||||
fclose (glob_file);
|
||||
}
|
||||
70
gio/xdgmime/xdgmimeglob.h
Normal file
70
gio/xdgmime/xdgmimeglob.h
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimeglob.h: Private file. Datastructure for storing the globs.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
|
||||
*
|
||||
* 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.1 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.
|
||||
*/
|
||||
|
||||
#ifndef __XDG_MIME_GLOB_H__
|
||||
#define __XDG_MIME_GLOB_H__
|
||||
|
||||
#include "xdgmime.h"
|
||||
|
||||
typedef struct XdgGlobHash XdgGlobHash;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
XDG_GLOB_LITERAL, /* Makefile */
|
||||
XDG_GLOB_SIMPLE, /* *.gif */
|
||||
XDG_GLOB_FULL /* x*.[ch] */
|
||||
} XdgGlobType;
|
||||
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define _xdg_mime_glob_read_from_file XDG_RESERVED_ENTRY(glob_read_from_file)
|
||||
#define _xdg_glob_hash_new XDG_RESERVED_ENTRY(hash_new)
|
||||
#define _xdg_glob_hash_free XDG_RESERVED_ENTRY(hash_free)
|
||||
#define _xdg_glob_hash_lookup_file_name XDG_RESERVED_ENTRY(hash_lookup_file_name)
|
||||
#define _xdg_glob_hash_append_glob XDG_RESERVED_ENTRY(hash_append_glob)
|
||||
#define _xdg_glob_determine_type XDG_RESERVED_ENTRY(determine_type)
|
||||
#define _xdg_glob_hash_dump XDG_RESERVED_ENTRY(hash_dump)
|
||||
#endif
|
||||
|
||||
void _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,
|
||||
const char *file_name,
|
||||
int version_two);
|
||||
XdgGlobHash *_xdg_glob_hash_new (void);
|
||||
void _xdg_glob_hash_free (XdgGlobHash *glob_hash);
|
||||
int _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
|
||||
const char *text,
|
||||
const char *mime_types[],
|
||||
int n_mime_types);
|
||||
void _xdg_glob_hash_append_glob (XdgGlobHash *glob_hash,
|
||||
const char *glob,
|
||||
const char *mime_type,
|
||||
int weight,
|
||||
int case_sensitive);
|
||||
XdgGlobType _xdg_glob_determine_type (const char *glob);
|
||||
void _xdg_glob_hash_dump (XdgGlobHash *glob_hash);
|
||||
|
||||
#endif /* __XDG_MIME_GLOB_H__ */
|
||||
183
gio/xdgmime/xdgmimeicon.c
Normal file
183
gio/xdgmime/xdgmimeicon.c
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimeicon.c: Private file. Datastructure for storing the aliases.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2008 Red Hat, Inc.
|
||||
*
|
||||
* 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.1 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 "xdgmimeicon.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 XdgIcon XdgIcon;
|
||||
|
||||
struct XdgIcon
|
||||
{
|
||||
char *mime_type;
|
||||
char *icon_name;
|
||||
};
|
||||
|
||||
struct XdgIconList
|
||||
{
|
||||
struct XdgIcon *icons;
|
||||
int n_icons;
|
||||
};
|
||||
|
||||
XdgIconList *
|
||||
_xdg_mime_icon_list_new (void)
|
||||
{
|
||||
XdgIconList *list;
|
||||
|
||||
list = malloc (sizeof (XdgIconList));
|
||||
|
||||
list->icons = NULL;
|
||||
list->n_icons = 0;
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_mime_icon_list_free (XdgIconList *list)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (list->icons)
|
||||
{
|
||||
for (i = 0; i < list->n_icons; i++)
|
||||
{
|
||||
free (list->icons[i].mime_type);
|
||||
free (list->icons[i].icon_name);
|
||||
}
|
||||
free (list->icons);
|
||||
}
|
||||
free (list);
|
||||
}
|
||||
|
||||
static int
|
||||
icon_entry_cmp (const void *v1, const void *v2)
|
||||
{
|
||||
return strcmp (((XdgIcon *)v1)->mime_type, ((XdgIcon *)v2)->mime_type);
|
||||
}
|
||||
|
||||
const char *
|
||||
_xdg_mime_icon_list_lookup (XdgIconList *list,
|
||||
const char *mime_type)
|
||||
{
|
||||
XdgIcon *entry;
|
||||
XdgIcon key;
|
||||
|
||||
if (list->n_icons > 0)
|
||||
{
|
||||
key.mime_type = (char *)mime_type;
|
||||
key.icon_name = NULL;
|
||||
|
||||
entry = bsearch (&key, list->icons, list->n_icons,
|
||||
sizeof (XdgIcon), icon_entry_cmp);
|
||||
if (entry)
|
||||
return entry->icon_name;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_mime_icon_read_from_file (XdgIconList *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_icons + 16;
|
||||
list->icons = realloc (list->icons, alloc * sizeof (XdgIcon));
|
||||
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_icons == alloc)
|
||||
{
|
||||
alloc <<= 1;
|
||||
list->icons = realloc (list->icons,
|
||||
alloc * sizeof (XdgIcon));
|
||||
}
|
||||
list->icons[list->n_icons].mime_type = strdup (line);
|
||||
list->icons[list->n_icons].icon_name = strdup (sep);
|
||||
list->n_icons++;
|
||||
}
|
||||
list->icons = realloc (list->icons,
|
||||
list->n_icons * sizeof (XdgIcon));
|
||||
|
||||
fclose (file);
|
||||
|
||||
if (list->n_icons > 1)
|
||||
qsort (list->icons, list->n_icons,
|
||||
sizeof (XdgIcon), icon_entry_cmp);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
_xdg_mime_icon_list_dump (XdgIconList *list)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (list->icons)
|
||||
{
|
||||
for (i = 0; i < list->n_icons; i++)
|
||||
{
|
||||
printf ("%s %s\n",
|
||||
list->icons[i].mime_type,
|
||||
list->icons[i].icon_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
50
gio/xdgmime/xdgmimeicon.h
Normal file
50
gio/xdgmime/xdgmimeicon.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimeicon.h: Private file. Datastructure for storing the aliases.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2008 Red Hat, Inc.
|
||||
*
|
||||
* 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.1 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.
|
||||
*/
|
||||
|
||||
#ifndef __XDG_MIME_ICON_H__
|
||||
#define __XDG_MIME_ICON_H__
|
||||
|
||||
#include "xdgmime.h"
|
||||
|
||||
typedef struct XdgIconList XdgIconList;
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define _xdg_mime_icon_read_from_file XDG_ENTRY(icon_read_from_file)
|
||||
#define _xdg_mime_icon_list_new XDG_ENTRY(icon_list_new)
|
||||
#define _xdg_mime_icon_list_free XDG_ENTRY(icon_list_free)
|
||||
#define _xdg_mime_icon_list_lookup XDG_ENTRY(icon_list_lookup)
|
||||
#define _xdg_mime_icon_list_dump XDG_ENTRY(icon_list_dump)
|
||||
#endif
|
||||
|
||||
void _xdg_mime_icon_read_from_file (XdgIconList *list,
|
||||
const char *file_name);
|
||||
XdgIconList *_xdg_mime_icon_list_new (void);
|
||||
void _xdg_mime_icon_list_free (XdgIconList *list);
|
||||
const char *_xdg_mime_icon_list_lookup (XdgIconList *list,
|
||||
const char *mime);
|
||||
void _xdg_mime_icon_list_dump (XdgIconList *list);
|
||||
|
||||
#endif /* __XDG_MIME_ICON_H__ */
|
||||
206
gio/xdgmime/xdgmimeint.c
Normal file
206
gio/xdgmime/xdgmimeint.c
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimeint.c: Internal defines and functions.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
|
||||
*
|
||||
* 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.1 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 "xdgmimeint.h"
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE (0)
|
||||
#endif
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE (!FALSE)
|
||||
#endif
|
||||
|
||||
static const char _xdg_utf8_skip_data[256] = {
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
||||
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
|
||||
};
|
||||
|
||||
const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
|
||||
|
||||
|
||||
|
||||
/* Returns the number of unprocessed characters. */
|
||||
xdg_unichar_t
|
||||
_xdg_utf8_to_ucs4(const char *source)
|
||||
{
|
||||
xdg_unichar_t ucs32;
|
||||
if( ! ( *source & 0x80 ) )
|
||||
{
|
||||
ucs32 = *source;
|
||||
}
|
||||
else
|
||||
{
|
||||
int bytelength = 0;
|
||||
xdg_unichar_t result;
|
||||
if ( ! (*source & 0x40) )
|
||||
{
|
||||
ucs32 = *source;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ! (*source & 0x20) )
|
||||
{
|
||||
result = *source++ & 0x1F;
|
||||
bytelength = 2;
|
||||
}
|
||||
else if ( ! (*source & 0x10) )
|
||||
{
|
||||
result = *source++ & 0x0F;
|
||||
bytelength = 3;
|
||||
}
|
||||
else if ( ! (*source & 0x08) )
|
||||
{
|
||||
result = *source++ & 0x07;
|
||||
bytelength = 4;
|
||||
}
|
||||
else if ( ! (*source & 0x04) )
|
||||
{
|
||||
result = *source++ & 0x03;
|
||||
bytelength = 5;
|
||||
}
|
||||
else if ( ! (*source & 0x02) )
|
||||
{
|
||||
result = *source++ & 0x01;
|
||||
bytelength = 6;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = *source++;
|
||||
bytelength = 1;
|
||||
}
|
||||
|
||||
for ( bytelength --; bytelength > 0; bytelength -- )
|
||||
{
|
||||
result <<= 6;
|
||||
result |= *source++ & 0x3F;
|
||||
}
|
||||
ucs32 = result;
|
||||
}
|
||||
}
|
||||
return ucs32;
|
||||
}
|
||||
|
||||
|
||||
/* hullo. this is great code. don't rewrite it */
|
||||
|
||||
xdg_unichar_t
|
||||
_xdg_ucs4_to_lower (xdg_unichar_t source)
|
||||
{
|
||||
/* FIXME: Do a real to_upper sometime */
|
||||
/* CaseFolding-3.2.0.txt has a table of rules. */
|
||||
if ((source & 0xFF) == source)
|
||||
return (xdg_unichar_t) tolower ((unsigned char) source);
|
||||
return source;
|
||||
}
|
||||
|
||||
int
|
||||
_xdg_utf8_validate (const char *source)
|
||||
{
|
||||
/* FIXME: actually write */
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
const char *
|
||||
_xdg_get_base_name (const char *file_name)
|
||||
{
|
||||
const char *base_name;
|
||||
|
||||
if (file_name == NULL)
|
||||
return NULL;
|
||||
|
||||
base_name = strrchr (file_name, '/');
|
||||
|
||||
if (base_name == NULL)
|
||||
return file_name;
|
||||
else
|
||||
return base_name + 1;
|
||||
}
|
||||
|
||||
xdg_unichar_t *
|
||||
_xdg_convert_to_ucs4 (const char *source, int *len)
|
||||
{
|
||||
xdg_unichar_t *out;
|
||||
int i;
|
||||
const char *p;
|
||||
|
||||
out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));
|
||||
|
||||
p = source;
|
||||
i = 0;
|
||||
while (*p)
|
||||
{
|
||||
out[i++] = _xdg_utf8_to_ucs4 (p);
|
||||
p = _xdg_utf8_next_char (p);
|
||||
}
|
||||
out[i] = 0;
|
||||
*len = i;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
|
||||
{
|
||||
xdg_unichar_t c;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len - i - 1; i++)
|
||||
{
|
||||
c = source[i];
|
||||
source[i] = source[len - i - 1];
|
||||
source[len - i - 1] = c;
|
||||
}
|
||||
}
|
||||
|
||||
const char *
|
||||
_xdg_binary_or_text_fallback(const void *data, size_t len)
|
||||
{
|
||||
unsigned char *chardata;
|
||||
size_t i;
|
||||
|
||||
chardata = (unsigned char *) data;
|
||||
for (i = 0; i < 128 && i < len; ++i)
|
||||
{
|
||||
if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13)
|
||||
return XDG_MIME_TYPE_UNKNOWN; /* binary data */
|
||||
}
|
||||
|
||||
return XDG_MIME_TYPE_TEXTPLAIN;
|
||||
}
|
||||
78
gio/xdgmime/xdgmimeint.h
Normal file
78
gio/xdgmime/xdgmimeint.h
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimeint.h: Internal defines and functions.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
|
||||
*
|
||||
* 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.1 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.
|
||||
*/
|
||||
|
||||
#ifndef __XDG_MIME_INT_H__
|
||||
#define __XDG_MIME_INT_H__
|
||||
|
||||
#include "xdgmime.h"
|
||||
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE (0)
|
||||
#endif
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE (!FALSE)
|
||||
#endif
|
||||
|
||||
/* FIXME: Needs to be configure check */
|
||||
typedef unsigned int xdg_unichar_t;
|
||||
typedef unsigned char xdg_uchar8_t;
|
||||
typedef unsigned short xdg_uint16_t;
|
||||
typedef unsigned int xdg_uint32_t;
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define _xdg_utf8_skip XDG_RESERVED_ENTRY(utf8_skip)
|
||||
#define _xdg_utf8_to_ucs4 XDG_RESERVED_ENTRY(utf8_to_ucs4)
|
||||
#define _xdg_ucs4_to_lower XDG_RESERVED_ENTRY(ucs4_to_lower)
|
||||
#define _xdg_utf8_validate XDG_RESERVED_ENTRY(utf8_validate)
|
||||
#define _xdg_get_base_name XDG_RESERVED_ENTRY(get_base_name)
|
||||
#define _xdg_convert_to_ucs4 XDG_RESERVED_ENTRY(convert_to_ucs4)
|
||||
#define _xdg_reverse_ucs4 XDG_RESERVED_ENTRY(reverse_ucs4)
|
||||
#endif
|
||||
|
||||
#define SWAP_BE16_TO_LE16(val) (xdg_uint16_t)(((xdg_uint16_t)(val) << 8)|((xdg_uint16_t)(val) >> 8))
|
||||
|
||||
#define SWAP_BE32_TO_LE32(val) (xdg_uint32_t)((((xdg_uint32_t)(val) & 0xFF000000U) >> 24) | \
|
||||
(((xdg_uint32_t)(val) & 0x00FF0000U) >> 8) | \
|
||||
(((xdg_uint32_t)(val) & 0x0000FF00U) << 8) | \
|
||||
(((xdg_uint32_t)(val) & 0x000000FFU) << 24))
|
||||
/* UTF-8 utils
|
||||
*/
|
||||
extern const char *const _xdg_utf8_skip;
|
||||
#define _xdg_utf8_next_char(p) (char *)((p) + _xdg_utf8_skip[*(unsigned char *)(p)])
|
||||
#define _xdg_utf8_char_size(p) (int) (_xdg_utf8_skip[*(unsigned char *)(p)])
|
||||
|
||||
xdg_unichar_t _xdg_utf8_to_ucs4 (const char *source);
|
||||
xdg_unichar_t _xdg_ucs4_to_lower (xdg_unichar_t source);
|
||||
int _xdg_utf8_validate (const char *source);
|
||||
xdg_unichar_t *_xdg_convert_to_ucs4 (const char *source, int *len);
|
||||
void _xdg_reverse_ucs4 (xdg_unichar_t *source, int len);
|
||||
const char *_xdg_get_base_name (const char *file_name);
|
||||
const char *_xdg_binary_or_text_fallback(const void *data, size_t len);
|
||||
|
||||
#endif /* __XDG_MIME_INT_H__ */
|
||||
830
gio/xdgmime/xdgmimemagic.c
Normal file
830
gio/xdgmime/xdgmimemagic.c
Normal file
|
|
@ -0,0 +1,830 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimemagic.: Private file. Datastructure for storing magic files.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
|
||||
*
|
||||
* 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.1 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 <assert.h>
|
||||
#include "xdgmimemagic.h"
|
||||
#include "xdgmimeint.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE (0)
|
||||
#endif
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE (!FALSE)
|
||||
#endif
|
||||
|
||||
#if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
|
||||
# define getc_unlocked(fp) getc (fp)
|
||||
#endif
|
||||
|
||||
typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
|
||||
typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
XDG_MIME_MAGIC_SECTION,
|
||||
XDG_MIME_MAGIC_MAGIC,
|
||||
XDG_MIME_MAGIC_ERROR,
|
||||
XDG_MIME_MAGIC_EOF
|
||||
} XdgMimeMagicState;
|
||||
|
||||
struct XdgMimeMagicMatch
|
||||
{
|
||||
const char *mime_type;
|
||||
int priority;
|
||||
XdgMimeMagicMatchlet *matchlet;
|
||||
XdgMimeMagicMatch *next;
|
||||
};
|
||||
|
||||
|
||||
struct XdgMimeMagicMatchlet
|
||||
{
|
||||
int indent;
|
||||
int offset;
|
||||
unsigned int value_length;
|
||||
unsigned char *value;
|
||||
unsigned char *mask;
|
||||
unsigned int range_length;
|
||||
unsigned int word_size;
|
||||
XdgMimeMagicMatchlet *next;
|
||||
};
|
||||
|
||||
|
||||
struct XdgMimeMagic
|
||||
{
|
||||
XdgMimeMagicMatch *match_list;
|
||||
int max_extent;
|
||||
};
|
||||
|
||||
static XdgMimeMagicMatch *
|
||||
_xdg_mime_magic_match_new (void)
|
||||
{
|
||||
return calloc (1, sizeof (XdgMimeMagicMatch));
|
||||
}
|
||||
|
||||
|
||||
static XdgMimeMagicMatchlet *
|
||||
_xdg_mime_magic_matchlet_new (void)
|
||||
{
|
||||
XdgMimeMagicMatchlet *matchlet;
|
||||
|
||||
matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
|
||||
if (matchlet == NULL)
|
||||
return NULL;
|
||||
|
||||
matchlet->indent = 0;
|
||||
matchlet->offset = 0;
|
||||
matchlet->value_length = 0;
|
||||
matchlet->value = NULL;
|
||||
matchlet->mask = NULL;
|
||||
matchlet->range_length = 1;
|
||||
matchlet->word_size = 1;
|
||||
matchlet->next = NULL;
|
||||
|
||||
return matchlet;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
_xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
|
||||
{
|
||||
if (mime_magic_matchlet)
|
||||
{
|
||||
if (mime_magic_matchlet->next)
|
||||
_xdg_mime_magic_matchlet_free (mime_magic_matchlet->next);
|
||||
if (mime_magic_matchlet->value)
|
||||
free (mime_magic_matchlet->value);
|
||||
if (mime_magic_matchlet->mask)
|
||||
free (mime_magic_matchlet->mask);
|
||||
free (mime_magic_matchlet);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Frees mime_magic_match and the remainder of its list
|
||||
*/
|
||||
static void
|
||||
_xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
|
||||
{
|
||||
XdgMimeMagicMatch *ptr, *next;
|
||||
|
||||
ptr = mime_magic_match;
|
||||
while (ptr)
|
||||
{
|
||||
next = ptr->next;
|
||||
|
||||
if (ptr->mime_type)
|
||||
free ((void *) ptr->mime_type);
|
||||
if (ptr->matchlet)
|
||||
_xdg_mime_magic_matchlet_free (ptr->matchlet);
|
||||
free (ptr);
|
||||
|
||||
ptr = next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Reads in a hunk of data until a newline character or a '\000' is hit. The
|
||||
* returned string is null terminated, and doesn't include the newline.
|
||||
*/
|
||||
static unsigned char *
|
||||
_xdg_mime_magic_read_to_newline (FILE *magic_file,
|
||||
int *end_of_file)
|
||||
{
|
||||
unsigned char *retval;
|
||||
int c;
|
||||
int len, pos;
|
||||
|
||||
len = 128;
|
||||
pos = 0;
|
||||
retval = malloc (len);
|
||||
*end_of_file = FALSE;
|
||||
|
||||
while (TRUE)
|
||||
{
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
{
|
||||
*end_of_file = TRUE;
|
||||
break;
|
||||
}
|
||||
if (c == '\n' || c == '\000')
|
||||
break;
|
||||
retval[pos++] = (unsigned char) c;
|
||||
if (pos % 128 == 127)
|
||||
{
|
||||
len = len + 128;
|
||||
retval = realloc (retval, len);
|
||||
}
|
||||
}
|
||||
|
||||
retval[pos] = '\000';
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* Returns the number read from the file, or -1 if no number could be read.
|
||||
*/
|
||||
static int
|
||||
_xdg_mime_magic_read_a_number (FILE *magic_file,
|
||||
int *end_of_file)
|
||||
{
|
||||
/* LONG_MAX is about 20 characters on my system */
|
||||
#define MAX_NUMBER_SIZE 30
|
||||
char number_string[MAX_NUMBER_SIZE + 1];
|
||||
int pos = 0;
|
||||
int c;
|
||||
long retval = -1;
|
||||
|
||||
while (TRUE)
|
||||
{
|
||||
c = getc_unlocked (magic_file);
|
||||
|
||||
if (c == EOF)
|
||||
{
|
||||
*end_of_file = TRUE;
|
||||
break;
|
||||
}
|
||||
if (! isdigit (c))
|
||||
{
|
||||
ungetc (c, magic_file);
|
||||
break;
|
||||
}
|
||||
number_string[pos] = (char) c;
|
||||
pos++;
|
||||
if (pos == MAX_NUMBER_SIZE)
|
||||
break;
|
||||
}
|
||||
if (pos > 0)
|
||||
{
|
||||
number_string[pos] = '\000';
|
||||
errno = 0;
|
||||
retval = strtol (number_string, NULL, 10);
|
||||
|
||||
if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
|
||||
return -1;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* Headers are of the format:
|
||||
* [<priority>:<mime-type>]
|
||||
*/
|
||||
static XdgMimeMagicState
|
||||
_xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
|
||||
{
|
||||
int c;
|
||||
char *buffer;
|
||||
char *end_ptr;
|
||||
int end_of_file = 0;
|
||||
|
||||
assert (magic_file != NULL);
|
||||
assert (match != NULL);
|
||||
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
if (c != '[')
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
|
||||
match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
|
||||
if (end_of_file)
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
if (match->priority == -1)
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
if (c != ':')
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
|
||||
buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
|
||||
if (end_of_file)
|
||||
{
|
||||
free (buffer);
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
}
|
||||
|
||||
end_ptr = buffer;
|
||||
while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
|
||||
end_ptr++;
|
||||
if (*end_ptr != ']')
|
||||
{
|
||||
free (buffer);
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
*end_ptr = '\000';
|
||||
|
||||
match->mime_type = strdup (buffer);
|
||||
free (buffer);
|
||||
|
||||
return XDG_MIME_MAGIC_MAGIC;
|
||||
}
|
||||
|
||||
static XdgMimeMagicState
|
||||
_xdg_mime_magic_parse_error (FILE *magic_file)
|
||||
{
|
||||
int c;
|
||||
|
||||
while (1)
|
||||
{
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
if (c == '\n')
|
||||
return XDG_MIME_MAGIC_SECTION;
|
||||
}
|
||||
}
|
||||
|
||||
/* Headers are of the format:
|
||||
* [ indent ] ">" start-offset "=" value
|
||||
* [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
|
||||
*/
|
||||
static XdgMimeMagicState
|
||||
_xdg_mime_magic_parse_magic_line (FILE *magic_file,
|
||||
XdgMimeMagicMatch *match)
|
||||
{
|
||||
XdgMimeMagicMatchlet *matchlet;
|
||||
int c;
|
||||
int end_of_file;
|
||||
int indent = 0;
|
||||
size_t bytes_read;
|
||||
|
||||
assert (magic_file != NULL);
|
||||
|
||||
/* Sniff the buffer to make sure it's a valid line */
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
else if (c == '[')
|
||||
{
|
||||
ungetc (c, magic_file);
|
||||
return XDG_MIME_MAGIC_SECTION;
|
||||
}
|
||||
else if (c == '\n')
|
||||
return XDG_MIME_MAGIC_MAGIC;
|
||||
|
||||
/* At this point, it must be a digit or a '>' */
|
||||
end_of_file = FALSE;
|
||||
if (isdigit (c))
|
||||
{
|
||||
ungetc (c, magic_file);
|
||||
indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
|
||||
if (end_of_file)
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
if (indent == -1)
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
}
|
||||
|
||||
if (c != '>')
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
|
||||
matchlet = _xdg_mime_magic_matchlet_new ();
|
||||
|
||||
/* OOM */
|
||||
if (matchlet == NULL)
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
|
||||
matchlet->indent = indent;
|
||||
matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
|
||||
if (end_of_file)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
}
|
||||
if (matchlet->offset == -1)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
}
|
||||
else if (c != '=')
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
|
||||
/* Next two bytes determine how long the value is */
|
||||
matchlet->value_length = 0;
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
}
|
||||
matchlet->value_length = c & 0xFF;
|
||||
matchlet->value_length = matchlet->value_length << 8;
|
||||
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == EOF)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
}
|
||||
matchlet->value_length = matchlet->value_length + (c & 0xFF);
|
||||
|
||||
matchlet->value = malloc (matchlet->value_length);
|
||||
|
||||
/* OOM */
|
||||
if (matchlet->value == NULL)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
|
||||
if (bytes_read != (size_t) matchlet->value_length)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
if (feof (magic_file))
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
else
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
|
||||
c = getc_unlocked (magic_file);
|
||||
if (c == '&')
|
||||
{
|
||||
matchlet->mask = malloc (matchlet->value_length);
|
||||
/* OOM */
|
||||
if (matchlet->mask == NULL)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
|
||||
if (bytes_read != (size_t) matchlet->value_length)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
if (feof (magic_file))
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
else
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
c = getc_unlocked (magic_file);
|
||||
}
|
||||
|
||||
if (c == '~')
|
||||
{
|
||||
matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
|
||||
if (end_of_file)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
}
|
||||
if (matchlet->word_size != 0 &&
|
||||
matchlet->word_size != 1 &&
|
||||
matchlet->word_size != 2 &&
|
||||
matchlet->word_size != 4)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
c = getc_unlocked (magic_file);
|
||||
}
|
||||
|
||||
if (c == '+')
|
||||
{
|
||||
matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
|
||||
if (end_of_file)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
}
|
||||
if (matchlet->range_length == (unsigned int) -1)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
c = getc_unlocked (magic_file);
|
||||
}
|
||||
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
/* We clean up the matchlet, byte swapping if needed */
|
||||
if (matchlet->word_size > 1)
|
||||
{
|
||||
#if LITTLE_ENDIAN
|
||||
unsigned int i;
|
||||
#endif
|
||||
if (matchlet->value_length % matchlet->word_size != 0)
|
||||
{
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
/* FIXME: need to get this defined in a <config.h> style file */
|
||||
#if LITTLE_ENDIAN
|
||||
for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
|
||||
{
|
||||
if (matchlet->word_size == 2)
|
||||
*((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
|
||||
else if (matchlet->word_size == 4)
|
||||
*((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
|
||||
if (matchlet->mask)
|
||||
{
|
||||
if (matchlet->word_size == 2)
|
||||
*((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
|
||||
else if (matchlet->word_size == 4)
|
||||
*((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
matchlet->next = match->matchlet;
|
||||
match->matchlet = matchlet;
|
||||
|
||||
|
||||
return XDG_MIME_MAGIC_MAGIC;
|
||||
}
|
||||
|
||||
_xdg_mime_magic_matchlet_free (matchlet);
|
||||
if (c == EOF)
|
||||
return XDG_MIME_MAGIC_EOF;
|
||||
|
||||
return XDG_MIME_MAGIC_ERROR;
|
||||
}
|
||||
|
||||
static int
|
||||
_xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
|
||||
const void *data,
|
||||
size_t len)
|
||||
{
|
||||
unsigned int i, j;
|
||||
for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
|
||||
{
|
||||
int valid_matchlet = TRUE;
|
||||
|
||||
if (i + matchlet->value_length > len)
|
||||
return FALSE;
|
||||
|
||||
if (matchlet->mask)
|
||||
{
|
||||
for (j = 0; j < matchlet->value_length; j++)
|
||||
{
|
||||
if ((matchlet->value[j] & matchlet->mask[j]) !=
|
||||
((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
|
||||
{
|
||||
valid_matchlet = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (j = 0; j < matchlet->value_length; j++)
|
||||
{
|
||||
if (matchlet->value[j] != ((unsigned char *) data)[j + i])
|
||||
{
|
||||
valid_matchlet = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (valid_matchlet)
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int
|
||||
_xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
|
||||
const void *data,
|
||||
size_t len,
|
||||
int indent)
|
||||
{
|
||||
while ((matchlet != NULL) && (matchlet->indent == indent))
|
||||
{
|
||||
if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
|
||||
{
|
||||
if ((matchlet->next == NULL) ||
|
||||
(matchlet->next->indent <= indent))
|
||||
return TRUE;
|
||||
|
||||
if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
|
||||
data,
|
||||
len,
|
||||
indent + 1))
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
matchlet = matchlet->next;
|
||||
}
|
||||
while (matchlet && matchlet->indent > indent);
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int
|
||||
_xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
|
||||
const void *data,
|
||||
size_t len)
|
||||
{
|
||||
return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
_xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
|
||||
XdgMimeMagicMatch *match)
|
||||
{
|
||||
XdgMimeMagicMatch *list;
|
||||
|
||||
if (mime_magic->match_list == NULL)
|
||||
{
|
||||
mime_magic->match_list = match;
|
||||
return;
|
||||
}
|
||||
|
||||
if (match->priority > mime_magic->match_list->priority)
|
||||
{
|
||||
match->next = mime_magic->match_list;
|
||||
mime_magic->match_list = match;
|
||||
return;
|
||||
}
|
||||
|
||||
list = mime_magic->match_list;
|
||||
while (list->next != NULL)
|
||||
{
|
||||
if (list->next->priority < match->priority)
|
||||
{
|
||||
match->next = list->next;
|
||||
list->next = match;
|
||||
return;
|
||||
}
|
||||
list = list->next;
|
||||
}
|
||||
list->next = match;
|
||||
match->next = NULL;
|
||||
}
|
||||
|
||||
XdgMimeMagic *
|
||||
_xdg_mime_magic_new (void)
|
||||
{
|
||||
return calloc (1, sizeof (XdgMimeMagic));
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_mime_magic_free (XdgMimeMagic *mime_magic)
|
||||
{
|
||||
if (mime_magic) {
|
||||
_xdg_mime_magic_match_free (mime_magic->match_list);
|
||||
free (mime_magic);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
_xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
|
||||
{
|
||||
return mime_magic->max_extent;
|
||||
}
|
||||
|
||||
const char *
|
||||
_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
|
||||
const void *data,
|
||||
size_t len,
|
||||
int *result_prio,
|
||||
const char *mime_types[],
|
||||
int n_mime_types)
|
||||
{
|
||||
XdgMimeMagicMatch *match;
|
||||
const char *mime_type;
|
||||
int n;
|
||||
int prio;
|
||||
|
||||
prio = 0;
|
||||
mime_type = NULL;
|
||||
for (match = mime_magic->match_list; match; match = match->next)
|
||||
{
|
||||
if (_xdg_mime_magic_match_compare_to_data (match, data, len))
|
||||
{
|
||||
prio = match->priority;
|
||||
mime_type = match->mime_type;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (n = 0; n < n_mime_types; n++)
|
||||
{
|
||||
if (mime_types[n] &&
|
||||
_xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
|
||||
mime_types[n] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mime_type == NULL)
|
||||
{
|
||||
for (n = 0; n < n_mime_types; n++)
|
||||
{
|
||||
if (mime_types[n])
|
||||
mime_type = mime_types[n];
|
||||
}
|
||||
}
|
||||
|
||||
if (result_prio)
|
||||
*result_prio = prio;
|
||||
|
||||
return mime_type;
|
||||
}
|
||||
|
||||
static void
|
||||
_xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
|
||||
{
|
||||
XdgMimeMagicMatch *match;
|
||||
int max_extent = 0;
|
||||
|
||||
for (match = mime_magic->match_list; match; match = match->next)
|
||||
{
|
||||
XdgMimeMagicMatchlet *matchlet;
|
||||
|
||||
for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
|
||||
{
|
||||
int extent;
|
||||
|
||||
extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
|
||||
if (max_extent < extent)
|
||||
max_extent = extent;
|
||||
}
|
||||
}
|
||||
|
||||
mime_magic->max_extent = max_extent;
|
||||
}
|
||||
|
||||
static XdgMimeMagicMatchlet *
|
||||
_xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
|
||||
{
|
||||
XdgMimeMagicMatchlet *new_list;
|
||||
XdgMimeMagicMatchlet *tmp;
|
||||
|
||||
if ((matchlets == NULL) || (matchlets->next == NULL))
|
||||
return matchlets;
|
||||
|
||||
new_list = NULL;
|
||||
tmp = matchlets;
|
||||
while (tmp != NULL)
|
||||
{
|
||||
XdgMimeMagicMatchlet *matchlet;
|
||||
|
||||
matchlet = tmp;
|
||||
tmp = tmp->next;
|
||||
matchlet->next = new_list;
|
||||
new_list = matchlet;
|
||||
}
|
||||
|
||||
return new_list;
|
||||
|
||||
}
|
||||
|
||||
static void
|
||||
_xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
|
||||
FILE *magic_file)
|
||||
{
|
||||
XdgMimeMagicState state;
|
||||
XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
|
||||
|
||||
state = XDG_MIME_MAGIC_SECTION;
|
||||
|
||||
while (state != XDG_MIME_MAGIC_EOF)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case XDG_MIME_MAGIC_SECTION:
|
||||
match = _xdg_mime_magic_match_new ();
|
||||
|
||||
/* OOM */
|
||||
if (match == NULL)
|
||||
return;
|
||||
|
||||
state = _xdg_mime_magic_parse_header (magic_file, match);
|
||||
if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
|
||||
_xdg_mime_magic_match_free (match);
|
||||
break;
|
||||
case XDG_MIME_MAGIC_MAGIC:
|
||||
state = _xdg_mime_magic_parse_magic_line (magic_file, match);
|
||||
if (state == XDG_MIME_MAGIC_SECTION ||
|
||||
(state == XDG_MIME_MAGIC_EOF && match->mime_type))
|
||||
{
|
||||
match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
|
||||
_xdg_mime_magic_insert_match (mime_magic, match);
|
||||
}
|
||||
else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
|
||||
_xdg_mime_magic_match_free (match);
|
||||
break;
|
||||
case XDG_MIME_MAGIC_ERROR:
|
||||
state = _xdg_mime_magic_parse_error (magic_file);
|
||||
break;
|
||||
case XDG_MIME_MAGIC_EOF:
|
||||
default:
|
||||
/* Make the compiler happy */
|
||||
assert (0);
|
||||
}
|
||||
}
|
||||
_xdg_mime_update_mime_magic_extents (mime_magic);
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
|
||||
const char *file_name)
|
||||
{
|
||||
FILE *magic_file;
|
||||
char header[12];
|
||||
|
||||
magic_file = fopen (file_name, "r");
|
||||
|
||||
if (magic_file == NULL)
|
||||
return;
|
||||
|
||||
if (fread (header, 1, 12, magic_file) == 12)
|
||||
{
|
||||
if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
|
||||
_xdg_mime_magic_read_magic_file (mime_magic, magic_file);
|
||||
}
|
||||
|
||||
fclose (magic_file);
|
||||
}
|
||||
57
gio/xdgmime/xdgmimemagic.h
Normal file
57
gio/xdgmime/xdgmimemagic.h
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimemagic.h: Private file. Datastructure for storing the magic files.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
|
||||
*
|
||||
* 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.1 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.
|
||||
*/
|
||||
|
||||
#ifndef __XDG_MIME_MAGIC_H__
|
||||
#define __XDG_MIME_MAGIC_H__
|
||||
|
||||
#include <unistd.h>
|
||||
#include "xdgmime.h"
|
||||
typedef struct XdgMimeMagic XdgMimeMagic;
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define _xdg_mime_glob_read_from_file XDG_RESERVED_ENTRY(glob_read_from_file)
|
||||
#define _xdg_mime_magic_new XDG_RESERVED_ENTRY(magic_new)
|
||||
#define _xdg_mime_magic_read_from_file XDG_RESERVED_ENTRY(magic_read_from_file)
|
||||
#define _xdg_mime_magic_free XDG_RESERVED_ENTRY(magic_free)
|
||||
#define _xdg_mime_magic_get_buffer_extents XDG_RESERVED_ENTRY(magic_get_buffer_extents)
|
||||
#define _xdg_mime_magic_lookup_data XDG_RESERVED_ENTRY(magic_lookup_data)
|
||||
#endif
|
||||
|
||||
|
||||
XdgMimeMagic *_xdg_mime_magic_new (void);
|
||||
void _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
|
||||
const char *file_name);
|
||||
void _xdg_mime_magic_free (XdgMimeMagic *mime_magic);
|
||||
int _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic);
|
||||
const char *_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
|
||||
const void *data,
|
||||
size_t len,
|
||||
int *result_prio,
|
||||
const char *mime_types[],
|
||||
int n_mime_types);
|
||||
|
||||
#endif /* __XDG_MIME_MAGIC_H__ */
|
||||
220
gio/xdgmime/xdgmimeparent.c
Normal file
220
gio/xdgmime/xdgmimeparent.c
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimealias.c: Private file. Datastructure for storing the hierarchy.
|
||||
*
|
||||
* 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.1 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 "xdgmimeparent.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 XdgMimeParents XdgMimeParents;
|
||||
|
||||
struct XdgMimeParents
|
||||
{
|
||||
char *mime;
|
||||
char **parents;
|
||||
int n_parents;
|
||||
};
|
||||
|
||||
struct XdgParentList
|
||||
{
|
||||
struct XdgMimeParents *parents;
|
||||
int n_mimes;
|
||||
};
|
||||
|
||||
XdgParentList *
|
||||
_xdg_mime_parent_list_new (void)
|
||||
{
|
||||
XdgParentList *list;
|
||||
|
||||
list = malloc (sizeof (XdgParentList));
|
||||
|
||||
list->parents = NULL;
|
||||
list->n_mimes = 0;
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_mime_parent_list_free (XdgParentList *list)
|
||||
{
|
||||
int i;
|
||||
char **p;
|
||||
|
||||
if (list->parents)
|
||||
{
|
||||
for (i = 0; i < list->n_mimes; i++)
|
||||
{
|
||||
for (p = list->parents[i].parents; *p; p++)
|
||||
free (*p);
|
||||
|
||||
free (list->parents[i].parents);
|
||||
free (list->parents[i].mime);
|
||||
}
|
||||
free (list->parents);
|
||||
}
|
||||
free (list);
|
||||
}
|
||||
|
||||
static int
|
||||
parent_entry_cmp (const void *v1, const void *v2)
|
||||
{
|
||||
return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime);
|
||||
}
|
||||
|
||||
const char **
|
||||
_xdg_mime_parent_list_lookup (XdgParentList *list,
|
||||
const char *mime)
|
||||
{
|
||||
XdgMimeParents *entry;
|
||||
XdgMimeParents key;
|
||||
|
||||
if (list->n_mimes > 0)
|
||||
{
|
||||
key.mime = (char *)mime;
|
||||
key.parents = NULL;
|
||||
key.n_parents = 0;
|
||||
|
||||
entry = bsearch (&key, list->parents, list->n_mimes,
|
||||
sizeof (XdgMimeParents), &parent_entry_cmp);
|
||||
if (entry)
|
||||
return (const char **)entry->parents;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
_xdg_mime_parent_read_from_file (XdgParentList *list,
|
||||
const char *file_name)
|
||||
{
|
||||
FILE *file;
|
||||
char line[255];
|
||||
int i, alloc;
|
||||
XdgMimeParents *entry;
|
||||
|
||||
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_mimes + 16;
|
||||
list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents));
|
||||
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';
|
||||
entry = NULL;
|
||||
for (i = 0; i < list->n_mimes; i++)
|
||||
{
|
||||
if (strcmp (list->parents[i].mime, line) == 0)
|
||||
{
|
||||
entry = &(list->parents[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!entry)
|
||||
{
|
||||
if (list->n_mimes == alloc)
|
||||
{
|
||||
alloc <<= 1;
|
||||
list->parents = realloc (list->parents,
|
||||
alloc * sizeof (XdgMimeParents));
|
||||
}
|
||||
list->parents[list->n_mimes].mime = strdup (line);
|
||||
list->parents[list->n_mimes].parents = NULL;
|
||||
entry = &(list->parents[list->n_mimes]);
|
||||
list->n_mimes++;
|
||||
}
|
||||
|
||||
if (!entry->parents)
|
||||
{
|
||||
entry->n_parents = 1;
|
||||
entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *));
|
||||
}
|
||||
else
|
||||
{
|
||||
entry->n_parents += 1;
|
||||
entry->parents = realloc (entry->parents,
|
||||
(entry->n_parents + 2) * sizeof (char *));
|
||||
}
|
||||
entry->parents[entry->n_parents - 1] = strdup (sep);
|
||||
entry->parents[entry->n_parents] = NULL;
|
||||
}
|
||||
|
||||
list->parents = realloc (list->parents,
|
||||
list->n_mimes * sizeof (XdgMimeParents));
|
||||
|
||||
fclose (file);
|
||||
|
||||
if (list->n_mimes > 1)
|
||||
qsort (list->parents, list->n_mimes,
|
||||
sizeof (XdgMimeParents), &parent_entry_cmp);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
_xdg_mime_parent_list_dump (XdgParentList *list)
|
||||
{
|
||||
int i;
|
||||
char **p;
|
||||
|
||||
if (list->parents)
|
||||
{
|
||||
for (i = 0; i < list->n_mimes; i++)
|
||||
{
|
||||
for (p = list->parents[i].parents; *p; p++)
|
||||
printf ("%s %s\n", list->parents[i].mime, *p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
51
gio/xdgmime/xdgmimeparent.h
Normal file
51
gio/xdgmime/xdgmimeparent.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* xdgmimeparent.h: Private file. Datastructure for storing the hierarchy.
|
||||
*
|
||||
* More info can be found at http://www.freedesktop.org/standards/
|
||||
*
|
||||
* Copyright (C) 2004 Red Hat, Inc.
|
||||
* Copyright (C) 200 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.1 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.
|
||||
*/
|
||||
|
||||
#ifndef __XDG_MIME_PARENT_H__
|
||||
#define __XDG_MIME_PARENT_H__
|
||||
|
||||
#include "xdgmime.h"
|
||||
|
||||
typedef struct XdgParentList XdgParentList;
|
||||
|
||||
#ifdef XDG_PREFIX
|
||||
#define _xdg_mime_parent_read_from_file XDG_RESERVED_ENTRY(parent_read_from_file)
|
||||
#define _xdg_mime_parent_list_new XDG_RESERVED_ENTRY(parent_list_new)
|
||||
#define _xdg_mime_parent_list_free XDG_RESERVED_ENTRY(parent_list_free)
|
||||
#define _xdg_mime_parent_list_lookup XDG_RESERVED_ENTRY(parent_list_lookup)
|
||||
#define _xdg_mime_parent_list_dump XDG_RESERVED_ENTRY(parent_list_dump)
|
||||
#endif
|
||||
|
||||
void _xdg_mime_parent_read_from_file (XdgParentList *list,
|
||||
const char *file_name);
|
||||
XdgParentList *_xdg_mime_parent_list_new (void);
|
||||
void _xdg_mime_parent_list_free (XdgParentList *list);
|
||||
const char **_xdg_mime_parent_list_lookup (XdgParentList *list,
|
||||
const char *mime);
|
||||
void _xdg_mime_parent_list_dump (XdgParentList *list);
|
||||
|
||||
#endif /* __XDG_MIME_PARENT_H__ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue