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 *****************************************************************************/
23 #include <stdlib.h> /* free(), strtol() */
24 #include <stdio.h> /* sprintf() */
25 #include <string.h> /* strerror() */
28 #include <vlc/input.h>
32 #include "vlc_playlist.h"
35 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
36 playlist_item_t **pp_items, int i_mode,
42 * This function must be entered with the playlist lock !
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
50 int playlist_NodeSort( playlist_t * p_playlist , playlist_item_t *p_node,
51 int i_mode, int i_type )
53 playlist_ItemArraySort( p_playlist,p_node->i_children,
54 p_node->pp_children, i_mode, i_type );
59 * Sort a node recursively.
61 * This function must be entered with the playlist lock !
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
69 int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
70 int i_mode, int i_type )
73 playlist_NodeSort( p_playlist, p_node, i_mode, i_type );
74 for( i = 0 ; i< p_node->i_children; i++ )
76 if( p_node->pp_children[i]->i_children != -1 )
78 playlist_RecursiveNodeSort( p_playlist, p_node->pp_children[i],
86 int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
87 playlist_item_t **pp_items, int i_mode,
90 int i , i_small , i_position;
91 playlist_item_t *p_temp;
93 val.b_bool = VLC_TRUE;
95 if( i_mode == SORT_RANDOM )
97 for( i_position = 0; i_position < i_items ; i_position ++ )
102 i_new = rand() % (i_items - 1);
105 p_temp = pp_items[i_position];
106 pp_items[i_position] = pp_items[i_new];
107 pp_items[i_new] = p_temp;
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 ) \
121 else if( pp_items[i]->i_children >= 0 &&\
122 pp_items[i_small]->i_children == -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 ) \
128 i_test = strcasecmp( pp_items[i]->p_input->psz_name, \
129 pp_items[i_small]->p_input->psz_name ); \
131 /* Both are items */ \
132 else if( psz_a == NULL && psz_b != NULL ) \
134 else if( psz_a != NULL && psz_b == NULL ) \
136 /* No meta, sort by name */ \
137 else if( psz_a == NULL && psz_b == NULL ) \
139 i_test = strcasecmp( pp_items[i]->p_input->psz_name, \
140 pp_items[i_small]->p_input->psz_name ); \
144 i_test = strcmp( psz_b, psz_a ); \
148 for( i_position = 0; i_position < i_items -1 ; i_position ++ )
150 i_small = i_position;
151 for( i = i_position + 1 ; i< i_items ; i++)
155 if( i_mode == SORT_TITLE )
157 i_test = strcasecmp( pp_items[i]->p_input->psz_name,
158 pp_items[i_small]->p_input->psz_name );
160 else if( i_mode == SORT_TITLE_NUMERIC )
162 i_test = atoi( pp_items[i]->p_input->psz_name ) -
163 atoi( pp_items[i_small]->p_input->psz_name );
165 else if( i_mode == SORT_DURATION )
167 i_test = pp_items[i]->p_input->i_duration -
168 pp_items[i_small]->p_input->i_duration;
170 else if( i_mode == SORT_ARTIST )
172 DO_META_SORT( artist );
174 else if( i_mode == SORT_ALBUM )
176 DO_META_SORT( album );
178 else if( i_mode == SORT_TITLE_NODES_FIRST )
180 /* Alphabetic sort, all nodes first */
182 if( pp_items[i]->i_children == -1 &&
183 pp_items[i_small]->i_children >= 0 )
187 else if( pp_items[i]->i_children >= 0 &&
188 pp_items[i_small]->i_children == -1 )
194 i_test = strcasecmp( pp_items[i]->p_input->psz_name,
195 pp_items[i_small]->p_input->psz_name );
199 if( ( i_type == ORDER_NORMAL && i_test < 0 ) ||
200 ( i_type == ORDER_REVERSE && i_test > 0 ) )
205 p_temp = pp_items[i_position];
206 pp_items[i_position] = pp_items[i_small];
207 pp_items[i_small] = p_temp;