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

utf8_scandir: Unicode wrapper for scandir()

parent d51c8aac
......@@ -37,6 +37,7 @@ VLC_EXPORT( char *, ToLocale, ( const char * ) );
VLC_EXPORT( FILE *, utf8_fopen, ( const char *filename, const char *mode ) );
VLC_EXPORT( void *, utf8_opendir, ( const char *dirname ) );
VLC_EXPORT( const char *, utf8_readdir, ( void *dir ) );
VLC_EXPORT( int, utf8_scandir, ( const char *dirname, char ***namelist, int (*select)( const char * ), int (*compar)( const char **, const char ** ) ) );
VLC_EXPORT( int, utf8_stat, ( const char *filename, void *buf ) );
VLC_EXPORT( int, utf8_lstat, ( const char *filename, void *buf ) );
VLC_EXPORT( int, utf8_mkdir, ( const char *filename ) );
......
......@@ -487,6 +487,7 @@ struct module_symbols_t
char * (*FromUTF16_inner) (const uint16_t *);
const char * (*IsUTF8_inner) (const char *);
const char * (*GetFallbackEncoding_inner) (void);
int (*utf8_scandir_inner) (const char *dirname, char ***namelist, int (*select)( const char * ), int (*compar)( const char **, const char ** ));
};
# if defined (__PLUGIN__)
# define aout_FiltersCreatePipeline (p_symbols)->aout_FiltersCreatePipeline_inner
......@@ -954,6 +955,7 @@ struct module_symbols_t
# define FromUTF16 (p_symbols)->FromUTF16_inner
# define IsUTF8 (p_symbols)->IsUTF8_inner
# define GetFallbackEncoding (p_symbols)->GetFallbackEncoding_inner
# define utf8_scandir (p_symbols)->utf8_scandir_inner
# elif defined (HAVE_DYNAMIC_PLUGINS) && !defined (__BUILTIN__)
/******************************************************************
* STORE_SYMBOLS: store VLC APIs into p_symbols for plugin access.
......@@ -1424,6 +1426,7 @@ struct module_symbols_t
((p_symbols)->FromUTF16_inner) = FromUTF16; \
((p_symbols)->IsUTF8_inner) = IsUTF8; \
((p_symbols)->GetFallbackEncoding_inner) = GetFallbackEncoding; \
((p_symbols)->utf8_scandir_inner) = utf8_scandir; \
(p_symbols)->net_ConvertIPv4_deprecated = NULL; \
(p_symbols)->__stats_CounterGet_deprecated = NULL; \
(p_symbols)->__stats_TimerDumpAll_deprecated = NULL; \
......
......@@ -34,6 +34,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_DIRENT_H
......@@ -420,6 +421,66 @@ const char *utf8_readdir( void *dir )
return FromLocale( ent->d_name );
}
static int dummy_select( const char *str )
{
(void)str;
return 1;
}
int utf8_scandir( const char *dirname, char ***namelist,
int (*select)( const char * ),
int (*compar)( const char **, const char ** ) )
{
DIR *dir = utf8_opendir( dirname );
if( select == NULL )
select = dummy_select;
if( dir == NULL )
return -1;
else
{
char **tab = NULL;
const char *entry;
unsigned num = 0;
while( ( entry = utf8_readdir( dir ) ) != NULL )
{
char **newtab;
char *utf_entry = strdup( entry );
LocaleFree( entry );
if( utf_entry == NULL )
goto error;
if( !select( utf_entry ) )
continue;
newtab = realloc( tab, sizeof( char * ) * (num + 1) );
if( newtab == NULL )
goto error;
tab = newtab;
tab[num++] = utf_entry;
}
closedir( dir );
if( compar != NULL )
qsort( tab, num, sizeof( tab[0] ),
(int (*)( const void *, const void *))compar );
*namelist = tab;
return num;
error:{
unsigned i;
for( i = 0; i < num; i++ )
free( tab[i] );
if( tab != NULL )
free( tab );
return -1;}
}
}
static int utf8_statEx( const char *filename, void *buf,
vlc_bool_t deref )
......
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