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