]> git.sesse.net Git - vlc/blob - src/playlist/loadsave.c
* Makefile.am : Added src/playlist/item-ext.c and src/playlist/info.c
[vlc] / src / playlist / loadsave.c
1 /*****************************************************************************
2  * loadsave.c : Playlist loading / saving functions
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: loadsave.c,v 1.2 2004/01/05 12:59:43 zorglub Exp $
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., 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() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/sout.h>
30
31 #include "stream_control.h"
32 #include "input_ext-intf.h"
33
34 #include "vlc_playlist.h"
35
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"
38
39
40 /*****************************************************************************
41  * playlist_LoadFile: load a playlist file.
42  ****************************************************************************/
43 int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
44 {
45     FILE *file;
46     char line[1024];
47     int i_current_status;
48     int i_format;
49     int i;
50
51     msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
52
53     file = fopen( psz_filename, "rt" );
54     if( !file )
55     {
56         msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
57         return -1;
58     }
59     fseek( file, 0L, SEEK_SET );
60
61     /* check the file is not empty */
62     if ( ! fgets( line, 1024, file ) )
63     {
64         msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
65         fclose( file );
66         return -1;
67     }
68
69     /* get rid of line feed */
70     if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
71     {
72        line[strlen(line)-1] = (char)0;
73        if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
74     }
75     /* check the file format is valid */
76     if ( !strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
77     {
78        i_format = 5;
79     }
80     else if( !strcmp ( line , PLAYLIST_FILE_HEADER_0_6 ) )
81     {
82        i_format = 6;
83     }
84     else
85     {
86         msg_Err( p_playlist, "playlist file %s format is unsupported"
87                 , psz_filename );
88         fclose( file );
89         return -1;
90     }
91
92     /* stop playing */
93     i_current_status = p_playlist->i_status;
94     if ( p_playlist->i_status != PLAYLIST_STOPPED )
95     {
96         playlist_Stop ( p_playlist );
97     }
98
99     /* delete current content of the playlist */
100     for( i = p_playlist->i_size - 1; i >= 0; i-- )
101     {
102         playlist_Delete ( p_playlist , i );
103     }
104
105     /* simply add each line */
106     while( fgets( line, 1024, file ) )
107     {
108        /* ignore comments or empty lines */
109        if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
110                || (line[0] == (char)0) )
111            continue;
112
113        /* get rid of line feed */
114        if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
115        {
116            line[strlen(line)-1] = (char)0;
117            if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
118        }
119        if( i_format == 5 )
120        {
121            playlist_Add ( p_playlist , (char *)&line , (char *)&line,
122                          PLAYLIST_APPEND , PLAYLIST_END );
123        }
124        else
125        {
126            msg_Warn( p_playlist, "Not supported yet");
127        }
128     }
129
130     /* start playing */
131     if ( i_current_status != PLAYLIST_STOPPED )
132     {
133         playlist_Play ( p_playlist );
134     }
135
136     fclose( file );
137
138     return 0;
139 }
140
141 /*****************************************************************************
142  * playlist_SaveFile: Save a playlist in a file.
143  *****************************************************************************/
144 int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
145 {
146     FILE *file;
147     int i;
148
149     vlc_mutex_lock( &p_playlist->object_lock );
150
151     msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
152
153     file = fopen( psz_filename, "wt" );
154     if( !file )
155     {
156         msg_Err( p_playlist , "could not create playlist file %s"
157                 , psz_filename );
158         return -1;
159     }
160     /* Save is done in 0_5 mode at the moment*/
161
162     fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
163
164     for ( i = 0 ; i < p_playlist->i_size ; i++ )
165     {
166         fprintf( file , p_playlist->pp_items[i]->psz_uri );
167         fprintf( file , "\n" );
168     }
169 #if 0
170     fprintf( file, PLAYLIST_FILE_HEADER_0_6 "\n" );
171
172     for ( i=0 ; i< p_playlist->i_size ; i++ )
173     {
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 ?
179                        1:0 );
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" );
185     }
186 #endif
187     fclose( file );
188
189     vlc_mutex_unlock( &p_playlist->object_lock );
190
191     return 0;
192 }