X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fplaylist%2Fsort.c;h=b2db64bd3f8fbf89ca13cecb195dc4215b44295a;hb=6ee1e193fd896ab9a4729fde14f009d9ce629815;hp=6d6e25f85bfcc29f10b8bd3e3a0f91ec6ff3c1ed;hpb=ed0b72e3714ad87cb4e10b9a7239e19b9ef0724e;p=vlc diff --git a/src/playlist/sort.c b/src/playlist/sort.c index 6d6e25f85b..b2db64bd3f 100644 --- a/src/playlist/sort.c +++ b/src/playlist/sort.c @@ -1,7 +1,7 @@ /***************************************************************************** * sort.c : Playlist sorting functions ***************************************************************************** - * Copyright (C) 1999-2004 the VideoLAN team + * Copyright (C) 1999-2007 the VideoLAN team * $Id$ * * Authors: Clément Stenac @@ -20,52 +20,41 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ -#include /* free(), strtol() */ -#include /* sprintf() */ -#include /* strerror() */ - #include -#include -#include -#include - #include "vlc_playlist.h" +#include "playlist_internal.h" -int playlist_ItemArraySort( playlist_t *p_playlist, int i_items, - playlist_item_t **pp_items, int i_mode, - int i_type ); +static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items, + playlist_item_t **pp_items, int i_mode, + int i_type ); /** * 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_AUTHOR, SORT_ALBUM, SORT_RANDOM + * \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_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node, - int i_mode, int i_type ) +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 p_node the node to sort - * \param i_mode: SORT_ID, SORT_TITLE, SORT_AUTHOR, SORT_ALBUM, SORT_RANDOM + * \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 */ @@ -73,7 +62,6 @@ 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++ ) { @@ -83,21 +71,21 @@ int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node, i_mode,i_type ); } } - return VLC_SUCCESS; - } -int playlist_ItemArraySort( playlist_t *p_playlist, int i_items, - playlist_item_t **pp_items, int i_mode, - int i_type ) +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; + (void)p_playlist; // a bit surprising we don't need p_playlist! + if( i_mode == SORT_RANDOM ) { for( i_position = 0; i_position < i_items ; i_position ++ ) @@ -116,6 +104,48 @@ int playlist_ItemArraySort( playlist_t *p_playlist, int i_items, return VLC_SUCCESS; } +#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( node ) { \ + 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 \ + { \ + i_test = strcmp( psz_b, psz_a ); \ + } \ + free( psz_a ); \ + free( psz_b ); \ +} + for( i_position = 0; i_position < i_items -1 ; i_position ++ ) { i_small = i_position; @@ -125,96 +155,29 @@ int playlist_ItemArraySort( playlist_t *p_playlist, int i_items, if( i_mode == SORT_TITLE ) { - i_test = strcasecmp( pp_items[i]->p_input->psz_name, - pp_items[i_small]->p_input->psz_name ); + META_STRCASECMP_NAME( i, i_small ); } else if( i_mode == SORT_TITLE_NUMERIC ) { - i_test = atoi( pp_items[i]->p_input->psz_name ) - - atoi( pp_items[i_small]->p_input->psz_name ); + 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 = pp_items[i]->p_input->i_duration - - pp_items[i_small]->p_input->i_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_AUTHOR ) + else if( i_mode == SORT_ARTIST ) { - char *psz_a = pp_items[i]->p_input->p_meta->psz_artist; - char *psz_b = pp_items[i_small]->p_input->p_meta->psz_artist; - 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 - else if( pp_items[i]->i_children >= 0 && - pp_items[i_small]->i_children >= 0 ) - { - i_test = strcasecmp( pp_items[i]->p_input->psz_name, - pp_items[i_small]->p_input->psz_name ); - } - else if( psz_a == NULL && psz_b != NULL ) - { - i_test = 1; - } - else if( psz_a != NULL && psz_b == NULL ) - { - i_test = -1; - } - else if( psz_a == NULL && psz_b == NULL ) - { - i_test = strcasecmp( pp_items[i]->p_input->psz_name, - pp_items[i_small]->p_input->psz_name ); - } - else - { - i_test = strcmp( psz_b, psz_a ); - } + DO_META_SORT( Artist ); } else if( i_mode == SORT_ALBUM ) { - char *psz_a = pp_items[i]->p_input->p_meta->psz_album; - char *psz_b = pp_items[i_small]->p_input->p_meta->psz_album; - 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 - else if( pp_items[i]->i_children >= 0 && - pp_items[i_small]->i_children >= 0 ) - { - i_test = strcasecmp( pp_items[i]->p_input->psz_name, - pp_items[i_small]->p_input->psz_name ); - } - else if( psz_a == NULL && psz_b != NULL ) - { - i_test = 1; - } - else if( psz_a != NULL && psz_b == NULL ) - { - i_test = -1; - } - else if( psz_a == NULL && psz_b == NULL ) - { - i_test = strcasecmp( pp_items[i]->p_input->psz_name, - pp_items[i_small]->p_input->psz_name ); - } - else - { - i_test = strcmp( psz_b, psz_a ); - } + DO_META_SORT( Album ); } else if( i_mode == SORT_TITLE_NODES_FIRST ) { @@ -249,73 +212,3 @@ int playlist_ItemArraySort( playlist_t *p_playlist, int i_items, } return VLC_SUCCESS; } - - -int playlist_NodeGroup( playlist_t * p_playlist , playlist_item_t *p_root, - playlist_item_t **pp_items,int i_item, - int i_mode, int i_type ) -{ - char *psz_search = NULL; - int i_nodes = 0; - playlist_item_t **pp_nodes = NULL; - playlist_item_t *p_node; - vlc_bool_t b_found; - int i,j; - for( i = 0; i< i_item ; i++ ) - { - if( psz_search ) free( psz_search ); - if( i_mode == SORT_TITLE ) - { - psz_search = strdup( pp_items[i]->p_input->psz_name ); - } - else if ( i_mode == SORT_AUTHOR ) - { - psz_search = pp_items[i]->p_input->p_meta->psz_artist; - } - else if ( i_mode == SORT_ALBUM ) - { - psz_search = pp_items[i]->p_input->p_meta->psz_album; - } - else if ( i_mode == SORT_GENRE ) - { - psz_search = pp_items[i]->p_input->p_meta->psz_genre; - } - - if( psz_search && !strcmp( psz_search, "" ) ) - { - free( psz_search ); - psz_search = strdup( _("Undefined") ); - } - - b_found = VLC_FALSE; - for( j = 0 ; j< i_nodes; j++ ) - { - if( !strcasecmp( psz_search, pp_nodes[j]->p_input->psz_name ) ) - { - playlist_NodeAppend( p_playlist, pp_items[i], pp_nodes[j] ); - b_found = VLC_TRUE; - break; - } - } - if( !b_found ) - { - p_node = playlist_NodeCreate( p_playlist, psz_search, NULL ); - INSERT_ELEM( pp_nodes, i_nodes, i_nodes, p_node ); - playlist_NodeAppend( p_playlist, pp_items[i],p_node ); - } - } - - /* Now, sort the nodes by name */ - playlist_ItemArraySort( p_playlist, i_nodes, pp_nodes, SORT_TITLE, - i_type ); - - /* Now, sort each node and append it to the root node*/ - for( i = 0 ; i< i_nodes ; i++ ) - { - playlist_ItemArraySort( p_playlist, pp_nodes[i]->i_children, - pp_nodes[i]->pp_children, SORT_TITLE, i_type ); - - playlist_NodeAppend( p_playlist, pp_nodes[i], p_root ); - } - return VLC_SUCCESS; -}