2014-02-27 10:55:04 +00:00
|
|
|
/** \file xdgmimeglob.cpp
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu" -*- */
|
2014-02-27 10:55:04 +00:00
|
|
|
/* xdgmimeglob.cpp: Private file. Datastructure for storing the globs.
|
2005-09-20 13:26:39 +00:00
|
|
|
*
|
|
|
|
* 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 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
|
2012-11-18 10:23:22 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2005-09-20 13:26:39 +00:00
|
|
|
* 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
|
2013-12-13 20:51:52 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE (0)
|
2005-09-20 13:26:39 +00:00
|
|
|
#endif
|
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE (!FALSE)
|
2005-09-20 13:26:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct XdgGlobHashNode XdgGlobHashNode;
|
|
|
|
typedef struct XdgGlobList XdgGlobList;
|
|
|
|
|
|
|
|
struct XdgGlobHashNode
|
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
xdg_unichar_t character;
|
|
|
|
const char *mime_type;
|
|
|
|
XdgGlobHashNode *next;
|
|
|
|
XdgGlobHashNode *child;
|
2005-09-20 13:26:39 +00:00
|
|
|
};
|
|
|
|
struct XdgGlobList
|
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
const char *data;
|
|
|
|
const char *mime_type;
|
|
|
|
XdgGlobList *next;
|
2005-09-20 13:26:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct XdgGlobHash
|
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobList *literal_list;
|
|
|
|
XdgGlobHashNode *simple_node;
|
|
|
|
XdgGlobList *full_list;
|
2005-09-20 13:26:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* XdgGlobList
|
|
|
|
*/
|
|
|
|
static XdgGlobList *
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_list_new(void)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobList *new_element;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
new_element = (XdgGlobList *)calloc(1, sizeof(XdgGlobList));
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return new_element;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Frees glob_list and all of it's children */
|
|
|
|
static void
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_list_free(XdgGlobList *glob_list)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobList *ptr, *next;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
ptr = glob_list;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
while (ptr != NULL)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
next = ptr->next;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (ptr->data)
|
|
|
|
free((void *) ptr->data);
|
|
|
|
if (ptr->mime_type)
|
|
|
|
free((void *) ptr->mime_type);
|
|
|
|
free(ptr);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
ptr = next;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static XdgGlobList *
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_list_append(XdgGlobList *glob_list,
|
|
|
|
void *data,
|
|
|
|
const char *mime_type)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobList *new_element;
|
|
|
|
XdgGlobList *tmp_element;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
new_element = _xdg_glob_list_new();
|
|
|
|
new_element->data = (const char *)data;
|
|
|
|
new_element->mime_type = mime_type;
|
|
|
|
if (glob_list == NULL)
|
|
|
|
return new_element;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
tmp_element = glob_list;
|
|
|
|
while (tmp_element->next != NULL)
|
|
|
|
tmp_element = tmp_element->next;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
tmp_element->next = new_element;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return glob_list;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static XdgGlobList *
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_list_prepend(XdgGlobList *glob_list,
|
|
|
|
void *data,
|
|
|
|
const char *mime_type)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobList *new_element;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
new_element = _xdg_glob_list_new();
|
|
|
|
new_element->data = data;
|
|
|
|
new_element->next = glob_list;
|
|
|
|
new_element->mime_type = mime_type;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return new_element;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* XdgGlobHashNode
|
|
|
|
*/
|
|
|
|
|
|
|
|
static XdgGlobHashNode *
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_node_new(void)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobHashNode *glob_hash_node;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
glob_hash_node = (XdgGlobHashNode *)calloc(1, sizeof(XdgGlobHashNode));
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return glob_hash_node;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_node_dump(XdgGlobHashNode *glob_hash_node,
|
|
|
|
int depth)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < depth; i++)
|
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
printf("%c", (char)glob_hash_node->character);
|
|
|
|
if (glob_hash_node->mime_type)
|
|
|
|
printf(" - %s\n", glob_hash_node->mime_type);
|
|
|
|
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);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static XdgGlobHashNode *
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_insert_text(XdgGlobHashNode *glob_hash_node,
|
|
|
|
const char *text,
|
|
|
|
const char *mime_type)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobHashNode *node;
|
|
|
|
xdg_unichar_t character;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
character = _xdg_utf8_to_ucs4(text);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if ((glob_hash_node == NULL) ||
|
|
|
|
(character < glob_hash_node->character))
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
node = _xdg_glob_hash_node_new();
|
|
|
|
node->character = character;
|
|
|
|
node->next = glob_hash_node;
|
|
|
|
glob_hash_node = node;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
else if (character == glob_hash_node->character)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
node = glob_hash_node;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
else
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
text = _xdg_utf8_next_char(text);
|
|
|
|
if (*text == '\000')
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
node->mime_type = mime_type;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
else
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
node->child = _xdg_glob_hash_insert_text(node->child, text, mime_type);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
return glob_hash_node;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_node_lookup_file_name(XdgGlobHashNode *glob_hash_node,
|
|
|
|
const char *file_name,
|
|
|
|
int ignore_case)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobHashNode *node;
|
|
|
|
xdg_unichar_t character;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (glob_hash_node == NULL)
|
|
|
|
return NULL;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
character = _xdg_utf8_to_ucs4(file_name);
|
|
|
|
if (ignore_case)
|
|
|
|
character = _xdg_ucs4_to_lower(character);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
for (node = glob_hash_node; node && character >= node->character; node = node->next)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
if (character == node->character)
|
|
|
|
{
|
|
|
|
file_name = _xdg_utf8_next_char(file_name);
|
|
|
|
if (*file_name == '\000')
|
|
|
|
return node->mime_type;
|
|
|
|
else
|
|
|
|
return _xdg_glob_hash_node_lookup_file_name(node->child,
|
|
|
|
file_name,
|
|
|
|
ignore_case);
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
return NULL;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_lookup_file_name(XdgGlobHash *glob_hash,
|
|
|
|
const char *file_name)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobList *list;
|
|
|
|
const char *mime_type;
|
|
|
|
const char *ptr;
|
|
|
|
/* First, check the literals */
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
assert(file_name != NULL);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
for (list = glob_hash->literal_list; list; list = list->next)
|
|
|
|
if (strcmp((const char *)list->data, file_name) == 0)
|
|
|
|
return list->mime_type;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
ptr = strchr(file_name, '.');
|
|
|
|
while (ptr != NULL)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
mime_type = (_xdg_glob_hash_node_lookup_file_name(glob_hash->simple_node, ptr, FALSE));
|
|
|
|
if (mime_type != NULL)
|
|
|
|
return mime_type;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
mime_type = (_xdg_glob_hash_node_lookup_file_name(glob_hash->simple_node, ptr, TRUE));
|
|
|
|
if (mime_type != NULL)
|
|
|
|
return mime_type;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
ptr = strchr(ptr+1, '.');
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/* FIXME: Not UTF-8 safe */
|
|
|
|
for (list = glob_hash->full_list; list; list = list->next)
|
|
|
|
if (fnmatch((const char *)list->data, file_name, 0) == 0)
|
|
|
|
return list->mime_type;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return NULL;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* XdgGlobHash
|
|
|
|
*/
|
|
|
|
|
|
|
|
XdgGlobHash *
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_new(void)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobHash *glob_hash;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
glob_hash = (XdgGlobHash *)calloc(1, sizeof(XdgGlobHash));
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return glob_hash;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_free_nodes(XdgGlobHashNode *node)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
if (node)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
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);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_free(XdgGlobHash *glob_hash)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
_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);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XdgGlobType
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_determine_type(const char *glob)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
const char *ptr;
|
|
|
|
int maybe_in_simple_glob = FALSE;
|
|
|
|
int first_char = TRUE;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
ptr = glob;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
while (*ptr != '\000')
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
if (*ptr == '*' && first_char)
|
|
|
|
maybe_in_simple_glob = TRUE;
|
|
|
|
else if (*ptr == '\\' || *ptr == '[' || *ptr == '?' || *ptr == '*')
|
|
|
|
return XDG_GLOB_FULL;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
first_char = FALSE;
|
|
|
|
ptr = _xdg_utf8_next_char(ptr);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
if (maybe_in_simple_glob)
|
|
|
|
return XDG_GLOB_SIMPLE;
|
|
|
|
else
|
|
|
|
return XDG_GLOB_LITERAL;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* glob must be valid UTF-8 */
|
|
|
|
void
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_append_glob(XdgGlobHash *glob_hash,
|
|
|
|
const char *glob,
|
|
|
|
const char *mime_type)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobType type;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
assert(glob_hash != NULL);
|
|
|
|
assert(glob != NULL);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
type = _xdg_glob_determine_type(glob);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
switch (type)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 08:31:03 +00:00
|
|
|
case XDG_GLOB_LITERAL:
|
|
|
|
glob_hash->literal_list = _xdg_glob_list_append(glob_hash->literal_list, strdup(glob), strdup(mime_type));
|
|
|
|
break;
|
|
|
|
case XDG_GLOB_SIMPLE:
|
|
|
|
glob_hash->simple_node = _xdg_glob_hash_insert_text(glob_hash->simple_node, glob + 1, strdup(mime_type));
|
|
|
|
break;
|
|
|
|
case XDG_GLOB_FULL:
|
|
|
|
glob_hash->full_list = _xdg_glob_list_append(glob_hash->full_list, strdup(glob), strdup(mime_type));
|
|
|
|
break;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_glob_hash_dump(XdgGlobHash *glob_hash)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
XdgGlobList *list;
|
|
|
|
printf("LITERAL STRINGS\n");
|
|
|
|
if (glob_hash->literal_list == NULL)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
printf(" None\n");
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
else
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
for (list = glob_hash->literal_list; list; list = list->next)
|
|
|
|
printf(" %s - %s\n", (char *)list->data, list->mime_type);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
printf("\nSIMPLE GLOBS\n");
|
|
|
|
_xdg_glob_hash_node_dump(glob_hash->simple_node, 4);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
printf("\nFULL GLOBS\n");
|
|
|
|
if (glob_hash->full_list == NULL)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
printf(" None\n");
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
else
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
for (list = glob_hash->full_list; list; list = list->next)
|
|
|
|
printf(" %s - %s\n", (char *)list->data, list->mime_type);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-11-19 00:30:30 +00:00
|
|
|
_xdg_mime_glob_read_from_file(XdgGlobHash *glob_hash,
|
|
|
|
const char *file_name)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
FILE *glob_file;
|
|
|
|
char line[255];
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/* OK to not use CLO_EXEC here because mimedb is single threaded */
|
|
|
|
glob_file = fopen(file_name, "r");
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (glob_file == NULL)
|
|
|
|
return;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
|
|
|
|
* Blah */
|
|
|
|
while (fgets(line, 255, glob_file) != NULL)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
char *colon;
|
|
|
|
if (line[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
colon = strchr(line, ':');
|
|
|
|
if (colon == NULL)
|
|
|
|
continue;
|
|
|
|
*(colon++) = '\000';
|
|
|
|
colon[strlen(colon) -1] = '\000';
|
|
|
|
_xdg_glob_hash_append_glob(glob_hash, colon, line);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
fclose(glob_file);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|