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>
42 /*****************************************************************************
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 );
49 /*****************************************************************************
50 * Import_B4S: main import function
51 *****************************************************************************/
52 int Import_B4S( vlc_object_t *p_this )
54 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".b4s", "b4s-open",
55 "using B4S playlist reader" );
59 /*****************************************************************************
60 * Deactivate: frees unused data
61 *****************************************************************************/
62 void Close_B4S( vlc_object_t *p_this )
64 demux_t *p_demux = (demux_t *)p_this;
65 demux_sys_t *p_sys = p_demux->p_sys;
70 static int Demux( demux_t *p_demux )
74 xml_reader_t *p_xml_reader = NULL;
75 char *psz_elname = NULL;
77 input_item_t *p_input;
78 char *psz_mrl = NULL, *psz_title = NULL, *psz_genre = NULL;
79 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
80 input_item_node_t *p_subitems = NULL;
82 input_item_t *p_current_input = GetCurrentItem(p_demux);
84 free( stream_ReadLine( p_demux->s ) );
86 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
92 if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
94 msg_Err( p_demux, "invalid file (no root node)" );
98 if( strcmp( node, "WinampXML" ) )
100 msg_Err( p_demux, "invalid root node: %s", node );
104 /* root node should not have any attributes, and should only
105 * contain the "playlist node */
107 /* Skip until 1st child node */
108 while( (i_ret = xml_ReaderNextNode( p_xml_reader, &node )) != XML_READER_STARTELEM )
111 msg_Err( p_demux, "invalid file (no child node)" );
115 if( strcmp( node, "playlist" ) )
117 msg_Err( p_demux, "invalid child node %s", node );
121 // Read the attributes
122 const char *attr, *value;
123 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
125 if( !strcmp( attr, "num_entries" ) )
126 msg_Dbg( p_demux, "playlist has %d entries", atoi(value) );
127 else if( !strcmp( attr, "label" ) )
128 input_item_SetName( p_current_input, value );
130 msg_Warn( p_demux, "stray attribute %s with value %s in element"
131 " <playlist>", attr, value );
134 p_subitems = input_item_node_Create( p_current_input );
136 while( (i_ret = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
141 case XML_READER_STARTELEM:
143 // Read the element name
145 psz_elname = strdup( node );
146 if( unlikely(!psz_elname) )
149 // Read the attributes
150 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) )
152 if( !strcmp( psz_elname, "entry" ) &&
153 !strcmp( attr, "Playstring" ) )
156 psz_mrl = strdup( value );
160 msg_Warn( p_demux, "unexpected attribute %s in <%s>",
167 case XML_READER_TEXT:
171 if( psz_elname == NULL )
173 if( IsWhitespace( node ) )
175 if( !strcmp( psz_elname, "Name" ) )
177 else if( !strcmp( psz_elname, "Genre" ) )
179 else if( !strcmp( psz_elname, "Nowplaying" ) )
181 else if( !strcmp( psz_elname, "Listeners" ) )
183 else if( !strcmp( psz_elname, "Bitrate" ) )
187 msg_Warn( p_demux, "unexpected text in element <%s>",
197 case XML_READER_ENDELEM:
199 // Read the element name
200 if( !strcmp( node, "entry" ) )
202 p_input = input_item_New( psz_mrl, psz_title );
204 input_item_SetNowPlaying( p_input, psz_now );
206 input_item_SetGenre( p_input, psz_genre );
208 msg_Err( p_demux, "Unsupported meta listeners" );
210 msg_Err( p_demux, "Unsupported meta bitrate" );
212 input_item_node_AppendItem( p_subitems, p_input );
213 vlc_gc_decref( p_input );
214 FREENULL( psz_title );
216 FREENULL( psz_genre );
217 FREENULL( psz_bitrate );
218 FREENULL( psz_listeners );
221 FREENULL( psz_elname );
229 msg_Warn( p_demux, "error while parsing data" );
230 i_ret = 0; /* Needed for correct operation of go back */
237 input_item_node_PostAndDelete( p_subitems );
239 vlc_gc_decref( p_current_input );
241 xml_ReaderDelete( p_xml_reader );
245 static int Control( demux_t *p_demux, int i_query, va_list args )
247 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
251 static bool IsWhitespace( const char *psz_string )
253 psz_string += strspn( psz_string, " \t\r\n" );