1 /*****************************************************************************
2 * zpl.c : ZPL playlist format import
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
6 * Authors: Su Heaven <suheaven@gmail.com>
7 * RĂ©mi Duraffort <ivoire@videolan.org>
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)
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
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_demux.h>
42 /*****************************************************************************
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);
49 /*****************************************************************************
50 * Import_ZPL: main import function
51 *****************************************************************************/
52 int Import_ZPL( vlc_object_t *p_this )
54 demux_t *p_demux = (demux_t *)p_this;
56 if(! ( demux_IsPathExtension( p_demux, ".zpl" ) || demux_IsForced( p_demux, "zpl" )))
59 STANDARD_DEMUX_INIT_MSG( "found valid ZPL playlist" );
60 p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
66 /*****************************************************************************
67 * Deactivate: frees unused data
68 *****************************************************************************/
69 void Close_ZPL( vlc_object_t *p_this )
71 demux_t *p_demux = (demux_t *)p_this;
72 free( p_demux->p_sys->psz_prefix );
73 free( p_demux->p_sys );
76 static int Demux( demux_t *p_demux )
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,
87 input_item_t *p_current_input = GetCurrentItem(p_demux);
89 psz_line = stream_ReadLine( p_demux->s );
90 char *psz_parse = psz_line;
92 /* Skip leading tabs and spaces */
93 while( *psz_parse == ' ' || *psz_parse == '\t' ||
94 *psz_parse == '\n' || *psz_parse == '\r' )
97 /* if the 1st line is "AC", skip it */
98 /* TODO: using this information ? */
99 if( !strncasecmp( psz_parse, "AC", strlen( "AC" ) ) )
102 psz_line = stream_ReadLine( p_demux->s );
105 input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
107 /* Loop on all lines */
110 psz_parse = psz_line;
112 /* Skip leading tabs and spaces */
113 while( *psz_parse == ' ' || *psz_parse == '\t' ||
114 *psz_parse == '\n' || *psz_parse == '\r' )
118 if( !strncasecmp( psz_parse, "NM", strlen( "NM" ) ) )
120 char *psz_tabvalue = ParseTabValue( psz_parse );
121 if( !EMPTY_STR(psz_tabvalue) )
123 psz_mrl = ProcessMRL( psz_tabvalue, p_demux->p_sys->psz_prefix );
125 free( psz_tabvalue );
129 else if( !strncasecmp( psz_parse, "DR", strlen( "DR" ) ) )
131 char *psz_tabvalue = ParseTabValue( psz_parse );
132 if( !EMPTY_STR(psz_tabvalue) )
134 int i_parsed_duration = atoi( psz_tabvalue );
135 if( i_parsed_duration >= 0 )
136 i_duration = i_parsed_duration * INT64_C(1000);
138 free( psz_tabvalue );
141 #define PARSE(tag,variable) \
142 else if( !strncasecmp( psz_parse, tag, strlen( tag ) ) ) \
143 variable = ParseTabValue( psz_parse );
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 )
160 /* force a duration ? */
161 else if( !strncasecmp( psz_parse, "FD", strlen( "FD" ) ) )
164 /* end of file entry */
165 else if( !strncasecmp( psz_parse, "BR!", strlen( "BR!" ) ) )
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 );
172 FREENULL( psz_title );
175 #define SET(variable, type) \
176 if( !EMPTY_STR(variable) ) \
178 input_item_Set##type( p_input, variable ); \
179 FREENULL( variable ); \
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 );
193 vlc_gc_decref( p_input );
196 msg_Warn( p_demux, "invalid line '%s'", psz_parse );
198 /* Fetch another line */
200 psz_line = stream_ReadLine( p_demux->s );
203 input_item_node_PostAndDelete( p_subitems );
205 vlc_gc_decref(p_current_input);
206 var_Destroy( p_demux, "zpl-extvlcopt" );
207 return 0; /* Needed for correct operation of go back */
210 static int Control( demux_t *p_demux, int i_query, va_list args )
212 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
216 static char* ParseTabValue(char* psz_string)
218 int i_len = strlen( psz_string );
221 char* psz_value = calloc( i_len, 1 );
225 sscanf( psz_string,"%*[^=]=%[^\r\t\n]", psz_value );