]> git.sesse.net Git - vlc/blobdiff - src/input/subtitles.c
Fixed vout reuse regression.
[vlc] / src / input / subtitles.c
index 82fa84383192966d7585a0af5eacdc3b80fb068f..2ca105ff08618b16702eea71c662e485639ebcd2 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * subtitles.c
+ * subtitles.c : subtitles detection
  *****************************************************************************
- * Copyright (C) 2003-2006 the VideoLAN team
+ * Copyright (C) 2003-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Derk-Jan Hartman <hartman at videolan.org>
  *  This file contains functions to dectect subtitle files.
  */
 
-#include <vlc/vlc.h>
-#include <vlc_input.h>
-#include <vlc_charset.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_fs.h>
+#include <vlc_url.h>
 
 #ifdef HAVE_DIRENT_H
 #   include <dirent.h>
 #endif
 
-#ifdef HAVE_LIMITS_H
-#   include <limits.h>
-#endif
-
 #ifdef HAVE_UNISTD_H
 #   include <unistd.h>
 #endif
+
 #include <sys/stat.h>
 
-#include <ctype.h>
+#include <ctype.h> /* isalnum */
+
 #include "input_internal.h"
 
 /**
  */
 #define MAX_SUBTITLE_FILES 128
 
-
 /**
  * The possible extensions for subtitle files we support
  */
 static const char const sub_exts[][6] = {
+    "idx", "sub",  "srt",
+    "ssa", "ass",  "smi",
     "utf", "utf8", "utf-8",
-    "sub", "srt", "smi",
-    "txt", "ssa", "idx",
-
-    "cdg",
-
+    "txt", "rt",   "aqt",
+    "usf", "jss",  "cdg",
+    "psb", "mpsub","mpl2",
+    "pjs", "dks",
     ""
 };
 
