#include <stdio.h>
#include <stdarg.h>
+#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_DIRENT_H
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
+
#ifndef HAVE_LSTAT
# define lstat( a, b ) stat(a, b)
#endif
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
#endif
}
-/*****************************************************************************
+/**
* utf8_fopen: Calls fopen() after conversion of file name to OS locale
- *****************************************************************************/
+ */
FILE *utf8_fopen( const char *filename, const char *mode )
{
-#if !(defined (WIN32) || defined (UNDER_CE))
+#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 );
+ }
+#endif
const char *local_name = ToLocale( filename );
if( local_name != NULL )
}
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';
-
- /*
- * fopen() cannot open files with non-“ANSI” characters on Windows.
- * We use _wfopen() instead. Same thing for mkdir() and stat().
- */
- return _wfopen( wpath, wmode );
-#endif
+ return NULL;
}
-/*****************************************************************************
+/**
* utf8_mkdir: Calls mkdir() after conversion of file name to OS locale
- *****************************************************************************/
+ */
int utf8_mkdir( const char *dirname )
{
#if defined (UNDER_CE) || defined (WIN32)
{
if( GetLastError( ) == ERROR_ALREADY_EXISTS )
errno = EEXIST;
- errno = ENOENT;
+ else
+ errno = ENOENT;
return -1;
}
return 0;
void *utf8_opendir( const char *dirname )
{
+ /* TODO: support for WinNT non-ACP filenames */
const char *local_name = ToLocale( dirname );
if( local_name != NULL )
{
- DIR *dir = opendir( local_name );
+ DIR *dir = vlc_opendir_wrapper( local_name );
LocaleFree( local_name );
return dir;
}
{
struct dirent *ent;
- ent = readdir( (DIR *)dir );
+ ent = vlc_readdir_wrapper( (DIR *)dir );
if( ent == NULL )
return NULL;
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 ) )
+ {
+ free( utf_entry );
+ continue;
+ }
+
+ newtab = realloc( tab, sizeof( char * ) * (num + 1) );
+ if( newtab == NULL )
+ {
+ free( utf_entry );
+ goto error;
+ }
+ tab = newtab;
+ tab[num++] = utf_entry;
+ }
+ vlc_closedir_wrapper( 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 )
{
-#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 )
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';
-
- /* struct _stat is just a silly Microsoft alias for struct stat */
- return _wstat( wpath, (struct _stat *)buf );
#endif
+ return -1;
}
return utf8_statEx( filename, buf, VLC_FALSE );
}
-/*****************************************************************************
+/**
* utf8_*printf: *printf with conversion from UTF-8 to local encoding
- *****************************************************************************/
+ */
static int utf8_vasprintf( char **str, const char *fmt, va_list ap )
{
char *utf8;
return res;
}
-static int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
+int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
{
char *str;
int res = utf8_vasprintf( &str, fmt, ap );
return res;
}
-/*****************************************************************************
- * EnsureUTF8: replaces invalid/overlong UTF-8 sequences with question marks
- *****************************************************************************
- * Not Todo : convert Latin1 to UTF-8 on the fly
- * It is not possible given UTF-8 needs more space
- * Returns str if it was valid UTF-8, NULL if not.
- *****************************************************************************/
+
+static char *CheckUTF8( char *str, char rep )
#define isutf8cont( c ) (((c) >= 0x80) && ((c) <= 0xBF))
-char *EnsureUTF8( char *str )
{
unsigned char *ptr, c;
+ assert (str != NULL);
+
ptr = (unsigned char *)str;
while( (c = *ptr) != '\0' )
{
continue;
error:
+ if( rep == 0 )
+ return NULL;
*ptr++ = '?';
str = NULL;
}
return str;
}
+/**
+ * EnsureUTF8: replaces invalid/overlong UTF-8 sequences with question marks
+ * Note that it is not possible to convert from Latin-1 to UTF-8 on the fly,
+ * so we don't try that, even though it would be less disruptive.
+ *
+ * @return str if it was valid UTF-8, NULL if not.
+ */
+char *EnsureUTF8( char *str )
+{
+ return CheckUTF8( str, '?' );
+}
+
+
+/**
+ * IsUTF8: checks whether a string is a valid UTF-8 byte sequence.
+ *
+ * @param str nul-terminated string to be checked
+ *
+ * @return str if it was valid UTF-8, NULL if not.
+ */
+const char *IsUTF8( const char *str )
+{
+ return CheckUTF8( (char *)str, 0 );
+}
+
+
/**
* UTF32toUTF8(): converts an array from UTF-32 (host byte order)
* to UTF-8.
/* determine the size of the string */
for( len = 1, in = src; *in; len++ )
- in += 2;
+ in++;
return UTF16toUTF8( src, len, NULL );
}