]> git.sesse.net Git - vlc/blob - src/playlist/loadsave.c
Do not pass too much argument to sprintf
[vlc] / src / playlist / loadsave.c
1 /*****************************************************************************
2  * loadsave.c : Playlist loading / saving functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.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 #include <vlc/vlc.h>
24 #include <vlc_playlist.h>
25 #include "playlist_internal.h"
26 #include "misc/configuration.h"
27 #include <vlc_charset.h>
28
29 #include <errno.h>
30
31 int playlist_Export( playlist_t * p_playlist, const char *psz_filename ,
32                      playlist_item_t *p_export_root,const char *psz_type )
33 {
34     module_t *p_module;
35     playlist_export_t *p_export;
36
37     if( p_export_root == NULL ) return VLC_EGENERIC;
38
39     msg_Info( p_playlist, "saving %s to file %s",
40                     p_export_root->p_input->psz_name, psz_filename );
41
42     /* Prepare the playlist_export_t structure */
43     p_export = (playlist_export_t *)malloc( sizeof(playlist_export_t) );
44     if( !p_export)
45     {
46         msg_Err( p_playlist, "out of memory" );
47         return VLC_ENOMEM;
48     }
49     p_export->psz_filename = NULL;
50     if ( psz_filename )
51         p_export->psz_filename = strdup( psz_filename );
52     p_export->p_file = utf8_fopen( psz_filename, "wt" );
53     if( !p_export->p_file )
54     {
55         msg_Err( p_playlist , "could not create playlist file %s"
56                  " (%s)", psz_filename, strerror(errno) );
57         return VLC_EGENERIC;
58     }
59
60     p_export->p_root = p_export_root;
61
62     /* Lock the playlist */
63     vlc_mutex_lock( &p_playlist->object_lock );
64     p_playlist->p_private = (void *)p_export;
65
66     /* And call the module ! All work is done now */
67     p_module = module_Need( p_playlist, "playlist export", psz_type, VLC_TRUE);
68     if( !p_module )
69     {
70         msg_Warn( p_playlist, "exporting playlist failed" );
71         vlc_mutex_unlock( &p_playlist->object_lock );
72         return VLC_ENOOBJ;
73     }
74     module_Unneed( p_playlist , p_module );
75
76     /* Clean up */
77     fclose( p_export->p_file );
78     if ( p_export->psz_filename )
79         free( p_export->psz_filename );
80     free ( p_export );
81     p_playlist->p_private = NULL;
82     vlc_mutex_unlock( &p_playlist->object_lock );
83
84     return VLC_SUCCESS;
85 }
86
87 int playlist_MLLoad( playlist_t *p_playlist )
88 {
89 #ifndef THIS_PIECE_OF_CODE_IS_FIXED // see #1047 and possibly others
90     (void)p_playlist;
91 #else
92     char *psz_uri, *psz_homedir =p_playlist->p_libvlc->psz_homedir;
93     input_item_t *p_input;
94
95     if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
96     if( !psz_homedir )
97     {
98         msg_Err( p_playlist, "no home directory, cannot load media library") ;
99         return VLC_EGENERIC;
100     }
101     asprintf( &psz_uri, "file/xspf-open://%s" DIR_SEP CONFIG_DIR DIR_SEP
102                         "ml.xsp", psz_homedir );
103
104     p_input = input_ItemNewExt( p_playlist, psz_uri,
105                                 _("Media Library"), 0, NULL, -1 );
106     p_playlist->p_ml_category->p_input = p_input;
107     p_playlist->p_ml_onelevel->p_input = p_input;
108
109     p_playlist->b_doing_ml = VLC_TRUE;
110     stats_TimerStart( p_playlist, "ML Load", STATS_TIMER_ML_LOAD );
111     input_Read( p_playlist, p_input, VLC_TRUE );
112     stats_TimerStop( p_playlist,STATS_TIMER_ML_LOAD );
113     p_playlist->b_doing_ml = VLC_FALSE;
114
115     free( psz_uri );
116 #endif
117     return VLC_SUCCESS;
118 }
119
120 int playlist_MLDump( playlist_t *p_playlist )
121 {
122     char *psz_uri, *psz_homedir = p_playlist->p_libvlc->psz_homedir;
123     if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
124     if( !psz_homedir )
125     {
126         msg_Err( p_playlist, "no home directory, cannot save media library") ;
127         return VLC_EGENERIC;
128     }
129
130     char psz_dirname[ strlen( psz_homedir ) + sizeof( DIR_SEP CONFIG_DIR ) ];
131     sprintf( psz_dirname, "%s" DIR_SEP CONFIG_DIR, psz_homedir );
132     if( config_CreateDir( (vlc_object_t *)p_playlist, psz_dirname ) )
133     {
134         return VLC_EGENERIC;
135     }
136
137     asprintf( &psz_uri, "%s" DIR_SEP "%s", psz_dirname, "ml.xsp" );
138     stats_TimerStart( p_playlist, "ML Dump", STATS_TIMER_ML_DUMP );
139     playlist_Export( p_playlist, psz_uri, p_playlist->p_ml_category,
140                      "export-xspf" );
141     stats_TimerStop( p_playlist, STATS_TIMER_ML_DUMP );
142
143     free( psz_uri );
144     return VLC_SUCCESS;
145 }