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
29 #include <vlc_memory.h>
31 typedef int * libvlc_media_list_path_t; /* (Media List Player Internal) */
33 /**************************************************************************
34 * path_dump (Media List Player Internal)
35 **************************************************************************/
36 static inline void libvlc_media_list_path_dump( const libvlc_media_list_path_t path )
40 printf("NULL path\n");
45 for(i = 0; path[i] != -1; i++)
46 printf("%s%d", i > 0 ? "/" : "", path[i]);
50 /**************************************************************************
51 * path_empty (Media List Player Internal)
52 **************************************************************************/
53 static inline libvlc_media_list_path_t libvlc_media_list_path_empty( void )
55 libvlc_media_list_path_t ret = malloc(sizeof(int));
61 /**************************************************************************
62 * path_with_root_index (Media List Player Internal)
63 **************************************************************************/
64 static inline libvlc_media_list_path_t libvlc_media_list_path_with_root_index( int index )
66 libvlc_media_list_path_t ret = malloc(sizeof(int)*2);
73 /**************************************************************************
74 * path_depth (Media List Player Internal)
75 **************************************************************************/
76 static inline int libvlc_media_list_path_depth( const libvlc_media_list_path_t path )
79 for( i = 0; path[i] != -1; i++ );
83 /**************************************************************************
84 * path_append (Media List Player Internal)
85 **************************************************************************/
86 static inline void libvlc_media_list_path_append( libvlc_media_list_path_t * p_path, int index )
88 int old_depth = libvlc_media_list_path_depth( *p_path );
89 *p_path = realloc_or_free( *p_path, sizeof(int)*(old_depth+2));
91 *p_path[old_depth] = index;
92 *p_path[old_depth+1] = -1;
95 /**************************************************************************
96 * path_copy_by_appending (Media List Player Internal)
97 **************************************************************************/
98 static inline libvlc_media_list_path_t libvlc_media_list_path_copy_by_appending( const libvlc_media_list_path_t path, int index )
100 libvlc_media_list_path_t ret;
101 int old_depth = libvlc_media_list_path_depth( path );
102 ret = malloc( sizeof(int) * (old_depth + 2) );
104 memcpy( ret, path, sizeof(int) * old_depth );
105 ret[old_depth] = index;
106 ret[old_depth+1] = -1;
110 /**************************************************************************
111 * path_copy (Media List Player Internal)
112 **************************************************************************/
113 static inline libvlc_media_list_path_t libvlc_media_list_path_copy( const libvlc_media_list_path_t path )
115 libvlc_media_list_path_t ret;
116 int depth = libvlc_media_list_path_depth( path );
117 ret = malloc( sizeof(int)*(depth+1) );
119 memcpy( ret, path, sizeof(int)*(depth+1) );
123 /**************************************************************************
124 * get_path_rec (Media List Player Internal)
125 **************************************************************************/
126 static libvlc_media_list_path_t
127 get_path_rec( const libvlc_media_list_path_t path, libvlc_media_list_t * p_current_mlist, libvlc_media_t * p_searched_md )
130 count = libvlc_media_list_count( p_current_mlist, NULL );
131 for( i = 0; i < count; i++ )
133 libvlc_media_t * p_md = libvlc_media_list_item_at_index( p_current_mlist, i, NULL );
135 if( p_md == p_searched_md )
136 return libvlc_media_list_path_copy_by_appending( path, i ); /* Found! */
138 libvlc_media_list_t * p_subitems = libvlc_media_subitems( p_md );
139 libvlc_media_release( p_md );
142 libvlc_media_list_path_t new_path = libvlc_media_list_path_copy_by_appending( path, i );
143 libvlc_media_list_lock( p_subitems );
144 libvlc_media_list_path_t ret = get_path_rec( new_path, p_subitems, p_searched_md );
145 libvlc_media_list_unlock( p_subitems );
147 libvlc_media_list_release( p_subitems );
149 return ret; /* Found in sublist! */
155 /**************************************************************************
156 * path_of_item (Media List Player Internal)
157 **************************************************************************/
158 static inline libvlc_media_list_path_t libvlc_media_list_path_of_item( libvlc_media_list_t * p_mlist, libvlc_media_t * p_md )
160 libvlc_media_list_path_t path = libvlc_media_list_path_empty();
161 libvlc_media_list_path_t ret;
162 ret = get_path_rec( path, p_mlist, p_md );
167 /**************************************************************************
168 * item_at_path (Media List Player Internal)
169 **************************************************************************/
170 static libvlc_media_t *
171 libvlc_media_list_item_at_path( libvlc_media_list_t * p_mlist, const libvlc_media_list_path_t path )
173 libvlc_media_list_t * p_current_mlist = p_mlist;
174 libvlc_media_t * p_md = NULL;
176 for( i = 0; path[i] != -1; i++ )
178 p_md = libvlc_media_list_item_at_index( p_current_mlist, path[i], NULL );
180 if( p_current_mlist != p_mlist )
181 libvlc_media_list_release( p_current_mlist );
183 if( path[i+1] == -1 )
186 p_current_mlist = libvlc_media_subitems( p_md );
187 libvlc_media_release( p_md );
189 if( !p_current_mlist )
194 /* Not found, shouldn't happen if the p_path is not empty */
195 if( p_current_mlist != p_mlist )
196 libvlc_media_list_release( p_current_mlist );
200 /**************************************************************************
201 * parentlist_at_path (Media List Player Internal)
202 **************************************************************************/
203 static libvlc_media_list_t *
204 libvlc_media_list_parentlist_at_path( libvlc_media_list_t * p_mlist, const libvlc_media_list_path_t path )
206 libvlc_media_list_t * p_current_mlist = p_mlist;
207 libvlc_media_t * p_md = NULL;
209 for( i = 0; path[i] != -1; i++ )
211 if( p_current_mlist != p_mlist )
212 libvlc_media_list_release( p_current_mlist );
214 if( path[i+1] == -1 )
216 libvlc_media_list_retain(p_current_mlist);
217 return p_current_mlist;
220 p_md = libvlc_media_list_item_at_index( p_current_mlist, path[i], NULL );
222 p_current_mlist = libvlc_media_subitems( p_md );
223 libvlc_media_release( p_md );
225 if( !p_current_mlist )
230 /* Not found, shouldn't happen if the p_path is not empty */
231 if( p_current_mlist != p_mlist )
232 libvlc_media_list_release( p_current_mlist );
236 /**************************************************************************
237 * sublist_at_path (Media List Player Internal)
238 **************************************************************************/
239 static libvlc_media_list_t *
240 libvlc_media_list_sublist_at_path( libvlc_media_list_t * p_mlist, const libvlc_media_list_path_t path )
242 libvlc_media_list_t * ret;
243 libvlc_media_t * p_md = libvlc_media_list_item_at_path( p_mlist, path );
247 ret = libvlc_media_subitems( p_md );
248 libvlc_media_release( p_md );