1 /*****************************************************************************
2 * info.c : Playlist info management
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: info.c,v 1.8 2004/01/29 17:51:08 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 * Get one special info
35 * Must be entered with playlist lock
37 * \param p_playlist the playlist to get the info from
38 * \param i_item position of the item on
39 * which we want the info ( -1 for current )
40 * \param psz_cat the category in which the info is stored
41 * \param psz_name the name of the info
42 * \return the info value if any, an empty string else
44 char * playlist_GetInfo( playlist_t *p_playlist, int i_pos,
45 const char * psz_cat, const char *psz_name )
47 playlist_item_t *p_item;
49 /* Check the existence of the playlist */
50 if( p_playlist == NULL)
54 p_item = playlist_ItemGetByPos( p_playlist, i_pos );
59 vlc_mutex_lock( &p_item->lock );
60 psz_buffer = playlist_ItemGetInfo( p_item ,
62 vlc_mutex_unlock( &p_item->lock );
68 * Get one special info, from an item (no need for p_playlist)
70 * \param p_item the item on which we want the info
71 * \param psz_cat the category in which the info is stored
72 * \param psz_name the name of the info
73 * \return the info value if any, an empty string else
75 char * playlist_ItemGetInfo( playlist_item_t *p_item,
76 const char * psz_cat, const char *psz_name )
79 for( i = 0 ; i< p_item->i_categories ; i++ )
81 if( !strcmp( p_item->pp_categories[i]->psz_name , psz_cat ) )
83 for( j = 0 ; j< p_item->pp_categories[i]->i_infos ; j++ )
85 if( !strcmp( p_item->pp_categories[i]->pp_infos[j]->psz_name,
89 strdup(p_item->pp_categories[i]->pp_infos[j]->psz_value );
98 * Get one info category. Create it if it does not exist
100 * \param p_playlist the playlist to get the category from
101 * \param i_item the position of the item on which we want
102 * the info ( -1 for current )
103 * \param psz_cat the category we want
104 * \return the info category.
106 item_info_category_t *
107 playlist_GetCategory( playlist_t *p_playlist, int i_pos,
108 const char * psz_cat )
110 playlist_item_t *p_item;
111 /* Check the existence of the playlist */
112 if( p_playlist == NULL)
116 p_item= playlist_ItemGetByPos( p_playlist , i_pos );
121 return playlist_ItemGetCategory( p_item , psz_cat );
125 * Get one info category (no p_playlist). Create it if it does not exist
127 * \param p_item the playlist item to get the category from
128 * \param psz_cat the category we want
129 * \return the info category.
131 item_info_category_t *playlist_ItemGetCategory( playlist_item_t *p_item,
132 const char *psz_cat )
135 /* Search the category */
136 for( i = 0 ; i< p_item->i_categories ; i++ )
138 if( !strncmp( p_item->pp_categories[i]->psz_name , psz_cat,
141 return p_item->pp_categories[i];
145 /* We did not find the category, create it */
146 return playlist_ItemCreateCategory( p_item, psz_cat );
151 * Create one info category.
153 * \param p_playlist the playlist
154 * \param i_item the position of the item for which we create
155 * the category ( -1 for current )
156 * \param psz_cat the category we want to create
157 * \return the info category.
159 item_info_category_t *
160 playlist_CreateCategory( playlist_t *p_playlist, int i_pos,
161 const char * psz_cat )
163 playlist_item_t *p_item = NULL;
165 /* Check the existence of the playlist */
166 if( p_playlist == NULL)
170 p_item = playlist_ItemGetByPos( p_playlist , i_pos );
176 return playlist_ItemCreateCategory( p_item, psz_cat );
180 * Create one info category for an item ( no p_playlist required )
182 * \param p_playlist the playlist
183 * \param p_item the item to create category for
184 * \param psz_cat the category we want to create
185 * \return the info category.
187 item_info_category_t *
188 playlist_ItemCreateCategory( playlist_item_t *p_item, const char *psz_cat )
190 item_info_category_t *p_cat;
192 for( i = 0 ; i< p_item->i_categories ; i++)
194 if( !strcmp( p_item->pp_categories[i]->psz_name,psz_cat ) )
196 return p_item->pp_categories[i];
199 if( ( p_cat = malloc( sizeof( item_info_category_t) ) ) == NULL )
204 p_cat->psz_name = strdup( psz_cat);
206 p_cat->pp_infos = NULL;
208 INSERT_ELEM( p_item->pp_categories ,
209 p_item->i_categories ,
210 p_item->i_categories ,
219 * \param p_playlist the playlist
220 * \param i_item the position of the item on which we want
221 * the info ( -1 for current )
222 * \param psz_cat the category we want to put the info into
223 * (gets created if needed)
224 * \param psz_name the name of the info
225 * \param psz_format printf-style info
226 * \return VLC_SUCCESS
228 int playlist_AddInfo( playlist_t *p_playlist, int i_item,
229 const char * psz_cat, const char *psz_name,
230 const char * psz_format, ...)
234 playlist_item_t *p_item;
237 /* Check the existence of the playlist */
238 if( p_playlist == NULL)
243 p_item = playlist_ItemGetByPos( p_playlist, i_item );
249 va_start( args, psz_format );
250 vasprintf( &psz_value, psz_format, args );
253 vlc_mutex_lock( &p_item->lock );
254 i_ret = playlist_ItemAddInfo( p_item , psz_cat , psz_name , psz_value );
255 vlc_mutex_unlock( &p_item->lock );
263 * Add info to one item ( no need for p_playlist )
265 * \param p_item the item for which we add the info
266 * \param psz_cat the category in which the info is stored
267 * \param psz_name the name of the info
268 * \param psz_format printf-style info
269 * \return VLC_SUCCESS on success
271 int playlist_ItemAddInfo( playlist_item_t *p_item,
272 const char *psz_cat, const char *psz_name,
273 const char *psz_format, ... )
277 int i_new = VLC_TRUE;
278 item_info_t *p_info = NULL;
279 item_info_category_t *p_cat;
281 /* Find or create the category */
282 p_cat = playlist_ItemGetCategory( p_item, psz_cat );
288 for( i = 0 ; i< p_cat->i_infos ; i++)
290 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
292 /* This info is not new */
293 p_info = p_cat->pp_infos[i];
299 /* New info, create it */
302 if( ( p_info = malloc( sizeof( item_info_t) ) ) == NULL )
306 p_info->psz_name = strdup( psz_name);
310 if( p_info->psz_value != NULL ) free( p_info->psz_value ) ;
313 va_start( args, psz_format );
314 vasprintf( &p_info->psz_value, psz_format, args );
317 /* If this is new, insert it */
318 if( i_new == VLC_TRUE )
320 INSERT_ELEM( p_cat->pp_infos,
330 * Add a special info : option
332 * \param p_playlist the playlist
333 * \param i_item the position of the item on which we
334 * add the option ( -1 for current )
335 * \param psz_value the option to add
336 * \return the info category.
338 int playlist_AddOption( playlist_t *p_playlist, int i_pos,
339 const char *psz_option)
341 playlist_item_t *p_item;
343 /* Check the existence of the playlist */
344 if( p_playlist == NULL)
349 p_item = playlist_ItemGetByPos( p_playlist , i_pos );
355 vlc_mutex_lock( &p_item->lock );
356 INSERT_ELEM( p_item->ppsz_options,
359 (char *)psz_option );
360 vlc_mutex_unlock( &p_item->lock );
366 * Add a option to one item ( no need for p_playlist )
368 * \param p_item the item on which we want the info
369 * \param psz_format the option
370 * \return 0 on success
372 int playlist_ItemAddOption( playlist_item_t *p_item,
373 const char *psz_option )
375 INSERT_ELEM( p_item->ppsz_options,