]> git.sesse.net Git - vlc/blob - src/playlist/sort.c
32c630dd1bc9268182b0cf7de5a96c663d1c58e9
[vlc] / src / playlist / sort.c
1 /*****************************************************************************
2  * sort.c : Playlist sorting functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #include <vlc/vlc.h>
24 #include "vlc_playlist.h"
25 #include "playlist_internal.h"
26
27
28 static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
29                                    playlist_item_t **pp_items, int i_mode,
30                                    int i_type );
31
32 /**
33  * Sort a node.
34  * This function must be entered with the playlist lock !
35  *
36  * \param p_playlist the playlist
37  * \param p_node the node to sort
38  * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM
39  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
40  * \return VLC_SUCCESS on success
41  */
42 static int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
43                               int i_mode, int i_type )
44 {
45     playlist_ItemArraySort( p_playlist,p_node->i_children,
46                             p_node->pp_children, i_mode, i_type );
47     return VLC_SUCCESS;
48 }
49
50 /**
51  * Sort a node recursively.
52  *
53  * This function must be entered with the playlist lock !
54  *
55  * \param p_playlist the playlist
56  * \param p_node the node to sort
57  * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM
58  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
59  * \return VLC_SUCCESS on success
60  */
61 int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
62                                 int i_mode, int i_type )
63 {
64     int i;
65     playlist_NodeSort( p_playlist, p_node, i_mode, i_type );
66     for( i = 0 ; i< p_node->i_children; i++ )
67     {
68         if( p_node->pp_children[i]->i_children != -1 )
69         {
70             playlist_RecursiveNodeSort( p_playlist, p_node->pp_children[i],
71                                         i_mode,i_type );
72         }
73     }
74     return VLC_SUCCESS;
75 }
76
77
78 static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
79                                    playlist_item_t **pp_items, int i_mode,
80                                    int i_type )
81 {
82     int i , i_small , i_position;
83     playlist_item_t *p_temp;
84     vlc_value_t val;
85     val.b_bool = VLC_TRUE;
86
87     (void)p_playlist; // a bit surprising we don't need p_playlist!
88
89     if( i_mode == SORT_RANDOM )
90     {
91         for( i_position = 0; i_position < i_items ; i_position ++ )
92         {
93             int i_new;
94
95             if( i_items > 1 )
96                 i_new = rand() % (i_items - 1);
97             else
98                 i_new = 0;
99             p_temp = pp_items[i_position];
100             pp_items[i_position] = pp_items[i_new];
101             pp_items[i_new] = p_temp;
102         }
103
104         return VLC_SUCCESS;
105     }
106
107 #define META_STRCASECMP_NAME( i, i_small ) { \
108     char *psz_i = input_item_GetName( pp_items[i]->p_input ); \
109     char *psz_ismall = input_item_GetName( pp_items[i_small]->p_input ); \
110     i_test = strcasecmp( psz_i, psz_ismall ); \
111     free( psz_i ); \
112     free( psz_ismall ); \
113 }    
114         
115
116 #define DO_META_SORT( node ) { \
117     char *psz_a = input_item_GetMeta( pp_items[i]->p_input, vlc_meta_##node ); \
118     char *psz_b = input_item_GetMeta( pp_items[i_small]->p_input, vlc_meta_##node ); \
119     /* Nodes go first */ \
120     if( pp_items[i]->i_children == -1 && pp_items[i_small]->i_children >= 0 ) \
121         i_test = 1;\
122     else if( pp_items[i]->i_children >= 0 &&\
123              pp_items[i_small]->i_children == -1 ) \
124        i_test = -1; \
125     /* Both are nodes, sort by name */ \
126     else if( pp_items[i]->i_children >= 0 && \
127                pp_items[i_small]->i_children >= 0 ) \
128     { \
129         META_STRCASECMP_NAME( i, i_small ) \
130     } \
131     /* Both are items */ \
132     else if( psz_a == NULL && psz_b != NULL ) \
133         i_test = 1; \
134     else if( psz_a != NULL && psz_b == NULL ) \
135         i_test = -1;\
136     /* No meta, sort by name */ \
137     else if( psz_a == NULL && psz_b == NULL ) \
138     { \
139         META_STRCASECMP_NAME( i, i_small ); \
140     } \
141     else \
142     { \
143         i_test = strcmp( psz_b, psz_a ); \
144     } \
145     free( psz_a ); \
146     free( psz_b ); \
147 }
148
149     for( i_position = 0; i_position < i_items -1 ; i_position ++ )
150     {
151         i_small  = i_position;
152         for( i = i_position + 1 ; i< i_items ; i++)
153         {
154             int i_test = 0;
155
156             if( i_mode == SORT_TITLE )
157             {
158                 META_STRCASECMP_NAME( i, i_small );
159             }
160             else if( i_mode == SORT_TITLE_NUMERIC )
161             {
162                 char *psz_i = input_item_GetName( pp_items[i]->p_input );
163                 char *psz_ismall =
164                         input_item_GetName( pp_items[i_small]->p_input );
165                 i_test = atoi( psz_i ) - atoi( psz_ismall );
166                 free( psz_i );
167                 free( psz_ismall );
168             }
169             else if( i_mode == SORT_DURATION )
170             {
171                 i_test = input_item_GetDuration( pp_items[i]->p_input ) -
172                          input_item_GetDuration( pp_items[i_small]->p_input );
173             }
174             else if( i_mode == SORT_ARTIST )
175             {
176                 DO_META_SORT( Artist );
177             }
178             else if( i_mode == SORT_ALBUM )
179             {
180                 DO_META_SORT( Album );
181             }
182             else if( i_mode == SORT_TITLE_NODES_FIRST )
183             {
184                 /* Alphabetic sort, all nodes first */
185
186                 if( pp_items[i]->i_children == -1 &&
187                     pp_items[i_small]->i_children >= 0 )
188                 {
189                     i_test = 1;
190                 }
191                 else if( pp_items[i]->i_children >= 0 &&
192                          pp_items[i_small]->i_children == -1 )
193                 {
194                     i_test = -1;
195                 }
196                 else
197                 {
198                     i_test = strcasecmp( pp_items[i]->p_input->psz_name,
199                                          pp_items[i_small]->p_input->psz_name );
200                 }
201             }
202
203             if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
204                 ( i_type == ORDER_REVERSE && i_test > 0 ) )
205             {
206                 i_small = i;
207             }
208         }
209         p_temp = pp_items[i_position];
210         pp_items[i_position] = pp_items[i_small];
211         pp_items[i_small] = p_temp;
212     }
213     return VLC_SUCCESS;
214 }