1 /*****************************************************************************
2 * media_list_path.h : Some inlined function that allows media_list_path
3 * manipulation. This is internal and used only by media_list_player.
4 *****************************************************************************
5 * Copyright (C) 2005 the VideoLAN team
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifndef _LIBVLC_MEDIA_LIST_PATH_H
26 #define _LIBVLC_MEDIA_LIST_PATH_H 1
28 typedef int * libvlc_media_list_path_t; /* (Media List Player Internal) */
30 /**************************************************************************
31 * path_empty (Media List Player Internal)
32 **************************************************************************/
33 static inline libvlc_media_list_path_t libvlc_media_list_path_empty( void )
35 libvlc_media_list_path_t ret = malloc(sizeof(int));
40 /**************************************************************************
41 * path_with_root_index (Media List Player Internal)
42 **************************************************************************/
43 static inline libvlc_media_list_path_t libvlc_media_list_path_with_root_index( int index )
45 libvlc_media_list_path_t ret = malloc(sizeof(int)*2);
51 /**************************************************************************
52 * path_depth (Media List Player Internal)
53 **************************************************************************/
54 static inline int libvlc_media_list_path_depth( libvlc_media_list_path_t path )
57 for( i = 0; path[i] != -1; i++ );
61 /**************************************************************************
62 * path_append (Media List Player Internal)
63 **************************************************************************/
64 static inline void libvlc_media_list_path_append( libvlc_media_list_path_t * p_path, int index )
66 int old_depth = libvlc_media_list_path_depth( *p_path );
67 *p_path = realloc( *p_path, sizeof(int)*(old_depth+2));
68 *p_path[old_depth] = index;
69 *p_path[old_depth+1] = -1;
72 /**************************************************************************
73 * path_copy_by_appending (Media List Player Internal)
74 **************************************************************************/
75 static inline libvlc_media_list_path_t libvlc_media_list_path_copy_by_appending( libvlc_media_list_path_t path, int index )
77 libvlc_media_list_path_t ret;
78 int old_depth = libvlc_media_list_path_depth( path );
79 ret = malloc( sizeof(int)*(old_depth+2) );
80 memcpy( ret, path, sizeof(int)*(old_depth+2) );
81 ret[old_depth] = index;
82 ret[old_depth+1] = -1;
86 /**************************************************************************
87 * path_copy (Media List Player Internal)
88 **************************************************************************/
89 static inline libvlc_media_list_path_t libvlc_media_list_path_copy( libvlc_media_list_path_t path )
91 libvlc_media_list_path_t ret;
92 int depth = libvlc_media_list_path_depth( path );
93 ret = malloc( sizeof(int)*(depth+1) );
94 memcpy( ret, path, sizeof(int)*(depth+1) );
98 /**************************************************************************
99 * get_path_rec (Media List Player Internal)
100 **************************************************************************/
101 static libvlc_media_list_path_t
102 get_path_rec( libvlc_media_list_path_t path, libvlc_media_list_t * p_current_mlist, libvlc_media_t * p_searched_md )
105 count = libvlc_media_list_count( p_current_mlist, NULL );
106 for( i = 0; i < count; i++ )
108 libvlc_media_t * p_md = libvlc_media_list_item_at_index( p_current_mlist, i, NULL );
110 if( p_md == p_searched_md )
111 return libvlc_media_list_path_copy_by_appending( path, i ); /* Found! */
113 libvlc_media_list_t * p_subitems = libvlc_media_subitems( p_md, NULL );
114 libvlc_media_release( p_md );
117 libvlc_media_list_path_t new_path = libvlc_media_list_path_copy_by_appending( path, i );
118 libvlc_media_list_lock( p_subitems );
119 libvlc_media_list_path_t ret = get_path_rec( new_path, p_subitems, p_searched_md );
120 libvlc_media_list_unlock( p_subitems );
122 libvlc_media_list_release( p_subitems );
124 return ret; /* Found in sublist! */
130 /**************************************************************************
131 * path_of_item (Media List Player Internal)
132 **************************************************************************/
133 static inline libvlc_media_list_path_t libvlc_media_list_path_of_item( libvlc_media_list_t * p_mlist, libvlc_media_t * p_md )
135 libvlc_media_list_path_t path = libvlc_media_list_path_empty();
136 libvlc_media_list_path_t ret;
137 ret = get_path_rec( path, p_mlist, p_md );
142 /**************************************************************************
143 * item_at_path (Media List Player Internal)
144 **************************************************************************/
145 static libvlc_media_t *
146 libvlc_media_list_item_at_path( libvlc_media_list_t * p_mlist, libvlc_media_list_path_t path )
148 libvlc_media_list_t * p_current_mlist = p_mlist;
149 libvlc_media_t * p_md = NULL;
151 for( i = 0; path[i] != -1; i++ )
153 p_md = libvlc_media_list_item_at_index( p_current_mlist, path[i], NULL );
155 if( p_current_mlist != p_mlist )
156 libvlc_media_list_release( p_current_mlist );
158 if( path[i+1] == -1 )
161 p_current_mlist = libvlc_media_subitems( p_md, NULL );
162 libvlc_media_release( p_md );
164 if( !p_current_mlist )
169 /* Not found, shouldn't happen if the p_path is not empty */
170 if( p_current_mlist != p_mlist )
171 libvlc_media_list_release( p_current_mlist );
175 /**************************************************************************
176 * parentlist_at_path (Media List Player Internal)
177 **************************************************************************/
178 static libvlc_media_list_t *
179 libvlc_media_list_parentlist_at_path( libvlc_media_list_t * p_mlist, libvlc_media_list_path_t path )
181 libvlc_media_list_t * p_current_mlist = p_mlist;
182 libvlc_media_t * p_md = NULL;
184 for( i = 0; path[i] != -1; i++ )
186 if( p_current_mlist != p_mlist )
187 libvlc_media_list_release( p_current_mlist );
189 if( path[i+1] == -1 )
190 return p_current_mlist;
192 p_md = libvlc_media_list_item_at_index( p_current_mlist, path[i], NULL );
194 p_current_mlist = libvlc_media_subitems( p_md, NULL );
195 libvlc_media_release( p_md );
197 if( !p_current_mlist )
202 /* Not found, shouldn't happen if the p_path is not empty */
203 if( p_current_mlist != p_mlist )
204 libvlc_media_list_release( p_current_mlist );
208 /**************************************************************************
209 * sublist_at_path (Media List Player Internal)
210 **************************************************************************/
211 static libvlc_media_list_t *
212 libvlc_media_list_sublist_at_path( libvlc_media_list_t * p_mlist, libvlc_media_list_path_t path )
214 libvlc_media_list_t * ret;
215 libvlc_media_t * p_md = libvlc_media_list_item_at_path( p_mlist, path );
219 ret = libvlc_media_subitems( p_md, NULL );
220 libvlc_media_release( p_md );