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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <ctype.h> /* isspace() */
31 #include <vlc/input.h>
34 #include <errno.h> /* ENOMEM */
42 xml_reader_t *p_xml_reader;
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Demux( demux_t *p_demux);
49 static int Control( demux_t *p_demux, int i_query, va_list args );
50 static char *GetNextToken(char *psz_cur_string);
51 static int IsWhitespace( char *psz_string );
53 /*****************************************************************************
54 * Import_B4S: main import function
55 *****************************************************************************/
56 int E_(Import_B4S)( vlc_object_t *p_this )
58 demux_t *p_demux = (demux_t *)p_this;
63 psz_ext = strrchr ( p_demux->psz_path, '.' );
65 if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
66 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "b4s-open") ) )
74 msg_Dbg( p_demux, "using b4s playlist import");
76 p_demux->pf_control = Control;
77 p_demux->pf_demux = Demux;
78 p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
81 msg_Err( p_demux, "out of memory" );
84 p_sys->psz_prefix = E_(FindPrefix)( p_demux );
86 p_sys->p_xml_reader = NULL;
91 /*****************************************************************************
92 * Deactivate: frees unused data
93 *****************************************************************************/
94 void E_(Close_B4S)( vlc_object_t *p_this )
96 demux_t *p_demux = (demux_t *)p_this;
97 demux_sys_t *p_sys = p_demux->p_sys;
99 if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
100 if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
101 if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
105 static int Demux( demux_t *p_demux )
107 demux_sys_t *p_sys = p_demux->p_sys;
108 int i_ret, i_parent_id;
111 xml_reader_t *p_xml_reader;
112 char *psz_elname = NULL;
114 char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
115 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
119 p_xml = p_sys->p_xml = xml_Create( p_demux );
120 if( !p_xml ) return -1;
122 psz_elname = stream_ReadLine( p_demux->s );
123 if( psz_elname ) free( psz_elname );
126 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
127 if( !p_xml_reader ) return -1;
128 p_sys->p_xml_reader = p_xml_reader;
131 /* check root node */
132 if( xml_ReaderRead( p_xml_reader ) != 1 )
134 msg_Err( p_demux, "invalid file (no root node)" );
135 vlc_object_release( p_playlist );
139 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
140 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
141 strcmp( psz_elname, "WinampXML" ) )
143 msg_Err( p_demux, "invalid root node %i, %s",
144 xml_ReaderNodeType( p_xml_reader ), psz_elname );
145 if( psz_elname ) free( psz_elname );
146 vlc_object_release( p_playlist );
151 /* root node should not have any attributes, and should only
152 * contain the "playlist node */
154 /* Skip until 1st child node */
155 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
156 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
159 msg_Err( p_demux, "invalid file (no child node)" );
163 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
164 strcmp( psz_elname, "playlist" ) )
166 msg_Err( p_demux, "invalid child node %s", psz_elname );
167 if( psz_elname ) free( psz_elname );
170 free( psz_elname ); psz_elname = 0;
172 // Read the attributes
173 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
175 char *psz_name = xml_ReaderName( p_xml_reader );
176 char *psz_value = xml_ReaderValue( p_xml_reader );
177 if( !psz_name || !psz_value ) return -1;
178 if( !strcmp( psz_name, "num_entries" ) )
180 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
182 else if( !strcmp( psz_name, "label" ) )
184 playlist_ItemSetName( p_current, psz_value );
188 msg_Warn( p_demux, "stray attribute %s with value %s in element"
189 " 'playlist'", psz_name, psz_value );
195 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
198 i_type = xml_ReaderNodeType( p_xml_reader );
206 case XML_READER_STARTELEM:
208 // Read the element name
209 if( psz_elname ) free( psz_elname );
210 psz_elname = xml_ReaderName( p_xml_reader );
211 if( !psz_elname ) return -1;
214 // Read the attributes
215 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
217 char *psz_name = xml_ReaderName( p_xml_reader );
218 char *psz_value = xml_ReaderValue( p_xml_reader );
219 if( !psz_name || !psz_value ) return -1;
220 if( !strcmp( psz_elname, "entry" ) &&
221 !strcmp( psz_name, "Playstring" ) )
223 psz_mrl = strdup( psz_value );
227 msg_Warn( p_demux, "unexpected attribure %s in element %s",
228 psz_name, psz_elname );
235 case XML_READER_TEXT:
237 char *psz_text = xml_ReaderValue( p_xml_reader );
238 if( IsWhitespace( psz_text ) )
243 if( !strcmp( psz_elname, "Name" ) )
245 psz_name = strdup( psz_text );
247 else if( !strcmp( psz_elname, "Genre" ) )
249 psz_genre = strdup( psz_text );
251 else if( !strcmp( psz_elname, "Nowplaying" ) )
253 psz_now = strdup( psz_text );
255 else if( !strcmp( psz_elname, "Listeners" ) )
257 psz_listeners = strdup( psz_text );
259 else if( !strcmp( psz_elname, "Bitrate" ) )
261 psz_bitrate = strdup( psz_text );
263 else if( !strcmp( psz_elname, "" ) )
269 msg_Warn( p_demux, "unexpected text in element '%s'",
276 case XML_READER_ENDELEM:
278 // Read the element name
280 psz_elname = xml_ReaderName( p_xml_reader );
281 if( !psz_elname ) return -1;
282 if( !strcmp( psz_elname, "entry" ) )
284 p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
287 vlc_meta_SetNowPlaying( p_input->p_meta, psz_now );
289 vlc_meta_SetGenre( p_input->p_meta, psz_genre );
291 msg_Err( p_playlist, "Unsupported meta listeners" );
293 msg_Err( p_playlist, "Unsupported meta bitrate" );
295 playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
296 p_item_in_category, (i_parent_id > 0 ) ? VLC_TRUE:
297 VLC_FALSE, PLAYLIST_APPEND );
299 #define FREE(a) if( a ) free( a ); a = NULL;
304 FREE( psz_listeners );
309 psz_elname = strdup("");
318 msg_Warn( p_demux, "error while parsing data" );
321 HANDLE_PLAY_AND_RELEASE;
325 static int Control( demux_t *p_demux, int i_query, va_list args )
331 * Get a in-string pointer to the start of the next token from a
332 * string terminating the pointer returned by a previous call.
334 * \param psz_cur_string The string to search for the token from
335 * \return a pointer to withing psz_cur_string, or NULL if no token
337 * \note The returned pointer may contain more than one
338 * token, Run GetNextToken once more to terminate the token properly
340 static char *GetNextToken(char *psz_cur_string) {
341 while (*psz_cur_string && !isspace(*psz_cur_string))
343 if (!*psz_cur_string)
345 *psz_cur_string++ = '\0';
346 while (*psz_cur_string && isspace(*psz_cur_string))
348 return psz_cur_string;
351 static int IsWhitespace( char *psz_string )
355 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
356 *psz_string != '\n' )