]> git.sesse.net Git - vlc/blob - src/playlist/sort.c
* oops, bug
[vlc] / src / playlist / sort.c
1 /*****************************************************************************
2  * sort.c : Playlist sorting functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id: sort.c,v 1.8 2004/01/10 23:44:19 hartman Exp $
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/vout.h>
29 #include <vlc/sout.h>
30
31 #include "vlc_playlist.h"
32
33 /**
34  * Sort the playlist
35  * \param p_playlist the playlist
36  * \param i_mode: SORT_ID, SORT_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM
37  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
38  * \return 0 on success
39  */
40 int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
41 {
42     int i , i_small , i_position;
43     playlist_item_t *p_temp;
44     vlc_value_t val;
45     val.b_bool = VLC_TRUE;
46
47     vlc_mutex_lock( &p_playlist->object_lock );
48
49     p_playlist->i_sort = i_mode;
50     p_playlist->i_order = i_type;
51
52     if( i_mode == SORT_RANDOM )
53     {
54         for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ )
55         {
56             int i_new  = rand() % (p_playlist->i_size - 1);
57
58             /* Keep the correct current index */
59             if( i_new == p_playlist->i_index )
60                 p_playlist->i_index = i_position;
61             else if( i_position == p_playlist->i_index )
62                 p_playlist->i_index = i_new;
63
64             p_temp = p_playlist->pp_items[i_position];
65             p_playlist->pp_items[i_position] = p_playlist->pp_items[i_new];
66             p_playlist->pp_items[i_new] = p_temp;
67         }
68         vlc_mutex_unlock( &p_playlist->object_lock );
69
70         /* Notify the interfaces */
71         var_Set( p_playlist, "intf-change", val );
72
73         return 0;
74     }
75
76     for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
77     {
78         i_small  = i_position;
79         for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
80         {
81             int i_test = 0;
82
83             if( i_mode == SORT_ID )
84             {
85                 i_test = p_playlist->pp_items[i]->i_id -
86                                  p_playlist->pp_items[i_small]->i_id;
87             }
88             else if( i_mode == SORT_TITLE )
89             {
90                 i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
91                                  p_playlist->pp_items[i_small]->psz_name );
92             }
93             else if( i_mode == SORT_GROUP )
94             {
95                 i_test = p_playlist->pp_items[i]->i_group -
96                                  p_playlist->pp_items[i_small]->i_group;
97             }
98             else if( i_mode == SORT_AUTHOR )
99             {
100                  i_test = strcasecmp(
101                           playlist_GetInfo( p_playlist, i,
102                                             _("General") , _("Author") ),
103                           playlist_GetInfo( p_playlist, i_small,
104                                             _("General") , _("Author") ) );
105             }
106
107             if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
108                 ( i_type == ORDER_REVERSE && i_test > 0 ) )
109             {
110                 i_small = i;
111             }
112         }
113         /* Keep the correct current index */
114         if( i_small == p_playlist->i_index )
115             p_playlist->i_index = i_position;
116         else if( i_position == p_playlist->i_index )
117             p_playlist->i_index = i_small;
118
119         p_temp = p_playlist->pp_items[i_position];
120         p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
121         p_playlist->pp_items[i_small] = p_temp;
122     }
123     vlc_mutex_unlock( &p_playlist->object_lock );
124
125     /* Notify the interfaces  */
126     var_Set( p_playlist, "intf-change", val );
127
128     return 0;
129 }