]> git.sesse.net Git - vlc/commitdiff
vlc_readdir: remove string duplication, simplify
authorRémi Denis-Courmont <remi@remlab.net>
Mon, 27 Jan 2014 16:46:51 +0000 (18:46 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Mon, 27 Jan 2014 16:56:15 +0000 (18:56 +0200)
13 files changed:
include/vlc_fs.h
modules/access/dvb/scan.c
modules/demux/mkv/mkv.cpp
modules/gui/ncurses.c
modules/gui/skins2/src/theme_loader.cpp
modules/gui/skins2/src/theme_repository.cpp
modules/lua/libs/net.c
src/input/subtitles.c
src/modules/bank.c
src/playlist/art.c
src/posix/filesystem.c
src/text/filesystem.c
src/win32/filesystem.c

index 86be4c27be9fb5d9e393fbdb58e0e1c437f4e529..533aa9bc91d51d0dbad6d79118a0ed8de3b3506e 100644 (file)
@@ -46,11 +46,25 @@ VLC_API int vlc_rename( const char *oldpath, const char *newpath );
 VLC_API char *vlc_getcwd( void ) VLC_USED;
 
 #if defined( _WIN32 )
+typedef struct vlc_DIR
+{
+    _WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
+    char *entry;
+    union
+    {
+        DWORD drives;
+        bool insert_dot_dot;
+    } u;
+} vlc_DIR;
+
 static inline int vlc_closedir( DIR *dir )
 {
-    _WDIR *wdir = *(_WDIR **)dir;
-    free( dir );
-    return wdir ? _wclosedir( wdir ) : 0;
+    vlc_DIR *vdir = (vlc_DIR *)dir;
+    _WDIR *wdir = vdir->wdir;
+
+    free( vdir->entry );
+    free( vdir );
+    return (wdir != NULL) ? _wclosedir( wdir ) : 0;
 }
 # undef closedir
 # define closedir vlc_closedir
index 98540129e0c58568a036fb36ebf029de36eda81b..d8a808c32564f25b781edec74c69610761a4e08a 100644 (file)
@@ -271,20 +271,17 @@ static int ScanDvbSNextFast( scan_t *p_scan, scan_configuration_t *p_cfg, double
 
         /* find the requested file in the directory */
         for( ; ; ) {
-            char *psz_filename;
+            const char *psz_filename = vlc_readdir( p_dir );
 
-            if( ! (psz_filename = vlc_readdir( p_dir ) ) )
+            if( psz_filename == NULL )
                 break;
 
             if( !strncmp( p_scan->parameter.sat_info.psz_name, psz_filename, 20 ) )
             {
                 if( asprintf( &p_scan->parameter.sat_info.psz_path, "%s" DIR_SEP "%s", psz_dir, psz_filename ) == -1 )
                     p_scan->parameter.sat_info.psz_path = NULL;
-
-                free( psz_filename );
                 break;
             }
-            free( psz_filename );
         }
 
         closedir( p_dir );
index edf6a6f271688df5be4389079fd147c7864bc61c..ac2c977d60cf213b838a8bf996ef838c0bf4f85f 100644 (file)
@@ -166,7 +166,7 @@ static int Open( vlc_object_t * p_this )
 
             if (p_src_dir != NULL)
             {
-                char *psz_file;
+                const char *psz_file;
                 while ((psz_file = vlc_readdir(p_src_dir)) != NULL)
                 {
                     if (strlen(psz_file) > 4)
@@ -179,7 +179,6 @@ static int Open( vlc_object_t * p_this )
                         if (!s_filename.compare(p_demux->psz_file))
 #endif
                         {
-                            free (psz_file);
                             continue; // don't reuse the original opened file
                         }
 
@@ -229,7 +228,6 @@ static int Open( vlc_object_t * p_this )
                             }
                         }
                     }
-                    free (psz_file);
                 }
                 closedir( p_src_dir );
             }
index 444cdb338c910b03044dfe12f522b7cc6541dea8..7a4e1755f8ac4ab6e2eac3b061be60e30a98015e 100644 (file)
@@ -287,23 +287,20 @@ static void ReadDir(intf_thread_t *intf)
 
     DirsDestroy(sys);
 
