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