]> git.sesse.net Git - vlc/blobdiff - src/input/subtitles.c
* If input file in current dir specified, then retrieve CWD when we try to look for...
[vlc] / src / input / subtitles.c
index eee4364678b1b839d0c7d34b7f747c4ada705d5b..875fa7429e99440007f20c3d92397e40e20be382 100644 (file)
@@ -19,7 +19,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /**
 #   include <limits.h>  
 #endif
 
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>
+#endif
+
 #include <ctype.h>
 
 /**
@@ -288,6 +292,8 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
     char *psz_fname_original = strdup( psz_name );
     char *psz_fname = psz_fname_original;
 
+    if( psz_fname == NULL ) return NULL;
+
     if( !strncmp( psz_fname, "file://", 7 ) )
     {
         psz_fname += 7;
@@ -302,16 +308,25 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
         f_fname = malloc( strlen(tmp) );
         if( f_fname )
             strcpy( f_fname, tmp+1 ); // we skip the seperator, so it will still fit in the allocated space
-        dirlen = strlen(psz_fname) - strlen(tmp);
+        dirlen = strlen(psz_fname) - strlen(tmp) + 1; // add the seperator
         f_dir = malloc( dirlen + 1 );
         if( f_dir )
         {
-            strncpy( f_dir, psz_fname, dirlen + 1 );
+            strncpy( f_dir, psz_fname, dirlen );
             f_dir[dirlen] = 0;
         }
     }
     else
     {
+        /* Get the current working directory */
+#ifdef HAVE_UNISTD_H
+        f_dir = getcwd( NULL, 0 );
+#endif
+        if( f_dir == NULL )
+        {
+            if( psz_fname_original ) free( psz_fname_original );
+            return NULL;
+        }
         f_fname = strdup( psz_fname );
     }
 
@@ -337,6 +352,9 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
         pp_dir_content = NULL;
         i_dir_content = 0;
 
+        if( j < 0 && f_dir == NULL )
+            continue;
+
         /* parse psz_src dir */  
         if( ( i_dir_content = scandir( j < 0 ? f_dir : *subdirs, &pp_dir_content, Filter,
                                 NULL ) ) != -1 )
@@ -388,24 +406,35 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
                 if( i_prio >= fuzzy.i_int )
                 {
                     FILE *f;
-                    char *tmpresult;
+                    char *psz_UTF8_path;
+                    char *psz_locale_path;
 
-                    asprintf( &tmpresult, "%s%s", j < 0 ? f_dir : *subdirs, p_fixed_name );
+                    asprintf( &psz_UTF8_path, "%s%s", j < 0 ? f_dir : *subdirs, p_fixed_name );
+                    psz_locale_path = ToLocale( psz_UTF8_path );
                     msg_Dbg( p_this, "autodetected subtitle: %s with priority %d", p_fixed_name, i_prio );
-                    if( ( f = fopen( tmpresult, "rt" ) ) )
+                    if( ( f = fopen( psz_locale_path, "rt" ) ) )
                     {
                         fclose( f );
+                        LocaleFree( psz_locale_path );
                         result[i_sub_count].priority = i_prio;
-                        result[i_sub_count].psz_fname = tmpresult;
+                        result[i_sub_count].psz_fname = psz_UTF8_path;
                         result[i_sub_count].psz_ext = strdup(tmp_fname_ext);
                         i_sub_count++;
-                    } else free( tmpresult );
+                    }
+                    else
+                    {
+                        if( psz_UTF8_path ) free( psz_UTF8_path );
+                        LocaleFree( psz_locale_path );
+                    }
                 }
                 if( i_sub_count >= MAX_SUBTITLE_FILES ) break;
-                free( p_fixed_name );
+                if( p_fixed_name ) free( p_fixed_name );
             }
+            for( a = 0; a < i_dir_content; a++ )
+                if( pp_dir_content[a] ) free( pp_dir_content[a] );
+            if( pp_dir_content ) free( pp_dir_content );
         }
-        if( j >= 0 ) free( *subdirs++ );
+        if( j >= 0 ) if( *subdirs ) free( *subdirs++ );
     }
 
     if( tmp_subdirs )   free( tmp_subdirs );