[vlc-devel] [PATCH 1/5] input: constify input_SubtitleFileAdd

Thomas Guillem thomas at gllm.fr
Sat Feb 13 18:43:45 CET 2016


And don't do a strdup when it's not necessary
---
 src/input/input.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/src/input/input.c b/src/input/input.c
index 9836b78..24032be 100644
--- a/src/input/input.c
+++ b/src/input/input.c
@@ -103,7 +103,7 @@ enum {
 };
 
 static void input_SubtitleAdd( input_thread_t *, const char *, unsigned );
-static void input_SubtitleFileAdd( input_thread_t *, char *, unsigned );
+static void input_SubtitleFileAdd( input_thread_t *, const char *, unsigned );
 static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
 
 #undef input_Create
@@ -2811,32 +2811,34 @@ static void input_SubtitleAdd( input_thread_t *p_input,
     var_FreeList( &list, NULL );
 }
 
-static void input_SubtitleFileAdd( input_thread_t *p_input, char *psz_subtitle,
-                                   unsigned i_flags )
+static void input_SubtitleFileAdd( input_thread_t *p_input,
+                                   const char *psz_subtitle, unsigned i_flags )
 {
     /* if we are provided a subtitle.sub file,
      * see if we don't have a subtitle.idx and use it instead */
-    char *psz_path = strdup( psz_subtitle );
-    if( likely(psz_path != NULL) )
+    char *psz_idxpath = NULL;
+    char *psz_extension = strrchr( psz_subtitle, '.');
+    if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
     {
-        char *psz_extension = strrchr( psz_path, '.');
-        if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
+        psz_idxpath = strdup( psz_subtitle );
+        if( psz_idxpath )
         {
             struct stat st;
 
+            psz_extension = psz_extension - psz_subtitle + psz_idxpath;
             strcpy( psz_extension, ".idx" );
 
-            if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
+            if( !vlc_stat( psz_idxpath, &st ) && S_ISREG( st.st_mode ) )
             {
                 msg_Dbg( p_input, "using %s as subtitle file instead of %s",
-                         psz_path, psz_subtitle );
-                strcpy( psz_subtitle, psz_path ); /* <- FIXME! constify */
+                         psz_idxpath, psz_subtitle );
+                psz_subtitle = psz_idxpath;
             }
         }
-        free( psz_path );
     }
 
     char *url = vlc_path2uri( psz_subtitle, NULL );
+    free( psz_idxpath );
     if( url == NULL )
         return;
 
-- 
2.7.0



More information about the vlc-devel mailing list