]> git.sesse.net Git - vlc/blob - modules/demux/playlist/b4s.c
simple mixer: remove useless allocation and simplify
[vlc] / modules / demux / playlist / b4s.c
1 /*****************************************************************************
2  * b4s.c : B4S playlist format import
3  *****************************************************************************
4  * Copyright (C) 2005-2009 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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_xml.h>
35 #include <vlc_strings.h>
36
37 #include "playlist.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int Demux( demux_t *p_demux);
43 static bool IsWhitespace( const char *psz_string );
44
45 /*****************************************************************************
46  * Import_B4S: main import function
47  *****************************************************************************/
48 int Import_B4S( vlc_object_t *p_this )
49 {
50     demux_t *demux = (demux_t *)p_this;
51
52     if( !demux_IsPathExtension( demux, ".b4s" )
53      && !demux_IsForced( demux, "b4s-open" ) )
54         return VLC_EGENERIC;
55
56     demux->pf_demux = Demux;
57     demux->pf_control = Control;
58
59     return VLC_SUCCESS;
60 }
61
62 static int Demux( demux_t *p_demux )
63 {
64     int i_ret = -1;
65
66     xml_reader_t *p_xml_reader = NULL;
67     char *psz_elname = NULL;
68     const char *node;
69     input_item_t *p_input;
70     char *psz_mrl = NULL, *psz_title = NULL, *psz_genre = NULL;
71     char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
72     input_item_node_t *p_subitems = NULL;
73
74     input_item_t *p_current_input = GetCurrentItem(p_demux);
75
76     free( stream_ReadLine( p_demux->s ) );
77
78     p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
79     if( !p_xml_reader )
80         return -1;
81
82     /* xml */
83     /* check root node */
84     if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
85     {
86         msg_Err( p_demux, "invalid file (no root node)" );
87         goto end;
88     }
89
90     if( strcmp( node, "WinampXML" ) )
91     {
92         msg_Err( p_demux, "invalid root node: %s", node );
93         goto end;
94     }
95
96     /* root node should not have any attributes, and should only
97      * contain the "playlist node */
98
99     /* Skip until 1st child node */
100     while( (i_ret = xml_ReaderNextNode( p_xml_reader, &node )) != XML_READER_STARTELEM )
101         if( i_ret <= 0 )
102         {
103             msg_Err( p_demux, "invalid file (no child node)" );
104             goto end;
105         }
106
107     if( strcmp( node, "playlist" ) )
108     {
109         msg_Err( p_demux, "invalid child node %s", node );
110         goto end;
111     }
112
113     // Read the attributes
114     const char *attr, *value;
115     while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
116     {
117         if( !strcmp( attr, "num_entries" ) )
118             msg_Dbg( p_demux, "playlist has %d entries", atoi(value) );
119         else if( !strcmp( attr, "label" ) )
120             input_item_SetName( p_current_input, value );
121         else
122             msg_Warn( p_demux, "stray attribute %s with value %s in element"
123                       " <playlist>", attr, value );
124     }
125
126     p_subitems = input_item_node_Create( p_current_input );
127
128     while( (i_ret = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
129     {
130         // Get the node type
131         switch( i_ret )
132         {
133             case XML_READER_STARTELEM:
134             {
135                 // Read the element name
136                 free( psz_elname );
137                 psz_elname = strdup( node );
138                 if( unlikely(!psz_elname) )
139                     goto end;
140
141                 // Read the attributes
142                 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) )
143                 {
144                     if( !strcmp( psz_elname, "entry" ) &&
145                         !strcmp( attr, "Playstring" ) )
146                     {
147                         free( psz_mrl );
148                         psz_mrl = strdup( value );
149                     }
150                     else
151                     {
152                         msg_Warn( p_demux, "unexpected attribute %s in <%s>",
153                                   attr, psz_elname );
154                     }
155                 }
156                 break;
157             }
158
159             case XML_READER_TEXT:
160             {
161                 char **p;
162
163                 if( psz_elname == NULL )
164                     break;
165                 if( IsWhitespace( node ) )
166                     break;
167                 if( !strcmp( psz_elname, "Name" ) )
168                     p = &psz_title;
169                 else if( !strcmp( psz_elname, "Genre" ) )
170                     p = &psz_genre;
171                 else if( !strcmp( psz_elname, "Nowplaying" ) )
172                     p = &psz_now;
173                 else if( !strcmp( psz_elname, "Listeners" ) )
174                     p = &psz_listeners;
175                 else if( !strcmp( psz_elname, "Bitrate" ) )
176                     p = &psz_bitrate;
177                 else
178                 {
179                     msg_Warn( p_demux, "unexpected text in element <%s>",
180                               psz_elname );
181                     break;
182                 }
183                 free( *p );
184                 *p = strdup( node );
185                 break;
186             }
187
188             // End element
189             case XML_READER_ENDELEM:
190             {
191                 // Read the element name
192                 if( !strcmp( node, "entry" ) )
193                 {
194                     resolve_xml_special_chars( psz_mrl );
195                     p_input = input_item_New( psz_mrl, psz_title );
196                     if( psz_now )
197                         input_item_SetNowPlaying( p_input, psz_now );
198                     if( psz_genre )
199                         input_item_SetGenre( p_input, psz_genre );
200                     if( psz_listeners )
201                         msg_Err( p_demux, "Unsupported meta listeners" );
202                     if( psz_bitrate )
203                         msg_Err( p_demux, "Unsupported meta bitrate" );
204
205                     input_item_node_AppendItem( p_subitems, p_input );
206                     vlc_gc_decref( p_input );
207                     FREENULL( psz_title );
208                     FREENULL( psz_mrl );
209                     FREENULL( psz_genre );
210                     FREENULL( psz_bitrate );
211                     FREENULL( psz_listeners );
212                     FREENULL( psz_now );
213                 }
214                 FREENULL( psz_elname );
215                 break;
216             }
217         }
218     }
219
220     if( i_ret < 0 )
221     {
222         msg_Warn( p_demux, "error while parsing data" );
223         i_ret = 0; /* Needed for correct operation of go back */
224     }
225
226 end:
227     free( psz_elname );
228
229     if( p_subitems )
230         input_item_node_PostAndDelete( p_subitems );
231
232     vlc_gc_decref( p_current_input );
233     if( p_xml_reader )
234         xml_ReaderDelete( p_xml_reader );
235     return i_ret;
236 }
237
238 static bool IsWhitespace( const char *psz_string )
239 {
240     psz_string += strspn( psz_string, " \t\r\n" );
241     return !*psz_string;
242 }