1 /*****************************************************************************
2 * sort.c : Playlist sorting functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: sort.c,v 1.6 2004/01/10 03:36:03 hartman Exp $
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
23 #include <stdlib.h> /* free(), strtol() */
24 #include <stdio.h> /* sprintf() */
25 #include <string.h> /* strerror() */
31 #include "vlc_playlist.h"
35 * \param p_playlist the playlist
36 * \param i_mode: SORT_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM SORT_ID
37 * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
38 * \return 0 on success
40 int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
42 int i , i_small , i_position;
43 playlist_item_t *p_temp;
45 val.b_bool = VLC_TRUE;
47 vlc_mutex_lock( &p_playlist->object_lock );
49 if( i_mode == SORT_RANDOM )
51 for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ )
53 int i_new = rand() % (p_playlist->i_size - 1);
55 /* Keep the correct current index */
56 if( i_new == p_playlist->i_index )
57 p_playlist->i_index = i_position;
58 else if( i_position == p_playlist->i_index )
59 p_playlist->i_index = i_new;
61 p_temp = p_playlist->pp_items[i_position];
62 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_new];
63 p_playlist->pp_items[i_new] = p_temp;
65 vlc_mutex_unlock( &p_playlist->object_lock );
67 /* Notify the interfaces */
68 var_Set( p_playlist, "intf-change", val );
73 for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
76 for( i = i_position + 1 ; i< p_playlist->i_size ; i++)
80 if( i_mode == SORT_TITLE )
82 i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
83 p_playlist->pp_items[i_small]->psz_name );
85 else if( i_mode == SORT_GROUP )
87 i_test = p_playlist->pp_items[i]->i_group -
88 p_playlist->pp_items[i_small]->i_group;
90 else if( i_mode == SORT_AUTHOR )
93 playlist_GetInfo( p_playlist, i,
94 _("General") , _("Author") ),
95 playlist_GetInfo( p_playlist, i_small,
96 _("General") , _("Author") ) );
99 if( ( i_type == ORDER_NORMAL && i_test < 0 ) ||
100 ( i_type == ORDER_REVERSE && i_test > 0 ) )
105 /* Keep the correct current index */
106 if( i_small == p_playlist->i_index )
107 p_playlist->i_index = i_position;
108 else if( i_position == p_playlist->i_index )
109 p_playlist->i_index = i_small;
111 p_temp = p_playlist->pp_items[i_position];
112 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
113 p_playlist->pp_items[i_small] = p_temp;
115 vlc_mutex_unlock( &p_playlist->object_lock );
117 /* Notify the interfaces */
118 var_Set( p_playlist, "intf-change", val );