]> git.sesse.net Git - vlc/blobdiff - modules/demux/m3u.c
* Stringreview !!!
[vlc] / modules / demux / m3u.c
index 00723a6b7ccc9109310616b2ef06673c55ee2b20..58b914e9df08c52a1144fa8ceb4404d8f68ec55f 100644 (file)
@@ -1,17 +1,18 @@
 /*****************************************************************************
- * m3u.c: a meta demux to parse pls, m3u and asx playlists
+ * m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: m3u.c,v 1.19 2003/06/26 14:42:04 zorglub Exp $
+ * Copyright (C) 2001-2004 VideoLAN
+ * $Id: m3u.c,v 1.26 2004/01/25 20:05:28 hartman Exp $
  *
  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
  *          Gildas Bazin <gbazin@netcourrier.com>
+ *          Clément Stenac <zorglub@via.ecp.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * Preamble
  *****************************************************************************/
 #include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>                                              /* strdup() */
-#include <errno.h>
 
 #include <vlc/vlc.h>
 #include <vlc/input.h>
 #include <vlc_playlist.h>
 
-#include <sys/types.h>
-
 /*****************************************************************************
  * Constants and structures
  *****************************************************************************/
@@ -63,7 +60,7 @@ static int  Demux ( input_thread_t * );
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-    set_description( _("playlist metademux") );
+    set_description( _("Playlist metademux") );
     set_capability( "demux", 180 );
     set_callbacks( Activate, Deactivate );
     add_shortcut( "m3u" );
@@ -80,9 +77,8 @@ static int Activate( vlc_object_t * p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
     char           *psz_ext;
-    demux_sys_t    *p_m3u;
-    int             i_type = 0;
-    int             i_type2 = 0;
+    int             i_type  = TYPE_UNKNOWN;
+    int             i_type2 = TYPE_UNKNOWN;
 
     /* Initialize access plug-in structures. */
     if( p_input->i_mtu == 0 )
@@ -125,8 +121,9 @@ static int Activate( vlc_object_t * p_this )
 
     /* we had no luck looking at the file extention, so we have a look
      * at the content. This is useful for .asp, .php and similar files
-     * that are actually html. Also useful for som asx files that have
-     * another extention */
+     * that are actually html. Also useful for some asx files that have
+     * another extension */
+    /* XXX we double check for file != m3u as some asx ... are just m3u file */
     if( i_type != TYPE_M3U )
     {
         byte_t *p_peek;
@@ -136,7 +133,7 @@ static int Activate( vlc_object_t * p_this )
             while ( i_size
                     && strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") - 1 )
                     && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
-                    && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 ) 
+                    && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 )
                     && strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
             {
                 p_peek++;
@@ -162,14 +159,13 @@ static int Activate( vlc_object_t * p_this )
             {
                 i_type2 = TYPE_B4S;
             }
-            
         }
     }
