]> git.sesse.net Git - vlc/blob - src/playlist/sort.c
* Merged sort functions
[vlc] / src / playlist / sort.c
1 /*****************************************************************************
2  * sort.c : Playlist sorting functions
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: sort.c,v 1.2 2003/11/26 10:45:21 zorglub 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_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM
37  * \param i_type: SORT_NORMAL or SORT_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
45     vlc_mutex_lock( &p_playlist->object_lock );
46
47     if( i_mode == SORT_RANDOM )
48     {
49         for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ )
50         {
51             int i_new  = rand() % (p_playlist->i_size - 1);
52
53             /* Keep the correct current index */
54             if( i_new == p_playlist->i_index )
55                 p_playlist->i_index = i_position;
56             else if( i_position == p_playlist->i_index )
57                 p_playlist->i_index = i_new;
58
59             p_temp = p_playlist->pp_items[i_position];
60             p_playlist->pp_items[i_position] = p_playlist->pp_items[i_new];
61             p_playlist->pp_items[i_new] = p_temp;
62         }
63         vlc_mutex_unlock( &p_playlist->object_lock );
64
65         return 0;
66     }
67
68     for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
69     {
70         i_small  = i_position;
71         for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
72         {
73             int i_test = 0;
74
75             if( i_mode == SORT_TITLE )
76             {
77                 i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
78                                  p_playlist->pp_items[i_small]->psz_name );
79             }
80             else if( i_mode == SORT_GROUP )
81             {
82                 i_test = p_playlist->pp_items[i]->i_group -
83                                  p_playlist->pp_items[i_small]->i_group;
84             }
85             else if( i_mode == SORT_AUTHOR )
86             {
87                  i_test = strcasecmp( p_playlist->pp_items[i]->psz_author,
88                                  p_playlist->pp_items[i_small]->psz_author );
89             }
90
91             if( ( i_type == SORT_NORMAL  && i_test < 0 ) ||
92                 ( i_type == SORT_REVERSE && i_test > 0 ) )
93             {
94                 i_small = i;
95             }
96         }
97         /* Keep the correct current index */
98         if( i_small == p_playlist->i_index )
99             p_playlist->i_index = i_position;
100         else if( i_position == p_playlist->i_index )
101             p_playlist->i_index = i_small;
102
103         p_temp = p_playlist->pp_items[i_position];
104         p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
105         p_playlist->pp_items[i_small] = p_temp;
106     }
107     vlc_mutex_unlock( &p_playlist->object_lock );
108
109     return 0;
110 }