2005-09-20 23:26:39 +10:00
/* -*- mode: C; c-file-style: "gnu" -*- */
/* xdgmimealias.c: Private file. Datastructure for storing the aliases.
*
* More info can be found at http : //www.freedesktop.org/standards/
*
* Copyright ( C ) 2004 Red Hat , Inc .
* Copyright ( C ) 2004 Matthias Clasen < mclasen @ redhat . com >
*
* Licensed under the Academic Free License version 2.0
* Or under the following terms :
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
2012-11-18 11:23:22 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
2005-09-20 23:26:39 +10: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 21:51:52 +01:00
* Free Software Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
* Boston , MA 02110 - 1301 , USA .
2005-09-20 23:26:39 +10:00
*/
# 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>
2012-11-18 11:23:22 +01:00
# ifndef FALSE
# define FALSE (0)
2005-09-20 23:26:39 +10:00
# endif
2012-11-18 11:23:22 +01:00
# ifndef TRUE
# define TRUE (!FALSE)
2005-09-20 23:26:39 +10:00
# endif
typedef struct XdgAlias XdgAlias ;
2012-11-18 11:23:22 +01:00
struct XdgAlias
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
char * alias ;
char * mime_type ;
2005-09-20 23:26:39 +10:00
} ;
struct XdgAliasList
{
2012-11-18 16:30:30 -08:00
struct XdgAlias * aliases ;
int n_aliases ;
2005-09-20 23:26:39 +10:00
} ;
XdgAliasList *
2012-11-18 16:30:30 -08:00
_xdg_mime_alias_list_new ( void )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
XdgAliasList * list ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
list = ( XdgAliasList * ) malloc ( sizeof ( XdgAliasList ) ) ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
list - > aliases = NULL ;
list - > n_aliases = 0 ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
return list ;
2005-09-20 23:26:39 +10:00
}
2012-11-18 11:23:22 +01:00
void
2012-11-18 16:30:30 -08:00
_xdg_mime_alias_list_free ( XdgAliasList * list )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
int i ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
if ( list - > aliases )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
for ( i = 0 ; i < list - > n_aliases ; i + + )
{
free ( list - > aliases [ i ] . alias ) ;
free ( list - > aliases [ i ] . mime_type ) ;
}
free ( list - > aliases ) ;
2005-09-20 23:26:39 +10:00
}
2012-11-18 16:30:30 -08:00
free ( list ) ;
2005-09-20 23:26:39 +10:00
}
static int
2012-11-18 16:30:30 -08:00
alias_entry_cmp ( const void * v1 , const void * v2 )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
return strcmp ( ( ( XdgAlias * ) v1 ) - > alias , ( ( XdgAlias * ) v2 ) - > alias ) ;
2005-09-20 23:26:39 +10:00
}
const char *
2012-11-18 16:30:30 -08:00
_xdg_mime_alias_list_lookup ( XdgAliasList * list ,
const char * alias )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
XdgAlias * entry ;
XdgAlias key ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
if ( list - > n_aliases > 0 )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
key . alias = ( char * ) alias ;
key . mime_type = 0 ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
entry = ( XdgAlias * ) bsearch ( & key , list - > aliases , list - > n_aliases ,
sizeof ( XdgAlias ) , alias_entry_cmp ) ;
if ( entry )
return entry - > mime_type ;
2005-09-20 23:26:39 +10:00
}
2012-11-18 16:30:30 -08:00
return NULL ;
2005-09-20 23:26:39 +10:00
}
void
2012-11-18 16:30:30 -08:00
_xdg_mime_alias_read_from_file ( XdgAliasList * list ,
const char * file_name )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
FILE * file ;
char line [ 255 ] ;
int alloc ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
/* OK to not use CLO_EXEC here because mimedb is single threaded */
file = fopen ( file_name , " r " ) ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
if ( file = = NULL )
return ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
/* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
* Blah */
alloc = list - > n_aliases + 16 ;
list - > aliases = ( XdgAlias * ) realloc ( list - > aliases , alloc * sizeof ( XdgAlias ) ) ;
while ( fgets ( line , 255 , file ) ! = NULL )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
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 = ( XdgAlias * ) 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 + + ;
2005-09-20 23:26:39 +10:00
}
2012-11-18 16:30:30 -08:00
list - > aliases = ( XdgAlias * ) realloc ( list - > aliases ,
list - > n_aliases * sizeof ( XdgAlias ) ) ;
2012-11-18 11:23:22 +01:00
2012-11-18 16:30:30 -08:00
fclose ( file ) ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
if ( list - > n_aliases > 1 )
qsort ( list - > aliases , list - > n_aliases ,
sizeof ( XdgAlias ) , alias_entry_cmp ) ;
2005-09-20 23:26:39 +10:00
}
void
2012-11-18 16:30:30 -08:00
_xdg_mime_alias_list_dump ( XdgAliasList * list )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
int i ;
2005-09-20 23:26:39 +10:00
2012-11-18 16:30:30 -08:00
if ( list - > aliases )
2005-09-20 23:26:39 +10:00
{
2012-11-18 16:30:30 -08:00
for ( i = 0 ; i < list - > n_aliases ; i + + )
{
printf ( " %s %s \n " ,
list - > aliases [ i ] . alias ,
list - > aliases [ i ] . mime_type ) ;
}
2005-09-20 23:26:39 +10:00
}
}