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 *****************************************************************************/
29 #include <vlc_demux.h>
30 #include <vlc_interface.h>
39 xml_reader_t *p_xml_reader;
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 char *GetNextToken(char *psz_cur_string);
48 static int IsWhitespace( char *psz_string );
50 /*****************************************************************************
51 * Import_B4S: main import function
52 *****************************************************************************/
53 int E_(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 = E_(FindPrefix)( p_demux );
58 p_demux->p_sys->p_xml = NULL;
59 p_demux->p_sys->p_xml_reader = NULL;
63 /*****************************************************************************
64 * Deactivate: frees unused data
65 *****************************************************************************/
66 void E_(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 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
72 if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
73 if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
77 static int Demux( demux_t *p_demux )
79 demux_sys_t *p_sys = p_demux->p_sys;
83 xml_reader_t *p_xml_reader;
84 char *psz_elname = NULL;
86 input_item_t *p_input;
87 char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
88 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
92 p_xml = p_sys->p_xml = xml_Create( p_demux );
93 if( !p_xml ) return -1;
95 psz_elname = stream_ReadLine( p_demux->s );
96 if( psz_elname ) free( psz_elname );
99 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
100 if( !p_xml_reader ) return -1;
101 p_sys->p_xml_reader = p_xml_reader;
104 /* check root node */
105 if( xml_ReaderRead( p_xml_reader ) != 1 )
107 msg_Err( p_demux, "invalid file (no root node)" );
108 vlc_object_release( p_playlist );
112 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
113 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
114 strcmp( psz_elname, "WinampXML" ) )
116 msg_Err( p_demux, "invalid root node %i, %s",
117 xml_ReaderNodeType( p_xml_reader ), psz_elname );
118 if( psz_elname ) free( psz_elname );
119 vlc_object_release( p_playlist );
124 /* root node should not have any attributes, and should only
125 * contain the "playlist node */
127 /* Skip until 1st child node */
128 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
129 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
132 msg_Err( p_demux, "invalid file (no child node)" );
136 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
137 strcmp( psz_elname, "playlist" ) )
139 msg_Err( p_demux, "invalid child node %s", psz_elname );
140 if( psz_elname ) free( psz_elname );
143 free( psz_elname ); psz_elname = 0;
145 // Read the attributes
146 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
148 char *psz_name = xml_ReaderName( p_xml_reader );
149 char *psz_value = xml_ReaderValue( p_xml_reader );
150 if( !psz_name || !psz_value ) return -1;
151 if( !strcmp( psz_name, "num_entries" ) )
153 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
155 else if( !strcmp( psz_name, "label" ) )
157 input_item_SetName( p_current_input, psz_value );
161 msg_Warn( p_demux, "stray attribute %s with value %s in element"
162 " 'playlist'", psz_name, psz_value );
168 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
171 i_type = xml_ReaderNodeType( p_xml_reader );
179 case XML_READER_STARTELEM:
181 // Read the element name
182 if( psz_elname ) free( psz_elname );
183 psz_elname = xml_ReaderName( p_xml_reader );
184 if( !psz_elname ) return -1;
187 // Read the attributes
188 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
190 char *psz_name = xml_ReaderName( p_xml_reader );
191 char *psz_value = xml_ReaderValue( p_xml_reader );
192 if( !psz_name || !psz_value ) return -1;
193 if( !strcmp( psz_elname, "entry" ) &&
194 !strcmp( psz_name, "Playstring" ) )
196 psz_mrl = strdup( psz_value );
200 msg_Warn( p_demux, "unexpected attribure %s in element %s",
201 psz_name, psz_elname );
208 case XML_READER_TEXT:
210 char *psz_text = xml_ReaderValue( p_xml_reader );
211 if( IsWhitespace( psz_text ) )
216 if( !strcmp( psz_elname, "Name" ) )
218 psz_name = strdup( psz_text );
220 else if( !strcmp( psz_elname, "Genre" ) )
222 psz_genre = strdup( psz_text );
224 else if( !strcmp( psz_elname, "Nowplaying" ) )
226 psz_now = strdup( psz_text );
228 else if( !strcmp( psz_elname, "Listeners" ) )
230 psz_listeners = strdup( psz_text );
232 else if( !strcmp( psz_elname, "Bitrate" ) )
234 psz_bitrate = strdup( psz_text );
236 else if( !strcmp( psz_elname, "" ) )
242 msg_Warn( p_demux, "unexpected text in element '%s'",
249 case XML_READER_ENDELEM:
251 // Read the element name
253 psz_elname = xml_ReaderName( p_xml_reader );
254 if( !psz_elname ) return -1;
255 if( !strcmp( psz_elname, "entry" ) )
257 p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
260 input_item_SetNowPlaying( p_input, psz_now );
262 input_item_SetGenre( p_input, psz_genre );
264 msg_Err( p_playlist, "Unsupported meta listeners" );
266 msg_Err( p_playlist, "Unsupported meta bitrate" );
268 input_ItemAddSubItem( p_current_input, p_input );
269 FREENULL( psz_name );
271 FREENULL( psz_genre );
272 FREENULL( psz_bitrate );
273 FREENULL( psz_listeners );
277 psz_elname = strdup("");
286 msg_Warn( p_demux, "error while parsing data" );
289 HANDLE_PLAY_AND_RELEASE;
290 return -1; /* Needed for correct operation of go back */
293 static int Control( demux_t *p_demux, int i_query, va_list args )
300 * Get a in-string pointer to the start of the next token from a
301 * string terminating the pointer returned by a previous call.
303 * \param psz_cur_string The string to search for the token from
304 * \return a pointer to withing psz_cur_string, or NULL if no token
306 * \note The returned pointer may contain more than one
307 * token, Run GetNextToken once more to terminate the token properly
309 static char *GetNextToken(char *psz_cur_string) {
310 while (*psz_cur_string && !isspace(*psz_cur_string))
312 if (!*psz_cur_string)
314 *psz_cur_string++ = '\0';
315 while (*psz_cur_string && isspace(*psz_cur_string))
317 return psz_cur_string;
321 static int IsWhitespace( char *psz_string )
325 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
326 *psz_string != '\n' )