]> git.sesse.net Git - vlc/blob - modules/demux/playlist/b4s.c
70234dae0339fdf2f718c6c7b0fce380d1c8a170
[vlc] / modules / demux / playlist / b4s.c
1 /*****************************************************************************
2  * b4s.c : B4S playlist format import
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_demux.h>
34 #include <vlc_interface.h>
35 #include <vlc_xml.h>
36
37 #include "playlist.h"
38
39 struct demux_sys_t
40 {
41     char *psz_prefix;
42 };
43
44 /*****************************************************************************
45  * Local prototypes
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 );
51
52 /*****************************************************************************
53  * Import_B4S: main import function
54  *****************************************************************************/
55 int Import_B4S( vlc_object_t *p_this )
56 {
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 );
60     return VLC_SUCCESS;
61 }
62
63 /*****************************************************************************
64  * Deactivate: frees unused data
65  *****************************************************************************/
66 void Close_B4S( vlc_object_t *p_this )
67 {
68     demux_t *p_demux = (demux_t *)p_this;
69     demux_sys_t *p_sys = p_demux->p_sys;
70
71     free( p_sys->psz_prefix );
72     free( p_sys );
73 }
74
75 static int Demux( demux_t *p_demux )
76 {
77     int i_ret = -1;
78
79     xml_t *p_xml;
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;
85
86     input_item_t *p_current_input = GetCurrentItem(p_demux);
87
88     p_xml = xml_Create( p_demux );
89     if( !p_xml )
90         goto end;
91
92     psz_elname = stream_ReadLine( p_demux->s );
93     free( psz_elname );
94     psz_elname = NULL;
95
96     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
97     if( !p_xml_reader )
98         goto end;
99
100     /* xml */
101     /* check root node */
102     if( xml_ReaderRead( p_xml_reader ) != 1 )
103     {
104         msg_Err( p_demux, "invalid file (no root node)" );
105         goto end;
106     }
107
108     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
109         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
110         strcmp( psz_elname, "WinampXML" ) )
111     {
112         msg_Err( p_demux, "invalid root node %i, %s",
113                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
114         goto end;
115     }
116     FREENULL( psz_elname );
117
118     /* root node should not have any attributes, and should only
119      * contain the "playlist node */
120
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 );
124     if( i_ret != 1 )
125     {
126         msg_Err( p_demux, "invalid file (no child node)" );
127         goto end;
128     }
129
130     if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
131         strcmp( psz_elname, "playlist" ) )
132     {
133         msg_Err( p_demux, "invalid child node %s", psz_elname );
134         goto end;
135     }
136     FREENULL( psz_elname );
137
138     // Read the attributes
139     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
140     {
141         char *psz_name = xml_ReaderName( p_xml_reader );
142         char *psz_value = xml_ReaderValue( p_xml_reader );
143         if( !psz_name || !psz_value )
144         {
145             free( psz_name );
146             free( psz_value );
147             goto end;
148         }
149         if( !strcmp( psz_name, "num_entries" ) )
150         {
151             msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
152         }
153         else if( !strcmp( psz_name, "label" ) )
154         {
155             input_item_SetName( p_current_input, psz_value );
156         }
157         else
158         {
159             msg_Warn( p_demux, "stray attribute %s with value %s in element"
160                       " 'playlist'", psz_name, psz_value );
161         }
162         free( psz_name );
163         free( psz_value );
164     }
165
166     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
167     {
168         // Get the node type
169         switch( xml_ReaderNodeType( p_xml_reader ) )
170         {
171             // Error
172             case -1:
173                 goto end;
174
175             case XML_READER_STARTELEM:
176             {
177                 // Read the element name
178                 free( psz_elname );
179                 psz_elname = xml_ReaderName( p_xml_reader );
180                 if( !psz_elname )
181                     goto end;
182
183                 // Read the attributes
184                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
185                 {
186                     char *psz_name = xml_ReaderName( p_xml_reader );
187                     char *psz_value = xml_ReaderValue( p_xml_reader );
188                     if( !psz_name || !psz_value )
189                     {
190                         free( psz_name );
191                         free( psz_value );
192                         goto end;
193                     }
194                     if( !strcmp( psz_elname, "entry" ) &&
195                         !strcmp( psz_name, "Playstring" ) )
196                     {
197                         psz_mrl = psz_value;
198                     }
199                     else
200                     {
201                         msg_Warn( p_demux, "unexpected attribure %s in element %s",
202                                   psz_name, psz_elname );
203                         free( psz_value );
204                     }
205                     free( psz_name );
206                 }
207                 break;
208             }
209             case XML_READER_TEXT:
210             {
211                 char *psz_text = xml_ReaderValue( p_xml_reader );
212                 if( IsWhitespace( psz_text ) )
213                 {
214                     free( psz_text );
215                     break;
216                 }
217                 if( !strcmp( psz_elname, "Name" ) )
218                 {
219                     psz_title = psz_text;
220                 }
221                 else if( !strcmp( psz_elname, "Genre" ) )
222                 {
223                     psz_genre = psz_text;
224                 }
225                 else if( !strcmp( psz_elname, "Nowplaying" ) )
226                 {
227                     psz_now = psz_text;
228                 }
229                 else if( !strcmp( psz_elname, "Listeners" ) )
230                 {
231                     psz_listeners = psz_text;
232                 }
233                 else if( !strcmp( psz_elname, "Bitrate" ) )
234                 {
235                     psz_bitrate = psz_text;
236                 }
237                 else if( !strcmp( psz_elname, "" ) )
238                 {
239                     free( psz_text );
240                 }
241                 else
242                 {
243                     msg_Warn( p_demux, "unexpected text in element '%s'",
244                               psz_elname );
245                     free( psz_text );
246                 }
247                 break;
248             }
249             // End element
250             case XML_READER_ENDELEM:
251             {
252                 // Read the element name
253                 free( psz_elname );
254                 psz_elname = xml_ReaderName( p_xml_reader );
255                 if( !psz_elname )
256                     goto end;
257                 if( !strcmp( psz_elname, "entry" ) )
258                 {
259                     p_input = input_item_New( p_demux, psz_mrl, psz_title );
260                     if( psz_now )
261                         input_item_SetNowPlaying( p_input, psz_now );
262                     if( psz_genre )
263                         input_item_SetGenre( p_input, psz_genre );
264                     if( psz_listeners )
265                         msg_Err( p_demux, "Unsupported meta listeners" );
266                     if( psz_bitrate )
267                         msg_Err( p_demux, "Unsupported meta bitrate" );
268
269                     input_item_AddSubItem( p_current_input, p_input );
270                     vlc_gc_decref( p_input );
271                     FREENULL( psz_title );
272                     FREENULL( psz_mrl );
273                     FREENULL( psz_genre );
274                     FREENULL( psz_bitrate );
275                     FREENULL( psz_listeners );
276                     FREENULL( psz_now );
277                 }
278                 free( psz_elname );
279                 psz_elname = strdup( "" );
280
281                 break;
282             }
283         }
284     }
285
286     if( i_ret != 0 )
287     {
288         msg_Warn( p_demux, "error while parsing data" );
289         i_ret = 0; /* Needed for correct operation of go back */
290     }
291
292 end:
293     free( psz_elname );
294
295     vlc_gc_decref( p_current_input );
296     if( p_xml_reader )
297         xml_ReaderDelete( p_xml, p_xml_reader );
298     if( p_xml )
299         xml_Delete( p_xml );
300     return i_ret;
301 }
302
303 static int Control( demux_t *p_demux, int i_query, va_list args )
304 {
305     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
306     return VLC_EGENERIC;
307 }
308
309 #if 0
310 /**
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.
313  *
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
316  * was found
317  * \note The returned pointer may contain more than one
318  * token, Run GetNextToken once more to terminate the token properly
319  */
320 static char *GetNextToken(char *psz_cur_string) {
321     while (*psz_cur_string && !isspace(*psz_cur_string))
322         psz_cur_string++;
323     if (!*psz_cur_string)
324         return NULL;
325     *psz_cur_string++ = '\0';
326     while (*psz_cur_string && isspace(*psz_cur_string))
327         psz_cur_string++;
328     return psz_cur_string;
329 }
330 #endif
331
332 static int IsWhitespace( char *psz_string )
333 {
334     while( *psz_string )
335     {
336         if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
337             *psz_string != '\n' )
338         {
339             return false;
340         }
341         psz_string++;
342     }
343     return true;
344 }