]> git.sesse.net Git - vlc/blobdiff - modules/demux/playlist/asx.c
Might fix compile
[vlc] / modules / demux / playlist / asx.c
index cc6a5c2a534652357b12c0a17a089ca2f51d00f6..472d225a014baf6af3c5d95ec56dc4727899f2ad 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * asx.c : ASX playlist format import
  *****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
+ * Copyright (C) 2005-2006 the VideoLAN team
  * $Id$
  *
  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+#define _GNU_SOURCE
 #include <stdlib.h>                                      /* malloc(), free() */
 #include <ctype.h>                                              /* isspace() */
 
 #include <vlc/vlc.h>
 #include <vlc/input.h>
-#include <vlc/intf.h>
 
 #include <errno.h>                                                 /* ENOMEM */
+#include "charset.h"
 #include "playlist.h"
 #include "vlc_meta.h"
 
-#define FREE( p ) if( p ) { free( p ); (p) = NULL; }
-
 struct demux_sys_t
 {
     char    *psz_prefix;
@@ -54,29 +53,42 @@ struct demux_sys_t
 static int Demux( demux_t *p_demux);
 static int Control( demux_t *p_demux, int i_query, va_list args );
 
-static int StoreString( demux_t *p_demux, char **ppsz_string, char *psz_source_start, char *psz_source_end )
+static int StoreString( demux_t *p_demux, char **ppsz_string,
+                        const char *psz_source_start,
+                        const char *psz_source_end )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-    int i_strlen = psz_source_end-psz_source_start;
-    if( i_strlen < 1 )
-        return VLC_EGENERIC;
+    unsigned len = psz_source_end - psz_source_start;
 
     if( *ppsz_string ) free( *ppsz_string );
-    *ppsz_string = malloc( i_strlen*sizeof( char ) +1);
-    memcpy( *ppsz_string, psz_source_start, i_strlen );
-    (*ppsz_string)[i_strlen] = '\0';
+
+    char *buf = *ppsz_string = malloc ((len * (1 + !p_sys->b_utf8)) + 1);
+    if (buf == NULL)
+        return VLC_ENOMEM;
 
     if( p_sys->b_utf8 )
-        EnsureUTF8( *ppsz_string );
+    {
+        memcpy (buf, psz_source_start, len);
+        (*ppsz_string)[len] = '\0';
+        EnsureUTF8 (*ppsz_string);
+    }
     else
     {
-        char *psz_temp;
-        psz_temp = FromLocaleDup( *ppsz_string );
-        if( psz_temp )
+        /* Latin-1 -> UTF-8 */
+        for (unsigned i = 0; i < len; i++)
         {
-            free( *ppsz_string );
-            *ppsz_string = psz_temp;
-        } else EnsureUTF8( *ppsz_string );
+            unsigned char c = psz_source_start[i];
+            if (c & 0x80)
+            {
+                *buf++ = 0xc0 | (c >> 6);
+                *buf++ = 0x80 | (c & 0x3f);
+            }
+            else
+                *buf++ = c;
+        }
+        *buf++ = '\0';
+
+        buf = *ppsz_string = realloc (*ppsz_string, buf - *ppsz_string);
     }
     return VLC_SUCCESS;
 }
@@ -87,38 +99,29 @@ static int StoreString( demux_t *p_demux, char **ppsz_string, char *psz_source_s
 int E_(Import_ASX)( vlc_object_t *p_this )
 {
     demux_t *p_demux = (demux_t *)p_this;
-    demux_sys_t *p_sys;
+    uint8_t *p_peek, *p_peek_stop;
+    CHECK_PEEK( p_peek, 10 );
 
-    char    *psz_ext;
+    p_peek_stop = p_peek+6;
 
-    psz_ext = strrchr ( p_demux->psz_path, '.' );
+    // skip over possible leading empty lines
+    while( (p_peek < p_peek_stop) && (*p_peek == '\n' || *p_peek == '\r')) ++p_peek;
 
-    if( ( psz_ext && !strcasecmp( psz_ext, ".asx") ) ||
-        ( psz_ext && !strcasecmp( psz_ext, ".wax") ) ||
-        ( psz_ext && !strcasecmp( psz_ext, ".wvx") ) ||
-        ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "asx-open") ) )
+    if( POKE( p_peek, "<asx", 4 ) || isExtension( p_demux, ".asx" ) ||
+        isExtension( p_demux, ".wax" ) || isExtension( p_demux, ".wvx" ) ||
+        isDemux( p_demux, "asx-open" ) )
     {
         ;
     }
     else
