]> git.sesse.net Git - vlc/blob - modules/demux/playlist/b4s.c
* modules/demux/playlist/b4s.c: misc fixes.
[vlc] / modules / demux / playlist / b4s.c
1 /*****************************************************************************
2  * b4s.c : B4S playlist format import
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: m3u.c 10101 2005-03-02 16:47:31Z robux4 $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/intf.h>
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include "playlist.h"
35 #include "vlc_xml.h"
36
37 struct demux_sys_t
38 {
39     char *psz_prefix;
40     playlist_t *p_playlist;
41     xml_t *p_xml;
42     xml_reader_t *p_xml_reader;
43 };
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int Demux( demux_t *p_demux);
49 static int Control( demux_t *p_demux, int i_query, va_list args );
50 static int IsWhitespace( char *psz_string );
51
52 /*****************************************************************************
53  * Import_B4S: main import function
54  *****************************************************************************/
55 int Import_B4S( vlc_object_t *p_this )
56 {
57     demux_t *p_demux = (demux_t *)p_this;
58
59     char    *psz_ext;
60
61     psz_ext = strrchr ( p_demux->psz_path, '.' );
62
63     if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
64         ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s") ) )
65     {
66         ;
67     }
68     else
69     {
70         return VLC_EGENERIC;
71     }
72     msg_Dbg( p_demux, "using b4s playlist import");
73
74     p_demux->pf_control = Control;
75     p_demux->pf_demux = Demux;
76     p_demux->p_sys = malloc( sizeof(demux_sys_t) );
77     if( p_demux->p_sys == NULL )
78     {
79         msg_Err( p_demux, "Out of memory" );
80         return VLC_ENOMEM;
81     }
82     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
83
84     return VLC_SUCCESS;
85 }
86
87 /*****************************************************************************
88  * Deactivate: frees unused data
89  *****************************************************************************/
90 void Close_B4S( vlc_object_t *p_this )
91 {
92     demux_t *p_demux = (demux_t *)p_this;
93     demux_sys_t *p_sys = p_demux->p_sys;
94
95     if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
96     if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
97     if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
98     if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
99     free( p_sys );
100 }
101
102 static int Demux( demux_t *p_demux )
103 {
104     demux_sys_t *p_sys = p_demux->p_sys;
105     playlist_t *p_playlist;
106     playlist_item_t *p_item, *p_current;
107
108     vlc_bool_t b_play;
109     int i_ret;
110
111     xml_t *p_xml;
112     xml_reader_t *p_xml_reader;
113     char *psz_elname = NULL;
114     int i_type;
115     char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
116     char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
117         
118
119     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
120                                                  FIND_PARENT );
121     if( !p_playlist )
122     {
123         msg_Err( p_demux, "can't find playlist" );
124         return -1;
125     }
126     p_sys->p_playlist = p_playlist;
127
128     b_play = FindItem( p_demux, p_playlist, &p_current );
129
130     playlist_ItemToNode( p_playlist, p_current );
131     p_current->input.i_type = ITEM_TYPE_PLAYLIST;
132
133     p_xml = p_sys->p_xml = xml_Create( p_demux );
134     if( !p_xml ) return -1;
135
136     stream_ReadLine( p_demux->s );
137     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
138     if( !p_xml_reader ) return -1;
139     p_sys->p_xml_reader = p_xml_reader;
140
141     /* xml */
142     /* check root node */
143     if( xml_ReaderRead( p_xml_reader ) != 1 )
144     {
145         msg_Err( p_demux, "invalid file (no root node)" );
146         return -1;
147     }
148
149     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
150         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
151         strcmp( psz_elname, "WinampXML" ) )
152     {
153         msg_Err( p_demux, "invalid root node %i, %s",
154                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
155         if( psz_elname ) free( psz_elname );
156         return -1;
157     }
158     free( psz_elname );
159
160     /* root node should not have any attributes, and should only
161      * contain the "playlist node */
162
163     /* Skip until 1st child node */
164     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
165            xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
166     if( i_ret != 1 )
167     {
168         msg_Err( p_demux, "invalid file (no child node)" );
169         return -1;
170     }
171
172     if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
173         strcmp( psz_elname, "playlist" ) )
174     {
175         msg_Err( p_demux, "invalid child node %s", psz_elname );
176         if( psz_elname ) free( psz_elname );
177         return -1;
178     }
179     free( psz_elname ); psz_elname = 0;
180
181     // Read the attributes
182     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
183     {
184         char *psz_name = xml_ReaderName( p_xml_reader );
185         char *psz_value = xml_ReaderValue( p_xml_reader );
186         if( !psz_name || !psz_value ) return -1;
187         if( !strcmp( psz_name, "num_entries" ) )
188         {
189             msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
190         }
191         else if( !strcmp( psz_name, "label" ) )
192         {
193             playlist_ItemSetName( p_current, psz_value );
194         }
195         else
196         {
197             msg_Warn( p_demux, "stray attribute %s with value %s in element"
198                       " 'playlist'", psz_name, psz_value );
199         }
200         free( psz_name );
201         free( psz_value );
202     }
203
204     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
205     {
206         // Get the node type
207         i_type = xml_ReaderNodeType( p_xml_reader );
208         switch( i_type )
209         {
210             // Error
211             case -1:
212                 return -1;
213                 break;
214
215             case XML_READER_STARTELEM:
216             {
217                 // Read the element name
218                 if( psz_elname ) free( psz_elname );
219                 psz_elname = xml_ReaderName( p_xml_reader );
220                 if( !psz_elname ) return -1;
221                 
222
223                 // Read the attributes
224                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
225                 {
226                     char *psz_name = xml_ReaderName( p_xml_reader );
227                     char *psz_value = xml_ReaderValue( p_xml_reader );
228                     if( !psz_name || !psz_value ) return -1;
229                     if( !strcmp( psz_elname, "entry" ) &&
230                         !strcmp( psz_name, "Playstring" ) )
231                     {
232                         psz_mrl = strdup( psz_value );
233                     }
234                     else
235                     {
236                         msg_Warn( p_demux, "unexpected attribure %s in element %s",
237                                   psz_name, psz_elname );
238                     }
239                     free( psz_name );
240                     free( psz_value );
241                 }
242                 break;
243             }
244             case XML_READER_TEXT:
245             {
246                 char *psz_text = xml_ReaderValue( p_xml_reader );
247                 if( IsWhitespace( psz_text ) )
248                 {
249                     free( psz_text );
250                     break;
251                 }
252                 if( !strcmp( psz_elname, "Name" ) )
253                 {
254                     psz_name = strdup( psz_text );
255                 }
256                 else if( !strcmp( psz_elname, "Genre" ) )
257                 {
258                     psz_genre = strdup( psz_text );
259                 }
260                 else if( !strcmp( psz_elname, "Nowplaying" ) )
261                 {
262                     psz_now = strdup( psz_text );
263                 }
264                 else if( !strcmp( psz_elname, "Listeners" ) )
265                 {
266                     psz_listeners = strdup( psz_text );
267                 }
268                 else if( !strcmp( psz_elname, "Bitrate" ) )
269                 {
270                     psz_bitrate = strdup( psz_text );
271                 }
272                 else if( !strcmp( psz_elname, "" ) )
273                 {
274                     ;
275                 }
276                 else
277                 {
278                     msg_Warn( p_demux, "unexpected text in element '%s'",
279                               psz_elname );
280                 }
281                 free( psz_text );
282                 break;
283             }
284             // End element
285             case XML_READER_ENDELEM:
286             {
287                 // Read the element name
288                 free( psz_elname );
289                 psz_elname = xml_ReaderName( p_xml_reader );
290                 if( !psz_elname ) return -1;
291                 if( !strcmp( psz_elname, "entry" ) )
292                 {
293                     p_item = playlist_ItemNew( p_playlist, psz_mrl, psz_name );
294                     if( psz_now )
295                     {
296                         vlc_input_item_AddInfo( &(p_item->input),
297                                                 _("Meta-information"),
298                                                 _( VLC_META_NOW_PLAYING ),
299                                                 "%s",
300                                                 psz_now );
301                     }
302                     if( psz_genre )
303                     {
304                         vlc_input_item_AddInfo( &p_item->input,
305                                                 _("Meta-information"),
306                                                 _( VLC_META_GENRE ),
307                                                 "%s",
308                                                 psz_genre );
309                     }
310                     if( psz_listeners )
311                     {
312                         vlc_input_item_AddInfo( &p_item->input,
313                                                 _("Meta-information"),
314                                                 _( "Listeners" ),
315                                                 "%s",
316                                                 psz_listeners );
317                     }
318                     if( psz_bitrate )
319                     {
320                         vlc_input_item_AddInfo( &p_item->input,
321                                                 _("Meta-information"),
322                                                 _( "Bitrate" ),
323                                                 "%s",
324                                                 psz_bitrate );
325                     }
326                     playlist_NodeAddItem( p_playlist, p_item,
327                                           p_current->pp_parents[0]->i_view,
328                                           p_current, PLAYLIST_APPEND,
329                                           PLAYLIST_END );
330
331                     /* We need to declare the parents of the node as the
332                      *                  * same of the parent's ones */
333                     playlist_CopyParents( p_current, p_item );
334                     
335                     vlc_input_item_CopyOptions( &p_current->input,
336                                                 &p_item->input );
337 #define FREE(a) if( a ) free( a ); a = NULL;
338                     FREE( psz_name );
339                     FREE( psz_mrl );
340                     FREE( psz_genre );
341                     FREE( psz_bitrate );
342                     FREE( psz_listeners );
343                     FREE( psz_now );
344 #undef FREE
345                 }
346                 free( psz_elname );
347                 psz_elname = strdup("");
348
349                 break;
350             }
351         }
352     }
353
354     if( i_ret != 0 )
355     {
356         msg_Warn( p_demux, "error while parsing data" );
357     }
358
359     /* Go back and play the playlist */
360     if( b_play )
361     {
362         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
363                           p_playlist->status.i_view,
364                           p_playlist->status.p_item, NULL );
365     }
366     
367     vlc_object_release( p_playlist );
368     p_sys->p_playlist = NULL;
369     return VLC_SUCCESS;
370 }
371
372 static int Control( demux_t *p_demux, int i_query, va_list args )
373 {
374     return VLC_EGENERIC;
375 }
376
377 static int IsWhitespace( char *psz_string )
378 {
379     while( *psz_string )
380     {
381         if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
382             *psz_string != '\n' )
383         {
384             return VLC_FALSE;
385         }
386         psz_string++;
387     }
388     return VLC_TRUE;
389 }