X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmisc%2Funicode.c;h=9dd49e4d1b3643e2f31cae92394ff3c0a646653f;hb=1f62202f719d7cd3ed5d34fd3b8a55a204b368f4;hp=0885205104bd34c719a1ba9616c00d37e9d5f18c;hpb=b6ec3349bcc616bad075dac166ab3a195d52af10;p=vlc diff --git a/src/misc/unicode.c b/src/misc/unicode.c index 0885205104..9dd49e4d1b 100644 --- a/src/misc/unicode.c +++ b/src/misc/unicode.c @@ -140,18 +140,18 @@ static char *MB2MB( const char *string, UINT fromCP, UINT toCP ) int len; len = MultiByteToWideChar( fromCP, 0, string, -1, NULL, 0 ); - assert( len > 0 ); - wide = (wchar_t *)malloc (len * sizeof (wchar_t)); - if( wide == NULL ) + if( len == 0 ) return NULL; + wchar_t wide[len]; + MultiByteToWideChar( fromCP, 0, string, -1, wide, len ); len = WideCharToMultiByte( toCP, 0, wide, -1, NULL, 0, NULL, NULL ); - assert( len > 0 ); + if( len == 0 ) + return NULL; out = malloc( len ); WideCharToMultiByte( toCP, 0, wide, -1, out, len, NULL, NULL ); - free( wide ); return out; } #endif @@ -306,56 +306,41 @@ void LocaleFree( const char *str ) */ FILE *utf8_fopen( const char *filename, const char *mode ) { -#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 - wchar_t wpath[MAX_PATH + 1]; - size_t len = strlen( mode ) + 1; - wchar_t wmode[len]; - - if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) - || !MultiByteToWideChar( CP_ACP, 0, mode, len, wmode, len ) ) - { - errno = ENOENT; - return NULL; - } - wpath[MAX_PATH] = L'\0'; - - /* retrieve OS version */ +#if defined (WIN32) || defined (UNDER_CE) if( GetVersion() < 0x80000000 ) { /* for Windows NT and above */ + wchar_t wpath[MAX_PATH + 1]; + size_t len = strlen( mode ) + 1; + wchar_t wmode[len]; + + if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) + || !MultiByteToWideChar( CP_ACP, 0, mode, len, wmode, len ) ) + { + errno = ENOENT; + return NULL; + } + wpath[MAX_PATH] = L'\0'; + /* * fopen() cannot open files with non-“ANSI” characters on Windows. * We use _wfopen() instead. Same thing for mkdir() and stat(). */ return _wfopen( wpath, wmode ); } - else +#endif + const char *local_name = ToLocale( filename ); + + if( local_name != NULL ) { - /* for Windows Me/98/95 */ - /* we use GetShortFileNameW to get the DOS 8.3 version of the file we need to open */ - char spath[MAX_PATH + 1]; - if( GetShortPathNameW( wpath, spath, MAX_PATH ) ) - { - fprintf( stderr, "fopen path: %s -> %s\n", wpath, spath ); - return fopen( spath, wmode ); - } - fprintf( stderr, "GetShortPathName for %s failed\n", wpath ); - errno = ENOENT; - return NULL; + FILE *stream = fopen( local_name, mode ); + LocaleFree( local_name ); + return stream; } -#endif + else + errno = ENOENT; + + return NULL; } /** @@ -418,6 +403,7 @@ int utf8_mkdir( const char *dirname ) void *utf8_opendir( const char *dirname ) { + /* TODO: support for WinNT non-ACP filenames */ const char *local_name = ToLocale( dirname ); if( local_name != NULL ) @@ -512,8 +498,24 @@ int utf8_scandir( const char *dirname, char ***namelist, static int utf8_statEx( const char *filename, void *buf, vlc_bool_t deref ) { -#if !(defined (WIN32) || defined (UNDER_CE)) -# ifdef HAVE_SYS_STAT_H +#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, (struct _stati64 *)buf ); + } +#endif +#ifdef HAVE_SYS_STAT_H const char *local_name = ToLocale( filename ); if( local_name != NULL ) @@ -524,39 +526,8 @@ static int utf8_statEx( const char *filename, void *buf, return res; } errno = ENOENT; -# endif - return -1; -#else - 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'; - - /* retrieve Windows OS version */ - if( GetVersion() < 0x80000000 ) - { - /* for Windows NT and above */ - return _wstati64( wpath, (struct _stati64 *)buf ); - } - else - { - /* for Windows Me/98/95 */ - /* we use GetShortFileNameW to get the DOS 8.3 version */ - char spath[MAX_PATH + 1]; - if( GetShortPathNameW( wpath, spath, MAX_PATH ) ) - { - fprintf( stderr, "stati path: %s -> %s\n", wpath, spath ); - return _stati64( spath, (struct _stati64 *)buf ); - } - fprintf( stderr, "GetShortPathName for %s failed\n", wpath ); - errno = ENOENT; - return -1; - } #endif + return -1; }