-    {
         return VLC_EGENERIC;
-    }
-    msg_Dbg( p_demux, "using asx playlist import");
 
-    p_demux->pf_control = Control;
-    p_demux->pf_demux = Demux;
-    p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
-    if( p_sys == NULL )
-    {
-        msg_Err( p_demux, "out of memory" );
-        return VLC_ENOMEM;
-    }
-    p_sys->psz_prefix = E_(FindPrefix)( p_demux );
-    p_sys->psz_data = NULL;
-    p_sys->i_data_len = -1;
-    p_sys->b_utf8 = VLC_FALSE;
-    
+    STANDARD_DEMUX_INIT_MSG( "found valid ASX playlist" );
+    p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
+    p_demux->p_sys->psz_data = NULL;
+    p_demux->p_sys->i_data_len = -1;
+    p_demux->p_sys->b_utf8 = VLC_FALSE;
+
     return VLC_SUCCESS;
 }
 
@@ -141,7 +144,7 @@ static int Demux( demux_t *p_demux )
     char        *psz_parse = NULL;
     char        *psz_backup = NULL;
     vlc_bool_t  b_entry = VLC_FALSE;
-
+    input_item_t *p_input;
     INIT_PLAYLIST_STUFF;
 
     /* init txt */
@@ -177,14 +180,14 @@ static int Demux( demux_t *p_demux )
 
         char *psz_base_asx = NULL;
         char *psz_title_asx = NULL;
-        char *psz_author_asx = NULL;
+        char *psz_artist_asx = NULL;
         char *psz_copyright_asx = NULL;
         char *psz_moreinfo_asx = NULL;
         char *psz_abstract_asx = NULL;
         
         char *psz_base_entry = NULL;
         char *psz_title_entry = NULL;
-        char *psz_author_entry = NULL;
+        char *psz_artist_entry = NULL;
         char *psz_copyright_entry = NULL;
         char *psz_moreinfo_entry = NULL;
         char *psz_abstract_entry = NULL;
@@ -303,7 +306,7 @@ static int Demux( demux_t *p_demux )
                 psz_backup = psz_parse+=8;
                 if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
                 {
-                    StoreString( p_demux, (b_entry ? &psz_author_entry : &psz_author_asx), psz_backup, psz_parse );
+                    StoreString( p_demux, (b_entry ? &psz_artist_entry : &psz_artist_asx), psz_backup, psz_parse );
                     psz_parse += 9;
                 }
                 else continue;
@@ -364,10 +367,11 @@ static int Demux( demux_t *p_demux )
                             memcpy( psz_string, psz_backup, i_strlen );
                             psz_string[i_strlen] = '\0';
                             p_input = input_ItemNew( p_playlist, psz_string, psz_title_asx );
-                            vlc_input_item_CopyOptions( p_current->p_input, p_input );
-                            playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
-                                 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
-                                 PLAYLIST_APPEND );
+                            input_ItemCopyOptions( p_current->p_input, p_input );
+                            playlist_BothAddInput( p_playlist, p_input,
+                                                   p_item_in_category,
+                                                   PLAYLIST_APPEND,
+                                                   PLAYLIST_END );
                             free( psz_string );
                         }
                         else continue;
@@ -388,17 +392,17 @@ static int Demux( demux_t *p_demux )
                     continue;
                 }
                 /* cleanup entry */
-                FREE( psz_title_entry )
-                FREE( psz_base_entry )
-                FREE( psz_author_entry )
-                FREE( psz_copyright_entry )
-                FREE( psz_moreinfo_entry )
-                FREE( psz_abstract_entry )
+                FREENULL( psz_title_entry )
+                FREENULL( psz_base_entry )
+                FREENULL( psz_artist_entry )
+                FREENULL( psz_copyright_entry )
+                FREENULL( psz_moreinfo_entry )
+                FREENULL( psz_abstract_entry )
                 b_entry = VLC_FALSE;
             }
