- * utf8_readdir: a readdir wrapper that returns the name of the next entry
- * in the directory as a UTF-8 string.
- *
- * @param dir The directory that is being read
- *
- * @return a UTF-8 string of the directory entry. Use free() to free this memory.
- */
-char *utf8_readdir( DIR *dir )
-{
-#ifdef WIN32
- struct _wdirent *ent = vlc_wreaddir (dir);
- if (ent == NULL)
- return NULL;
-
- return FromWide (ent->d_name);
-#else
- struct dirent *ent;
-
- ent = readdir( (DIR *)dir );
- if( ent == NULL )
- return NULL;
-
- return vlc_fix_readdir( ent->d_name );
-#endif
-}
-
-static int dummy_select( const char *str )
-{
- (void)str;
- return 1;
-}
-
-int utf8_loaddir( DIR *dir, char ***namelist,
- int (*select)( const char * ),
- int (*compar)( const char **, const char ** ) )
-{
- if( select == NULL )
- select = dummy_select;
-
- if( dir == NULL )
- return -1;
- else
- {
- char **tab = NULL;
- char *entry;
- unsigned num = 0;
-
- rewinddir( dir );
-
- while( ( entry = utf8_readdir( dir ) ) != NULL )
- {
- char **newtab;
-
- if( !select( entry ) )
- {
- free( entry );
- continue;
- }
-
- newtab = realloc( tab, sizeof( char * ) * (num + 1) );
- if( newtab == NULL )
- {
- free( entry );
- goto error;
- }
- tab = newtab;
- tab[num++] = entry;
- }
-
- 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;
-}
-
-int utf8_scandir( const char *dirname, char ***namelist,
- int (*select)( const char * ),
- int (*compar)( const char **, const char ** ) )
-{
- DIR *dir = utf8_opendir (dirname);
- int val = -1;
-
- if (dir != NULL)
- {
- val = utf8_loaddir (dir, namelist, select, compar);
- closedir (dir);
- }
- return val;
-}
-
-static int utf8_statEx( const char *filename, struct stat *buf,
- vlc_bool_t deref )
-{
-#if defined (WIN32) || defined (UNDER_CE)
- /* retrieve Windows OS version */
- if( GetVersion() < 0x80000000 )
- {
- /* for Windows NT and above */
- wchar_t wpath[MAX_PATH + 1];
-
- if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
- {
- errno = ENOENT;
- return -1;
- }
- wpath[MAX_PATH] = L'\0';
-
- return _wstati64( wpath, buf );
- }
-#endif
-#ifdef HAVE_SYS_STAT_H
- const char *local_name = ToLocale( filename );
-
- if( local_name != NULL )
- {
- int res = deref ? stat( local_name, buf )
- : lstat( local_name, buf );
- LocaleFree( local_name );
- return res;
- }
- errno = ENOENT;
-#endif
- return -1;
-}
-
-
-int utf8_stat( const char *filename, struct stat *buf)
-{
- return utf8_statEx( filename, buf, VLC_TRUE );
-}
-
-int utf8_lstat( const char *filename, struct stat *buf)
-{
- return utf8_statEx( filename, buf, VLC_FALSE );
-}
-
-/**
- * utf8_unlink: Calls unlink() after conversion of file name to OS locale
- *
- * @param filename a UTF-8 string with the name of the file you want to delete.
- * @return A 0 return value indicates success. A -1 return value indicates an
- * error, and an error code is stored in errno
- */
-int utf8_unlink( const char *filename )
-{
-#if defined (WIN32) || defined (UNDER_CE)
- if( GetVersion() < 0x80000000 )
- {
- /* for Windows NT and above */
- wchar_t wpath[MAX_PATH + 1];
-
- if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
- {
- errno = ENOENT;
- return -1;
- }
- wpath[MAX_PATH] = L'\0';
-
- /*
- * unlink() cannot open files with non-“ANSI” characters on Windows.
- * We use _wunlink() instead.
- */
- return _wunlink( wpath );
- }
-#endif
- const char *local_name = ToLocale( filename );
-
- if( local_name == NULL )
- {
- errno = ENOENT;
- return -1;
- }
-
- int ret = unlink( local_name );
- LocaleFree( local_name );
- return ret;
-}
-
-
-
-/**
- * utf8_*printf: *printf with conversion from UTF-8 to local encoding