1 /*****************************************************************************
2 * b4s.c : B4S playlist format import
3 *****************************************************************************
4 * Copyright (C) 2005 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>
34 #include <vlc_interface.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Demux( demux_t *p_demux);
48 static int Control( demux_t *p_demux, int i_query, va_list args );
49 //static char *GetNextToken(char *psz_cur_string);
50 static int IsWhitespace( char *psz_string );
52 /*****************************************************************************
53 * Import_B4S: main import function
54 *****************************************************************************/
55 int Import_B4S( vlc_object_t *p_this )
57 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".b4s", "b4s-open",
58 "using B4S playlist reader" );
59 p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
63 /*****************************************************************************
64 * Deactivate: frees unused data
65 *****************************************************************************/
66 void Close_B4S( vlc_object_t *p_this )
68 demux_t *p_demux = (demux_t *)p_this;
69 demux_sys_t *p_sys = p_demux->p_sys;
71 free( p_sys->psz_prefix );
75 static int Demux( demux_t *p_demux )
80 xml_reader_t *p_xml_reader = NULL;
81 char *psz_elname = NULL;
82 input_item_t *p_input;
83 char *psz_mrl = NULL, *psz_title = NULL, *psz_genre = NULL;
84 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
86 input_item_t *p_current_input = GetCurrentItem(p_demux);
88 p_xml = xml_Create( p_demux );
92 psz_elname = stream_ReadLine( p_demux->s );
96 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
101 /* check root node */
102 if( xml_ReaderRead( p_xml_reader ) != 1 )
104 msg_Err( p_demux, "invalid file (no root node)" );
108 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
109 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
110 strcmp( psz_elname, "WinampXML" ) )
112 msg_Err( p_demux, "invalid root node %i, %s",
113 xml_ReaderNodeType( p_xml_reader ), psz_elname );
116 FREENULL( psz_elname );
118 /* root node should not have any attributes, and should only
119 * contain the "playlist node */
121 /* Skip until 1st child node */
122 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
123 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
126 msg_Err( p_demux, "invalid file (no child node)" );
130 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
131 strcmp( psz_elname, "playlist" ) )
133 msg_Err( p_demux, "invalid child node %s", psz_elname );
136 FREENULL( psz_elname );
138 // Read the attributes
139 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
141 char *psz_name = xml_ReaderName( p_xml_reader );
142 char *psz_value = xml_ReaderValue( p_xml_reader );
143 if( !psz_name || !psz_value )
149 if( !strcmp( psz_name, "num_entries" ) )
151 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
153 else if( !strcmp( psz_name, "label" ) )
155 input_item_SetName( p_current_input, psz_value );
159 msg_Warn( p_demux, "stray attribute %s with value %s in element"
160 " 'playlist'", psz_name, psz_value );
166 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
169 switch( xml_ReaderNodeType( p_xml_reader ) )
175 case XML_READER_STARTELEM:
177 // Read the element name
179 psz_elname = xml_ReaderName( p_xml_reader );
183 // Read the attributes
184 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
186 char *psz_name = xml_ReaderName( p_xml_reader );
187 char *psz_value = xml_ReaderValue( p_xml_reader );
188 if( !psz_name || !psz_value )
194 if( !strcmp( psz_elname, "entry" ) &&
195 !strcmp( psz_name, "Playstring" ) )
201 msg_Warn( p_demux, "unexpected attribure %s in element %s",
202 psz_name, psz_elname );
209 case XML_READER_TEXT:
211 char *psz_text = xml_ReaderValue( p_xml_reader );
212 if( IsWhitespace( psz_text ) )
217 if( !strcmp( psz_elname, "Name" ) )
219 psz_title = psz_text;
221 else if( !strcmp( psz_elname, "Genre" ) )
223 psz_genre = psz_text;
225 else if( !strcmp( psz_elname, "Nowplaying" ) )
229 else if( !strcmp( psz_elname, "Listeners" ) )
231 psz_listeners = psz_text;
233 else if( !strcmp( psz_elname, "Bitrate" ) )
235 psz_bitrate = psz_text;
237 else if( !strcmp( psz_elname, "" ) )
243 msg_Warn( p_demux, "unexpected text in element '%s'",
250 case XML_READER_ENDELEM:
252 // Read the element name
254 psz_elname = xml_ReaderName( p_xml_reader );
257 if( !strcmp( psz_elname, "entry" ) )
259 p_input = input_item_New( p_demux, psz_mrl, psz_title );
261 input_item_SetNowPlaying( p_input, psz_now );
263 input_item_SetGenre( p_input, psz_genre );
265 msg_Err( p_demux, "Unsupported meta listeners" );
267 msg_Err( p_demux, "Unsupported meta bitrate" );
269 input_item_AddSubItem( p_current_input, p_input );
270 vlc_gc_decref( p_input );
271 FREENULL( psz_title );
273 FREENULL( psz_genre );
274 FREENULL( psz_bitrate );
275 FREENULL( psz_listeners );
279 psz_elname = strdup( "" );
288 msg_Warn( p_demux, "error while parsing data" );
289 i_ret = 0; /* Needed for correct operation of go back */
295 vlc_gc_decref( p_current_input );
297 xml_ReaderDelete( p_xml, p_xml_reader );
303 static int Control( demux_t *p_demux, int i_query, va_list args )
305 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
311 * Get a in-string pointer to the start of the next token from a
312 * string terminating the pointer returned by a previous call.
314 * \param psz_cur_string The string to search for the token from
315 * \return a pointer to withing psz_cur_string, or NULL if no token
317 * \note The returned pointer may contain more than one
318 * token, Run GetNextToken once more to terminate the token properly
320 static char *GetNextToken(char *psz_cur_string) {
321 while (*psz_cur_string && !isspace(*psz_cur_string))
323 if (!*psz_cur_string)
325 *psz_cur_string++ = '\0';
326 while (*psz_cur_string && isspace(*psz_cur_string))
328 return psz_cur_string;
332 static int IsWhitespace( char *psz_string )
336 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
337 *psz_string != '\n' )