-            else if( !strncasecmp( psz_parse, "<Entry>", 7 ) )
+            else if( !strncasecmp( psz_parse, "<Entry", 6 ) )
             {
-                psz_parse+=7;
+                psz_parse+=6;
                 if( b_entry )
                 {
                     msg_Err( p_demux, "We already are in an entry section" );
@@ -406,6 +410,7 @@ static int Demux( demux_t *p_demux )
                 }
                 i_entry_count += 1;
                 b_entry = VLC_TRUE;
+                psz_parse = strcasestr( psz_parse, ">" );
             }
             else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
             {
@@ -434,27 +439,26 @@ static int Demux( demux_t *p_demux )
                             /* create the new entry */
                             asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current->p_input->psz_name ) );
                             p_entry = input_ItemNew( p_playlist, psz_string, psz_name );
-                            FREE( psz_name );
+                            FREENULL( psz_name );
                             
-                            vlc_input_item_CopyOptions( p_current->p_input, p_entry );
+                            input_ItemCopyOptions( p_current->p_input, p_entry );
                             p_entry->p_meta = vlc_meta_New();
                             if( psz_title_entry ) vlc_meta_SetTitle( p_entry->p_meta, psz_title_entry );
-                            if( psz_author_entry ) vlc_meta_SetAuthor( p_entry->p_meta, psz_author_entry );
+                            if( psz_artist_entry ) vlc_meta_SetArtist( p_entry->p_meta, psz_artist_entry );
                             if( psz_copyright_entry ) vlc_meta_SetCopyright( p_entry->p_meta, psz_copyright_entry );
                             if( psz_moreinfo_entry ) vlc_meta_SetURL( p_entry->p_meta, psz_moreinfo_entry );
                             if( psz_abstract_entry ) vlc_meta_SetDescription( p_entry->p_meta, psz_abstract_entry );
-                            
-                            playlist_AddWhereverNeeded( p_playlist, p_entry, p_current,
-                                p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
-                                PLAYLIST_APPEND );
+                            playlist_BothAddInput( p_playlist, p_entry,
+                                                 p_item_in_category,
+                                                 PLAYLIST_APPEND, PLAYLIST_END);
                             free( psz_string );
                         }
                         else continue;
                     }
                     else continue;
                 }
-                if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
-                    psz_parse += 2;
+                if( ( psz_parse = strcasestr( psz_parse, ">" ) ) )
+                    psz_parse++;
                 else continue;
             }
             else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
@@ -462,17 +466,17 @@ static int Demux( demux_t *p_demux )
                 vlc_mutex_lock( &p_current->p_input->lock );
                 if( !p_current->p_input->p_meta ) p_current->p_input->p_meta = vlc_meta_New();
                 if( psz_title_asx ) vlc_meta_SetTitle( p_current->p_input->p_meta, psz_title_asx );
-                if( psz_author_asx ) vlc_meta_SetAuthor( p_current->p_input->p_meta, psz_author_asx );
+                if( psz_artist_asx ) vlc_meta_SetArtist( p_current->p_input->p_meta, psz_artist_asx );
                 if( psz_copyright_asx ) vlc_meta_SetCopyright( p_current->p_input->p_meta, psz_copyright_asx );
                 if( psz_moreinfo_asx ) vlc_meta_SetURL( p_current->p_input->p_meta, psz_moreinfo_asx );
                 if( psz_abstract_asx ) vlc_meta_SetDescription( p_current->p_input->p_meta, psz_abstract_asx );
                 vlc_mutex_unlock( &p_current->p_input->lock );
-                FREE( psz_base_asx );
-                FREE( psz_title_asx );
-                FREE( psz_author_asx );
-                FREE( psz_copyright_asx );
-                FREE( psz_moreinfo_asx );
-                FREE( psz_abstract_asx );
+                FREENULL( psz_base_asx );
+                FREENULL( psz_title_asx );
+                FREENULL( psz_artist_asx );
+                FREENULL( psz_copyright_asx );
+                FREENULL( psz_moreinfo_asx );
+                FREENULL( psz_abstract_asx );
                 psz_parse++;
             }
             else psz_parse++;
@@ -489,10 +493,10 @@ static int Demux( demux_t *p_demux )
 #endif
     }
     HANDLE_PLAY_AND_RELEASE;
-    return VLC_SUCCESS;
+    return -1; /* Needed for correct operation of go back */
 }
 
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     return VLC_EGENERIC;
-}
\ No newline at end of file
+}