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