]> git.sesse.net Git - vlc/blob - src/playlist/sort.c
quit fast when sorting a playlist with 1 or less entries. Fixes a crash when
[vlc] / src / playlist / sort.c
1 /*****************************************************************************
2  * sort.c : Playlist sorting functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29 #include <vlc/vout.h>
30 #include <vlc/sout.h>
31
32 #include "vlc_playlist.h"
33
34 /**
35  * Sort the playlist
36  * \param p_playlist the playlist
37  * \param i_mode: SORT_ID, SORT_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM
38  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
39  * \return VLC_SUCCESS on success
40  */
41 int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
42 {
43     int i , i_small , i_position;
44     playlist_item_t *p_temp;
45     vlc_value_t val;
46     val.b_bool = VLC_TRUE;
47
48     vlc_mutex_lock( &p_playlist->object_lock );
49
50     p_playlist->i_sort = i_mode;
51     p_playlist->i_order = i_type;
52     /* playlist with one or less items are allways sorted in all
53        manners, quit fast. */
54     if( p_playlist->i_size <= 1 )
55     {
56         vlc_mutex_unlock( &p_playlist->object_lock );
57
58         /* Notify the interfaces, is this necessary? */
59         var_Set( p_playlist, "intf-change", val );
60
61         return VLC_SUCCESS;
62     }
63
64     if( i_mode == SORT_RANDOM )
65     {
66         for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ )
67         {
68             int i_new  = rand() % (p_playlist->i_size - 1);
69
70             /* Keep the correct current index */
71             if( i_new == p_playlist->i_index )
72                 p_playlist->i_index = i_position;
73             else if( i_position == p_playlist->i_index )
74                 p_playlist->i_index = i_new;
75
76             p_temp = p_playlist->pp_items[i_position];
77             p_playlist->pp_items[i_position] = p_playlist->pp_items[i_new];
78             p_playlist->pp_items[i_new] = p_temp;
79         }
80         vlc_mutex_unlock( &p_playlist->object_lock );
81
82         /* Notify the interfaces */
83         var_Set( p_playlist, "intf-change", val );
84
85         return VLC_SUCCESS;
86     }
87
88     for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
89     {
90         i_small  = i_position;
91         for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
92         {
93             int i_test = 0;
94
95             if( i_mode == SORT_ID )
96             {
97                 i_test = p_playlist->pp_items[i]->i_id -
98                                  p_playlist->pp_items[i_small]->i_id;
99             }
100             else if( i_mode == SORT_TITLE )
101             {
102                 i_test = strcasecmp( p_playlist->pp_items[i]->input.psz_name,
103                              p_playlist->pp_items[i_small]->input.psz_name );
104             }
105             else if( i_mode == SORT_GROUP )
106             {
107                 i_test = p_playlist->pp_items[i]->i_group -
108                              p_playlist->pp_items[i_small]->i_group;
109             }
110             else if( i_mode == SORT_DURATION )
111             {
112                 i_test = p_playlist->pp_items[i]->input.i_duration -
113                              p_playlist->pp_items[i_small]->input.i_duration;
114             }
115             else if( i_mode == SORT_AUTHOR )
116             {
117                  i_test = strcasecmp(
118                           playlist_GetInfo( p_playlist, i,
119                                             _("General") , _("Author") ),
120                           playlist_GetInfo( p_playlist, i_small,
121                                             _("General") , _("Author") ) );
122             }
123
124             if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
125                 ( i_type == ORDER_REVERSE && i_test > 0 ) )
126             {
127                 i_small = i;
128             }
129         }
130         /* Keep the correct current index */
131         if( i_small == p_playlist->i_index )
132             p_playlist->i_index = i_position;
133         else if( i_position == p_playlist->i_index )
134             p_playlist->i_index = i_small;
135
136         p_temp = p_playlist->pp_items[i_position];
137         p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
138         p_playlist->pp_items[i_small] = p_temp;
139     }
140     vlc_mutex_unlock( &p_playlist->object_lock );
141
142     /* Notify the interfaces  */
143     var_Set( p_playlist, "intf-change", val );
144
145     return VLC_SUCCESS;
146 }