|
Packit |
5e46da |
/*
|
|
Packit |
5e46da |
* This file is part of libbluray
|
|
Packit |
5e46da |
* Copyright (C) 2009-2010 John Stebbins
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is free software; you can redistribute it and/or
|
|
Packit |
5e46da |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License as published by the Free Software Foundation; either
|
|
Packit |
5e46da |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
5e46da |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5e46da |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
5e46da |
* Lesser General Public License for more details.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License along with this library. If not, see
|
|
Packit |
5e46da |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#if HAVE_CONFIG_H
|
|
Packit |
5e46da |
#include "config.h"
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include "file.h"
|
|
Packit |
5e46da |
#include "util/macro.h"
|
|
Packit |
5e46da |
#include "util/logging.h"
|
|
Packit |
5e46da |
#include "util/strutl.h"
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include <stdlib.h>
|
|
Packit |
5e46da |
#include <string.h>
|
|
Packit |
5e46da |
#if defined(HAVE_DIRENT_H)
|
|
Packit |
5e46da |
# include <dirent.h>
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include <io.h>
|
|
Packit |
5e46da |
#include <windows.h>
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
typedef struct {
|
|
Packit |
5e46da |
intptr_t handle;
|
|
Packit |
5e46da |
struct _wfinddata_t info;
|
|
Packit |
5e46da |
} dir_data_t;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static void _dir_close_win32(BD_DIR_H *dir)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
if (dir) {
|
|
Packit |
5e46da |
dir_data_t *priv = (dir_data_t*)dir->internal;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
_findclose(priv->handle);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DEBUG(DBG_DIR, "Closed WIN32 dir (%p)\n", (void*)dir);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
X_FREE(dir->internal);
|
|
Packit |
5e46da |
X_FREE(dir);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _dir_read_win32(BD_DIR_H *dir, BD_DIRENT *entry)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
dir_data_t *priv = (dir_data_t*)dir->internal;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (!priv->info.name[0]) {
|
|
Packit |
5e46da |
return 1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
if (!WideCharToMultiByte(CP_UTF8, 0, priv->info.name, -1, entry->d_name, sizeof(entry->d_name), NULL, NULL)) {
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
priv->info.name[0] = 0;
|
|
Packit |
5e46da |
_wfindnext(priv->handle, &priv->info);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static dir_data_t *_open_impl(const char *dirname)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
dir_data_t *priv;
|
|
Packit |
5e46da |
char *filespec;
|
|
Packit |
5e46da |
wchar_t wfilespec[MAX_PATH];
|
|
Packit |
5e46da |
int result;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
filespec = str_printf("%s" DIR_SEP "*", dirname);
|
|
Packit |
5e46da |
if (!filespec) {
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filespec, -1, wfilespec, MAX_PATH);
|
|
Packit |
5e46da |
X_FREE(filespec);
|
|
Packit |
5e46da |
if (!result) {
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
priv = calloc(1, sizeof(dir_data_t));
|
|
Packit |
5e46da |
if (!priv) {
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
priv->handle = _wfindfirst(wfilespec, &priv->info);
|
|
Packit |
5e46da |
if (priv->handle == -1) {
|
|
Packit |
5e46da |
X_FREE(priv);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return priv;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static BD_DIR_H *_dir_open_win32(const char* dirname)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
BD_DIR_H *dir = calloc(1, sizeof(BD_DIR_H));
|
|
Packit |
5e46da |
dir_data_t *priv = NULL;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DEBUG(DBG_DIR, "Opening WIN32 dir %s... (%p)\n", dirname, (void*)dir);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (!dir) {
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
priv = _open_impl(dirname);
|
|
Packit |
5e46da |
if (priv) {
|
|
Packit |
5e46da |
dir->close = _dir_close_win32;
|
|
Packit |
5e46da |
dir->read = _dir_read_win32;
|
|
Packit |
5e46da |
dir->internal = priv;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return dir;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DEBUG(DBG_DIR, "Error opening dir %s\n", dirname);
|
|
Packit |
5e46da |
X_FREE(dir);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DIR_H* (*dir_open)(const char* dirname) = _dir_open_win32;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DIR_OPEN dir_open_default(void)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
return _dir_open_win32;
|
|
Packit |
5e46da |
}
|