]> git.sesse.net Git - vlc/blobdiff - src/input/subtitles.c
* src/input/*, modules/demux/util/sub.[ch]: cleanup and fixed memory leaks.
[vlc] / src / input / subtitles.c
index 91b0301528cab1eaacd909e082f76554ae16c709..78b1d00aff78ea7394969ac58fd7bf085223b147 100644 (file)
@@ -2,7 +2,7 @@
  * subtitles.c
  *****************************************************************************
  * Copyright (C) 2003-2004 VideoLAN
- * $Id: subtitles.c,v 1.7 2004/01/06 12:02:06 zorglub Exp $
+ * $Id: subtitles.c,v 1.10 2004/01/26 20:26:54 gbazin Exp $
  *
  * Authors: Derk-Jan Hartman <hartman at videolan.org>
  * This is adapted code from the GPL'ed MPlayer (http://mplayerhq.hu)
@@ -165,7 +165,8 @@ static int compare_sub_priority( const void *a, const void *b )
  * \return a NULL terminated array of filenames with detected possible subtitles.
  * The array contains max MAX_SUBTITLE_FILES items and you need to free it after use.
  */
-char** subtitles_Detect( input_thread_t *p_this, char *psz_path, char *psz_fname )
+char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
+                         char *psz_fname )
 {
     /* variables to be used for derivatives of psz_fname */
     char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp;
@@ -204,14 +205,16 @@ char** subtitles_Detect( input_thread_t *p_this, char *psz_path, char *psz_fname
     if( tmp )
     {
         int pos;
-        strcpy( f_fname, tmp + 1 );
-        pos = tmp - psz_fname;
-        strncpy( f_dir, psz_fname, pos + 1 );
-        f_dir[pos + 1] = 0;
+        strncpy( f_fname, tmp + 1, len - 1 );
+        f_fname[len - 1] = 0;
+        pos = tmp - psz_fname + 1;
+        strncpy( f_dir, psz_fname, __MIN(pos,len-1) );
+        f_dir[__MIN(pos,len-1)] = 0;
     }
     else
     {
-        strcpy( f_fname, psz_fname );
+        strncpy( f_fname, psz_fname, len - 1 );
+        f_fname[len - 1] = 0;
         strcpy( f_dir, "" );
     }
 
@@ -240,7 +243,8 @@ char** subtitles_Detect( input_thread_t *p_this, char *psz_path, char *psz_fname
                     if( strcmp(sub_exts[i], tmp_fname_ext ) == 0 )
                     {
                         b_found = 1;
-                        msg_Dbg( p_this, "found a possible subtitle: %s", de->d_name );
+                        msg_Dbg( p_this, "found a possible subtitle: %s",
+                                 de->d_name );
                         break;
                     }
                 }
@@ -249,12 +253,13 @@ char** subtitles_Detect( input_thread_t *p_this, char *psz_path, char *psz_fname
                 if( b_found )
                 {
                     int i_prio = 0;
-                    if( !i_prio && strcmp( tmp_fname_trim, f_fname_trim ) == 0 )
+                    if( !i_prio && !strcmp( tmp_fname_trim, f_fname_trim ) )
                     {
                         /* matches the movie name exactly */
                         i_prio = 4;
                     }
-                    if( !i_prio && ( tmp = strstr( tmp_fname_trim, f_fname_trim ) ) )
+                    if( !i_prio &&
+                        ( tmp = strstr( tmp_fname_trim, f_fname_trim ) ) )
                     {
                         /* contains the movie name */
                         tmp += strlen( f_fname_trim );
@@ -265,7 +270,8 @@ char** subtitles_Detect( input_thread_t *p_this, char *psz_path, char *psz_fname
                         }
                         else
                         {
-                            /* chars after (and possibly in front of) the movie name */
+                            /* chars after (and possibly in front of)
+                             * the movie name */
                             i_prio = 3;
                         }
                     }
@@ -277,17 +283,18 @@ char** subtitles_Detect( input_thread_t *p_this, char *psz_path, char *psz_fname
 
                     if( i_prio >= fuzzy.i_int )
                     {
-                        sprintf( tmpresult, "%s%s", j == 0 ? f_dir : psz_path, de->d_name );
-                        msg_Dbg( p_this, "autodetected subtitle: %s with priority %d", de->d_name, i_prio );
+                        sprintf( tmpresult, "%s%s", j == 0 ? f_dir : psz_path,
+                                 de->d_name );
+                        msg_Dbg( p_this, "autodetected subtitle: %s with "
+                                 "priority %d", de->d_name, i_prio );
                         if( ( f = fopen( tmpresult, "rt" ) ) )
                         {
                             fclose( f );
                             result[i_sub_count].priority = i_prio;
-                            result[i_sub_count].psz_fname = strdup( tmpresult );
+                            result[i_sub_count].psz_fname = strdup(tmpresult);
                             i_sub_count++;
                         }
                     }
-
                 }
                 if( i_sub_count >= MAX_SUBTITLE_FILES ) break;
             }
@@ -319,4 +326,3 @@ char** subtitles_Detect( input_thread_t *p_this, char *psz_path, char *psz_fname
     free( result );
     return result2;
 }
-