-    char *entry;
+    const char *entry;
     while ((entry = vlc_readdir(current_dir))) {
         if (!sys->show_hidden_files && *entry == '.' && strcmp(entry, ".."))
-            goto next;
+            continue;
 
         struct dir_entry_t *dir_entry = malloc(sizeof *dir_entry);
-        if (!dir_entry)
-            goto next;
+        if (unlikely(dir_entry == NULL))
+            continue;
 
         dir_entry->file = IsFile(sys->current_dir, entry);
-        dir_entry->path = entry;
+        dir_entry->path = xstrdup(entry);
         INSERT_ELEM(sys->dir_entries, sys->n_dir_entries,
              sys->n_dir_entries, dir_entry);
         continue;
-
-next:
-        free(entry);
     }
 
     qsort(sys->dir_entries, sys->n_dir_entries,
index 866f4b4b74da830e2a3a3c674c82226f495a2a08..5783cdaa707222fb95c4247a53d2bcefd12f6322 100644 (file)
@@ -392,11 +392,10 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
     // Path separator
     const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
 
-    DIR *pCurrDir;
-    char *pszDirContent;
+    const char *pszDirContent;
 
     // Open the dir
-    pCurrDir = vlc_opendir( rootDir.c_str() );
+    DIR *pCurrDir = vlc_opendir( rootDir.c_str() );
 
     if( pCurrDir == NULL )
     {
@@ -428,7 +427,6 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
                 // Can we find the file in this subdirectory?
                 if( findFile( newURI, rFileName, themeFilePath ) )
                 {
-                    free( pszDirContent );
                     closedir( pCurrDir );
                     return true;
                 }
@@ -439,14 +437,11 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
                 if( rFileName == string( pszDirContent ) )
                 {
                     themeFilePath = newURI;
-                    free( pszDirContent );
                     closedir( pCurrDir );
                     return true;
                 }
             }
         }
-
-        free( pszDirContent );
     }
 
     closedir( pCurrDir );
index b908b5a9f980a0109763b9699d1509b383d79a0c..7df22830553940ba4fb5023933979e36db06c663 100644 (file)
@@ -136,15 +136,14 @@ ThemeRepository::~ThemeRepository()
 
 void ThemeRepository::parseDirectory( const string &rDir_locale )
 {
-    DIR *pDir;
-    char *pszDirContent;
+    const char *pszDirContent;
     // Path separator
     const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
 
     // Open the dir
     // FIXME: parseDirectory should be invoked with UTF-8 input instead!!
     string rDir = sFromLocale( rDir_locale );
-    pDir = vlc_opendir( rDir.c_str() );
+    DIR *pDir = vlc_opendir( rDir.c_str() );
 
     if( pDir == NULL )
     {
@@ -174,8 +173,6 @@ void ThemeRepository::parseDirectory( const string &rDir_locale )
 
             msg_Dbg( getIntf(), "found skin %s", path.c_str() );
         }
-
-        free( pszDirContent );
     }
 
     closedir( pDir );
index 74ffe93c5e844c1ea6c6050b89e92890e83cec27..d4fe299ff46f06e572f2af468c0b80b453dbf5c4 100644 (file)
@@ -477,12 +477,11 @@ static int vlclua_opendir( lua_State *L )
     lua_newtable( L );
     for( ;; )
     {
-        char *psz_filename = vlc_readdir( p_dir );
+        const char *psz_filename = vlc_readdir( p_dir );
         if( !psz_filename ) break;
         i++;
         lua_pushstring( L, psz_filename );
         lua_rawseti( L, -2, i );
-        free( psz_filename );
     }
     closedir( p_dir );
     return 1;
index 5a686ba950498e75fe89ea18f5ba3dd692f8f7d5..085187bf5576fe0513279a0c26c142af5cbe53da 100644 (file)
@@ -312,14 +312,11 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
 
         msg_Dbg( p_this, "looking for a subtitle file in %s", psz_dir );
 
-        char *psz_name;
+        const char *psz_name;
         while( (psz_name = vlc_readdir( dir )) && i_sub_count < MAX_SUBTITLE_FILES )
         {
             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];
@@ -365,10 +362,7 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
 
                 sprintf( psz_path, "%s"DIR_SEP"%s", psz_dir, psz_name );
                 if( !strcmp( psz_path, psz_fname ) )
-                {
-                    free( psz_name );
                     continue;
-                }
 
                 if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) && result )
                 {
@@ -386,7 +380,6 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
                              psz_path, i_prio );
                 }
             }
-            free( psz_name );
         }
         closedir( dir );
     }
index ba2593b79f0880d9f2db4e73a7000900b546cd38..6655f006656adf205b37d4a5888bbacff5a7692d 100644 (file)
@@ -444,7 +444,7 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
 
         /* Skip ".", ".." */
         if (!strcmp (file, ".") || !strcmp (file, ".."))
-            goto skip;
+            continue;
 
         /* Compute path relative to plug-in base directory */
         if (reldir != NULL)
@@ -455,7 +455,7 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
         else
             relpath = strdup (file);
         if (unlikely(relpath == NULL))
-            goto skip;
+            continue;
 
         /* Compute absolute path */
         if (asprintf (&abspath, "%s"DIR_SEP"%s", bank->base, relpath) == -1)
@@ -493,7 +493,6 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
     skip:
         free (relpath);
         free (abspath);
-        free (file);
     }
     closedir (dh);
 }
