asx: fix mimetype and stream Peek
[vlc.git] / modules / demux / playlist / pls.c
blob8ea3202b7071e054545e86646c0fdc04f65ac9a7
1 /*****************************************************************************
2 * pls.c : PLS playlist format import
3 *****************************************************************************
4 * Copyright (C) 2004 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_access.h>
35 #include "playlist.h"
37 /*****************************************************************************
38 * Local prototypes
39 *****************************************************************************/
40 static int ReadDir( stream_t *, input_item_node_t * );
42 /*****************************************************************************
43 * Import_PLS: main import function
44 *****************************************************************************/
45 int Import_PLS( vlc_object_t *p_this )
47 stream_t *p_demux = (stream_t *)p_this;
48 const uint8_t *p_peek;
50 CHECK_FILE(p_demux);
52 if( vlc_stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) {
53 msg_Dbg( p_demux, "not enough data" );
54 return VLC_EGENERIC;
57 if( strncasecmp( (const char *)p_peek, "[playlist]", 10 )
58 && strncasecmp( (const char *)p_peek, "[Reference]", 10 )
59 && !stream_HasExtension( p_demux, ".pls" ) )
60 return VLC_EGENERIC;
62 msg_Dbg( p_demux, "found valid PLS playlist file");
63 p_demux->pf_readdir = ReadDir;
64 p_demux->pf_control = access_vaDirectoryControlHelper;
66 return VLC_SUCCESS;
69 static int ReadDir( stream_t *p_demux, input_item_node_t *p_subitems )
71 char *psz_name = NULL;
72 char *psz_line;
73 char *psz_mrl = NULL;
74 char *psz_mrl_orig = NULL;
75 char *psz_key;
76 char *psz_value;
77 int i_item = -1;
78 input_item_t *p_input;
80 input_item_t *p_current_input = GetCurrentItem(p_demux);
82 while( ( psz_line = vlc_stream_ReadLine( p_demux->s ) ) )
84 if( !strncasecmp( psz_line, "[playlist]", sizeof("[playlist]")-1 ) ||
85 !strncasecmp( psz_line, "[Reference]", sizeof("[Reference]")-1 ) )
87 free( psz_line );
88 continue;
90 psz_key = psz_line;
91 psz_value = strchr( psz_line, '=' );
92 if( psz_value )
94 *psz_value='\0';
95 psz_value++;
97 else
99 free( psz_line );
100 continue;
102 if( !strcasecmp( psz_key, "version" ) )
104 msg_Dbg( p_demux, "pls file version: %s", psz_value );
105 free( psz_line );
106 continue;
108 if( !strcasecmp( psz_key, "numberofentries" ) )
110 msg_Dbg( p_demux, "pls should have %d entries", atoi(psz_value) );
111 free( psz_line);
112 continue;
115 /* find the number part of of file1, title1 or length1 etc */
116 int i_new_item;
117 if( sscanf( psz_key, "%*[^0-9]%d", &i_new_item ) != 1 )
119 msg_Warn( p_demux, "couldn't find number of items" );
120 free( psz_line );
121 continue;
124 if( i_item == -1 )
125 i_item = i_new_item;
126 else if( i_item != i_new_item )
128 /* we found a new item, insert the previous */
129 if( psz_mrl )
131 p_input = input_item_New( psz_mrl, psz_name );
132 input_item_CopyOptions( p_input, p_current_input );
133 input_item_node_AppendItem( p_subitems, p_input );
134 input_item_Release( p_input );
135 free( psz_mrl_orig );
136 psz_mrl_orig = psz_mrl = NULL;
138 else
140 msg_Warn( p_demux, "no file= part found for item %d", i_item );
142 free( psz_name );
143 psz_name = NULL;
144 i_item = i_new_item;
147 if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) ||
148 !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
150 free( psz_mrl_orig );
151 psz_mrl_orig =
152 psz_mrl = ProcessMRL( psz_value, p_demux->psz_url );
154 if( !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
156 if( !strncasecmp( psz_mrl, "http://", sizeof("http://") -1 ) )
157 memcpy( psz_mrl, "mmsh", 4 );
160 else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
162 free( psz_name );
163 psz_name = strdup( psz_value );
165 else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
166 /* duration in seconds */;
167 else
169 msg_Warn( p_demux, "unknown key found in pls file: %s", psz_key );
171 free( psz_line );
173 /* Add last object */
174 if( psz_mrl )
176 p_input = input_item_New( psz_mrl, psz_name );
177 input_item_CopyOptions( p_input, p_current_input );
178 input_item_node_AppendItem( p_subitems, p_input );
179 input_item_Release( p_input );
180 free( psz_mrl_orig );
182 else
184 msg_Warn( p_demux, "no file= part found for item %d", i_item );
186 free( psz_name );
187 psz_name = NULL;
189 return VLC_SUCCESS;