-/* extensions from unsupported types */
-/* rt, aqt, jss, js, ass */
-
 static void strcpy_trim( char *d, const char *s )
 {
     /* skip leading whitespace */
@@ -135,11 +134,11 @@ static int whiteonly( const char *s )
 
 enum
 {
-    SUB_PRIORITY_NONE = 0,
-    SUB_PRIORITY_MATCH_NONE = 1,
+    SUB_PRIORITY_NONE        = 0,
+    SUB_PRIORITY_MATCH_NONE  = 1,
     SUB_PRIORITY_MATCH_RIGHT = 2,
-    SUB_PRIORITY_MATCH_LEFT = 3,
-    SUB_PRIORITY_MATCH_ALL = 4,
+    SUB_PRIORITY_MATCH_LEFT  = 3,
+    SUB_PRIORITY_MATCH_ALL   = 4,
 };
 typedef struct
 {
@@ -172,13 +171,12 @@ static int compare_sub_priority( const void *a, const void *b )
 int subtitles_Filter( const char *psz_dir_content )
 {
     const char *tmp = strrchr( psz_dir_content, '.');
-    int i;
 
     if( !tmp )
         return 0;
     tmp++;
 
-    for( i = 0; sub_exts[i][0]; i++ )
+    for( int i = 0; sub_exts[i][0]; i++ )
         if( strcasecmp( sub_exts[i], tmp ) == 0 )
             return 1;
     return 0;
@@ -207,7 +205,7 @@ static char **paths_to_list( const char *psz_dir, char *psz_path )
     if( !subdirs )
         return NULL;
 
-    for( i = 0; *psz_parser != '\0' ; )
+    for( i = 0; psz_parser && *psz_parser != '\0' ; )
     {
         char *psz_subdir = psz_parser;
         psz_parser = strchr( psz_subdir, ',' );
@@ -220,10 +218,12 @@ static char **paths_to_list( const char *psz_dir, char *psz_path )
         if( *psz_subdir == '\0' )
             continue;
 
-        asprintf( &subdirs[i++], "%s%s%c",
+        if( asprintf( &subdirs[i++], "%s%s%c",
                   psz_subdir[0] == '.' ? psz_dir : "",
                   psz_subdir,
-                  psz_subdir[strlen(psz_subdir) - 1] == DIR_SEP_CHAR ? '\0' : DIR_SEP_CHAR );
+                  psz_subdir[strlen(psz_subdir) - 1] == DIR_SEP_CHAR ?
+                                           '\0' : DIR_SEP_CHAR ) == -1 )
+            break;
     }
     subdirs[i] = NULL;
 
@@ -249,7 +249,7 @@ static char **paths_to_list( const char *psz_dir, char *psz_path )
 char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
                          const char *psz_name_org )
 {
-    vlc_value_t fuzzy;
+    int i_fuzzy;
     int j, i_result2, i_sub_count, i_fname_len;
     char *f_dir = NULL, *f_fname = NULL, *f_fname_noext = NULL, *f_fname_trim = NULL;
     char *tmp = NULL;
@@ -258,13 +258,13 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
 
     vlc_subfn_t *result = NULL; /* unsorted results */
     char **result2; /* sorted results */
-    const char *psz_fname = psz_name_org;
 
-    if( !psz_fname )
+    if( !psz_name_org )
         return NULL;
 
-    if( !strncmp( psz_fname, "file://", 7 ) )
-        psz_fname += 7;
+    char *psz_fname = make_path( psz_name_org );
+    if( !psz_fname )
+        return NULL;
 
     /* extract filename & dirname from psz_fname */
     tmp = strrchr( psz_fname, DIR_SEP_CHAR );
@@ -276,23 +276,28 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
     }
     else
     {
-#ifdef HAVE_UNISTD_H
+#if defined (HAVE_UNISTD_H) && !defined (UNDER_CE)
         /* Get the current working directory */
         char *psz_cwd = getcwd( NULL, 0 );
 #else
         char *psz_cwd = NULL;
 #endif
         if( !psz_cwd )
+        {
+            free( psz_fname );
             return NULL;
+        }
 
         f_fname = strdup( psz_fname );
-        asprintf( &f_dir, "%s%c", psz_cwd, DIR_SEP_CHAR );
+        if( asprintf( &f_dir, "%s%c", psz_cwd, DIR_SEP_CHAR ) == -1 )
+            f_dir = NULL; /* Assure that function will return in next test */
         free( psz_cwd );
     }
     if( !f_fname || !f_dir )
     {
-        FREE( f_fname );
-        FREE( f_dir );
+        free( f_fname );
+        free( f_dir );
+        free( psz_fname );
         return NULL;
     }
 
@@ -302,17 +307,18 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
     f_fname_trim = malloc(i_fname_len + 1 );
     if( !f_fname_noext || !f_fname_trim )
     {
-        FREE( f_fname );
-        FREE( f_dir );
-        FREE( f_fname_noext );
-        FREE( f_fname_trim );
+        free( f_fname );
+        free( f_dir );
+        free( f_fname_noext );
+        free( f_fname_trim );
+        free( psz_fname );
         return NULL;
     }
 
     strcpy_strip_ext( f_fname_noext, f_fname );
     strcpy_trim( f_fname_trim, f_fname_noext );
 
-    var_Get( p_this, "sub-autodetect-fuzzy", &fuzzy );
+    i_fuzzy = var_GetInteger( p_this, "sub-autodetect-fuzzy" );
 
     result = calloc( MAX_SUBTITLE_FILES+1, sizeof(vlc_subfn_t) ); /* We check it later (simplify code) */
     subdirs = paths_to_list( f_dir, psz_path );
@@ -321,19 +327,18 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
         const char *psz_dir = j < 0 ? f_dir : subdirs[j];
         char **ppsz_dir_content;
         int i_dir_content;
-        int a;
 
-        if( psz_dir == NULL )
+        if( psz_dir == NULL || ( j >= 0 && !strcmp( psz_dir, f_dir ) ) )
             continue;
 
         /* parse psz_src dir */
-        i_dir_content = utf8_scandir( psz_dir, &ppsz_dir_content,
+        i_dir_content = vlc_scandir( psz_dir, &ppsz_dir_content,
                                       subtitles_Filter, NULL );
         if( i_dir_content < 0 )
             continue;
 
         msg_Dbg( p_this, "looking for a subtitle file in %s", psz_dir );
-        for( a = 0; a < i_dir_content && i_sub_count < MAX_SUBTITLE_FILES ; a++ )
+        for( int a = 0; a < i_dir_content && i_sub_count < MAX_SUBTITLE_FILES ; a++ )
         {
             char *psz_name = ppsz_dir_content[a];
             char tmp_fname_noext[strlen( psz_name ) + 1];
@@ -342,7 +347,7 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
 
             int i_prio;
 
-            if( psz_name == NULL )
+            if( psz_name == NULL || psz_name[0] == '.' )
                 continue;
 
             /* retrieve various parts of the filename */
@@ -379,7 +384,7 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
                 /* doesn't contain the movie name, prefer files in f_dir over subdirs */
                 i_prio = SUB_PRIORITY_MATCH_NONE;
             }
-            if( i_prio >= fuzzy.i_int )
+            if( i_prio >= i_fuzzy )
             {
                 char psz_path[strlen( psz_dir ) + strlen( psz_name ) + 1];
                 struct stat st;
@@ -388,7 +393,7 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
                 if( !strcmp( psz_path, psz_fname ) )
                     continue;
 
-                if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) && result )
+                if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) && result )
                 {
                     msg_Dbg( p_this,
                             "autodetected subtitle: %s with priority %d",
@@ -407,21 +412,22 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
         }
         if( ppsz_dir_content )
         {
-            for( a = 0; a < i_dir_content; a++ )
-                FREE( ppsz_dir_content[a] );
+            for( int a = 0; a < i_dir_content; a++ )
+                free( ppsz_dir_content[a] );
             free( ppsz_dir_content );
         }
     }
     if( subdirs )
     {
         for( j = 0; subdirs[j]; j++ )
-            FREE( subdirs[j] );
+            free( subdirs[j] );
         free( subdirs );
     }
     free( f_fname );
     free( f_dir );
     free( f_fname_trim );
     free( f_fname_noext );
+    free( psz_fname );
 
     if( !result )
         return NULL;
@@ -432,7 +438,7 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
 
     for( j = 0, i_result2 = 0; j < i_sub_count && result2 != NULL; j++ )
     {
-        vlc_bool_t b_reject = VLC_FALSE;
+        bool b_reject = false;
 
         if( !result[j].psz_fname || !result[j].psz_ext ) /* memory out */
             break;
@@ -449,12 +455,12 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
                     break;
             }
             if( i < i_sub_count )
-                b_reject = VLC_TRUE;
+                b_reject = true;
         }
         else if( !strcasecmp( result[j].psz_ext, "cdg" ) )
         {
             if( result[j].priority < SUB_PRIORITY_MATCH_ALL )
-                b_reject = VLC_TRUE;
+                b_reject = true;
         }
 
         /* */
@@ -464,8 +470,8 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
 
     for( j = 0; j < i_sub_count; j++ )
     {
-        FREE( result[j].psz_fname );
-        FREE( result[j].psz_ext );
+        free( result[j].psz_fname );
+        free( result[j].psz_ext );
     }
     free( result );