]> git.sesse.net Git - vlc/blob - src/playlist/group.c
6ab0882e2a3385a24e32080077228c9dd8e58f48
[vlc] / src / playlist / group.c
1 /*****************************************************************************
2  * playlist.c : Playlist groups management functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
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., 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/input.h>
29
30 #include "vlc_playlist.h"
31
32 /**
33  * Create a group
34  *
35  * Create a new group
36  * \param p_playlist pointer to a playlist
37  * \param psz_name the name of the group to be created
38  * \return a pointer to the created group, or NULL on error
39  */
40 playlist_group_t *playlist_CreateGroup( playlist_t *p_playlist, char *psz_name)
41 {
42     playlist_group_t *p_group;
43     int i;
44
45     for( i = 0 ; i < p_playlist->i_groups; i++ )
46     {
47         if( !strcasecmp( p_playlist->pp_groups[i]->psz_name , psz_name ) )
48         {
49             msg_Info( p_playlist, "this group already exists");
50             return p_playlist->pp_groups[i];
51         }
52     }
53
54     /* Allocate the group structure */
55     if( ( p_group = malloc( sizeof(playlist_group_t) ) ) == NULL )
56     {
57         msg_Err( p_playlist, "out of memory" );
58         return NULL;
59     }
60
61     p_group->psz_name = strdup( psz_name );
62     p_group->i_id = ++p_playlist->i_last_group;
63
64     msg_Dbg(p_playlist,"creating group %s with id %i at position %i",
65                      p_group->psz_name,
66                      p_group->i_id,
67                      p_playlist->i_groups);
68
69     INSERT_ELEM ( p_playlist->pp_groups,
70                   p_playlist->i_groups,
71                   p_playlist->i_groups,
72                   p_group );
73
74     return p_group;
75 }
76
77 /**
78  * Destroy a group
79  *
80  * \param p_playlist the playlist to remove the group from
81  * \param i_id the identifier of the group to remove
82  * \return VLC_SUCCESS
83  */
84 int playlist_DeleteGroup( playlist_t *p_playlist, int i_id )
85 {
86     int i;
87
88     for( i=0 ; i<= p_playlist->i_groups; i++ )
89     {
90         playlist_group_t *p_group = p_playlist->pp_groups[i];
91
92         if( p_group->i_id == i_id )
93         {
94
95             if( p_group->psz_name )
96             {
97                 free( p_group->psz_name );
98             }
99             REMOVE_ELEM( p_playlist->pp_groups,
100                          p_playlist->i_groups,
101                          i );
102             free( p_group );
103
104             return VLC_SUCCESS;
105         }
106     }
107     return VLC_SUCCESS;
108 }
109
110 /**
111  * Find the name of the group given its ID
112  *
113  * \param p_playlist the playlist where to find the group
114  * \param i_id the ID to search for
115  * \return the name of the group
116  */
117 char *playlist_FindGroup( playlist_t *p_playlist, int i_id )
118 {
119     int i;
120     for( i=0 ; i< p_playlist->i_groups; i++ )
121     {
122         if( p_playlist->pp_groups[i]->i_id == i_id )
123         {
124             if( p_playlist->pp_groups[i]->psz_name)
125                 return strdup( p_playlist->pp_groups[i]->psz_name );
126         }
127     }
128     return NULL;
129 }
130
131 /**
132  * Find the id of a group given its name
133  *
134  * \param p_playlist the playlist where to find the group
135  * \param psz_name the name to search for
136  * \return the id of the group
137  */
138 int playlist_GroupToId( playlist_t *p_playlist, char *psz_name )
139 {
140     int i;
141     for( i = 0 ; i< p_playlist->i_groups; i++ )
142     {
143         if( p_playlist->pp_groups[i]->psz_name)
144         {
145             if( ! strcasecmp( p_playlist->pp_groups[i]->psz_name, psz_name ) )
146             {
147                 return p_playlist->pp_groups[i]->i_id;
148             }
149         }
150     }
151     return VLC_SUCCESS;
152 }