Commit ee0b9164 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

vlc_readdir: remove string duplication, simplify

parent 7625fc4b
......@@ -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
......
......@@ -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 );
......
......@@ -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 );
}
......
......@@ -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,
......
......@@ -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 );
......
......@@ -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 );
......
......@@ -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;
......
......@@ -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 );
}
......
......@@ -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);
}
......
......@@ -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 );
}
/* */
......
......@@ -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;
}
/**
......
......@@ -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)
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment