1 /*****************************************************************************
2 * sort.c : Playlist sorting functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: sort.c,v 1.1 2003/10/29 18:00:46 zorglub Exp $
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., 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() */
31 #include "vlc_playlist.h"
34 * Sort the playlist by title
35 * \param p_playlist the playlist
36 * \param i_type: SORT_NORMAL or SORT_REVERSE (reversed order)
37 * \return 0 on success
39 int playlist_SortTitle( playlist_t * p_playlist , int i_type )
41 int i , i_small , i_position;
42 playlist_item_t *p_temp;
44 vlc_mutex_lock( &p_playlist->object_lock );
46 for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
49 for( i = i_position + 1 ; i< p_playlist->i_size ; i++)
53 i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
54 p_playlist->pp_items[i_small]->psz_name );
56 if( ( i_type == SORT_NORMAL && i_test < 0 ) ||
57 ( i_type == SORT_REVERSE && i_test > 0 ) )
62 /* Keep the correct current index */
63 if( i_small == p_playlist->i_index )
64 p_playlist->i_index = i_position;
65 else if( i_position == p_playlist->i_index )
66 p_playlist->i_index = i_small;
68 p_temp = p_playlist->pp_items[i_position];
69 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
70 p_playlist->pp_items[i_small] = p_temp;
72 vlc_mutex_unlock( &p_playlist->object_lock );
78 * Sort the playlist by author
79 * \param p_playlist the playlist
80 * \param i_type: SORT_NORMAL or SORT_REVERSE (reversed order)
81 * \return 0 on success
83 int playlist_SortAuthor( playlist_t * p_playlist , int i_type )
85 int i , i_small , i_position;
86 playlist_item_t *p_temp;
88 vlc_mutex_lock( &p_playlist->object_lock );
90 for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
93 for( i = i_position + 1 ; i< p_playlist->i_size ; i++)
97 i_test = strcasecmp( p_playlist->pp_items[i]->psz_author,
98 p_playlist->pp_items[i_small]->psz_author );
100 if( ( i_type == SORT_NORMAL && i_test < 0 ) ||
101 ( i_type == SORT_REVERSE && i_test > 0 ) )
106 /* Keep the correct current index */
107 if( i_small == p_playlist->i_index )
108 p_playlist->i_index = i_position;
109 else if( i_position == p_playlist->i_index )
110 p_playlist->i_index = i_small;
112 p_temp = p_playlist->pp_items[i_position];
113 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
114 p_playlist->pp_items[i_small] = p_temp;
116 vlc_mutex_unlock( &p_playlist->object_lock );
121 int playlist_SortGroup( playlist_t * p_playlist , int i_type )
123 int i , i_small , i_position;
124 playlist_item_t *p_temp;
126 vlc_mutex_lock( &p_playlist->object_lock );
128 for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
130 i_small = i_position;
131 for( i = i_position + 1 ; i< p_playlist->i_size ; i++)
135 i_test = p_playlist->pp_items[i]->i_group -
136 p_playlist->pp_items[i_small]->i_group;
138 if( ( i_type == SORT_NORMAL && i_test < 0 ) ||
139 ( i_type == SORT_REVERSE && i_test > 0 ) )
144 /* Keep the correct current index */
145 if( i_small == p_playlist->i_index )
146 p_playlist->i_index = i_position;
147 else if( i_position == p_playlist->i_index )
148 p_playlist->i_index = i_small;
150 p_temp = p_playlist->pp_items[i_position];
151 p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
152 p_playlist->pp_items[i_small] = p_temp;
154 vlc_mutex_unlock( &p_playlist->object_lock );