]> git.sesse.net Git - vlc/blob - modules/demux/playlist/zpl.c
Real demux: kill warnings
[vlc] / modules / demux / playlist / zpl.c
1 /*****************************************************************************
2  * zpl.c : ZPL playlist format import
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  *
6  * Authors: Su Heaven <suheaven@gmail.com>
7  *          RĂ©mi Duraffort <ivoire@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc., 51
21  * Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_demux.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* ParseTabValue(char* psz_string);
48
49 /*****************************************************************************
50  * Import_ZPL: main import function
51  *****************************************************************************/
52 int Import_ZPL( vlc_object_t *p_this )
53 {
54     demux_t *p_demux = (demux_t *)p_this;
55
56     if(! ( demux_IsPathExtension( p_demux, ".zpl" ) || demux_IsForced( p_demux,  "zpl" )))
57         return VLC_EGENERIC;
58
59     STANDARD_DEMUX_INIT_MSG( "found valid ZPL playlist" );
60     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
61
62     return VLC_SUCCESS;
63 }
64
65
66 /*****************************************************************************
67  * Deactivate: frees unused data
68  *****************************************************************************/
69 void Close_ZPL( vlc_object_t *p_this )
70 {
71     demux_t *p_demux = (demux_t *)p_this;
72     free( p_demux->p_sys->psz_prefix );
73     free( p_demux->p_sys );
74 }
75
76 static int Demux( demux_t *p_demux )
77 {
78     char       *psz_line;
79
80     mtime_t i_duration = -1;
81     char *psz_title = NULL,       *psz_genre = NULL,      *psz_tracknum = NULL,
82          *psz_language = NULL,    *psz_artist = NULL,     *psz_album = NULL,
83          *psz_date = NULL,        *psz_publicher = NULL,  *psz_encodedby = NULL,
84          *psz_description = NULL, *psz_url = NULL,        *psz_copyright = NULL,
85          *psz_mrl = NULL;
86
87     input_item_t *p_current_input = GetCurrentItem(p_demux);
88
89     psz_line = stream_ReadLine( p_demux->s );
90     char *psz_parse = psz_line;
91
92     /* Skip leading tabs and spaces */
93     while( *psz_parse == ' '  || *psz_parse == '\t' ||
94            *psz_parse == '\n' || *psz_parse == '\r' )
95         psz_parse++;
96
97     /* if the 1st line is "AC", skip it */
98     /* TODO: using this information ? */
99     if( !strncasecmp( psz_parse, "AC", strlen( "AC" ) ) )
100     {
101         free( psz_line );
102         psz_line = stream_ReadLine( p_demux->s );
103     }
104
105     input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
106
107     /* Loop on all lines */
108     while( psz_line )
109     {
110         psz_parse = psz_line;
111
112         /* Skip leading tabs and spaces */
113         while( *psz_parse == ' '  || *psz_parse == '\t' ||
114                *psz_parse == '\n' || *psz_parse == '\r' )
115             psz_parse++;
116
117         /* filename */
118         if( !strncasecmp( psz_parse, "NM", strlen( "NM" ) ) )
119         {
120             char *psz_tabvalue = ParseTabValue( psz_parse );
121             if( !EMPTY_STR(psz_tabvalue) )
122             {
123                 psz_mrl = ProcessMRL( psz_tabvalue, p_demux->p_sys->psz_prefix );
124             }
125             free( psz_tabvalue );
126         }
127
128         /* duration */
129         else if( !strncasecmp( psz_parse, "DR", strlen( "DR" ) ) )
130         {
131             char *psz_tabvalue = ParseTabValue( psz_parse );
132             if( !EMPTY_STR(psz_tabvalue) )
133             {
134                 int i_parsed_duration = atoi( psz_tabvalue );
135                 if( i_parsed_duration >= 0 )
136                     i_duration = i_parsed_duration * INT64_C(1000);
137             }
138             free( psz_tabvalue );
139         }
140
141 #define PARSE(tag,variable)                                     \
142     else if( !strncasecmp( psz_parse, tag, strlen( tag ) ) )    \
143         variable = ParseTabValue( psz_parse );
144
145         PARSE( "TT", psz_title )
146         PARSE( "TG", psz_genre )
147         PARSE( "TR", psz_tracknum )
148         PARSE( "TL", psz_language )
149         PARSE( "TA", psz_artist )
150         PARSE( "TB", psz_album )
151         PARSE( "TY", psz_date )
152         PARSE( "TH", psz_publicher )
153         PARSE( "TE", psz_encodedby )
154         PARSE( "TC", psz_description )
155         PARSE( "TU", psz_url )
156         PARSE( "TO", psz_copyright )
157
158 #undef PARSE
159
160         /* force a duration ? */
161         else if( !strncasecmp( psz_parse, "FD", strlen( "FD" ) ) )
162         {}
163
164         /* end of file entry */
165         else if( !strncasecmp( psz_parse, "BR!", strlen( "BR!" ) ) )
166         {
167             /* create the input item */
168             input_item_t *p_input = input_item_NewExt( p_demux, psz_mrl,
169                                         psz_title, 0, NULL, 0, i_duration );
170             input_item_node_AppendItem( p_subitems, p_input );
171             FREENULL( psz_mrl );
172             FREENULL( psz_title );
173             i_duration = -1;
174
175 #define SET(variable, type)                             \
176     if( !EMPTY_STR(variable) )                          \
177     {                                                   \
178         input_item_Set##type( p_input, variable );      \
179         FREENULL( variable );                           \
180     }
181             /* set the meta */
182             SET( psz_genre, Genre );
183             SET( psz_tracknum, TrackNum );
184             SET( psz_language, Language );
185             SET( psz_artist, Artist );
186             SET( psz_album, Album );
187             SET( psz_date, Date );
188             SET( psz_encodedby, EncodedBy );
189             SET( psz_description, Description );
190             SET( psz_copyright, Copyright );
191 #undef SET
192
193             vlc_gc_decref( p_input );
194         }
195         else
196             msg_Warn( p_demux, "invalid line '%s'", psz_parse );
197
198         /* Fetch another line */
199         free( psz_line );
200         psz_line = stream_ReadLine( p_demux->s );
201     }
202
203     input_item_node_PostAndDelete( p_subitems );
204
205     vlc_gc_decref(p_current_input);
206     var_Destroy( p_demux, "zpl-extvlcopt" );
207     return 0; /* Needed for correct operation of go back */
208 }
209
210 static int Control( demux_t *p_demux, int i_query, va_list args )
211 {
212     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
213     return VLC_EGENERIC;
214 }
215
216 static char* ParseTabValue(char* psz_string)
217 {
218     int i_len = strlen( psz_string );
219     if(i_len <= 3 )
220         return NULL;
221     char* psz_value = calloc( i_len, 1 );
222     if( ! psz_value )
223         return NULL;
224
225     sscanf( psz_string,"%*[^=]=%[^\r\t\n]", psz_value );
226
227     return psz_value;
228 }
229
230