Clean up some file handling stuff ...

This commit is contained in:
Themaister 2011-08-22 18:13:30 +02:00
parent 96ce288120
commit c55263b956
3 changed files with 24 additions and 24 deletions

View File

@ -22,7 +22,7 @@ HAVE_PYTHON = 1
HAVE_FFMPEG = 0 HAVE_FFMPEG = 0
libsnes ?= -lsnes libsnes ?= -lsnes
LIBS = -lm LIBS = -lm -lshlwapi
DEFINES = -I. -DHAVE_CONFIGFILE DEFINES = -I. -DHAVE_CONFIGFILE
LDFLAGS = -L. -static-libgcc -s LDFLAGS = -L. -static-libgcc -s
LDCXXFLAGS = -static-libstdc++ LDCXXFLAGS = -static-libstdc++

View File

@ -21,7 +21,7 @@ HAVE_PYTHON = 0
HAVE_FFMPEG = 1 HAVE_FFMPEG = 1
libsnes ?= -lsnes libsnes ?= -lsnes
LIBS = -lm LIBS = -lm -lshlwapi
DEFINES = -I. -DHAVE_CONFIGFILE DEFINES = -I. -DHAVE_CONFIGFILE
LDFLAGS = -L. -static-libgcc -s LDFLAGS = -L. -static-libgcc -s
LDCXXFLAGS = -static-libstdc++ LDCXXFLAGS = -static-libstdc++

44
file.c
View File

@ -36,6 +36,7 @@
#include <io.h> #include <io.h>
#include <fcntl.h> #include <fcntl.h>
#include <windows.h> #include <windows.h>
#include <shlwapi.h>
#else #else
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -703,8 +704,6 @@ bool init_rom_file(enum ssnes_game_type type)
// Yep, this is C alright ;) // Yep, this is C alright ;)
char** dir_list_new(const char *dir, const char *ext) char** dir_list_new(const char *dir, const char *ext)
{ {
size_t path_len = strlen(dir);
size_t cur_ptr = 0; size_t cur_ptr = 0;
size_t cur_size = 32; size_t cur_size = 32;
char **dir_list = NULL; char **dir_list = NULL;
@ -713,18 +712,18 @@ char** dir_list_new(const char *dir, const char *ext)
WIN32_FIND_DATAW ffd; WIN32_FIND_DATAW ffd;
HANDLE hFind = INVALID_HANDLE_VALUE; HANDLE hFind = INVALID_HANDLE_VALUE;
wchar_t wchar_buf[MAXPATHLEN];
char utf8_buf[MAXPATHLEN];
size_t final_off = MAX_PATH + path_len + 2; if (strlcpy(utf8_buf, dir, sizeof(utf8_buf)) >= sizeof(utf8_buf))
goto error;
if (strlcat(utf8_buf, "/*", sizeof(utf8_buf)) >= sizeof(utf8_buf))
goto error;
if (strlcat(utf8_buf, ext, sizeof(utf8_buf)) >= sizeof(utf8_buf))
goto error;
wchar_t wchar_buf[MAX_PATH + 1]; if (MultiByteToWideChar(CP_UTF8, 0, utf8_buf, -1, wchar_buf, MAXPATHLEN) == 0)
char utf8_buf[MAX_PATH + 3]; goto error;
strlcpy(utf8_buf, dir, sizeof(utf8_buf));
strlcat(utf8_buf, "/*", sizeof(utf8_buf));
utf8_buf[MAX_PATH + 2] = '\0';
int ret = MultiByteToWideChar(CP_UTF8, 0, utf8_buf, strlen(utf8_buf), wchar_buf, MAX_PATH);
wchar_buf[ret] = 0;
hFind = FindFirstFileW(wchar_buf, &ffd); hFind = FindFirstFileW(wchar_buf, &ffd);
if (hFind == INVALID_HANDLE_VALUE) if (hFind == INVALID_HANDLE_VALUE)
@ -732,7 +731,6 @@ char** dir_list_new(const char *dir, const char *ext)
#else #else
DIR *directory = NULL; DIR *directory = NULL;
const struct dirent *entry = NULL; const struct dirent *entry = NULL;
size_t final_off = sizeof(entry->d_name) + path_len + 2;
directory = opendir(dir); directory = opendir(dir);
if (!directory) if (!directory)
@ -753,8 +751,8 @@ char** dir_list_new(const char *dir, const char *ext)
#ifdef _WIN32 #ifdef _WIN32
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue; continue;
int ret = WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, wcslen(ffd.cFileName), utf8_buf, MAX_PATH, NULL, NULL); if (WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, -1, utf8_buf, MAX_PATH, NULL, NULL) == 0)
utf8_buf[ret] = '\0'; continue;
if (ext && !strstr(utf8_buf, ext)) if (ext && !strstr(utf8_buf, ext))
continue; continue;
#else #else
@ -762,16 +760,16 @@ char** dir_list_new(const char *dir, const char *ext)
continue; continue;
#endif #endif
dir_list[cur_ptr] = malloc(final_off); dir_list[cur_ptr] = malloc(MAXPATHLEN);
if (!dir_list[cur_ptr]) if (!dir_list[cur_ptr])
goto error; goto error;
strlcpy(dir_list[cur_ptr], dir, final_off); strlcpy(dir_list[cur_ptr], dir, MAXPATHLEN);
strlcat(dir_list[cur_ptr], "/", final_off); strlcat(dir_list[cur_ptr], "/", MAXPATHLEN);
#ifdef _WIN32 #ifdef _WIN32
strlcat(dir_list[cur_ptr], utf8_buf, final_off); strlcat(dir_list[cur_ptr], utf8_buf, MAXPATHLEN);
#else #else
strlcat(dir_list[cur_ptr], entry->d_name, final_off); strlcat(dir_list[cur_ptr], entry->d_name, MAXPATHLEN);
#endif #endif
cur_ptr++; cur_ptr++;
@ -788,7 +786,6 @@ char** dir_list_new(const char *dir, const char *ext)
} }
#ifdef _WIN32 #ifdef _WIN32
while (FindNextFileW(hFind, &ffd) != 0); while (FindNextFileW(hFind, &ffd) != 0);
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
@ -825,7 +822,10 @@ void dir_list_free(char **dir_list)
bool path_is_directory(const char *path) bool path_is_directory(const char *path)
{ {
#ifdef _WIN32 #ifdef _WIN32
return false; wchar_t buf[MAXPATHLEN];
if (MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, MAXPATHLEN) == 0)
return false;
return PathIsDirectoryW(buf) == FILE_ATTRIBUTE_DIRECTORY;
#else #else
struct stat buf; struct stat buf;
if (stat(path, &buf) < 0) if (stat(path, &buf) < 0)