-    if ( !i_type && !i_type2 )
+    if ( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
     {
-        return -1;
+        return VLC_EGENERIC;
     }
-    if ( i_type  && !i_type2 )
+    if ( i_type  != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
     {
         i_type = TYPE_M3U;
     }
@@ -179,16 +175,10 @@ static int Activate( vlc_object_t * p_this )
     }
 
     /* Allocate p_m3u */
-    if( !( p_m3u = malloc( sizeof( demux_sys_t ) ) ) )
-    {
-        msg_Err( p_input, "out of memory" );
-        return -1;
-    }
-    p_input->p_demux_data = p_m3u;
+    p_input->p_demux_data = malloc( sizeof( demux_sys_t ) );
+    p_input->p_demux_data->i_type = i_type;
 
-    p_m3u->i_type = i_type;
-
-    return 0;
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
@@ -197,66 +187,91 @@ static int Activate( vlc_object_t * p_this )
 static void Deactivate( vlc_object_t *p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
-    demux_sys_t *p_m3u = (demux_sys_t *)p_input->p_demux_data  ; 
 
-    free( p_m3u );
+    free( p_input->p_demux_data );
 }
 
 /*****************************************************************************
- * XMLSpecialChars: Handle the special chars in a XML file. 
- * Returns 0 if successful
+ * XMLSpecialChars: Handle the special chars in a XML file.
  * ***************************************************************************/
-static int XMLSpecialChars ( char *psz_src , char *psz_dst )
+static void XMLSpecialChars ( char *str )
 {
-    unsigned int i;
-    unsigned int j=0;
-    char c_rplc=0;    
-    
-    for( i=0 ; i < strlen(psz_src) ; i++ )
+    char *src = str;
+    char *dst = str;
+
+    while( *src )
     {
-        if( psz_src[i]  == '&')
+        if( *src == '&' )
+        {
+            if( !strncasecmp( src, "&#xe0;", 6 ) ) *dst++ = 'à';
+            else if( !strncasecmp( src, "&#xee;", 6 ) ) *dst++ = 'î';
+            else if( !strncasecmp( src, "&apos;", 6 ) ) *dst++ = '\'';
+            else if( !strncasecmp( src, "&#xe8;", 6 ) ) *dst++ = 'è';
+            else if( !strncasecmp( src, "&#xe9;", 6 ) ) *dst++ = 'é';
+            else if( !strncasecmp( src, "&#xea;", 6 ) ) *dst++ = 'ê';
+            else
+            {
+                *dst++ = '?';
+            }
+            src += 6;
+        }
+        else
         {
-            if( !strncasecmp( &psz_src[i], "&#xe0;", 6) ) c_rplc = 'à';
-            else if( !strncasecmp( &psz_src[i], "&#xe9;", 6) ) c_rplc = 'é';
-            else if( !strncasecmp( &psz_src[i], "&#xee;", 6) ) c_rplc = 'î';
-            else if( !strncasecmp( &psz_src[i], "&apos;", 6) ) c_rplc = '\'';
-            else if( !strncasecmp( &psz_src[i], "&#xe8;", 6) ) c_rplc = 'è';
-            else if( !strncasecmp( &psz_src[i], "&#xea;", 6) ) c_rplc = 'ê';
-            else if( !strncasecmp( &psz_src[i], "&#xea;", 6) ) c_rplc = 'ê';
-            psz_dst[j]=c_rplc;
-            j++;
-            i = i+6;
+            *dst++ = *src++;
         }
-        psz_dst[j] = psz_src[i];
-        j++;
-    }        
-    psz_dst[j]='\0';
-    return 0;
+    }
+
+    *dst = '\0';
 }
 
 
 /*****************************************************************************
- * ProcessLine: read a "line" from the file and add any entries found
- * to the playlist. Return number of items added ( 0 or 1 )
- *****************************************************************************/
-static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
-        , playlist_t *p_playlist , char psz_line[MAX_LINE], int i_position )
+ * ParseLine: read a "line" from the file and add any entries found
+ * to the playlist. Returns:
+ * 0 if nothing was found
+ * 1 if a URI was found (it is then copied in psz_data)
+ * 2 if a name was found (  "  )
+ *
+ * XXX psz_data has the same length that psz_line so no problem if you don't
+ * expand it
+ *    psz_line is \0 terminated
+ ******************************************************************************/
+static int ParseLine ( input_thread_t *p_input, char *psz_line, char *psz_data, vlc_bool_t *pb_next )
 {
+    demux_sys_t   *p_m3u = p_input->p_demux_data;
+
     char          *psz_bol, *psz_name;
-    
+
     psz_bol = psz_line;
 
+    *pb_next = VLC_FALSE;
+
     /* Remove unnecessary tabs or spaces at the beginning of line */
     while( *psz_bol == ' ' || *psz_bol == '\t' ||
            *psz_bol == '\n' || *psz_bol == '\r' )
+    {
         psz_bol++;
+    }
 
     if( p_m3u->i_type == TYPE_M3U )
     {
         /* Check for comment line */
         if( *psz_bol == '#' )
-            /*line is comment or extended info, ignored for now */
-            return 0;
+        {
+            while( *psz_bol &&
+                   strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
+               psz_bol++;
+            if( !*psz_bol ) return 0;
+
+            psz_bol = strchr( psz_bol, ',' );
+            if ( !psz_bol ) return 0;
+            psz_bol++;
+            /* From now, we have a name line */
+
+            strcpy( psz_data , psz_bol );
+            return 2;
+        }
+        /* If we don't have a comment, the line is directly the URI */
     }
     else if ( p_m3u->i_type == TYPE_PLS )
     {
@@ -349,30 +364,67 @@ static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
     }
     else if ( p_m3u->i_type == TYPE_B4S )
     {
-        /* We are dealing with a B4S file from Winamp3
-         * We are looking for <entry Playstring="blabla"> */
 
         char *psz_eol;
-            
-         while ( *psz_bol &&
-         strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
-               psz_bol++;
 
-       if( !*psz_bol ) return 0;
+        msg_Dbg( p_input, "b4s line=%s", psz_line );
+        /* We are dealing with a B4S file from Winamp 3 */
+
+        /* First, search for name *
+         * <Name>Blabla</Name> */
+
+        if( strstr ( psz_bol, "<Name>" ) )
+        {
+            /* We have a name */
+            while ( *psz_bol &&
+                    strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
+                psz_bol++;
+
+            if( !*psz_bol ) return 0;
+
+            psz_bol = psz_bol + 5 ;
+            /* We are now at the beginning of the name */
+
+            if( !psz_bol ) return 0;
+
+
+            psz_eol = strchr(psz_bol, '<' );
+            if( !psz_eol) return 0;
+
+            *psz_eol='\0';
+
+            XMLSpecialChars( psz_bol );
+
+            strcpy( psz_data, psz_bol );
+            return 2;
+        }
+        else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
+        {
+            *pb_next = VLC_TRUE;
+            return 0;
+        }
+
+         /* We are looking for <entry Playstring="blabla"> */
+
+
+        while ( *psz_bol &&
+                strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
+            psz_bol++;
+
+        if( !*psz_bol ) return 0;
 
         psz_bol = strchr( psz_bol, '=' );
         if ( !psz_bol ) return 0;
-              psz_bol++;
-              psz_bol++;
+
+        psz_bol += 2;
 
         psz_eol= strchr(psz_bol, '"');
         if( !psz_eol ) return 0;
 
         *psz_eol= '\0';
 
-        /* Handle the XML special characters */    
-        if( XMLSpecialChars( psz_bol , psz_bol ) )
-                return 0;
+        /* Handle the XML special characters */
+        XMLSpecialChars( psz_bol );
     }
     else
     {
@@ -456,13 +508,63 @@ static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
         psz_name = strdup( psz_bol );
     }
 
-    playlist_Add( p_playlist, psz_name,
-                  PLAYLIST_INSERT, i_position );
+    strcpy(psz_data, psz_name ) ;
 
     free( psz_name );
+
+    if( p_m3u->i_type != TYPE_B4S )
+    {
+       *pb_next = VLC_TRUE;
+    }
+
     return 1;
 }
 
+static void ProcessLine ( input_thread_t *p_input, playlist_t *p_playlist,
+                          char *psz_line,
+                          char **ppsz_uri, char **ppsz_name,
+                          int *pi_position )
+{
+    char          psz_data[MAX_LINE];
+    vlc_bool_t    b_next;
+
+    switch( ParseLine( p_input, psz_line, psz_data, &b_next ) )
+    {
+        case 1:
+            if( *ppsz_uri )
+            {
+                free( *ppsz_uri );
+            }
+            *ppsz_uri = strdup( psz_data );
+            break;
+        case 2:
+            if( *ppsz_name )
+            {
+                free( *ppsz_name );
+            }
+            *ppsz_name = strdup( psz_data );
+            break;
+        case 0:
+        default:
+            break;
+    }
+
+    if( b_next && *ppsz_uri )
+    {
+        playlist_Add( p_playlist, *ppsz_uri,
+                         *ppsz_name ? *ppsz_name : *ppsz_uri,
+                          PLAYLIST_INSERT, *pi_position );
+        (*pi_position)++;
+        if( *ppsz_name )
+        {
+            free( *ppsz_name );
+        }
+        free( *ppsz_uri );
+        *ppsz_name = NULL;
+        *ppsz_uri  = NULL;
+    }
+}
+
 /*****************************************************************************
  * Demux: reads and demuxes data packets
  *****************************************************************************
@@ -470,14 +572,20 @@ static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
  *****************************************************************************/
 static int Demux ( input_thread_t *p_input )
 {
+    demux_sys_t   *p_m3u = p_input->p_demux_data;
+
     data_packet_t *p_data;
-    char          *p_buf, psz_line[MAX_LINE], eol_tok;
+    char          psz_line[MAX_LINE];
+    char          *p_buf, eol_tok;
     int           i_size, i_bufpos, i_linepos = 0;
     playlist_t    *p_playlist;
-    int           i_position;
     vlc_bool_t    b_discard = VLC_FALSE;
 
-    demux_sys_t   *p_m3u = (demux_sys_t *)p_input->p_demux_data;
+
+    char          *psz_name = NULL;
+    char          *psz_uri  = NULL;
+
+    int           i_position;
 
     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
                                                  FIND_ANYWHERE );
@@ -492,8 +600,7 @@ static int Demux ( input_thread_t *p_input )
 
     /* Depending on wether we are dealing with an m3u/asf file, the end of
      * line token will be different */
-    if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML 
-                    || p_m3u->i_type == TYPE_B4S )
+    if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
         eol_tok = '>';
     else
         eol_tok = '\n';
@@ -536,10 +643,9 @@ static int Demux ( input_thread_t *p_input )
 
             psz_line[i_linepos] = '\0';
             i_linepos = 0;
-            
-            i_position += ProcessLine ( p_input, p_m3u , p_playlist ,
-                                        psz_line, i_position );
 
+            ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
+                         &i_position );
         }
 
         input_DeletePacket( p_input->p_method_data, p_data );
@@ -548,9 +654,24 @@ static int Demux ( input_thread_t *p_input )
     if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
     {
         psz_line[i_linepos] = '\0';
-        i_linepos = 0;
-        i_position += ProcessLine ( p_input, p_m3u , p_playlist , psz_line,
-                                    i_position );
+
+        ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
+                     &i_position );
+        /* is there a pendding uri without b_next */
+        if( psz_uri )
+        {
+            playlist_Add( p_playlist, psz_uri, psz_uri,
+                          PLAYLIST_INSERT, i_position );
+        }
+    }
+
+    if( psz_uri )
+    {
+        free( psz_uri );
+    }
+    if( psz_name )
+    {
+        free( psz_name );
     }
 
     vlc_object_release( p_playlist );