From 7e52523541da76ec5a973d3309888e6fa0659028 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Wed, 8 Feb 2012 01:55:35 -0800 Subject: [PATCH] Migrate mimedb off of al_init --- mimedb.cpp | 127 ++++++++++++++++++++++------------------------------- 1 file changed, 53 insertions(+), 74 deletions(-) diff --git a/mimedb.cpp b/mimedb.cpp index 65133e19f..dcc1aa8a2 100644 --- a/mimedb.cpp +++ b/mimedb.cpp @@ -35,6 +35,8 @@ license. Read the source code of the library for more information. #include #include #include +#include + #ifdef HAVE_GETOPT_H @@ -50,6 +52,8 @@ license. Read the source code of the library for more information. #include "util.h" #include "print_help.h" +typedef std::vector string_list_t; + /** Location of the applications .desktop file, relative to a base mime directory */ @@ -296,7 +300,7 @@ static char *file_exists( const char *dir, const char *in ) \param all If zero, then stop after the first filename. \return The number of filenames added to the list. */ -static int append_filenames( std::vector &list, const char *f, int all ) +static int append_filenames( string_list_t &list, const char *f, int all ) { size_t prev_count = list.size(); char *result; @@ -393,7 +397,7 @@ static int append_filenames( std::vector &list, const char *f, int */ static std::string get_filename( char *f ) { - std::vector list; + string_list_t list; append_filenames( list, f, 0 ); if (list.empty()) { @@ -676,7 +680,7 @@ static char *get_action( const char *mimetype ) char *launcher; char *end; - std::vector mime_filenames; + string_list_t mime_filenames; const char *launcher_str = NULL; const char *launcher_command_str, *launcher_command; @@ -838,9 +842,9 @@ static char *my_getcwd () /** Return absolute filename of specified file */ -static char *get_fullfile( char *file ) +static const char *get_fullfile( const char *file ) { - char *fullfile; + const char *fullfile; if( file[0] == '/' ) { @@ -858,18 +862,19 @@ static char *get_fullfile( char *file ) int l = strlen(cwd); - fullfile = (char *)my_malloc( l + strlen(file)+2 ); - if( !fullfile ) + char *tmp = (char *)my_malloc( l + strlen(file)+2 ); + if( !tmp ) { free(cwd); return 0; } - strcpy( fullfile, cwd ); + strcpy( tmp, cwd ); if( cwd[l-1] != '/' ) - strcat(fullfile, "/" ); - strcat( fullfile, file ); + strcat(tmp, "/" ); + strcat( tmp, file ); free(cwd); + fullfile = tmp; } return fullfile; } @@ -878,10 +883,10 @@ static char *get_fullfile( char *file ) /** Write specified file as an URL */ -static void write_url( char *file ) +static void write_url( const char *file ) { - char *fullfile = get_fullfile( file ); - char *str = fullfile; + const char *fullfile = get_fullfile( file ); + const char *str = fullfile; if( str == 0 ) { @@ -918,17 +923,17 @@ static void write_url( char *file ) str++; } if( fullfile != file ) - free( fullfile ); + free( (void *)fullfile ); } /** Write specified file */ -static void write_file( char *file, int print_path ) +static void write_file( const char *file, int print_path ) { - char *fullfile; - char *str; + const char *fullfile; + const char *str; if( print_path ) { fullfile = get_fullfile( file ); @@ -936,12 +941,13 @@ static void write_file( char *file, int print_path ) } else { - fullfile = my_strdup( file ); - if( !fullfile ) + char *tmp = my_strdup( file ); + if( !tmp ) { return; } - str = basename( fullfile ); + str = basename( tmp ); + fullfile = tmp; } if( !str ) @@ -1012,7 +1018,7 @@ static void write_file( char *file, int print_path ) } if( fullfile != file ) - free( fullfile ); + free( (void *)fullfile ); } /** @@ -1022,13 +1028,13 @@ static void write_file( char *file, int print_path ) \param files the list of files for which to perform the action \param fileno an internal value. Should always be set to zero. */ -static void launch( char *filter, array_list_t *files, int fileno ) +static void launch( char *filter, const string_list_t &files, int fileno ) { char *filter_org=filter; int count=0; int launch_again=0; - if( al_get_count( files ) <= fileno ) + if( files.size() <= fileno ) return; @@ -1044,17 +1050,16 @@ static void launch( char *filter, array_list_t *files, int fileno ) case 'u': { launch_again = 1; - write_url( (char *)al_get( files, fileno ) ); + write_url( files.at(fileno).c_str() ); break; } case 'U': { - int i; - for( i=0; i launch_hash_t; + launch_hash_t launch_hash; locale_init(); @@ -1351,11 +1358,7 @@ int main (int argc, char *argv[]) fprintf( stderr, _("%s: Can not launch a mimetype\n"), MIMEDB ); print_help( argv[0], 2 ); exit(1); - } - - if( output_type == LAUNCH ) - hash_init( &launch_hash, &hash_str_func, &hash_str_cmp ); - + } /* Loop over all non option arguments and do the specified lookup @@ -1417,20 +1420,9 @@ int main (int argc, char *argv[]) them together after all the arguments have been parsed. */ - array_list_t *l= (array_list_t *)hash_get( &launch_hash, mimetype ); output = 0; - - if( !l ) - { - l = (array_list_t *)my_malloc( sizeof( array_list_t ) ); - if( l == 0 ) - { - break; - } - al_init( l ); - hash_put( &launch_hash, mimetype, l ); - } - al_push( l, argv[i] ); + string_list_t &l = launch_hash[mimetype]; + l.push_back(argv[i]); } } @@ -1451,20 +1443,10 @@ int main (int argc, char *argv[]) */ if( output_type == LAUNCH && !error ) { - int i; - array_list_t mimes; - al_init( &mimes ); - hash_get_keys( &launch_hash, &mimes ); - for( i=0; ifirst.c_str(); + string_list_t &files = iter->second; char *launcher = get_action( mimetype ); @@ -1474,9 +1456,6 @@ int main (int argc, char *argv[]) free( launcher ); } } - hash_foreach( &launch_hash, &clear_entry ); - hash_destroy( &launch_hash ); - al_destroy( &mimes ); } if( launch_buff )