index b38db74ce03c3e7ba33ab32c1a330b18262b080a..18a376441f26fabdfcf27894e49815c0990e6ae5 100644 (file)
@@ -173,7 +173,7 @@ int playlist_FindArtInCache( input_item_t *p_item )
     }
 
     bool b_found = false;
-    char *psz_filename;
+    const char *psz_filename;
     while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
     {
         if( !strncmp( psz_filename, "art", 3 ) )
@@ -193,7 +193,6 @@ int playlist_FindArtInCache( input_item_t *p_item )
 
             b_found = true;
         }
-        free( psz_filename );
     }
 
     /* */
index afb132b60acc1bd20dbb7b6f8fa91d6dcdbad7da..326af9ec30446e510a1eef87555757a2effd1ccb 100644 (file)
@@ -145,14 +145,15 @@ DIR *vlc_opendir (const char *dirname)
  * @param dir directory handle as returned by vlc_opendir()
  *            (must not be used by another thread concurrently)
  *
- * @return a UTF-8 string of the directory entry. Use free() to release it.
+ * @return a UTF-8 string of the directory entry. The string is valid until
+ * the next call to vlc_readdir() or closedir() on the handle.
  * If there are no more entries in the directory, NULL is returned.
  * If an error occurs, errno is set and NULL is returned.
  */
 char *vlc_readdir( DIR *dir )
 {
     struct dirent *ent = readdir (dir);
-    return (ent != NULL) ? strdup (ent->d_name) : NULL;
+    return (ent != NULL) ? ent->d_name : NULL;
 }
 
 /**
index 981a053a22915a269f7934237da52d241faff822..31d4dc664af5cd4c08a74fe3a701c33feed82236 100644 (file)
@@ -127,7 +127,7 @@ int vlc_loaddir( DIR *dir, char ***namelist,
     for (unsigned size = 0;;)
     {
         errno = 0;
-        char *entry = vlc_readdir (dir);
+        const char *entry = vlc_readdir (dir);
         if (entry == NULL)
         {
             if (errno)
@@ -136,10 +136,7 @@ int vlc_loaddir( DIR *dir, char ***namelist,
         }
 
         if (!select (entry))
-        {
-            free (entry);
             continue;
-        }
 
         if (num >= size)
         {
@@ -147,14 +144,13 @@ int vlc_loaddir( DIR *dir, char ***namelist,
             char **newtab = realloc (tab, sizeof (*tab) * (size));
 
             if (unlikely(newtab == NULL))
-            {
-                free (entry);
                 goto error;
-            }
             tab = newtab;
         }
 
-        tab[num++] = entry;
+        tab[num] = strdup(entry);
+        if (likely(tab[num] != NULL))
+            num++;
     }
 
     if (compar != NULL)
index 886a158841604236a5c629cad9e5403b9e539b0e..f938147021ce83e479ccda46c50c2c43ea7a1fe9 100644 (file)
@@ -129,17 +129,6 @@ char *vlc_getcwd (void)
 
 /* Under Windows, these wrappers return the list of drive letters
  * when called with an empty argument or just '\'. */
-typedef struct vlc_DIR
-{
-    _WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
-    union
-    {
-        DWORD drives;
-        bool insert_dot_dot;
-    } u;
-} vlc_DIR;
-
-
 DIR *vlc_opendir (const char *dirname)
 {
     wchar_t *wpath = widen_path (dirname);
@@ -175,6 +164,7 @@ DIR *vlc_opendir (const char *dirname)
         return NULL;
     }
     p_dir->wdir = wdir;
+    p_dir->entry = NULL;
     return (void *)p_dir;
 }
 
@@ -182,6 +172,8 @@ char *vlc_readdir (DIR *dir)
 {
     vlc_DIR *p_dir = (vlc_DIR *)dir;
 
+    free(p_dir->entry);
+
 #if !VLC_WINSTORE_APP
     /* Drive letters mode */
     if (p_dir->wdir == NULL)
@@ -196,24 +188,23 @@ char *vlc_readdir (DIR *dir)
         p_dir->u.drives &= ~(1UL << i);
         assert (i < 26);
 
-        char *ret;
-        if (asprintf (&ret, "%c:\\", 'A' + i) == -1)
-            return NULL;
-        return ret;
+        if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
+            p_dir->entry = NULL;
     }
+    else
 #endif
-
     if (p_dir->u.insert_dot_dot)
     {
         /* Adds "..", gruik! */
         p_dir->u.insert_dot_dot = false;
-        return strdup ("..");
+        p_dir->entry = strdup ("..");
     }
-
-    struct _wdirent *ent = _wreaddir (p_dir->wdir);
-    if (ent == NULL)
-        return NULL;
-    return FromWide (ent->d_name);
+    else
+    {
+        struct _wdirent *ent = _wreaddir (p_dir->wdir);
+        p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
+    }
+    return p_dir->entry;
 }
 
 int vlc_stat (const char *filename, struct stat *buf)