(file_path.c) Declare variables at top
This commit is contained in:
parent
e0c2379f4e
commit
c3c0341f8c
39
file_path.c
39
file_path.c
|
@ -331,10 +331,10 @@ static int qstrcmp_dir(const void *a_, const void *b_)
|
||||||
{
|
{
|
||||||
const struct string_list_elem *a = (const struct string_list_elem*)a_;
|
const struct string_list_elem *a = (const struct string_list_elem*)a_;
|
||||||
const struct string_list_elem *b = (const struct string_list_elem*)b_;
|
const struct string_list_elem *b = (const struct string_list_elem*)b_;
|
||||||
|
|
||||||
// Sort directories before files.
|
|
||||||
int a_dir = a->attr.b;
|
int a_dir = a->attr.b;
|
||||||
int b_dir = b->attr.b;
|
int b_dir = b->attr.b;
|
||||||
|
|
||||||
|
// Sort directories before files.
|
||||||
if (a_dir != b_dir)
|
if (a_dir != b_dir)
|
||||||
return b_dir - a_dir;
|
return b_dir - a_dir;
|
||||||
else
|
else
|
||||||
|
@ -373,6 +373,8 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
union string_list_elem_attr attr;
|
||||||
|
char file_path[PATH_MAX];
|
||||||
const char *name = ffd.cFileName;
|
const char *name = ffd.cFileName;
|
||||||
const char *file_ext = path_get_extension(name);
|
const char *file_ext = path_get_extension(name);
|
||||||
bool is_dir = ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
bool is_dir = ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
||||||
|
@ -386,10 +388,8 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_
|
||||||
if (!is_dir && ext_list && !string_list_find_elem_prefix(ext_list, ".", file_ext))
|
if (!is_dir && ext_list && !string_list_find_elem_prefix(ext_list, ".", file_ext))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
char file_path[PATH_MAX];
|
|
||||||
fill_pathname_join(file_path, dir, name, sizeof(file_path));
|
fill_pathname_join(file_path, dir, name, sizeof(file_path));
|
||||||
|
|
||||||
union string_list_elem_attr attr;
|
|
||||||
attr.b = is_dir;
|
attr.b = is_dir;
|
||||||
|
|
||||||
if (!string_list_append(list, file_path, attr))
|
if (!string_list_append(list, file_path, attr))
|
||||||
|
@ -430,14 +430,14 @@ static bool dirent_is_directory(const char *path, const struct dirent *entry)
|
||||||
|
|
||||||
struct string_list *dir_list_new(const char *dir, const char *ext, bool include_dirs)
|
struct string_list *dir_list_new(const char *dir, const char *ext, bool include_dirs)
|
||||||
{
|
{
|
||||||
struct string_list *list = string_list_new();
|
DIR *directory = NULL;
|
||||||
|
const struct dirent *entry = NULL;
|
||||||
|
struct string_list *ext_list = NULL;
|
||||||
|
struct string_list *list = (struct string_list*)string_list_new();
|
||||||
|
|
||||||
if (!list)
|
if (!list)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
DIR *directory = NULL;
|
|
||||||
const struct dirent *entry = NULL;
|
|
||||||
|
|
||||||
struct string_list *ext_list = NULL;
|
|
||||||
if (ext)
|
if (ext)
|
||||||
ext_list = string_split(ext, "|");
|
ext_list = string_split(ext, "|");
|
||||||
|
|
||||||
|
@ -447,13 +447,15 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_
|
||||||
|
|
||||||
while ((entry = readdir(directory)))
|
while ((entry = readdir(directory)))
|
||||||
{
|
{
|
||||||
|
bool is_dir;
|
||||||
|
char file_path[PATH_MAX];
|
||||||
|
union string_list_elem_attr attr;
|
||||||
const char *name = entry->d_name;
|
const char *name = entry->d_name;
|
||||||
const char *file_ext = path_get_extension(name);
|
const char *file_ext = path_get_extension(name);
|
||||||
|
|
||||||
char file_path[PATH_MAX];
|
|
||||||
fill_pathname_join(file_path, dir, name, sizeof(file_path));
|
fill_pathname_join(file_path, dir, name, sizeof(file_path));
|
||||||
|
|
||||||
bool is_dir = dirent_is_directory(file_path, entry);
|
is_dir = dirent_is_directory(file_path, entry);
|
||||||
if (!include_dirs && is_dir)
|
if (!include_dirs && is_dir)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -463,7 +465,6 @@ struct string_list *dir_list_new(const char *dir, const char *ext, bool include_
|
||||||
if (!is_dir && ext_list && !string_list_find_elem_prefix(ext_list, ".", file_ext))
|
if (!is_dir && ext_list && !string_list_find_elem_prefix(ext_list, ".", file_ext))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
union string_list_elem_attr attr;
|
|
||||||
attr.b = is_dir;
|
attr.b = is_dir;
|
||||||
|
|
||||||
if (!string_list_append(list, file_path, attr))
|
if (!string_list_append(list, file_path, attr))
|
||||||
|
@ -528,21 +529,23 @@ bool path_is_directory(const char *path)
|
||||||
bool path_file_exists(const char *path)
|
bool path_file_exists(const char *path)
|
||||||
{
|
{
|
||||||
FILE *dummy = fopen(path, "rb");
|
FILE *dummy = fopen(path, "rb");
|
||||||
|
|
||||||
if (dummy)
|
if (dummy)
|
||||||
{
|
{
|
||||||
fclose(dummy);
|
fclose(dummy);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fill_pathname(char *out_path, const char *in_path, const char *replace, size_t size)
|
void fill_pathname(char *out_path, const char *in_path, const char *replace, size_t size)
|
||||||
{
|
{
|
||||||
char tmp_path[PATH_MAX];
|
char tmp_path[PATH_MAX];
|
||||||
|
char *tok;
|
||||||
|
|
||||||
rarch_assert(strlcpy(tmp_path, in_path, sizeof(tmp_path)) < sizeof(tmp_path));
|
rarch_assert(strlcpy(tmp_path, in_path, sizeof(tmp_path)) < sizeof(tmp_path));
|
||||||
char *tok = (char*)strrchr(path_basename(tmp_path), '.');
|
if ((tok = (char*)strrchr(path_basename(tmp_path), '.')))
|
||||||
if (tok)
|
|
||||||
*tok = '\0';
|
*tok = '\0';
|
||||||
|
|
||||||
rarch_assert(strlcpy(out_path, tmp_path, size) < size);
|
rarch_assert(strlcpy(out_path, tmp_path, size) < size);
|
||||||
|
@ -560,6 +563,7 @@ static char *find_last_slash(const char *str)
|
||||||
const char *slash = strrchr(str, '/');
|
const char *slash = strrchr(str, '/');
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
const char *backslash = strrchr(str, '\\');
|
const char *backslash = strrchr(str, '\\');
|
||||||
|
|
||||||
if (backslash && ((slash && backslash > slash) || !slash))
|
if (backslash && ((slash && backslash > slash) || !slash))
|
||||||
slash = backslash;
|
slash = backslash;
|
||||||
#endif
|
#endif
|
||||||
|
@ -692,12 +696,13 @@ void path_resolve_realpath(char *buf, size_t size)
|
||||||
|
|
||||||
static bool path_mkdir_norecurse(const char *dir)
|
static bool path_mkdir_norecurse(const char *dir)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
int ret = _mkdir(dir);
|
ret = _mkdir(dir);
|
||||||
#elif defined(IOS)
|
#elif defined(IOS)
|
||||||
int ret = mkdir(dir, 0755);
|
ret = mkdir(dir, 0755);
|
||||||
#else
|
#else
|
||||||
int ret = mkdir(dir, 0750);
|
ret = mkdir(dir, 0750);
|
||||||
#endif
|
#endif
|
||||||
if (ret < 0 && errno == EEXIST && path_is_directory(dir)) // Don't treat this as an error.
|
if (ret < 0 && errno == EEXIST && path_is_directory(dir)) // Don't treat this as an error.
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
Loading…
Reference in New Issue