]> git.sesse.net Git - vlc/blob - modules/demux/playlist/b4s.c
playlist: remove no-op pf_deactivate functions
[vlc] / modules / demux / playlist / b4s.c
1 /*****************************************************************************
2  * b4s.c : B4S playlist format import
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_demux.h>
34 #include <vlc_xml.h>
35
36 #include "playlist.h"
37
38 struct demux_sys_t
39 {
40 };
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int Demux( demux_t *p_demux);
46 static int Control( demux_t *p_demux, int i_query, va_list args );
47 static bool IsWhitespace( const char *psz_string );
48
49 /*****************************************************************************
50  * Import_B4S: main import function
51  *****************************************************************************/
52 int Import_B4S( vlc_object_t *p_this )
53 {
54     demux_t *demux = (demux_t *)p_this;
55
56     if( !demux_IsPathExtension( demux, ".b4s" )
57      && !demux_IsForced( demux, "b4s-open" ) )
58         return VLC_EGENERIC;
59
60     demux->pf_demux = Demux;
61     demux->pf_control = Control;
62
63     return VLC_SUCCESS;
64 }
65
66 static int Demux( demux_t *p_demux )
67 {
68     int i_ret = -1;
69
70     xml_reader_t *p_xml_reader = NULL;
71     char *psz_elname = NULL;
72     const char *node;
73     input_item_t *p_input;
74     char *psz_mrl = NULL, *psz_title = NULL, *psz_genre = NULL;
75     char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
76     input_item_node_t *p_subitems = NULL;
77
78     input_item_t *p_current_input = GetCurrentItem(p_demux);
79
80     free( stream_ReadLine( p_demux->s ) );
81
82     p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
83     if( !p_xml_reader )
84         return -1;
85
86     /* xml */
87     /* check root node */
88     if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
89     {
90         msg_Err( p_demux, "invalid file (no root node)" );
91         goto end;
92     }
93
94     if( strcmp( node, "WinampXML" ) )
95     {
96         msg_Err( p_demux, "invalid root node: %s", node );
97         goto end;
98     }
99
100     /* root node should not have any attributes, and should only
101      * contain the "playlist node */
102
103     /* Skip until 1st child node */
104     while( (i_ret = xml_ReaderNextNode( p_xml_reader, &node )) != XML_READER_STARTELEM )
105         if( i_ret <= 0 )
106         {
107             msg_Err( p_demux, "invalid file (no child node)" );
108             goto end;
109         }
110
111     if( strcmp( node, "playlist" ) )
112     {
113         msg_Err( p_demux, "invalid child node %s", node );
114         goto end;
115     }
116
117     // Read the attributes
118     const char *attr, *value;
119     while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
120     {
121         if( !strcmp( attr, "num_entries" ) )
122             msg_Dbg( p_demux, "playlist has %d entries", atoi(value) );
123         else if( !strcmp( attr, "label" ) )
124             input_item_SetName( p_current_input, value );
125         else
126             msg_Warn( p_demux, "stray attribute %s with value %s in element"
127                       " <playlist>", attr, value );
128     }
129
130     p_subitems = input_item_node_Create( p_current_input );
131
132     while( (i_ret = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
133     {
134         // Get the node type
135         switch( i_ret )
136         {
137             case XML_READER_STARTELEM:
138             {
139                 // Read the element name
140                 free( psz_elname );
141                 psz_elname = strdup( node );
142                 if( unlikely(!psz_elname) )
143                     goto end;
144
145                 // Read the attributes
146                 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) )
147                 {
148                     if( !strcmp( psz_elname, "entry" ) &&
149                         !strcmp( attr, "Playstring" ) )
150                     {
151                         free( psz_mrl );
152                         psz_mrl = strdup( value );
153                     }
154                     else
155                     {
156                         msg_Warn( p_demux, "unexpected attribute %s in <%s>",
157                                   attr, psz_elname );
158                     }
159                 }
160                 break;
161             }
162
163             case XML_READER_TEXT:
164             {
165                 char **p;
166
167                 if( psz_elname == NULL )
168                     break;
169                 if( IsWhitespace( node ) )
170                     break;
171                 if( !strcmp( psz_elname, "Name" ) )
172                     p = &psz_title;
173                 else if( !strcmp( psz_elname, "Genre" ) )
174                     p = &psz_genre;
175                 else if( !strcmp( psz_elname, "Nowplaying" ) )
176                     p = &psz_now;
177                 else if( !strcmp( psz_elname, "Listeners" ) )
178                     p = &psz_listeners;
179                 else if( !strcmp( psz_elname, "Bitrate" ) )
180                     p = &psz_bitrate;
181                 else
182                 {
183                     msg_Warn( p_demux, "unexpected text in element <%s>",
184                               psz_elname );
185                     break;
186                 }
187                 free( *p );
188                 *p = strdup( node );
189                 break;
190             }
191
192             // End element
193             case XML_READER_ENDELEM:
194             {
195                 // Read the element name
196                 if( !strcmp( node, "entry" ) )
197                 {
198                     p_input = input_item_New( psz_mrl, psz_title );
199                     if( psz_now )
200                         input_item_SetNowPlaying( p_input, psz_now );
201                     if( psz_genre )
202                         input_item_SetGenre( p_input, psz_genre );
203                     if( psz_listeners )
204                         msg_Err( p_demux, "Unsupported meta listeners" );
205                     if( psz_bitrate )
206                         msg_Err( p_demux, "Unsupported meta bitrate" );
207
208                     input_item_node_AppendItem( p_subitems, p_input );
209                     vlc_gc_decref( p_input );
210                     FREENULL( psz_title );
211                     FREENULL( psz_mrl );
212                     FREENULL( psz_genre );
213                     FREENULL( psz_bitrate );
214                     FREENULL( psz_listeners );
215                     FREENULL( psz_now );
216                 }
217                 FREENULL( psz_elname );
218                 break;
219             }
220         }
221     }
222
223     if( i_ret < 0 )
224     {
225         msg_Warn( p_demux, "error while parsing data" );
226         i_ret = 0; /* Needed for correct operation of go back */
227     }
228
229 end:
230     free( psz_elname );
231
232     if( p_subitems )
233         input_item_node_PostAndDelete( p_subitems );
234
235     vlc_gc_decref( p_current_input );
236     if( p_xml_reader )
237         xml_ReaderDelete( p_xml_reader );
238     return i_ret;
239 }
240
241 static int Control( demux_t *p_demux, int i_query, va_list args )
242 {
243     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
244     return VLC_EGENERIC;
245 }
246
247 static bool IsWhitespace( const char *psz_string )
248 {
249     psz_string += strspn( psz_string, " \t\r\n" );
250     return !*psz_string;
251 }