| 1 |
/* |
| 2 |
* This file is part of libbluray |
| 3 |
* Copyright (C) 2009-2010 John Stebbins |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library. If not, see |
| 17 |
* <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#if HAVE_CONFIG_H |
| 21 |
#include "config.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "file.h" |
| 25 |
#include "util/macro.h" |
| 26 |
#include "util/logging.h" |
| 27 |
#include "util/strutl.h" |
| 28 |
|
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#if defined(HAVE_DIRENT_H) |
| 32 |
# include <dirent.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <io.h> |
| 36 |
#include <windows.h> |
| 37 |
|
| 38 |
|
| 39 |
typedef struct { |
| 40 |
intptr_t handle; |
| 41 |
struct _wfinddata_t info; |
| 42 |
} dir_data_t; |
| 43 |
|
| 44 |
static void _dir_close_win32(BD_DIR_H *dir) |
| 45 |
{ |
| 46 |
if (dir) { |
| 47 |
dir_data_t *priv = (dir_data_t*)dir->internal; |
| 48 |
|
| 49 |
_findclose(priv->handle); |
| 50 |
|
| 51 |
BD_DEBUG(DBG_DIR, "Closed WIN32 dir (%p)\n", (void*)dir); |
| 52 |
|
| 53 |
X_FREE(dir->internal); |
| 54 |
X_FREE(dir); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
static int _dir_read_win32(BD_DIR_H *dir, BD_DIRENT *entry) |
| 59 |
{ |
| 60 |
dir_data_t *priv = (dir_data_t*)dir->internal; |
| 61 |
|
| 62 |
if (!priv->info.name[0]) { |
| 63 |
return 1; |
| 64 |
} |
| 65 |
if (!WideCharToMultiByte(CP_UTF8, 0, priv->info.name, -1, entry->d_name, sizeof(entry->d_name), NULL, NULL)) { |
| 66 |
return -1; |
| 67 |
} |
| 68 |
|
| 69 |
priv->info.name[0] = 0; |
| 70 |
_wfindnext(priv->handle, &priv->info); |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
static dir_data_t *_open_impl(const char *dirname) |
| 76 |
{ |
| 77 |
dir_data_t *priv; |
| 78 |
char *filespec; |
| 79 |
wchar_t wfilespec[MAX_PATH]; |
| 80 |
int result; |
| 81 |
|
| 82 |
filespec = str_printf("%s" DIR_SEP "*", dirname); |
| 83 |
if (!filespec) { |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
|
| 87 |
result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filespec, -1, wfilespec, MAX_PATH); |
| 88 |
X_FREE(filespec); |
| 89 |
if (!result) { |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
priv = calloc(1, sizeof(dir_data_t)); |
| 94 |
if (!priv) { |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
priv->handle = _wfindfirst(wfilespec, &priv->info); |
| 99 |
if (priv->handle == -1) { |
| 100 |
X_FREE(priv); |
| 101 |
} |
| 102 |
|
| 103 |
return priv; |
| 104 |
} |
| 105 |
|
| 106 |
static BD_DIR_H *_dir_open_win32(const char* dirname) |
| 107 |
{ |
| 108 |
BD_DIR_H *dir = calloc(1, sizeof(BD_DIR_H)); |
| 109 |
dir_data_t *priv = NULL; |
| 110 |
|
| 111 |
if (!dir) { |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
priv = _open_impl(dirname); |
| 116 |
if (priv) { |
| 117 |
BD_DEBUG(DBG_DIR, "Opened WIN32 dir %s (%p)\n", dirname, (void*)dir); |
| 118 |
dir->close = _dir_close_win32; |
| 119 |
dir->read = _dir_read_win32; |
| 120 |
dir->internal = priv; |
| 121 |
|
| 122 |
return dir; |
| 123 |
} |
| 124 |
|
| 125 |
BD_DEBUG(DBG_DIR, "Error opening dir %s\n", dirname); |
| 126 |
X_FREE(dir); |
| 127 |
|
| 128 |
return NULL; |
| 129 |
} |
| 130 |
|
| 131 |
BD_DIR_OPEN dir_open_default(void) |
| 132 |
{ |
| 133 |
return _dir_open_win32; |
| 134 |
} |