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

Avoid code duplication

parent 9ddc8d6a
...@@ -306,20 +306,7 @@ void LocaleFree( const char *str ) ...@@ -306,20 +306,7 @@ void LocaleFree( const char *str )
*/ */
FILE *utf8_fopen( const char *filename, const char *mode ) FILE *utf8_fopen( const char *filename, const char *mode )
{ {
#if !(defined (WIN32) || defined (UNDER_CE)) #if defined (WIN32) || defined (UNDER_CE)
const char *local_name = ToLocale( filename );
if( local_name != NULL )
{
FILE *stream = fopen( local_name, mode );
LocaleFree( local_name );
return stream;
}
else
errno = ENOENT;
return NULL;
#else
/* retrieve OS version */
if( GetVersion() < 0x80000000 ) if( GetVersion() < 0x80000000 )
{ {
/* for Windows NT and above */ /* for Windows NT and above */
...@@ -341,43 +328,19 @@ FILE *utf8_fopen( const char *filename, const char *mode ) ...@@ -341,43 +328,19 @@ FILE *utf8_fopen( const char *filename, const char *mode )
*/ */
return _wfopen( wpath, wmode ); return _wfopen( wpath, wmode );
} }
else #endif
{ const char *local_name = ToLocale( filename );
/* for Windows Me/98/95 */
const char *local_name = ToLocale( filename );
if( local_name != NULL ) if( local_name != NULL )
{ {
FILE *p_file = fopen( local_name, mode ); FILE *stream = fopen( local_name, mode );
LocaleFree( local_name ); LocaleFree( local_name );
return p_file; return stream;
}
errno = ENOENT;
return NULL;
#if 0
/* Following code might work better in most cases but fails if file
doesn't already exist on call to GetShortPathNameW.
I'll keep it here in case we ever decide to try this first and
then fallback to previous solution if it fails. */
/* we use GetShortFileNameW to get the DOS 8.3 version of the file we need to open */
wchar_t spath[MAX_PATH + 1];
if( GetShortPathNameW( wpath, spath, MAX_PATH ) )
{
char path[ MAX_PATH + 1 ];
int len;
UINT i_codepage = AreFileApisANSI()?GetACP():GetOEMCP();
len = WideCharToMultiByte( i_codepage, 0, spath, -1, NULL, 0, NULL, NULL );
if( len == 0 )
return NULL;
WideCharToMultiByte( i_codepage, 0, spath, -1, path, len, NULL, NULL );
return fopen( path, mode );
}
errno = ENOENT;
return NULL;
#endif
} }
#endif else
errno = ENOENT;
return NULL;
} }
/** /**
...@@ -440,6 +403,7 @@ int utf8_mkdir( const char *dirname ) ...@@ -440,6 +403,7 @@ int utf8_mkdir( const char *dirname )
void *utf8_opendir( const char *dirname ) void *utf8_opendir( const char *dirname )
{ {
/* TODO: support for WinNT non-ACP filenames */
const char *local_name = ToLocale( dirname ); const char *local_name = ToLocale( dirname );
if( local_name != NULL ) if( local_name != NULL )
...@@ -534,21 +498,7 @@ int utf8_scandir( const char *dirname, char ***namelist, ...@@ -534,21 +498,7 @@ int utf8_scandir( const char *dirname, char ***namelist,
static int utf8_statEx( const char *filename, void *buf, static int utf8_statEx( const char *filename, void *buf,
vlc_bool_t deref ) vlc_bool_t deref )
{ {
#if !(defined (WIN32) || defined (UNDER_CE)) #if defined (WIN32) || defined (UNDER_CE)
# ifdef HAVE_SYS_STAT_H
const char *local_name = ToLocale( filename );
if( local_name != NULL )
{
int res = deref ? stat( local_name, (struct stat *)buf )
: lstat( local_name, (struct stat *)buf );
LocaleFree( local_name );
return res;
}
errno = ENOENT;
# endif
return -1;
#else
/* retrieve Windows OS version */ /* retrieve Windows OS version */
if( GetVersion() < 0x80000000 ) if( GetVersion() < 0x80000000 )
{ {
...@@ -564,43 +514,20 @@ static int utf8_statEx( const char *filename, void *buf, ...@@ -564,43 +514,20 @@ static int utf8_statEx( const char *filename, void *buf,
return _wstati64( wpath, (struct _stati64 *)buf ); return _wstati64( wpath, (struct _stati64 *)buf );
} }
else #endif
{ #ifdef HAVE_SYS_STAT_H
/* for Windows Me/98/95 */ const char *local_name = ToLocale( filename );
const char *local_name = ToLocale( filename );
if( local_name != NULL ) if( local_name != NULL )
{ {
int res = _stati64( local_name, (struct stat *)buf ); int res = deref ? stat( local_name, (struct stat *)buf )
LocaleFree( local_name ); : lstat( local_name, (struct stat *)buf );
return res; LocaleFree( local_name );
} return res;
errno = ENOENT;
return -1;
#if 0
/* Following code might work better in most cases but fails if file
doesn't already exist on call to GetShortPathNameW.
I'll keep it here in case we ever decide to try this first and
then fallback to previous solution if it fails. */
/* we use GetShortFileNameW to get the DOS 8.3 version */
wchar_t spath[MAX_PATH + 1];
if( GetShortPathNameW( wpath, spath, MAX_PATH ) )
{
char path[ MAX_PATH + 1 ];
int len;
UINT i_codepage = AreFileApisANSI()?GetACP():GetOEMCP();
len = WideCharToMultiByte( i_codepage, 0, spath, -1, NULL, 0, NULL, NULL );
if( len == 0 )
return -1;
WideCharToMultiByte( i_codepage, 0, spath, -1, path, len, NULL, NULL );
return _stati64( path, (struct _stati64 *)buf );
}
errno = ENOENT;
return -1;
#endif
} }
errno = ENOENT;
#endif #endif
return -1;
} }
......
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