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