1 /*****************************************************************************
2 * b4s.c : B4S playlist format import
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_demux.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Demux( demux_t *p_demux);
47 static int Control( demux_t *p_demux, int i_query, va_list args );
48 static int IsWhitespace( char *psz_string );
50 /*****************************************************************************
51 * Import_B4S: main import function
52 *****************************************************************************/
53 int Import_B4S( vlc_object_t *p_this )
55 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".b4s", "b4s-open",
56 "using B4S playlist reader" );
57 p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
61 /*****************************************************************************
62 * Deactivate: frees unused data
63 *****************************************************************************/
64 void Close_B4S( vlc_object_t *p_this )
66 demux_t *p_demux = (demux_t *)p_this;
67 demux_sys_t *p_sys = p_demux->p_sys;
69 free( p_sys->psz_prefix );
73 static int Demux( demux_t *p_demux )
78 xml_reader_t *p_xml_reader = NULL;
79 char *psz_elname = NULL;
80 input_item_t *p_input;
81 char *psz_mrl = NULL, *psz_title = NULL, *psz_genre = NULL;
82 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
83 input_item_node_t *p_subitems = NULL;
85 input_item_t *p_current_input = GetCurrentItem(p_demux);
87 p_xml = xml_Create( p_demux );
91 psz_elname = stream_ReadLine( p_demux->s );
95 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
100 /* check root node */
101 if( xml_ReaderRead( p_xml_reader ) != 1 )
103 msg_Err( p_demux, "invalid file (no root node)" );
107 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
108 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
109 strcmp( psz_elname, "WinampXML" ) )
111 msg_Err( p_demux, "invalid root node %i, %s",
112 xml_ReaderNodeType( p_xml_reader ), psz_elname );
115 FREENULL( psz_elname );
117 /* root node should not have any attributes, and should only
118 * contain the "playlist node */
120 /* Skip until 1st child node */
121 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
122 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
125 msg_Err( p_demux, "invalid file (no child node)" );
129 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
130 strcmp( psz_elname, "playlist" ) )
132 msg_Err( p_demux, "invalid child node %s", psz_elname );
135 FREENULL( psz_elname );
137 // Read the attributes
138 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
140 char *psz_name = xml_ReaderName( p_xml_reader );
141 char *psz_value = xml_ReaderValue( p_xml_reader );
142 if( !psz_name || !psz_value )
148 if( !strcmp( psz_name, "num_entries" ) )
150 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
152 else if( !strcmp( psz_name, "label" ) )
154 input_item_SetName( p_current_input, psz_value );
158 msg_Warn( p_demux, "stray attribute %s with value %s in element"
159 " 'playlist'", psz_name, psz_value );
165 p_subitems = input_item_node_Create( p_current_input );
167 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
170 switch( xml_ReaderNodeType( p_xml_reader ) )
176 case XML_READER_STARTELEM:
178 // Read the element name
180 psz_elname = xml_ReaderName( p_xml_reader );
184 // Read the attributes
185 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
187 char *psz_name = xml_ReaderName( p_xml_reader );
188 char *psz_value = xml_ReaderValue( p_xml_reader );
189 if( !psz_name || !psz_value )
195 if( !strcmp( psz_elname, "entry" ) &&
196 !strcmp( psz_name, "Playstring" ) )
202 msg_Warn( p_demux, "unexpected attribute %s in element %s",
203 psz_name, psz_elname );
210 case XML_READER_TEXT:
212 char *psz_text = xml_ReaderValue( p_xml_reader );
213 if( IsWhitespace( psz_text ) )
218 if( !strcmp( psz_elname, "Name" ) )
220 psz_title = psz_text;
222 else if( !strcmp( psz_elname, "Genre" ) )
224 psz_genre = psz_text;
226 else if( !strcmp( psz_elname, "Nowplaying" ) )
230 else if( !strcmp( psz_elname, "Listeners" ) )
232 psz_listeners = psz_text;
234 else if( !strcmp( psz_elname, "Bitrate" ) )
236 psz_bitrate = psz_text;
238 else if( !strcmp( psz_elname, "" ) )
244 msg_Warn( p_demux, "unexpected text in element '%s'",
251 case XML_READER_ENDELEM:
253 // Read the element name
255 psz_elname = xml_ReaderName( p_xml_reader );
258 if( !strcmp( psz_elname, "entry" ) )
260 p_input = input_item_New( p_demux, psz_mrl, psz_title );
262 input_item_SetNowPlaying( p_input, psz_now );
264 input_item_SetGenre( p_input, psz_genre );
266 msg_Err( p_demux, "Unsupported meta listeners" );
268 msg_Err( p_demux, "Unsupported meta bitrate" );
270 input_item_node_AppendItem( p_subitems, p_input );
271 vlc_gc_decref( p_input );
272 FREENULL( psz_title );
274 FREENULL( psz_genre );
275 FREENULL( psz_bitrate );
276 FREENULL( psz_listeners );
280 psz_elname = strdup( "" );
289 msg_Warn( p_demux, "error while parsing data" );
290 i_ret = 0; /* Needed for correct operation of go back */
297 input_item_node_PostAndDelete( p_subitems );
299 vlc_gc_decref( p_current_input );
301 xml_ReaderDelete( p_xml, p_xml_reader );
307 static int Control( demux_t *p_demux, int i_query, va_list args )
309 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
313 static int IsWhitespace( char *psz_string )
317 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
318 *psz_string != '\n' )