]> git.sesse.net Git - vlc/blob - modules/demux/playlist/b4s.c
Plugins: include vlc_common.h directly instead of vlc/vlc.h
[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
36 #include "playlist.h"
37 #include "vlc_xml.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     INIT_PLAYLIST_STUFF;
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 = 0;
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         vlc_object_release( p_playlist );
113         return -1;
114     }
115
116     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
117         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
118         strcmp( psz_elname, "WinampXML" ) )
119     {
120         msg_Err( p_demux, "invalid root node %i, %s",
121                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
122         free( psz_elname );
123         vlc_object_release( p_playlist );
124         return -1;
125     }
126     free( psz_elname );
127
128     /* root node should not have any attributes, and should only
129      * contain the "playlist node */
130
131     /* Skip until 1st child node */
132     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 &&
133            xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
134     if( i_ret != 1 )
135     {
136         msg_Err( p_demux, "invalid file (no child node)" );
137         return -1;
138     }
139
140     if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
141         strcmp( psz_elname, "playlist" ) )
142     {
143         msg_Err( p_demux, "invalid child node %s", psz_elname );
144         free( psz_elname );
145         return -1;
146     }
147     free( psz_elname ); psz_elname = 0;
148
149     // Read the attributes
150     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
151     {
152         char *psz_name = xml_ReaderName( p_xml_reader );
153         char *psz_value = xml_ReaderValue( p_xml_reader );
154         if( !psz_name || !psz_value ) return -1;
155         if( !strcmp( psz_name, "num_entries" ) )
156         {
157             msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
158         }
159         else if( !strcmp( psz_name, "label" ) )
160         {
161             input_item_SetName( p_current_input, psz_value );
162         }
163         else
164         {
165             msg_Warn( p_demux, "stray attribute %s with value %s in element"
166                       " 'playlist'", psz_name, psz_value );
167         }
168         free( psz_name );
169         free( psz_value );
170     }
171
172     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
173     {
174         // Get the node type
175         i_type = xml_ReaderNodeType( p_xml_reader );
176         switch( i_type )
177         {
178             // Error
179             case -1:
180                 return -1;
181                 break;
182
183             case XML_READER_STARTELEM:
184             {
185                 // Read the element name
186                 free( psz_elname );
187                 psz_elname = xml_ReaderName( p_xml_reader );
188                 if( !psz_elname ) return -1;
189
190
191                 // Read the attributes
192                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
193                 {
194                     char *psz_name = xml_ReaderName( p_xml_reader );
195                     char *psz_value = xml_ReaderValue( p_xml_reader );
196                     if( !psz_name || !psz_value ) return -1;
197                     if( !strcmp( psz_elname, "entry" ) &&
198                         !strcmp( psz_name, "Playstring" ) )
199                     {
200                         psz_mrl = strdup( psz_value );
201                     }
202                     else
203                     {
204                         msg_Warn( p_demux, "unexpected attribure %s in element %s",
205                                   psz_name, psz_elname );
206                     }
207                     free( psz_name );
208                     free( psz_value );
209                 }
210                 break;
211             }
212             case XML_READER_TEXT:
213             {
214                 char *psz_text = xml_ReaderValue( p_xml_reader );
215                 if( IsWhitespace( psz_text ) )
216                 {
217                     free( psz_text );
218                     break;
219                 }
220                 if( !strcmp( psz_elname, "Name" ) )
221                 {
222                     psz_name = strdup( psz_text );
223                 }
224                 else if( !strcmp( psz_elname, "Genre" ) )
225                 {
226                     psz_genre = strdup( psz_text );
227                 }
228                 else if( !strcmp( psz_elname, "Nowplaying" ) )
229                 {
230                     psz_now = strdup( psz_text );
231                 }
232                 else if( !strcmp( psz_elname, "Listeners" ) )
233                 {
234                     psz_listeners = strdup( psz_text );
235                 }
236                 else if( !strcmp( psz_elname, "Bitrate" ) )
237                 {
238                     psz_bitrate = strdup( psz_text );
239                 }
240                 else if( !strcmp( psz_elname, "" ) )
241                 {
242                     ;
243                 }
244                 else
245                 {
246                     msg_Warn( p_demux, "unexpected text in element '%s'",
247                               psz_elname );
248                 }
249                 free( psz_text );
250                 break;
251             }
252             // End element
253             case XML_READER_ENDELEM:
254             {
255                 // Read the element name
256                 free( psz_elname );
257                 psz_elname = xml_ReaderName( p_xml_reader );
258                 if( !psz_elname ) return -1;
259                 if( !strcmp( psz_elname, "entry" ) )
260                 {
261                     p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
262                                                 0, NULL, -1 );
263                     if( psz_now )
264                         input_item_SetNowPlaying( p_input, psz_now );
265                     if( psz_genre )
266                         input_item_SetGenre( p_input, psz_genre );
267                     if( psz_listeners )
268                         msg_Err( p_playlist, "Unsupported meta listeners" );
269                     if( psz_bitrate )
270                         msg_Err( p_playlist, "Unsupported meta bitrate" );
271
272                     input_ItemAddSubItem( p_current_input, p_input );
273                     vlc_gc_decref( p_input );
274                     FREENULL( psz_name );
275                     FREENULL( psz_mrl );
276                     FREENULL( psz_genre );
277                     FREENULL( psz_bitrate );
278                     FREENULL( psz_listeners );
279                     FREENULL( psz_now );
280                 }
281                 free( psz_elname );
282                 psz_elname = strdup("");
283
284                 break;
285             }
286         }
287     }
288
289     if( i_ret != 0 )
290     {
291         msg_Warn( p_demux, "error while parsing data" );
292     }
293
294     HANDLE_PLAY_AND_RELEASE;
295     return 0; /* Needed for correct operation of go back */
296 }
297
298 static int Control( demux_t *p_demux, int i_query, va_list args )
299 {
300     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
301     return VLC_EGENERIC;
302 }
303
304 #if 0
305 /**
306  * Get a in-string pointer to the start of the next token from a
307  * string terminating the pointer returned by a previous call.
308  *
309  * \param psz_cur_string The string to search for the token from
310  * \return a pointer to withing psz_cur_string, or NULL if no token
311  * was found
312  * \note The returned pointer may contain more than one
313  * token, Run GetNextToken once more to terminate the token properly
314  */
315 static char *GetNextToken(char *psz_cur_string) {
316     while (*psz_cur_string && !isspace(*psz_cur_string))
317         psz_cur_string++;
318     if (!*psz_cur_string)
319         return NULL;
320     *psz_cur_string++ = '\0';
321     while (*psz_cur_string && isspace(*psz_cur_string))
322         psz_cur_string++;
323     return psz_cur_string;
324 }
325 #endif
326
327 static int IsWhitespace( char *psz_string )
328 {
329     while( *psz_string )
330     {
331         if( *psz_string != ' ' && *psz_string != '\t' && *psz_string != '\r' &&
332             *psz_string != '\n' )
333         {
334             return false;
335         }
336         psz_string++;
337     }
338     return true;
339 }