]> git.sesse.net Git - vlc/blob - src/playlist/sort.c
Fix sort by artist
[vlc] / src / playlist / sort.c
1 /*****************************************************************************
2  * sort.c : Playlist sorting functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 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 <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 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
36                 playlist_item_t **pp_items, int i_mode,
37                 int i_type );
38
39 /**
40  * Sort a node.
41  *
42  * This function must be entered with the playlist lock !
43  *
44  * \param p_playlist the playlist
45  * \param p_node the node to sort
46  * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM
47  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
48  * \return VLC_SUCCESS on success
49  */
50 int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
51                        int i_mode, int i_type )
52 {
53     playlist_ItemArraySort( p_playlist,p_node->i_children,
54                             p_node->pp_children, i_mode, i_type );
55     return VLC_SUCCESS;
56 }
57
58 /**
59  * Sort a node recursively.
60  *
61  * This function must be entered with the playlist lock !
62  *
63  * \param p_playlist the playlist
64  * \param p_node the node to sort
65  * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM
66  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
67  * \return VLC_SUCCESS on success
68  */
69 int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
70                                 int i_mode, int i_type )
71 {
72     int i;
73     playlist_NodeSort( p_playlist, p_node, i_mode, i_type );
74     for( i = 0 ; i< p_node->i_children; i++ )
75     {
76         if( p_node->pp_children[i]->i_children != -1 )
77         {
78             playlist_RecursiveNodeSort( p_playlist, p_node->pp_children[i],
79                                         i_mode,i_type );
80         }
81     }
82     return VLC_SUCCESS;
83 }
84
85
86 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
87                             playlist_item_t **pp_items, int i_mode,
88                             int i_type )
89 {
90     int i , i_small , i_position;
91     playlist_item_t *p_temp;
92     vlc_value_t val;
93     val.b_bool = VLC_TRUE;
94
95     if( i_mode == SORT_RANDOM )
96     {
97         for( i_position = 0; i_position < i_items ; i_position ++ )
98         {
99             int i_new;
100
101             if( i_items > 1 )
102                 i_new = rand() % (i_items - 1);
103             else
104                 i_new = 0;
105             p_temp = pp_items[i_position];
106             pp_items[i_position] = pp_items[i_new];
107             pp_items[i_new] = p_temp;
108         }
109
110         return VLC_SUCCESS;
111     }
112
113 #define DO_META_SORT( node ) { \
114     char *psz_a = pp_items[i]->p_input->p_meta ?  \
115                        pp_items[i]->p_input->p_meta->psz_##node : NULL ; \
116     char *psz_b = pp_items[i_small]->p_input->p_meta ?  \
117                        pp_items[i_small]->p_input->p_meta->psz_##node : NULL; \
118     /* Nodes go first */ \
119     if( pp_items[i]->i_children == -1 && pp_items[i_small]->i_children >= 0 ) \
120         i_test = 1;\
121     else if( pp_items[i]->i_children >= 0 &&\
122              pp_items[i_small]->i_children == -1 ) \
123        i_test = -1; \
124     /* Both are nodes, sort by name */ \
125     else if( pp_items[i]->i_children >= 0 && \
126                pp_items[i_small]->i_children >= 0 ) \
127     { \
128          i_test = strcasecmp( pp_items[i]->p_input->psz_name, \
129                               pp_items[i_small]->p_input->psz_name ); \
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         i_test = strcasecmp( pp_items[i]->p_input->psz_name, \
140                              pp_items[i_small]->p_input->psz_name ); \
141     } \
142     else \
143     { \
144         i_test = strcmp( psz_b, psz_a ); \
145     } \
146 }
147
148     for( i_position = 0; i_position < i_items -1 ; i_position ++ )
149     {
150         i_small  = i_position;
151         for( i = i_position + 1 ; i< i_items ; i++)
152         {
153             int i_test = 0;
154
155             if( i_mode == SORT_TITLE )
156             {
157                 i_test = strcasecmp( pp_items[i]->p_input->psz_name,
158                                      pp_items[i_small]->p_input->psz_name );
159             }
160             else if( i_mode == SORT_TITLE_NUMERIC )
161             {
162                 i_test = atoi( pp_items[i]->p_input->psz_name ) -
163                          atoi( pp_items[i_small]->p_input->psz_name );
164             }
165             else if( i_mode == SORT_DURATION )
166             {
167                 i_test = pp_items[i]->p_input->i_duration -
168                              pp_items[i_small]->p_input->i_duration;
169             }
170             else if( i_mode == SORT_ARTIST )
171             {
172                 DO_META_SORT( artist );
173             }
174             else if( i_mode == SORT_ALBUM )
175             {
176                 DO_META_SORT( album );
177             }
178             else if( i_mode == SORT_TITLE_NODES_FIRST )
179             {
180                 /* Alphabetic sort, all nodes first */
181
182                 if( pp_items[i]->i_children == -1 &&
183                     pp_items[i_small]->i_children >= 0 )
184                 {
185                     i_test = 1;
186                 }
187                 else if( pp_items[i]->i_children >= 0 &&
188                          pp_items[i_small]->i_children == -1 )
189                 {
190                     i_test = -1;
191                 }
192                 else
193                 {
194                     i_test = strcasecmp( pp_items[i]->p_input->psz_name,
195                                          pp_items[i_small]->p_input->psz_name );
196                 }
197             }
198
199             if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
200                 ( i_type == ORDER_REVERSE && i_test > 0 ) )
201             {
202                 i_small = i;
203             }
204         }
205         p_temp = pp_items[i_position];
206         pp_items[i_position] = pp_items[i_small];
207         pp_items[i_small] = p_temp;
208     }
209     return VLC_SUCCESS;
210 }