]> git.sesse.net Git - vlc/blobdiff - src/input/subtitles.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / input / subtitles.c
index a7814094c30f3199aafe85ce0b17d0bbcc331974..9c6843d6ba26f9ba9b0c86ea51b239a5e5b0c7e6 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>
 #endif
 
 #include <vlc_common.h>
-#include <vlc_input.h>
-#include <vlc_charset.h>
+#include <vlc_fs.h>
 #include <vlc_url.h>
 
-#ifdef HAVE_DIRENT_H
-#   include <dirent.h>
-#endif
-
-#include <limits.h>
-
 #ifdef HAVE_UNISTD_H
 #   include <unistd.h>
 #endif
+
 #include <sys/stat.h>
 
-#include <ctype.h>
+#include <ctype.h> /* isalnum */
+
 #include "input_internal.h"
 
 /**
@@ -55,7 +50,6 @@
  */
 #define MAX_SUBTITLE_FILES 128
 
-
 /**
  * The possible extensions for subtitle files we support
  */
@@ -136,11 +130,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
 {
@@ -173,13 +167,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;
@@ -221,11 +214,9 @@ static char **paths_to_list( const char *psz_dir, char *psz_path )
         if( *psz_subdir == '\0' )
             continue;
 
-        if( asprintf( &subdirs[i++], "%s%s%c",
+        if( asprintf( &subdirs[i++], "%s%s",
                   psz_subdir[0] == '.' ? psz_dir : "",
-                  psz_subdir,
-                  psz_subdir[strlen(psz_subdir) - 1] == DIR_SEP_CHAR ?
-                                           '\0' : DIR_SEP_CHAR ) == -1 )
+                  psz_subdir ) == -1 )
             break;
     }
     subdirs[i] = NULL;
@@ -254,55 +245,36 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
 {
     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;
+    char *f_fname_noext = NULL, *f_fname_trim = NULL;
 
     char **subdirs; /* list of subdirectories to look in */
 
     vlc_subfn_t *result = NULL; /* unsorted results */
     char **result2; /* sorted results */
 
-    if( !psz_fname )
+    if( !psz_name_org )
         return NULL;
 
-    const char *psz_fname = decode_URI( psz_name_org );
-    if( !strncmp( psz_fname, "file://", 7 ) )
-    {
-        psz_fname += 7;
-        if( !strncmp( psz_fname, "localhost", 9 ) )
-            psz_fname += 9;
-    }
+    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 );
-    if( tmp )
+    char *f_dir = strdup( psz_fname );
+    if( f_dir == NULL )
     {
-        const int i_dirlen = strlen(psz_fname)-strlen(tmp)+1; /* include the separator */
-        f_fname = strdup( &tmp[1] );    /* skip the separator */
-        f_dir = strndup( psz_fname, i_dirlen );
+        free( psz_fname );
+        return NULL;
     }
-    else
-    {
-#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 )
-            return NULL;
 
-        f_fname = strdup( psz_fname );
-        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 )
+    char *f_fname = strrchr( f_dir, DIR_SEP_CHAR );
+    if( !f_fname )
     {
-        free( f_fname );
         free( f_dir );
+        free( psz_fname );
         return NULL;
     }
+    *(f_fname++) = 0; /* skip dir separator */
 
     i_fname_len = strlen( f_fname );
 
@@ -310,10 +282,10 @@ 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( psz_fname );
         return NULL;
     }
 
@@ -326,33 +298,33 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
     subdirs = paths_to_list( f_dir, psz_path );
     for( j = -1, i_sub_count = 0; (j == -1) || ( j >= 0 && subdirs != NULL && subdirs[j] != NULL ); j++ )
     {
-        const char *psz_dir = j < 0 ? f_dir : subdirs[j];
-        char **ppsz_dir_content;
-        int i_dir_content;
-        int a;
-
+        const char *psz_dir = (j < 0) ? f_dir : subdirs[j];
         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,
-                                      subtitles_Filter, NULL );
-        if( i_dir_content < 0 )
+        DIR *dir = vlc_opendir( psz_dir );
+        if( dir == NULL )
             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++ )
+
+        char *psz_name;
+        while( (psz_name = vlc_readdir( dir )) && i_sub_count < MAX_SUBTITLE_FILES )
         {
-            char *psz_name = ppsz_dir_content[a];
+            if( psz_name[0] == '.' || !subtitles_Filter( psz_name ) )
+            {
+                free( psz_name );
+                continue;
+            }
+
             char tmp_fname_noext[strlen( psz_name ) + 1];
             char tmp_fname_trim[strlen( psz_name ) + 1];
             char tmp_fname_ext[strlen( psz_name ) + 1];
+            char *tmp;
 
             int i_prio;
 
-            if( psz_name == NULL || psz_name[0] == '.' )
-                continue;
-
             /* retrieve various parts of the filename */
             strcpy_strip_ext( tmp_fname_noext, psz_name );
             strcpy_get_ext( tmp_fname_ext, psz_name );
@@ -389,14 +361,17 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
             }
             if( i_prio >= i_fuzzy )
             {
-                char psz_path[strlen( psz_dir ) + strlen( psz_name ) + 1];
+                char psz_path[strlen( psz_dir ) + strlen( psz_name ) + 2];
                 struct stat st;
 
-                sprintf( psz_path, "%s%s", psz_dir, psz_name );
+                sprintf( psz_path, "%s"DIR_SEP"%s", psz_dir, psz_name );
                 if( !strcmp( psz_path, psz_fname ) )
+                {
+                    free( psz_name );
                     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",
@@ -412,13 +387,9 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
                              psz_path, i_prio );
                 }
             }
+            free( psz_name );
         }
-        if( ppsz_dir_content )
-        {
-            for( a = 0; a < i_dir_content; a++ )
-                free( ppsz_dir_content[a] );
-            free( ppsz_dir_content );
-        }
+        closedir( dir );
     }
     if( subdirs )
     {
@@ -426,10 +397,10 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
             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;