]> git.sesse.net Git - vlc/blob - modules/misc/playlist/xspf.c
A bit of headers cleanup
[vlc] / modules / misc / playlist / xspf.c
1 /******************************************************************************
2  * xspf.c : XSPF playlist export functions
3  ******************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Daniel Stränger <vlc at schmaller dot de>
8  *          Yoann Peronneau <yoann@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *******************************************************************************/
24
25 /**
26  * \file modules/misc/playlist/xspf.c
27  * \brief XSPF playlist export functions
28  */
29 #include <stdio.h>
30 #include <vlc/vlc.h>
31 #include <vlc_interface.h>
32 #include <vlc_playlist.h>
33 #include <vlc_input.h>
34 #include <vlc_meta.h>
35 #include <vlc_strings.h>
36 #include <vlc_charset.h>
37 #include "xspf.h"
38
39 /**
40  * \brief Prints the XSPF header to file, writes each item by xspf_export_item()
41  * and closes the open xml elements
42  * \param p_this the VLC playlist object
43  * \return VLC_SUCCESS if some memory is available, otherwise VLC_ENONMEM
44  */
45 int E_(xspf_export_playlist)( vlc_object_t *p_this )
46 {
47     const playlist_t *p_playlist = (playlist_t *)p_this;
48     const playlist_export_t *p_export =
49         (playlist_export_t *)p_playlist->p_private;
50     int               i, i_count;
51     char             *psz_temp;
52     playlist_item_t  *p_node = p_export->p_root;
53
54     /* write XSPF XML header */
55     fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
56     fprintf( p_export->p_file,
57              "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n" );
58
59     if( !p_node ) return VLC_SUCCESS;
60
61     /* save name of the playlist node */
62     psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
63     if( *psz_temp )
64     {
65         fprintf(  p_export->p_file, "\t<title>%s</title>\n", psz_temp );
66     }
67     free( psz_temp );
68
69     /* save location of the playlist node */
70     psz_temp = assertUTF8URI( p_export->psz_filename );
71     if( psz_temp && *psz_temp )
72     {
73         fprintf( p_export->p_file, "\t<location>%s</location>\n", psz_temp );
74         free( psz_temp );
75     }
76
77     /* export all items in a flat format */
78     fprintf( p_export->p_file, "\t<trackList>\n" );
79     i_count = 0;
80     for( i = 0; i < p_node->i_children; i++ )
81     {
82         xspf_export_item( p_node->pp_children[i], p_export->p_file,
83                           &i_count );
84     }
85     fprintf( p_export->p_file, "\t</trackList>\n" );
86
87     /* export the tree structure in <extension> */
88     fprintf( p_export->p_file, "\t<extension>\n" );
89     i_count = 0;
90     for( i = 0; i < p_node->i_children; i++ )
91     {
92         xspf_extension_item( p_node->pp_children[i], p_export->p_file,
93                              &i_count );
94     }
95     fprintf( p_export->p_file, "\t</extension>\n" );
96
97     /* close the header elements */
98     fprintf( p_export->p_file, "</playlist>\n" );
99
100     return VLC_SUCCESS;
101 }
102
103 /**
104  * \brief exports one item to file or traverse if item is a node
105  * \param p_item playlist item to export
106  * \param p_file file to write xml-converted item to
107  * \param p_i_count counter for track identifiers
108  */
109 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
110                               int *p_i_count )
111 {
112     char *psz;
113     char *psz_temp;
114
115     if( !p_item ) return;
116
117     /* if we get a node here, we must traverse it */
118     if( p_item->i_children > 0 )
119     {
120         int i;
121         for( i = 0; i < p_item->i_children; i++ )
122         {
123             xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
124         }
125         return;
126     }
127
128     /* don't write empty nodes */
129     if( p_item->i_children == 0 )
130     {
131         return;
132     }
133
134     /* leaves can be written directly */
135     fprintf( p_file, "\t\t<track>\n" );
136
137     /* print identifier and increase the counter */
138     fprintf( p_file, "\t\t\t<identifier>%i</identifier>\n", *p_i_count );
139     ( *p_i_count )++;
140
141     /* -> the location */
142     if( p_item->p_input->psz_uri && *p_item->p_input->psz_uri )
143     {
144         psz = assertUTF8URI( p_item->p_input->psz_uri );
145         fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
146         free( psz );
147     }
148
149     /* -> the name/title (only if different from uri)*/
150     if( p_item->p_input->psz_name &&
151         p_item->p_input->psz_uri &&
152         strcmp( p_item->p_input->psz_uri, p_item->p_input->psz_name ) )
153     {
154         psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
155         if( *psz_temp )
156             fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
157         free( psz_temp );
158     }
159
160     if( p_item->p_input->p_meta == NULL )
161     {
162         goto xspfexportitem_end;
163     }
164
165     /* -> the artist/creator */
166     psz = p_item->p_input->p_meta->psz_artist ?
167                         strdup( p_item->p_input->p_meta->psz_artist ):
168                         strdup( "" );
169     if( psz && !*psz )
170     {
171         free( psz );
172         psz = NULL;
173     }
174     if( !psz )
175     {
176         psz = p_item->p_input->p_meta->psz_artist ?
177                         strdup( p_item->p_input->p_meta->psz_artist ):
178                         strdup( "" );
179     }
180     psz_temp = convert_xml_special_chars( psz );
181     if( psz ) free( psz );
182     if( *psz_temp )
183     {
184         fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
185     }
186     free( psz_temp );
187
188     /* -> the album */
189     psz = p_item->p_input->p_meta->psz_album ?
190                         strdup( p_item->p_input->p_meta->psz_album ):
191                         strdup( "" );
192     psz_temp = convert_xml_special_chars( psz );
193     if( psz ) free( psz );
194     if( *psz_temp )
195     {
196         fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
197     }
198     free( psz_temp );
199
200     /* -> the track number */
201     psz = p_item->p_input->p_meta->psz_tracknum ?
202                         strdup( p_item->p_input->p_meta->psz_tracknum ):
203                         strdup( "" );
204     if( psz )
205     {
206         if( *psz )
207         {
208             fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
209         }
210         free( psz );
211     }
212
213 xspfexportitem_end:
214     /* -> the duration */
215     if( p_item->p_input->i_duration > 0 )
216     {
217         fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
218                  (long)(p_item->p_input->i_duration / 1000) );
219     }
220
221     fprintf( p_file, "\t\t</track>\n" );
222
223     return;
224 }
225
226 /**
227  * \brief exports one item in extension to file and traverse if item is a node
228  * \param p_item playlist item to export
229  * \param p_file file to write xml-converted item to
230  * \param p_i_count counter for track identifiers
231  */
232 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
233                                  int *p_i_count )
234 {
235     if( !p_item ) return;
236
237     /* if we get a node here, we must traverse it */
238     if( p_item->i_children >= 0 )
239     {
240         int i;
241         char *psz_temp;
242         psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
243         fprintf( p_file, "\t\t<node title=\"%s\">\n",
244                  *psz_temp ? p_item->p_input->psz_name : "" );
245         free( psz_temp );
246
247         for( i = 0; i < p_item->i_children; i++ )
248         {
249             xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
250         }
251
252         fprintf( p_file, "\t\t</node>\n" );
253         return;
254     }
255
256
257     /* print leaf and increase the counter */
258     fprintf( p_file, "\t\t\t<item href=\"%i\" />\n", *p_i_count );
259     ( *p_i_count )++;
260
261     return;
262 }
263
264 /**
265  * \param psz_name the location of the media ressource (e.g. local file,
266  *        device, network stream, etc.)
267  * \return a new char buffer which asserts that the location is valid UTF-8
268  *         and a valid URI
269  * \note the returned buffer must be freed, when it isn't used anymore
270  */
271 static char *assertUTF8URI( char *psz_name )
272 {
273     char *psz_ret = NULL;              /**< the new result buffer to return */
274     char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
275     vlc_bool_t b_name_is_uri = VLC_FALSE;
276
277     if( !psz_name || !*psz_name )
278         return NULL;
279
280     /* check that string is valid UTF-8 */
281     /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
282     if( !( psz_s = EnsureUTF8( psz_name ) ) )
283         return NULL;
284
285     /* max. 3x for URI conversion (percent escaping) and
286        8 bytes for "file://" and NULL-termination */
287     psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
288     if( !psz_ret )
289         return NULL;
290
291     /** \todo check for a valid scheme part preceding the colon */
292     if( strchr( psz_s, ':' ) )
293     {
294         psz_d = psz_ret;
295         b_name_is_uri = VLC_TRUE;
296     }
297     /* assume "file" scheme if no scheme-part is included */
298     else
299     {
300         strcpy( psz_ret, "file://" );
301         psz_d = psz_ret + 7;
302     }
303
304     while( *psz_s )
305     {
306         /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
307         if( *psz_s & B10000000 ||
308             *psz_s == '<' ||
309             *psz_s == '>' ||
310             *psz_s == '&' ||
311             *psz_s == ' ' ||
312             ( *psz_s == '%' && !b_name_is_uri ) )
313         {
314             *psz_d++ = '%';
315             *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
316             *psz_d++ = hexchars[*psz_s & B00001111];
317         }
318         else
319         {
320             *psz_d++ = *psz_s;
321         }
322
323         psz_s++;
324     }
325     *psz_d = '\0';
326
327     return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );
328 }