]> git.sesse.net Git - vlc/blob - modules/misc/playlist/m3u.c
Don't include config.h from the headers - refs #297.
[vlc] / modules / misc / playlist / m3u.c
1 /*****************************************************************************
2  * m3u.c :  M3U playlist export module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@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/vlc.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
35 #include <vlc_input.h>
36 #include <vlc_meta.h>
37
38 #include <errno.h>                                                 /* ENOMEM */
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 int Export_M3U ( vlc_object_t * );
44
45 /*****************************************************************************
46  * Export_M3U: main export function
47  *****************************************************************************/
48 static void DoChildren( playlist_t *p_playlist, playlist_export_t *p_export,
49                         playlist_item_t *p_root )
50 {
51     int i, j;
52
53     /* Go through the playlist and add items */
54     for( i = 0; i< p_root->i_children ; i++)
55     {
56         playlist_item_t *p_current = p_root->pp_children[i];
57         assert( p_current );
58
59         if( p_current->i_flags & PLAYLIST_SAVE_FLAG )
60             continue;
61
62         if( p_current->i_children >= 0 )
63         {
64             DoChildren( p_playlist, p_export, p_current );
65             continue;
66         }
67
68         /* General info */
69
70         char *psz_uri = input_item_GetURI( p_current->p_input );
71
72         assert( psz_uri );
73
74         char *psz_name = input_item_GetName( p_current->p_input );
75         if( psz_name && strcmp( psz_uri, psz_name ) )
76         {
77             char *psz_artist = input_item_GetArtist( p_current->p_input );
78             if( psz_artist == NULL ) psz_artist = strdup( "" );
79             mtime_t i_duration = input_item_GetDuration( p_current->p_input );
80             if( psz_artist && *psz_artist )
81             {
82                 /* write EXTINF with artist */
83                 fprintf( p_export->p_file, "#EXTINF:%i,%s - %s\n",
84                           (int)( i_duration / 1000000 ), psz_artist, psz_name);
85             }
86             else
87             {
88                 /* write EXTINF without artist */
89                 fprintf( p_export->p_file, "#EXTINF:%i,%s\n",
90                          (int)( i_duration / 1000000 ), psz_name);
91             }
92             free( psz_artist );
93         }
94         free( psz_name );
95
96         /* VLC specific options */
97         vlc_mutex_lock( &p_current->p_input->lock );
98         for( j = 0; j < p_current->p_input->i_options; j++ )
99         {
100             fprintf( p_export->p_file, "#EXTVLCOPT:%s\n",
101                      p_current->p_input->ppsz_options[j][0] == ':' ?
102                      p_current->p_input->ppsz_options[j] + 1 :
103                      p_current->p_input->ppsz_options[j] );
104         }
105         vlc_mutex_unlock( &p_current->p_input->lock );
106
107         fprintf( p_export->p_file, "%s\n", psz_uri );
108         free( psz_uri );
109     }
110 }
111
112 int Export_M3U( vlc_object_t *p_this )
113 {
114     playlist_t *p_playlist = (playlist_t*)p_this;
115     playlist_export_t *p_export = (playlist_export_t *)p_playlist->p_private;
116
117     msg_Dbg(p_playlist, "saving using M3U format");
118
119     /* Write header */
120     fprintf( p_export->p_file, "#EXTM3U\n" );
121
122     DoChildren( p_playlist, p_export, p_export->p_root );
123     return VLC_SUCCESS;
124 }