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 playlist_item_t *p_item;
109 playlist_item_t *p_bitrate = NULL, *p_genre = NULL;
111 int i_ret, i_parent_id;
114 xml_reader_t *p_xml_reader;
115 char *psz_elname = NULL;
117 char *psz_mrl = NULL, *psz_name = NULL, *psz_genre = NULL;
118 char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
122 p_xml = p_sys->p_xml = xml_Create( p_demux );
123 if( !p_xml ) return -1;
125 psz_elname = stream_ReadLine( p_demux->s );
126 if( psz_elname ) free( psz_elname );
129 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
130 if( !p_xml_reader ) return -1;
131 p_sys->p_xml_reader = p_xml_reader;
134 /* check root node */
135 if( xml_ReaderRead( p_xml_reader ) != 1 )
137 msg_Err( p_demux, "invalid file (no root node)" );
138 vlc_object_release( p_playlist );
142 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
143 ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
144 strcmp( psz_elname, "WinampXML" ) )
146 msg_Err( p_demux, "invalid root node %i, %s",
147 xml_ReaderNodeType( p_xml_reader ), psz_elname );
148 if( psz_elname ) free( psz_elname );
149 vlc_object_release( p_playlist );
154 /* root node should not have any attributes, and should only
155 * contain the "playlist node */
157 /* Skip until 1st child node */
158 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
159 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
162 msg_Err( p_demux, "invalid file (no child node)" );
166 if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
167 strcmp( psz_elname, "playlist" ) )
169 msg_Err( p_demux, "invalid child node %s", psz_elname );
170 if( psz_elname ) free( psz_elname );
173 free( psz_elname ); psz_elname = 0;
175 // Read the attributes
176 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
178 char *psz_name = xml_ReaderName( p_xml_reader );
179 char *psz_value = xml_ReaderValue( p_xml_reader );
180 if( !psz_name || !psz_value ) return -1;
181 if( !strcmp( psz_name, "num_entries" ) )
183 msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
185 else if( !strcmp( psz_name, "label" ) )
187 playlist_ItemSetName( p_current, psz_value );
191 msg_Warn( p_demux, "stray attribute %s with value %s in element"
192 " 'playlist'", psz_name, psz_value );
198 while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
201 i_type = xml_ReaderNodeType( p_xml_reader );
209 case XML_READER_STARTELEM:
211 // Read the element name
212 if( psz_elname ) free( psz_elname );
213 psz_elname = xml_ReaderName( p_xml_reader );
214 if( !psz_elname ) return -1;
217 // Read the attributes
218 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
220 char *psz_name = xml_ReaderName( p_xml_reader );
221 char *psz_value = xml_ReaderValue( p_xml_reader );
222 if( !psz_name || !psz_value ) return -1;
223 if( !strcmp( psz_elname, "entry" ) &&
224 !strcmp( psz_name, "Playstring" ) )
226 psz_mrl = strdup( psz_value );
230 msg_Warn( p_demux, "unexpected attribure %s in element %s",
231 psz_name, psz_elname );
238 case XML_READER_TEXT:
240 char *psz_text = xml_ReaderValue( p_xml_reader );
241 if( IsWhitespace( psz_text ) )
246 if( !strcmp( psz_elname, "Name" ) )
248 psz_name = strdup( psz_text );
250 else if( !strcmp( psz_elname, "Genre" ) )
252 psz_genre = strdup( psz_text );
254 else if( !strcmp( psz_elname, "Nowplaying" ) )
256 psz_now = strdup( psz_text );
258 else if( !strcmp( psz_elname, "Listeners" ) )
260 psz_listeners = strdup( psz_text );
262 else if( !strcmp( psz_elname, "Bitrate" ) )
264 psz_bitrate = strdup( psz_text );
266 else if( !strcmp( psz_elname, "" ) )
272 msg_Warn( p_demux, "unexpected text in element '%s'",
279 case XML_READER_ENDELEM:
281 // Read the element name
283 psz_elname = xml_ReaderName( p_xml_reader );
284 if( !psz_elname ) return -1;
285 if( !strcmp( psz_elname, "entry" ) )
287 p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
290 vlc_meta_SetNowPlaying( p_input->p_meta, psz_now );
292 vlc_meta_SetGenre( p_input->p_meta, psz_genre );
294 msg_Err( p_playlist, "Unsupported meta listeners" );
296 msg_Err( p_playlist, "Unsupported meta bitrate" );
298 playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
299 p_item_in_category, (i_parent_id > 0 ) ? VLC_TRUE:
300 VLC_FALSE, PLAYLIST_APPEND );
302 #define FREE(a) if( a ) free( a ); a = NULL;
307 FREE( psz_listeners );
312 psz_elname = strdup("");
321 msg_Warn( p_demux, "error while parsing data" );
324 HANDLE_PLAY_AND_RELEASE;
328 static int Control( demux_t *p_demux, int i_query, va_list args )
334 * Get a in-string pointer to the start of the next token from a
335 * string terminating the pointer returned by a previous call.
337 * \param psz_cur_string The string to search for the token from
338 * \return a pointer to withing psz_cur_string, or NULL if no token
340 * \note The returned pointer may contain more than one
341 * token, Run GetNextToken once more to terminate the token properly
343 static char *GetNextToken(char *psz_cur_string) {
344 while (*psz_cur_string && !isspace(*psz_cur_string))
346 if (!*psz_cur_string)
348 *psz_cur_string++ = '\0';
349 while (*psz_cur_string && isspace(*psz_cur_string))
351 return psz_cur_string;
354 static int IsWhitespace( char *psz_string )
358 if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
359 *psz_string != '\n' )