]> git.sesse.net Git - vlc/blob - src/playlist/sort.c
Grmbl, it was obvious I would forget to add the new files
[vlc] / src / playlist / sort.c
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 $
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., 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() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/sout.h>
30
31 #include "vlc_playlist.h"
32
33 /**
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
38  */
39 int playlist_SortTitle( playlist_t * p_playlist , int i_type )
40 {
41     int i , i_small , i_position;
42     playlist_item_t *p_temp;
43
44     vlc_mutex_lock( &p_playlist->object_lock );
45
46     for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
47     {
48         i_small  = i_position;
49         for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
50         {
51             int i_test;
52
53             i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
54                                  p_playlist->pp_items[i_small]->psz_name );
55
56             if( ( i_type == SORT_NORMAL  && i_test < 0 ) ||
57                 ( i_type == SORT_REVERSE && i_test > 0 ) )
58             {
59                 i_small = i;
60             }
61         }
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;
67
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;
71     }
72     vlc_mutex_unlock( &p_playlist->object_lock );
73
74     return 0;
75 }
76
77 /**
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
82  */
83 int playlist_SortAuthor( playlist_t * p_playlist , int i_type )
84 {
85     int i , i_small , i_position;
86     playlist_item_t *p_temp;
87
88     vlc_mutex_lock( &p_playlist->object_lock );
89
90     for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
91     {
92         i_small  = i_position;
93         for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
94         {
95             int i_test;
96
97             i_test = strcasecmp( p_playlist->pp_items[i]->psz_author,
98                                  p_playlist->pp_items[i_small]->psz_author );
99
100             if( ( i_type == SORT_NORMAL  && i_test < 0 ) ||
101                 ( i_type == SORT_REVERSE && i_test > 0 ) )
102             {
103                 i_small = i;
104             }
105         }
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;
111
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;
115     }
116     vlc_mutex_unlock( &p_playlist->object_lock );
117
118     return 0;
119 }
120
121 int playlist_SortGroup( playlist_t * p_playlist , int i_type )
122 {
123     int i , i_small , i_position;
124     playlist_item_t *p_temp;
125
126     vlc_mutex_lock( &p_playlist->object_lock );
127
128     for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
129     {
130         i_small  = i_position;
131         for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
132         {
133             int i_test;
134
135             i_test = p_playlist->pp_items[i]->i_group -
136                                  p_playlist->pp_items[i_small]->i_group;
137
138             if( ( i_type == SORT_NORMAL  && i_test < 0 ) ||
139                 ( i_type == SORT_REVERSE && i_test > 0 ) )
140             {
141                 i_small = i;
142             }
143         }
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;
149
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;
153     }
154     vlc_mutex_unlock( &p_playlist->object_lock );
155
156     return 0;
157 }