X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fplaylist%2Fsort.c;h=34e69737e3e3817ac6660038dcc3f246a41df32d;hb=7af51d108fe596be66dccdf79ea13f44bfcf655a;hp=1df97fbc59a3269d16791e1801c98604cf5c75d8;hpb=982c016f90b5992fc7d4e4f6ed5e11f6556d18a6;p=vlc diff --git a/src/playlist/sort.c b/src/playlist/sort.c index 1df97fbc59..34e69737e3 100644 --- a/src/playlist/sort.c +++ b/src/playlist/sort.c @@ -1,10 +1,10 @@ /***************************************************************************** * sort.c : Playlist sorting functions ***************************************************************************** - * Copyright (C) 1999-2004 VideoLAN - * $Id: sort.c,v 1.5 2004/01/06 08:50:20 zorglub Exp $ + * Copyright (C) 1999-2007 the VideoLAN team + * $Id$ * - * Authors: Clément Stenac + * Authors: Clément Stenac * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,104 +18,222 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ -#include /* free(), strtol() */ -#include /* sprintf() */ -#include /* strerror() */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif #include -#include -#include - #include "vlc_playlist.h" +#include "playlist_internal.h" + + +static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items, + playlist_item_t **pp_items, int i_mode, + int i_type ); /** - * Sort the playlist + * Sort a node. + * This function must be entered with the playlist lock ! + * + * \param p_playlist the playlist + * \param p_node the node to sort + * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM + * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order) + * \return VLC_SUCCESS on success + */ +static int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node, + int i_mode, int i_type ) +{ + playlist_ItemArraySort( p_playlist,p_node->i_children, + p_node->pp_children, i_mode, i_type ); + return VLC_SUCCESS; +} + +/** + * Sort a node recursively. + * + * This function must be entered with the playlist lock ! + * * \param p_playlist the playlist - * \param i_mode: SORT_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM - * \param i_type: SORT_NORMAL or SORT_REVERSE (reversed order) - * \return 0 on success + * \param p_node the node to sort + * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM + * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order) + * \return VLC_SUCCESS on success */ -int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type ) +int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node, + int i_mode, int i_type ) +{ + int i; + playlist_NodeSort( p_playlist, p_node, i_mode, i_type ); + for( i = 0 ; i< p_node->i_children; i++ ) + { + if( p_node->pp_children[i]->i_children != -1 ) + { + playlist_RecursiveNodeSort( p_playlist, p_node->pp_children[i], + i_mode,i_type ); + } + } + return VLC_SUCCESS; +} + + +static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items, + playlist_item_t **pp_items, int i_mode, + int i_type ) { int i , i_small , i_position; playlist_item_t *p_temp; vlc_value_t val; val.b_bool = VLC_TRUE; - vlc_mutex_lock( &p_playlist->object_lock ); + (void)p_playlist; // a bit surprising we don't need p_playlist! if( i_mode == SORT_RANDOM ) { - for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ ) + for( i_position = 0; i_position < i_items ; i_position ++ ) { - int i_new = rand() % (p_playlist->i_size - 1); + int i_new; - /* Keep the correct current index */ - if( i_new == p_playlist->i_index ) - p_playlist->i_index = i_position; - else if( i_position == p_playlist->i_index ) - p_playlist->i_index = i_new; - - p_temp = p_playlist->pp_items[i_position]; - p_playlist->pp_items[i_position] = p_playlist->pp_items[i_new]; - p_playlist->pp_items[i_new] = p_temp; + if( i_items > 1 ) + i_new = rand() % (i_items - 1); + else + i_new = 0; + p_temp = pp_items[i_position]; + pp_items[i_position] = pp_items[i_new]; + pp_items[i_new] = p_temp; } - vlc_mutex_unlock( &p_playlist->object_lock ); - - /* Notify the interfaces */ - var_Set( p_playlist, "intf-change", val ); - return 0; + return VLC_SUCCESS; } - for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ ) +#define META_STRCASECMP_NAME( i, i_small ) { \ + char *psz_i = input_item_GetName( pp_items[i]->p_input ); \ + char *psz_ismall = input_item_GetName( pp_items[i_small]->p_input ); \ + i_test = strcasecmp( psz_i, psz_ismall ); \ + free( psz_i ); \ + free( psz_ismall ); \ +} + + +#define DO_META_SORT_ADV( node, integer ) { \ + char *psz_a = input_item_GetMeta( pp_items[i]->p_input, vlc_meta_##node ); \ + char *psz_b = input_item_GetMeta( pp_items[i_small]->p_input, vlc_meta_##node ); \ + /* Nodes go first */ \ + if( pp_items[i]->i_children == -1 && pp_items[i_small]->i_children >= 0 ) \ + i_test = 1;\ + else if( pp_items[i]->i_children >= 0 &&\ + pp_items[i_small]->i_children == -1 ) \ + i_test = -1; \ + /* Both are nodes, sort by name */ \ + else if( pp_items[i]->i_children >= 0 && \ + pp_items[i_small]->i_children >= 0 ) \ + { \ + META_STRCASECMP_NAME( i, i_small ) \ + } \ + /* Both are items */ \ + else if( psz_a == NULL && psz_b != NULL ) \ + i_test = 1; \ + else if( psz_a != NULL && psz_b == NULL ) \ + i_test = -1;\ + /* No meta, sort by name */ \ + else if( psz_a == NULL && psz_b == NULL ) \ + { \ + META_STRCASECMP_NAME( i, i_small ); \ + } \ + else \ + { \ + if( !integer ) i_test = strcmp( psz_a, psz_b ); \ + else i_test = atoi( psz_a ) - atoi( psz_b ); \ + } \ + free( psz_a ); \ + free( psz_b ); \ +} +#define DO_META_SORT( node ) DO_META_SORT_ADV( node, VLC_FALSE ) + + for( i_position = 0; i_position < i_items -1 ; i_position ++ ) { i_small = i_position; - for( i = i_position + 1 ; i< p_playlist->i_size ; i++) + for( i = i_position + 1 ; i< i_items ; i++) { int i_test = 0; if( i_mode == SORT_TITLE ) { - i_test = strcasecmp( p_playlist->pp_items[i]->psz_name, - p_playlist->pp_items[i_small]->psz_name ); + META_STRCASECMP_NAME( i, i_small ); + } + else if( i_mode == SORT_TITLE_NUMERIC ) + { + char *psz_i = input_item_GetName( pp_items[i]->p_input ); + char *psz_ismall = + input_item_GetName( pp_items[i_small]->p_input ); + i_test = atoi( psz_i ) - atoi( psz_ismall ); + free( psz_i ); + free( psz_ismall ); + } + else if( i_mode == SORT_DURATION ) + { + i_test = input_item_GetDuration( pp_items[i]->p_input ) - + input_item_GetDuration( pp_items[i_small]->p_input ); + } + else if( i_mode == SORT_ARTIST ) + { + DO_META_SORT( Artist ); } - else if( i_mode == SORT_GROUP ) + else if( i_mode == SORT_GENRE ) { - i_test = p_playlist->pp_items[i]->i_group - - p_playlist->pp_items[i_small]->i_group; + DO_META_SORT( Genre ); } - else if( i_mode == SORT_AUTHOR ) + else if( i_mode == SORT_ALBUM ) { - i_test = strcasecmp( - playlist_GetInfo( p_playlist, i, - _("General") , _("Author") ), - playlist_GetInfo( p_playlist, i_small, - _("General") , _("Author") ) ); + DO_META_SORT( Album ); } + else if( i_mode == SORT_TRACK_NUMBER ) + { + DO_META_SORT_ADV( TrackNumber, VLC_TRUE ); + } + else if( i_mode == SORT_DESCRIPTION ) + { + DO_META_SORT( Description ); + } + else if( i_mode == SORT_ID ) + { + i_test = pp_items[i]->i_id - pp_items[i_small]->i_id; + } + else if( i_mode == SORT_TITLE_NODES_FIRST ) + { + /* Alphabetic sort, all nodes first */ - if( ( i_type == SORT_NORMAL && i_test < 0 ) || - ( i_type == SORT_REVERSE && i_test > 0 ) ) + if( pp_items[i]->i_children == -1 && + pp_items[i_small]->i_children >= 0 ) + { + i_test = 1; + } + else if( pp_items[i]->i_children >= 0 && + pp_items[i_small]->i_children == -1 ) + { + i_test = -1; + } + else + { + i_test = strcasecmp( pp_items[i]->p_input->psz_name, + pp_items[i_small]->p_input->psz_name ); + } + } + + if( ( i_type == ORDER_NORMAL && i_test < 0 ) || + ( i_type == ORDER_REVERSE && i_test > 0 ) ) { i_small = i; } } - /* Keep the correct current index */ - if( i_small == p_playlist->i_index ) - p_playlist->i_index = i_position; - else if( i_position == p_playlist->i_index ) - p_playlist->i_index = i_small; - - p_temp = p_playlist->pp_items[i_position]; - p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small]; - p_playlist->pp_items[i_small] = p_temp; + p_temp = pp_items[i_position]; + pp_items[i_position] = pp_items[i_small]; + pp_items[i_small] = p_temp; } - vlc_mutex_unlock( &p_playlist->object_lock ); - - /* Notify the interfaces */ - var_Set( p_playlist, "intf-change", val ); +#undef DO_META_SORT +#undef DO_META_SORT_ADV - return 0; + return VLC_SUCCESS; }