mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-27 05:13:10 +00:00
Removed al_list from mimedb.cpp
This commit is contained in:
parent
029c8c06c2
commit
8bc83c5967
1 changed files with 35 additions and 43 deletions
78
mimedb.cpp
78
mimedb.cpp
|
@ -33,6 +33,8 @@ license. Read the source code of the library for more information.
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
#ifdef HAVE_GETOPT_H
|
#ifdef HAVE_GETOPT_H
|
||||||
|
@ -48,7 +50,6 @@ license. Read the source code of the library for more information.
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "print_help.h"
|
#include "print_help.h"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Location of the applications .desktop file, relative to a base mime directory
|
Location of the applications .desktop file, relative to a base mime directory
|
||||||
*/
|
*/
|
||||||
|
@ -173,7 +174,7 @@ void *my_malloc( size_t s )
|
||||||
/**
|
/**
|
||||||
Duplicate string, set error flag and print message on failure
|
Duplicate string, set error flag and print message on failure
|
||||||
*/
|
*/
|
||||||
char *my_strdup( char *s )
|
char *my_strdup( const char *s )
|
||||||
{
|
{
|
||||||
char *res = strdup( s );
|
char *res = strdup( s );
|
||||||
if( !s )
|
if( !s )
|
||||||
|
@ -189,7 +190,7 @@ char *my_strdup( char *s )
|
||||||
Search the file \c filename for the first line starting with \c
|
Search the file \c filename for the first line starting with \c
|
||||||
match, which is returned in a newly allocated string.
|
match, which is returned in a newly allocated string.
|
||||||
*/
|
*/
|
||||||
static char * search_ini( const char *filename, const char *match )
|
static const char * search_ini( const char *filename, const char *match )
|
||||||
{
|
{
|
||||||
FILE *f = fopen( filename, "r" );
|
FILE *f = fopen( filename, "r" );
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
|
@ -295,9 +296,9 @@ static char *file_exists( const char *dir, const char *in )
|
||||||
\param all If zero, then stop after the first filename.
|
\param all If zero, then stop after the first filename.
|
||||||
\return The number of filenames added to the list.
|
\return The number of filenames added to the list.
|
||||||
*/
|
*/
|
||||||
static int append_filenames( array_list_t *list, const char *f, int all )
|
static int append_filenames( std::vector<std::string> &list, const char *f, int all )
|
||||||
{
|
{
|
||||||
int prev_count = al_get_count( list );
|
size_t prev_count = list.size();
|
||||||
char *result;
|
char *result;
|
||||||
const char *xdg_data_home;
|
const char *xdg_data_home;
|
||||||
const char *xdg_data_dirs;
|
const char *xdg_data_dirs;
|
||||||
|
@ -309,7 +310,7 @@ static int append_filenames( array_list_t *list, const char *f, int all )
|
||||||
result = file_exists( xdg_data_home, f );
|
result = file_exists( xdg_data_home, f );
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
al_push( list, result );
|
list.push_back(result);
|
||||||
if ( !all )
|
if ( !all )
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -334,7 +335,7 @@ static int append_filenames( array_list_t *list, const char *f, int all )
|
||||||
|
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
al_push( list, result );
|
list.push_back(result);
|
||||||
if ( !all )
|
if ( !all )
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -376,7 +377,7 @@ static int append_filenames( array_list_t *list, const char *f, int all )
|
||||||
|
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
al_push( list, result );
|
list.push_back(result);
|
||||||
if ( !all ) {
|
if ( !all ) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -384,22 +385,22 @@ static int append_filenames( array_list_t *list, const char *f, int all )
|
||||||
|
|
||||||
ptr = end_ptr;
|
ptr = end_ptr;
|
||||||
}
|
}
|
||||||
return al_get_count( list ) - prev_count;
|
return list.size() - prev_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Find at most one file relative to the XDG data directories.
|
Find at most one file relative to the XDG data directories; returns the empty string on failure
|
||||||
*/
|
*/
|
||||||
static char *get_filename( char *f )
|
static std::string get_filename( char *f )
|
||||||
{
|
{
|
||||||
array_list_t list;
|
std::vector<std::string> list;
|
||||||
char *first = NULL;
|
|
||||||
|
|
||||||
al_init( &list );
|
append_filenames( list, f, 0 );
|
||||||
append_filenames( &list, f, 0 );
|
if (list.empty()) {
|
||||||
first = (char *)al_pop( &list );
|
return "";
|
||||||
al_destroy( &list );
|
} else {
|
||||||
return first;
|
return list.back();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -512,7 +513,7 @@ static char *get_description( const char *mimetype )
|
||||||
{
|
{
|
||||||
char *fn_part;
|
char *fn_part;
|
||||||
|
|
||||||
char *fn;
|
std::string fn;
|
||||||
int fd;
|
int fd;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char *contents;
|
char *contents;
|
||||||
|
@ -574,12 +575,12 @@ static char *get_description( const char *mimetype )
|
||||||
fn = get_filename(fn_part); //malloc( strlen(MIME_DIR) +strlen( MIME_SUFFIX)+ strlen( mimetype ) + 1 );
|
fn = get_filename(fn_part); //malloc( strlen(MIME_DIR) +strlen( MIME_SUFFIX)+ strlen( mimetype ) + 1 );
|
||||||
free(fn_part );
|
free(fn_part );
|
||||||
|
|
||||||
if( !fn )
|
if( fn.empty() )
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = open( fn, O_RDONLY );
|
fd = open( fn.c_str(), O_RDONLY );
|
||||||
|
|
||||||
// fprintf( stderr, "%s\n", fn );
|
// fprintf( stderr, "%s\n", fn );
|
||||||
|
|
||||||
|
@ -590,7 +591,7 @@ static char *get_description( const char *mimetype )
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( stat( fn, &st) )
|
if( stat( fn.c_str(), &st) )
|
||||||
{
|
{
|
||||||
perror( "stat" );
|
perror( "stat" );
|
||||||
error=1;
|
error=1;
|
||||||
|
@ -614,7 +615,6 @@ static char *get_description( const char *mimetype )
|
||||||
Don't need to check exit status of close on read-only file descriptors
|
Don't need to check exit status of close on read-only file descriptors
|
||||||
*/
|
*/
|
||||||
close( fd );
|
close( fd );
|
||||||
free( fn );
|
|
||||||
|
|
||||||
contents[st.st_size]=0;
|
contents[st.st_size]=0;
|
||||||
regmatch_t match[1];
|
regmatch_t match[1];
|
||||||
|
@ -673,32 +673,27 @@ static char *get_description( const char *mimetype )
|
||||||
static char *get_action( const char *mimetype )
|
static char *get_action( const char *mimetype )
|
||||||
{
|
{
|
||||||
char *res=0;
|
char *res=0;
|
||||||
int i;
|
|
||||||
|
|
||||||
char *launcher;
|
char *launcher;
|
||||||
char *end;
|
char *end;
|
||||||
array_list_t mime_filenames;
|
std::vector<std::string> mime_filenames;
|
||||||
|
|
||||||
char *launcher_str = NULL;
|
const char *launcher_str = NULL;
|
||||||
char *launcher_filename, *launcher_command_str, *launcher_command;
|
const char *launcher_command_str, *launcher_command;
|
||||||
char *launcher_full;
|
char *launcher_full;
|
||||||
|
|
||||||
al_init( &mime_filenames );
|
if( !append_filenames( mime_filenames, DESKTOP_DEFAULT, 1 ) )
|
||||||
if( !append_filenames( &mime_filenames, DESKTOP_DEFAULT, 1 ) )
|
|
||||||
{
|
{
|
||||||
al_destroy( &mime_filenames );
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( i = 0; i < al_get_count( &mime_filenames ); i++ )
|
for ( size_t i = 0; i < mime_filenames.size(); i++ )
|
||||||
{
|
{
|
||||||
launcher_str = (char *)search_ini( (char *)al_get( &mime_filenames, i ), mimetype );
|
launcher_str = search_ini( mime_filenames.at(i).c_str(), mimetype );
|
||||||
if ( launcher_str )
|
if ( launcher_str )
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
al_foreach( &mime_filenames, free );
|
|
||||||
al_destroy( &mime_filenames );
|
|
||||||
|
|
||||||
if( !launcher_str )
|
if( !launcher_str )
|
||||||
{
|
{
|
||||||
|
@ -750,37 +745,34 @@ static char *get_action( const char *mimetype )
|
||||||
launcher_full = (char *)my_malloc( strlen( launcher) + strlen( APPLICATIONS_DIR)+1 );
|
launcher_full = (char *)my_malloc( strlen( launcher) + strlen( APPLICATIONS_DIR)+1 );
|
||||||
if( !launcher_full )
|
if( !launcher_full )
|
||||||
{
|
{
|
||||||
free( launcher_str );
|
free( (void *)launcher_str );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy( launcher_full, APPLICATIONS_DIR );
|
strcpy( launcher_full, APPLICATIONS_DIR );
|
||||||
strcat( launcher_full, launcher );
|
strcat( launcher_full, launcher );
|
||||||
free( launcher_str );
|
free( (void *)launcher_str );
|
||||||
|
|
||||||
launcher_filename = get_filename( launcher_full );
|
std::string launcher_filename = get_filename( launcher_full );
|
||||||
|
|
||||||
free( launcher_full );
|
free( launcher_full );
|
||||||
|
|
||||||
launcher_command_str = search_ini( launcher_filename, "Exec" );
|
launcher_command_str = search_ini( launcher_filename.c_str(), "Exec" );
|
||||||
|
|
||||||
if( !launcher_command_str )
|
if( !launcher_command_str )
|
||||||
{
|
{
|
||||||
fprintf( stderr,
|
fprintf( stderr,
|
||||||
_( "%s: Default launcher '%s' does not specify how to start\n"), MIMEDB,
|
_( "%s: Default launcher '%s' does not specify how to start\n"), MIMEDB,
|
||||||
launcher_filename );
|
launcher_filename.c_str() );
|
||||||
free( launcher_filename );
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
free( launcher_filename );
|
|
||||||
|
|
||||||
launcher_command = strchr( launcher_command_str, '=' );
|
launcher_command = strchr( launcher_command_str, '=' );
|
||||||
launcher_command++;
|
launcher_command++;
|
||||||
|
|
||||||
res = my_strdup( launcher_command );
|
res = my_strdup( launcher_command );
|
||||||
|
|
||||||
free( launcher_command_str );
|
free( (void *)launcher_command_str );
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue