1 /*****************************************************************************
2 * sort.c : Playlist sorting functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include <vlc/input.h>
25 #include "vlc_playlist.h"
26 #include "playlist_internal.h"
29 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
30 playlist_item_t **pp_items, int i_mode,
36 * This function must be entered with the playlist lock !
38 * \param p_playlist the playlist
39 * \param p_node the node to sort
40 * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM
41 * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
42 * \return VLC_SUCCESS on success
44 int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
45 int i_mode, int i_type )
47 playlist_ItemArraySort( p_playlist,p_node->i_children,
48 p_node->pp_children, i_mode, i_type );
53 * Sort a node recursively.
55 * This function must be entered with the playlist lock !
57 * \param p_playlist the playlist
58 * \param p_node the node to sort
59 * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM
60 * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
61 * \return VLC_SUCCESS on success
63 int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
64 int i_mode, int i_type )
67 playlist_NodeSort( p_playlist, p_node, i_mode, i_type );
68 for( i = 0 ; i< p_node->i_children; i++ )
70 if( p_node->pp_children[i]->i_children != -1 )
72 playlist_RecursiveNodeSort( p_playlist, p_node->pp_children[i],
80 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
81 playlist_item_t **pp_items, int i_mode,
84 int i , i_small , i_position;
85 playlist_item_t *p_temp;
87 val.b_bool = VLC_TRUE;
89 if( i_mode == SORT_RANDOM )
91 for( i_position = 0; i_position < i_items ; i_position ++ )
96 i_new = rand() % (i_items - 1);
99 p_temp = pp_items[i_position];
100 pp_items[i_position] = pp_items[i_new];
101 pp_items[i_new] = p_temp;
107 #define DO_META_SORT( node ) { \
108 char *psz_a = pp_items[i]->p_input->p_meta ? \
109 pp_items[i]->p_input->p_meta->psz_##node : NULL ; \
110 char *psz_b = pp_items[i_small]->p_input->p_meta ? \
111 pp_items[i_small]->p_input->p_meta->psz_##node : NULL; \
112 /* Nodes go first */ \
113 if( pp_items[i]->i_children == -1 && pp_items[i_small]->i_children >= 0 ) \
115 else if( pp_items[i]->i_children >= 0 &&\
116 pp_items[i_small]->i_children == -1 ) \
118 /* Both are nodes, sort by name */ \
119 else if( pp_items[i]->i_children >= 0 && \
120 pp_items[i_small]->i_children >= 0 ) \
122 i_test = strcasecmp( pp_items[i]->p_input->psz_name, \
123 pp_items[i_small]->p_input->psz_name ); \
125 /* Both are items */ \
126 else if( psz_a == NULL && psz_b != NULL ) \
128 else if( psz_a != NULL && psz_b == NULL ) \
130 /* No meta, sort by name */ \
131 else if( psz_a == NULL && psz_b == NULL ) \
133 i_test = strcasecmp( pp_items[i]->p_input->psz_name, \
134 pp_items[i_small]->p_input->psz_name ); \
138 i_test = strcmp( psz_b, psz_a ); \
142 for( i_position = 0; i_position < i_items -1 ; i_position ++ )
144 i_small = i_position;
145 for( i = i_position + 1 ; i< i_items ; i++)
149 if( i_mode == SORT_TITLE )
151 i_test = strcasecmp( pp_items[i]->p_input->psz_name,
152 pp_items[i_small]->p_input->psz_name );
154 else if( i_mode == SORT_TITLE_NUMERIC )
156 i_test = atoi( pp_items[i]->p_input->psz_name ) -
157 atoi( pp_items[i_small]->p_input->psz_name );
159 else if( i_mode == SORT_DURATION )
161 i_test = pp_items[i]->p_input->i_duration -
162 pp_items[i_small]->p_input->i_duration;
164 else if( i_mode == SORT_ARTIST )
166 DO_META_SORT( artist );
168 else if( i_mode == SORT_ALBUM )
170 DO_META_SORT( album );
172 else if( i_mode == SORT_TITLE_NODES_FIRST )
174 /* Alphabetic sort, all nodes first */
176 if( pp_items[i]->i_children == -1 &&
177 pp_items[i_small]->i_children >= 0 )
181 else if( pp_items[i]->i_children >= 0 &&
182 pp_items[i_small]->i_children == -1 )
188 i_test = strcasecmp( pp_items[i]->p_input->psz_name,
189 pp_items[i_small]->p_input->psz_name );
193 if( ( i_type == ORDER_NORMAL && i_test < 0 ) ||
194 ( i_type == ORDER_REVERSE && i_test > 0 ) )
199 p_temp = pp_items[i_position];
200 pp_items[i_position] = pp_items[i_small];
201 pp_items[i_small] = p_temp;