]> git.sesse.net Git - vlc/blob - modules/misc/playlist/m3u.c
c383be6624f963e919db60a13e842293c0097103
[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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include <errno.h>                                                 /* ENOMEM */
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 int Export_M3U ( vlc_object_t * );
38
39 /*****************************************************************************
40  * Export_M3U: main export function
41  *****************************************************************************/
42 int Export_M3U( vlc_object_t *p_this )
43 {
44     playlist_t *p_playlist = (playlist_t*)p_this;
45     playlist_export_t *p_export = (playlist_export_t *)p_playlist->p_private;
46     int i, j;
47
48     msg_Dbg(p_playlist, "Saving using M3U format");
49
50     /* Write header */
51     fprintf( p_export->p_file, "#EXTM3U\n" );
52
53     /* Go through the playlist and add items */
54     for( i = 0; i< p_playlist->i_size ; i++)
55     {
56         if( (p_playlist->pp_items[i]->i_flags & PLAYLIST_SAVE_FLAG) == 0 )
57         {
58             continue;
59         }
60
61         /* General info */
62         if( p_playlist->pp_items[i]->input.psz_name &&
63              strcmp( p_playlist->pp_items[i]->input.psz_name,
64                     p_playlist->pp_items[i]->input.psz_uri ) )
65         {
66             char *psz_author =
67                    vlc_input_item_GetInfo( &p_playlist->pp_items[i]->input,
68                                          _("Meta-information"), _("Artist") );
69             if( psz_author && *psz_author )
70             {
71                 /* the author must be escaped if it contains a comma */
72                 char *p_src; short i_cnt;
73                 /* so count the commas or backslash */
74                 for( i_cnt = 0, p_src = psz_author; *p_src; p_src++ )
75                     if(*p_src == ',' || *p_src == '\\' )
76                         i_cnt++;
77                 /* Is there a comma ? */
78                 if( i_cnt )
79                 {
80                     char *psz_escaped=NULL;
81                     char *p_dst;
82                     psz_escaped = (char *)malloc( ( strlen( psz_author )
83                                              + i_cnt + 1 ) * sizeof( char ) );
84                     if( !psz_escaped )
85                         return VLC_ENOMEM;
86                     /* copy the string and escape every comma with backslash */
87                     for( p_src=psz_author, p_dst=psz_escaped; *p_src;
88                          p_src++, p_dst++ )
89                     {
90                         if( *p_src == ',' || *p_src == '\\' )
91                             *p_dst++ = '\\';
92                         *p_dst = *p_src;
93                     }
94                     *p_dst = '\0';
95                     free( psz_author);
96                     psz_author = psz_escaped;
97                 }
98                 fprintf( p_export->p_file, "#EXTINF:%i,%s,%s\n",
99                        (int)(p_playlist->pp_items[i]->input.i_duration/1000000),
100                          psz_author,
101                          p_playlist->pp_items[i]->input.psz_name );
102             }
103             else
104             {
105                 /* write EXTINF without author */
106                 fprintf( p_export->p_file, "#EXTINF:%i,,%s\n",
107                        (int)(p_playlist->pp_items[i]->input.i_duration/1000000),
108                          p_playlist->pp_items[i]->input.psz_name );
109             }
110             if( psz_author )
111                 free( psz_author );
112         }
113
114         /* VLC specific options */
115         for( j = 0; j < p_playlist->pp_items[i]->input.i_options; j++ )
116         {
117             fprintf( p_export->p_file, "#EXTVLCOPT:%s\n",
118                      p_playlist->pp_items[i]->input.ppsz_options[j][0] == ':' ?
119                      p_playlist->pp_items[i]->input.ppsz_options[j] + 1 :
120                      p_playlist->pp_items[i]->input.ppsz_options[j] );
121         }
122
123         fprintf( p_export->p_file, "%s\n",
124                  p_playlist->pp_items[i]->input.psz_uri );
125     }
126     return VLC_SUCCESS;
127 }