- return locale_fast (utf8, &to_locale);
-}
-
-
-static char *ToLocaleDup (const char *utf8)
-{
- return locale_dup (utf8, &to_locale);
-}
-
-
-/**
- * utf8_open: open() wrapper for UTF-8 filenames
- */
-int utf8_open (const char *filename, int flags, mode_t mode)
-{
-#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';
-
- /*
- * open() cannot open files with non-“ANSI” characters on Windows.
- * We use _wopen() instead. Same thing for mkdir() and stat().
- */
- return _wopen (wpath, flags, mode);
- }
-#endif
- const char *local_name = ToLocale (filename);
-
- if (local_name == NULL)
- {
- errno = ENOENT;
- return -1;
- }
-
- int fd = open (local_name, flags, mode);
- LocaleFree (local_name);
- return fd;
-}
-
-/**
- * utf8_fopen: fopen() wrapper for UTF-8 filenames
- */
-FILE *utf8_fopen (const char *filename, const char *mode)
-{
- int rwflags = 0, oflags = 0;
- vlc_bool_t append = VLC_FALSE;
-
- for (const char *ptr = mode; *ptr; ptr++)
- {
- switch (*ptr)
- {
- case 'r':
- rwflags = O_RDONLY;
- break;
-
- case 'a':
- rwflags = O_WRONLY;
- oflags |= O_CREAT;
- append = VLC_TRUE;
- break;
-
- case 'w':
- rwflags = O_WRONLY;
- oflags |= O_CREAT | O_TRUNC;
- break;
-
- case '+':
- rwflags = O_RDWR;
- break;
- }
- }
-
- int fd = utf8_open (filename, rwflags | oflags, 0666);
- if (fd == -1)
- return NULL;
-
- if (append && (lseek (fd, 0, SEEK_END) == -1))
- {
- close (fd);
- return NULL;
- }
-
- FILE *stream = fdopen (fd, mode);
- if (stream == NULL)
- close (fd);
-
- return stream;
-}
-
-/**
- * utf8_mkdir: Calls mkdir() after conversion of file name to OS locale
- *
- * @param dirname a UTF-8 string with the name of the directory that you
- * want to create.
- * @return A 0 return value indicates success. A -1 return value indicates an
- * error, and an error code is stored in errno
- */
-int utf8_mkdir( const char *dirname )
-{
-#if defined (UNDER_CE) || defined (WIN32)
- wchar_t wname[MAX_PATH + 1];
- char mod[MAX_PATH + 1];
- int i;
-
- /* Convert '/' into '\' */
- for( i = 0; *dirname; i++ )
- {
- if( i == MAX_PATH )
- return -1; /* overflow */
-
- if( *dirname == '/' )
- mod[i] = '\\';
- else
- mod[i] = *dirname;
- dirname++;
-
- }
- mod[i] = 0;
-
- if( MultiByteToWideChar( CP_UTF8, 0, mod, -1, wname, MAX_PATH ) == 0 )
- {
- errno = ENOENT;
- return -1;
- }
- wname[MAX_PATH] = L'\0';
-
- if( CreateDirectoryW( wname, NULL ) == 0 )
- {
- if( GetLastError( ) == ERROR_ALREADY_EXISTS )
- errno = EEXIST;
- else
- errno = ENOENT;
- return -1;
- }
- return 0;
-#else
- char *locname = ToLocale( dirname );
- int res;
-
- if( locname == NULL )
- {
- errno = ENOENT;
- return -1;
- }
- res = mkdir( locname, 0755 );
-
- LocaleFree( locname );
- return res;
-#endif