]> git.sesse.net Git - vlc/blob - modules/demux/playlist/wpl.c
xspf: cosmetics and fix potential memleak.
[vlc] / modules / demux / playlist / wpl.c
1 /*****************************************************************************
2  * wpl.c : WPL playlist format import
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  *
6  * Authors: Su Heaven <suheaven@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
33 #include <vlc_charset.h>
34
35 #include "playlist.h"
36
37 struct demux_sys_t
38 {
39     char *psz_prefix;
40 };
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int Demux( demux_t *p_demux);
46 static int Control( demux_t *p_demux, int i_query, va_list args );
47 static char* ParseUriValue(char* psz_string);
48
49 /*****************************************************************************
50  * Import_WPL: main import function
51  *****************************************************************************/
52 int Import_WPL( vlc_object_t *p_this )
53 {
54     demux_t *p_demux = (demux_t *)p_this;
55
56     if(! ( demux_IsPathExtension( p_demux, ".wpl" ) || demux_IsForced( p_demux,  "wpl" )))
57         return VLC_EGENERIC;
58
59     STANDARD_DEMUX_INIT_MSG( "found valid WPL playlist" );
60     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
61
62     return VLC_SUCCESS;
63 }
64
65
66
67 /*****************************************************************************
68  * Deactivate: frees unused data
69  *****************************************************************************/
70 void Close_WPL( vlc_object_t *p_this )
71 {
72     demux_t *p_demux = (demux_t *)p_this;
73     free( p_demux->p_sys->psz_prefix );
74     free( p_demux->p_sys );
75 }
76
77 static inline void MaybeFromLocaleRep (char **str)
78 {
79     char *const orig_str = *str;
80
81     if ((orig_str != NULL) && !IsUTF8 (orig_str))
82     {
83         *str = FromLocaleDup (orig_str);
84         free (orig_str);
85     }
86 }
87
88
89 static int Demux( demux_t *p_demux )
90 {
91     char       *psz_line;
92     char       *psz_uri = NULL;
93     char       *psz_parse;
94     input_item_t *p_input;
95
96     input_item_t *p_current_input = GetCurrentItem(p_demux);
97
98     psz_line = stream_ReadLine( p_demux->s );
99     while( psz_line )
100     {
101         psz_parse = psz_line;
102         /* Skip leading tabs and spaces */
103         while( *psz_parse == ' '  || *psz_parse == '\t' ||
104                *psz_parse == '\n' || *psz_parse == '\r' )
105             psz_parse++;
106
107         /* if the line is the uri of the media item */
108         if( !strncasecmp( psz_parse, "<media src=\"", strlen( "<media src=\"" ) ) )
109         {
110             psz_uri = ParseUriValue( psz_parse );
111             if( !EMPTY_STR(psz_uri) )
112             {
113                 psz_uri = ProcessMRL( psz_uri, p_demux->p_sys->psz_prefix );
114                 MaybeFromLocaleRep( &psz_uri );
115                 p_input = input_item_NewExt( p_demux, psz_uri, psz_uri,
116                                         0, NULL, 0, -1 );
117                 input_item_AddSubItem( p_current_input, p_input );
118             }
119             free( psz_uri );
120         }
121
122         /* Fetch another line */
123         free( psz_line );
124         psz_line = stream_ReadLine( p_demux->s );
125
126     }
127     vlc_gc_decref(p_current_input);
128     var_Destroy( p_demux, "wpl-extvlcopt" );
129     return 0; /* Needed for correct operation of go back */
130 }
131
132 static int Control( demux_t *p_demux, int i_query, va_list args )
133 {
134     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
135     return VLC_EGENERIC;
136 }
137
138 static char* ParseUriValue( char* psz_string )
139 {
140     int i_len = strlen( psz_string );
141     if( i_len <= 3 )
142         return NULL;
143     char* psz_value = calloc( i_len, 1 );
144     if( !psz_value )
145         return NULL;
146
147     sscanf( psz_string, "%*[^=]=\"%[^\r\t\n\"]", psz_value );
148
149     return psz_value;
150 }
151
152