1 /*****************************************************************************
2 * loadsave.c : Playlist loading / saving functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: loadsave.c,v 1.3 2004/01/06 08:50:20 zorglub Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 #include <stdlib.h> /* free(), strtol() */
24 #include <stdio.h> /* sprintf() */
25 #include <string.h> /* strerror() */
31 #include "stream_control.h"
32 #include "input_ext-intf.h"
34 #include "vlc_playlist.h"
36 #define PLAYLIST_FILE_HEADER_0_5 "# vlc playlist file version 0.5"
37 #define PLAYLIST_FILE_HEADER_0_6 "# vlc playlist file version 0.6"
40 /*****************************************************************************
41 * playlist_LoadFile: load a playlist file.
42 ****************************************************************************/
43 int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
51 msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
53 file = fopen( psz_filename, "rt" );
56 msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
59 fseek( file, 0L, SEEK_SET );
61 /* check the file is not empty */
62 if ( ! fgets( line, 1024, file ) )
64 msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
69 /* get rid of line feed */
70 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
72 line[strlen(line)-1] = (char)0;
73 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
75 /* check the file format is valid */
76 if ( !strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
80 else if( !strcmp ( line , PLAYLIST_FILE_HEADER_0_6 ) )
86 msg_Err( p_playlist, "playlist file %s format is unsupported"
93 i_current_status = p_playlist->i_status;
94 if ( p_playlist->i_status != PLAYLIST_STOPPED )
96 playlist_Stop ( p_playlist );
99 /* delete current content of the playlist */
100 for( i = p_playlist->i_size - 1; i >= 0; i-- )
102 playlist_Delete ( p_playlist , i );
105 /* simply add each line */
106 while( fgets( line, 1024, file ) )
108 /* ignore comments or empty lines */
109 if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
110 || (line[0] == (char)0) )
113 /* get rid of line feed */
114 if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
116 line[strlen(line)-1] = (char)0;
117 if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
121 playlist_Add ( p_playlist , (char *)&line , (char *)&line,
122 PLAYLIST_APPEND , PLAYLIST_END );
126 msg_Warn( p_playlist, "Not supported yet");
131 if ( i_current_status != PLAYLIST_STOPPED )
133 playlist_Play ( p_playlist );
141 /*****************************************************************************
142 * playlist_SaveFile: Save a playlist in a file.
143 *****************************************************************************/
144 int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
149 vlc_mutex_lock( &p_playlist->object_lock );
151 msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
153 file = fopen( psz_filename, "wt" );
156 msg_Err( p_playlist , "could not create playlist file %s"
160 /* Save is done in 0_5 mode at the moment*/
162 fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
164 for ( i = 0 ; i < p_playlist->i_size ; i++ )
166 fprintf( file , p_playlist->pp_items[i]->psz_uri );
167 fprintf( file , "\n" );
170 fprintf( file, PLAYLIST_FILE_HEADER_0_6 "\n" );
172 for ( i=0 ; i< p_playlist->i_size ; i++ )
174 fprintf( file, p_playlist->pp_items[i]->psz_uri );
175 fprintf( file, "||" );
176 fprintf( file, p_playlist->pp_items[i]->psz_name );
177 fprintf( file, "||" );
178 fprintf( file, "%i",p_playlist->pp_items[i]->b_enabled = VLC_TRUE ?
180 fprintf( file, "||" );
181 fprintf( file, "%i", p_playlist->pp_items[i]->i_group );
182 fprintf( file, "||" );
183 fprintf( file, p_playlist->pp_items[i]->psz_author );
184 fprintf( file , "\n" );
189 vlc_mutex_unlock( &p_playlist->object_lock );