stream: replace input_thread_t by input_item_t
[vlc.git] / modules / demux / playlist / pls.c
blobb9ed4f88ac7a8b3a74bff7165121208671a8bd5f
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>
34 #include <vlc_charset.h>
36 #include "playlist.h"
38 /*****************************************************************************
39 * Local prototypes
40 *****************************************************************************/
41 static int ReadDir( stream_t *, input_item_node_t * );
43 /*****************************************************************************
44 * Import_PLS: main import function
45 *****************************************************************************/
46 int Import_PLS( vlc_object_t *p_this )
48 stream_t *p_demux = (stream_t *)p_this;
49 const uint8_t *p_peek;
51 CHECK_FILE(p_demux);
53 if( vlc_stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) {
54 msg_Dbg( p_demux, "not enough data" );
55 return VLC_EGENERIC;
58 if( strncasecmp( (const char *)p_peek, "[playlist]", 10 )
59 && strncasecmp( (const char *)p_peek, "[Reference]", 10 )
60 && !stream_HasExtension( p_demux, ".pls" ) )
61 return VLC_EGENERIC;
63 msg_Dbg( p_demux, "found valid PLS playlist file");
64 p_demux->pf_readdir = ReadDir;
65 p_demux->pf_control = access_vaDirectoryControlHelper;
67 return VLC_SUCCESS;
70 static int ReadDir( stream_t *p_demux, input_item_node_t *p_subitems )
72 char *psz_name = NULL;
73 char *psz_line;
74 char *psz_mrl = NULL;
75 char *psz_mrl_orig = NULL;
76 char *psz_key;
77 char *psz_value;
78 int i_item = -1;
79 input_item_t *p_input;
80 bool ascii = true;
81 bool unicode = true;
83 input_item_t *p_current_input = GetCurrentItem(p_demux);
85 while( ( psz_line = vlc_stream_ReadLine( p_demux->s ) ) )
87 if (ascii && !IsASCII(psz_line))
89 unicode = IsUTF8(psz_line);
90 ascii = false;
93 if (!unicode)
95 char *latin = FromLatin1(psz_line);
96 free(psz_line);
97 psz_line = latin;
100 if( !strncasecmp( psz_line, "[playlist]", sizeof("[playlist]")-1 ) ||
101 !strncasecmp( psz_line, "[Reference]", sizeof("[Reference]")-1 ) )
103 free( psz_line );
104 continue;
106 psz_key = psz_line;
107 psz_value = strchr( psz_line, '=' );
108 if( psz_value )
110 *psz_value='\0';
111 psz_value++;
113 else
115 free( psz_line );
116 continue;
118 if( !strcasecmp( psz_key, "version" ) )
120 msg_Dbg( p_demux, "pls file version: %s", psz_value );
121 free( psz_line );
122 continue;
124 if( !strcasecmp( psz_key, "numberofentries" ) )
126 msg_Dbg( p_demux, "pls should have %d entries", atoi(psz_value) );
127 free( psz_line);
128 continue;
131 /* find the number part of of file1, title1 or length1 etc */
132 int i_new_item;
133 if( sscanf( psz_key, "%*[^0-9]%d", &i_new_item ) != 1 )
135 msg_Warn( p_demux, "couldn't find number of items" );
136 free( psz_line );
137 continue;
140 if( i_item == -1 )
141 i_item = i_new_item;
142 else if( i_item != i_new_item )
144 /* we found a new item, insert the previous */
145 if( psz_mrl )
147 p_input = input_item_New( psz_mrl, psz_name );
148 input_item_CopyOptions( p_input, p_current_input );
149 input_item_node_AppendItem( p_subitems, p_input );
150 input_item_Release( p_input );
151 free( psz_mrl_orig );
152 psz_mrl_orig = psz_mrl = NULL;
154 else
156 msg_Warn( p_demux, "no file= part found for item %d", i_item );
158 free( psz_name );
159 psz_name = NULL;
160 i_item = i_new_item;
163 if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) ||
164 !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
166 free( psz_mrl_orig );
167 psz_mrl_orig =
168 psz_mrl = ProcessMRL( psz_value, p_demux->psz_url );
170 if( !strncasecmp( psz_key, "Ref", sizeof("Ref") -1 ) )
172 if( !strncasecmp( psz_mrl, "http://", sizeof("http://") -1 ) )
173 memcpy( psz_mrl, "mmsh", 4 );
176 else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
178 free( psz_name );
179 psz_name = strdup( psz_value );
181 else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
182 /* duration in seconds */;
183 else
185 msg_Warn( p_demux, "unknown key found in pls file: %s", psz_key );
187 free( psz_line );
189 /* Add last object */
190 if( psz_mrl )
192 p_input = input_item_New( psz_mrl, psz_name );
193 input_item_CopyOptions( p_input, p_current_input );
194 input_item_node_AppendItem( p_subitems, p_input );
195 input_item_Release( p_input );
196 free( psz_mrl_orig );
198 else
200 msg_Warn( p_demux, "no file= part found for item %d", i_item );
202 free( psz_name );
203 psz_name = NULL;
205 return VLC_SUCCESS;