resync with upstream sources.

Tue Jan 20 13:07:04 2004  Jonathan Blandford  <jrb@gnome.org>

        * xdgmime.c: resync with upstream sources.
This commit is contained in:
Jonathan Blandford 2004-01-20 18:07:07 +00:00 committed by Jonathan Blandford
parent 245308c38c
commit a99fb2ea6c
9 changed files with 146 additions and 74 deletions

View File

@ -1,3 +1,7 @@
Tue Jan 20 13:07:04 2004 Jonathan Blandford <jrb@gnome.org>
* xdgmime.c: resync with upstream sources.
Fri Oct 24 16:54:57 2003 Owen Taylor <otaylor@redhat.com>
* Makefile.am (libxdgmime_la_SOURCES): Add .h files to

View File

@ -35,9 +35,10 @@
#include <sys/types.h>
#include <unistd.h>
XdgGlobHash *global_hash = NULL;
XdgMimeMagic *global_magic = NULL;
static int initted = 0;
static XdgGlobHash *global_hash = NULL;
static XdgMimeMagic *global_magic = NULL;
const char *xdg_mime_type_unknown = "application/octet-stream";
static void
_xdg_mime_init_from_directory (const char *directory)
@ -60,8 +61,6 @@ _xdg_mime_init_from_directory (const char *directory)
static void
xdg_mime_init (void)
{
static int initted = 0;
if (initted == 0)
{
const char *xdg_data_home;
@ -122,8 +121,9 @@ xdg_mime_init (void)
len = end_ptr - ptr;
else
len = end_ptr - ptr + 1;
dir = malloc (len);
dir = malloc (len + 1);
strncpy (dir, ptr, len);
dir[len] = '\0';
_xdg_mime_init_from_directory (dir);
free (dir);
@ -234,3 +234,19 @@ xdg_mime_is_valid_mime_type (const char *mime_type)
*/
return _xdg_utf8_validate (mime_type);
}
void
xdg_mime_shutdown (void)
{
/* FIXME: Need to make this (and the whole library) thread safe */
if (initted)
{
_xdg_glob_hash_free (global_hash);
global_hash = NULL;
_xdg_mime_magic_free (global_magic);
global_magic = NULL;
initted = 0;
}
}

View File

@ -35,17 +35,22 @@
extern "C" {
#endif /* __cplusplus */
#define XDG_MIME_TYPE_UNKNOWN "application/octet-stream"
extern const char *xdg_mime_type_unknown;
#define XDG_MIME_TYPE_UNKNOWN xdg_mime_type_unknown
#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
#endif
#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_is_valid_mime_type XDG_ENTRY(is_valid_mime_type)
#define xdg_mime_type_unknown XDG_ENTRY(type_unknown)
#endif
const char *xdg_mime_get_mime_type_for_data (const void *data,
@ -53,6 +58,7 @@ const char *xdg_mime_get_mime_type_for_data (const void *data,
const char *xdg_mime_get_mime_type_for_file (const char *file_name);
const char *xdg_mime_get_mime_type_from_file_name (const char *file_name);
int xdg_mime_is_valid_mime_type (const char *mime_type);
void xdg_mime_shutdown (void);
#ifdef __cplusplus
}

View File

@ -1,5 +1,5 @@
/* -*- mode: C; c-file-style: "gnu" -*- */
/* xdgmime.c: Private file. Datastructure for storing the globs.
/* xdgmimeglob.c: Private file. Datastructure for storing the globs.
*
* More info can be found at http://www.freedesktop.org/standards/
*
@ -8,7 +8,7 @@
*
* 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
@ -78,13 +78,27 @@ _xdg_glob_list_new (void)
return new_element;
}
#if 0
/* Frees glob_list and all of it's children */
static void
_xdg_glob_list_free (XdgGlobList *glob_list)
{
free (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;
}
}
#endif
static XdgGlobList *
_xdg_glob_list_append (XdgGlobList *glob_list,
@ -139,14 +153,6 @@ _xdg_glob_hash_node_new (void)
return glob_hash_node;
}
#if 0
static void
_xdg_glob_hash_node_free (XdgGlobHashNode *glob_hash_node)
{
free (glob_hash_node);
}
#endif
void
_xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node,
int depth)
@ -346,12 +352,12 @@ _xdg_glob_hash_free_nodes (XdgGlobHashNode *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)
{

View File

@ -1,14 +1,14 @@
/* -*- mode: C; c-file-style: "gnu" -*- */
/* xdgmime.h: Private file. Datastructure for storing the globs.
/* 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
@ -28,6 +28,7 @@
#ifndef __XDG_MIME_GLOB_H__
#define __XDG_MIME_GLOB_H__
#include "xdgmime.h"
typedef struct XdgGlobHash XdgGlobHash;
@ -38,6 +39,16 @@ typedef enum
XDG_GLOB_FULL, /* x*.[ch] */
} XdgGlobType;
#ifdef XDG_PREFIX
#define _xdg_mime_glob_read_from_file XDG_ENTRY(glob_read_from_file)
#define _xdg_glob_hash_new XDG_ENTRY(hash_new)
#define _xdg_glob_hash_free XDG_ENTRY(hash_free)
#define _xdg_glob_hash_lookup_file_name XDG_ENTRY(hash_lookup_file_name)
#define _xdg_glob_hash_append_glob XDG_ENTRY(hash_append_glob)
#define _xdg_glob_determine_type XDG_ENTRY(determine_type)
#define _xdg_glob_hash_dump XDG_ENTRY(hash_dump)
#endif
void _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,
const char *file_name);

View File

@ -2,13 +2,13 @@
/* 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
@ -37,7 +37,7 @@
#define TRUE (!FALSE)
#endif
const unsigned char utf8_skip_data[256] = {
const unsigned 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,
@ -48,7 +48,7 @@ const unsigned char utf8_skip_data[256] = {
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 utf8_skip = utf8_skip_data;
const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
@ -65,33 +65,33 @@ _xdg_utf8_to_ucs4(const char *source)
{
int bytelength = 0;
xdg_unichar_t result;
if ( ! *source & 0x40 )
if ( ! (*source & 0x40) )
{
ucs32 = *source;
}
else
{
if ( ! *source & 0x20 )
if ( ! (*source & 0x20) )
{
result = *source++ & 0x1F;
bytelength = 2;
}
else if ( ! *source & 0x10 )
else if ( ! (*source & 0x10) )
{
result = *source++ & 0x0F;
bytelength = 3;
}
else if ( ! *source & 0x08 )
else if ( ! (*source & 0x08) )
{
result = *source++ & 0x07;
bytelength = 4;
}
else if ( ! *source & 0x04 )
else if ( ! (*source & 0x04) )
{
result = *source++ & 0x03;
bytelength = 5;
}
else if ( ! *source & 0x02 )
else if ( ! (*source & 0x02) )
{
result = *source++ & 0x01;
bytelength = 6;

View File

@ -2,13 +2,13 @@
/* 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
@ -28,12 +28,21 @@
#ifndef __XDG_MIME_INT_H__
#define __XDG_MIME_INT_H__
#include "xdgmime.h"
/* FIXME: Should 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_ENTRY(utf8_skip)
#define _xdg_utf8_to_ucs4 XDG_ENTRY(utf8_to_ucs4)
#define _xdg_ucs4_to_upper XDG_ENTRY(ucs4_to_upper)
#define _xdg_utf8_validate XDG_ENTRY(utf8_validate)
#define _xdg_get_base_name XDG_ENTRY(get_ase_name)
#endif
#define SWAP_BE16_TO_LE16(val) (xdg_uint16_t)(((xdg_uint16_t)(val) << 8)|((xdg_uint16_t)(val) >> 8))
@ -43,9 +52,9 @@ typedef unsigned int xdg_uint32_t;
(((xdg_uint32_t)(val) & 0x000000FFU) << 24))
/* UTF-8 utils
*/
const char *const utf8_skip;
#define _xdg_utf8_next_char(p) (char *)((p) + utf8_skip[*(unsigned char *)(p)])
#define _xdg_utf8_char_size(p) (int) (utf8_skip[*(unsigned char *)(p)])
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_upper (xdg_unichar_t source);

View File

@ -1,15 +1,14 @@
/* -*- mode: C; c-file-style: "gnu" -*- */
/* xdgmimemagic.: Private file. Datastructure for storing magic.
/* 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
@ -26,12 +25,12 @@
* Boston, MA 02111-1307, USA.
*/
#include <assert.h>
#include "xdgmimemagic.h"
#include "xdgmimeint.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
@ -111,17 +110,6 @@ _xdg_mime_magic_matchlet_new (void)
return matchlet;
}
void
_xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
{
if (mime_magic_match)
{
if (mime_magic_match->mime_type)
free ((char *)mime_magic_match->mime_type);
free (mime_magic_match);
}
}
void
_xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
@ -139,6 +127,27 @@ _xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
}
/* Frees mime_magic_match and the remainder of its list
*/
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.
@ -173,7 +182,7 @@ _xdg_mime_magic_read_to_newline (FILE *magic_file,
retval = realloc (retval, len);
}
}
retval[pos] = '\000';
return retval;
}
@ -189,7 +198,7 @@ _xdg_mime_magic_read_a_number (FILE *magic_file,
char number_string[MAX_NUMBER_SIZE];
int pos = 0;
int c;
int retval = -1;
long retval = -1;
while (TRUE)
{
@ -213,9 +222,10 @@ _xdg_mime_magic_read_a_number (FILE *magic_file,
if (pos > 0)
{
number_string[pos] = '\000';
errno = 0;
retval = strtol (number_string, NULL, 10);
if ((retval == LONG_MIN || retval == LONG_MAX) &&
(errno == ERANGE))
if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
return -1;
}
@ -233,8 +243,8 @@ _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
char *end_ptr;
int end_of_file = 0;
assert (magic_file);
assert (match);
assert (magic_file != NULL);
assert (match != NULL);
c = fgetc (magic_file);
if (c == EOF)
@ -303,7 +313,7 @@ _xdg_mime_magic_parse_magic_line (FILE *magic_file,
int indent = 0;
int bytes_read;
assert (magic_file);
assert (magic_file != NULL);
/* Sniff the buffer to make sure it's a valid line */
c = fgetc (magic_file);
@ -334,7 +344,7 @@ _xdg_mime_magic_parse_magic_line (FILE *magic_file,
if (c != '>')
return XDG_MIME_MAGIC_ERROR;
matchlet = _xdg_mime_magic_matchlet_new ();
matchlet->indent = indent;
matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
@ -489,7 +499,7 @@ _xdg_mime_magic_parse_magic_line (FILE *magic_file,
matchlet->next = match->matchlet;
match->matchlet = matchlet;
return XDG_MIME_MAGIC_MAGIC;
}
@ -506,7 +516,7 @@ _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
size_t len)
{
int i, j;
for (i = matchlet->offset; i <= matchlet->offset + matchlet->range_length; i++)
{
int valid_matchlet = TRUE;
@ -594,7 +604,7 @@ _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
return;
}
if (match->priority < mime_magic->match_list->priority)
if (match->priority > mime_magic->match_list->priority)
{
match->next = mime_magic->match_list;
mime_magic->match_list = match;
@ -641,7 +651,7 @@ _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
size_t len)
{
XdgMimeMagicMatch *match;
for (match = mime_magic->match_list; match; match = match->next)
{
if (_xdg_mime_magic_match_compare_to_data (match, data, len))
@ -649,7 +659,7 @@ _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
return match->mime_type;
}
}
return NULL;
}
@ -658,7 +668,7 @@ _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;

View File

@ -1,14 +1,14 @@
/* -*- mode: C; c-file-style: "gnu" -*- */
/* xdgmimemagic.h: Private file. Datastructure for storing the globs.
/* 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
@ -29,9 +29,19 @@
#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_ENTRY(glob_read_from_file)
#define _xdg_mime_magic_new XDG_ENTRY(magic_new)
#define _xdg_mime_magic_read_from_file XDG_ENTRY(magic_read_from_file)
#define _xdg_mime_magic_free XDG_ENTRY(magic_free)
#define _xdg_mime_magic_get_buffer_extents XDG_ENTRY(magic_get_buffer_extents)
#define _xdg_mime_magic_lookup_data XDG_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);