Add multifolder management in dir_list_new v2

v2: process the folder from the last to the
first to allow override.

Signed-off-by: XoD <xoddark@gmail.com>
This commit is contained in:
XoD 2015-02-01 16:40:55 +01:00
parent 26d90e91f3
commit d97bc6f084
1 changed files with 66 additions and 55 deletions

View File

@ -192,20 +192,20 @@ static int parse_dir_entry(const char *name, char *file_path,
/** /**
* dir_list_new: * dir_list_new:
* @dir : directory path. * @dir : directories path.
* @ext : allowed extensions of file directory entries to include. * @ext : allowed extensions of file directory entries to include.
* @include_dirs : include directories as part of the finished directory listing? * @include_dirs : include directories as part of the finished directory listing?
* *
* Create a directory listing. * Create a directory listing.
* *
* Returns: pointer to a directory listing of type 'struct string_list *' on success, * Returns: pointer to a file listing of type 'struct string_list *' on success,
* NULL in case of error. Has to be freed manually. * NULL in case of error. Has to be freed manually.
**/ **/
struct string_list *dir_list_new(const char *dir, struct string_list *dir_list_new(const char *dir,
const char *ext, bool include_dirs) const char *ext, bool include_dirs)
{ {
char path_buf[PATH_MAX_LENGTH]; char path_buf[PATH_MAX_LENGTH];
struct string_list *ext_list, *list; struct string_list *ext_list, *list, *dir_list;
#ifdef _WIN32 #ifdef _WIN32
WIN32_FIND_DATA ffd; WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE; HANDLE hFind = INVALID_HANDLE_VALUE;
@ -215,6 +215,7 @@ struct string_list *dir_list_new(const char *dir,
#endif #endif
ext_list = NULL; ext_list = NULL;
dir_list = NULL;
(void)path_buf; (void)path_buf;
if (!(list = string_list_new())) if (!(list = string_list_new()))
@ -223,6 +224,9 @@ struct string_list *dir_list_new(const char *dir,
if (ext) if (ext)
ext_list = string_split(ext, "|"); ext_list = string_split(ext, "|");
if (dir)
dir_list = string_split(dir, ";");
#ifdef _WIN32 #ifdef _WIN32
snprintf(path_buf, sizeof(path_buf), "%s\\*", dir); snprintf(path_buf, sizeof(path_buf), "%s\\*", dir);
@ -230,6 +234,11 @@ struct string_list *dir_list_new(const char *dir,
if (hFind == INVALID_HANDLE_VALUE) if (hFind == INVALID_HANDLE_VALUE)
goto error; goto error;
// Process the directory list from the end to the start to have an override
// of file from the first directory by the file from the last directory.
for (int i = dir_list->size -1; i >= 0; i--)
{
const char * dir_name = dir_list->elems[i].data;
do do
{ {
int ret = 0; int ret = 0;
@ -238,29 +247,31 @@ struct string_list *dir_list_new(const char *dir,
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;
fill_pathname_join(file_path, dir, name, sizeof(file_path)); fill_pathname_join(file_path, dir_name, name, sizeof(file_path));
ret = parse_dir_entry(name, file_path, is_dir, ret = parse_dir_entry(name, file_path, is_dir,
include_dirs, list, ext_list, file_ext); include_dirs, list, ext_list, file_ext);
if (ret == -1) if (ret == -1)
goto error; break; //invalid folder continue with next
if (ret == 1) if (ret == 1)
continue; continue;
}while (FindNextFile(hFind, &ffd) != 0); }while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind); FindClose(hFind);
string_list_free(ext_list); }
return list;
error:
if (hFind != INVALID_HANDLE_VALUE)
FindClose(hFind);
#else #else
directory = opendir(dir); // Process the directory list from the end to the start to have an override
// of file from the first directory by the file from the last directory.
for (int i = dir_list->size-1; i >= 0; i--)
{
const char * dir_name = dir_list->elems[i].data;
directory = opendir(dir_name);
if (!directory) if (!directory)
goto error; continue;
while ((entry = readdir(directory))) while ((entry = readdir(directory)))
{ {
@ -270,7 +281,7 @@ error:
const char *file_ext = path_get_extension(name); const char *file_ext = path_get_extension(name);
bool is_dir = false; bool is_dir = false;
fill_pathname_join(file_path, dir, name, sizeof(file_path)); fill_pathname_join(file_path, dir_name, name, sizeof(file_path));
is_dir = dirent_is_directory(file_path, entry); is_dir = dirent_is_directory(file_path, entry);
@ -278,24 +289,24 @@ error:
include_dirs, list, ext_list, file_ext); include_dirs, list, ext_list, file_ext);
if (ret == -1) if (ret == -1)
goto error; break; //invalid folder continue with next
if (ret == 1) if (ret == 1)
continue; continue;
} }
closedir(directory); closedir(directory);
}
string_list_free(ext_list);
return list;
error:
if (directory)
closedir(directory);
#endif #endif
string_list_free(list); string_list_free(dir_list);
string_list_free(ext_list); string_list_free(ext_list);
if (list->size == 0)
{
string_list_free(list);
return NULL; return NULL;
}
return list;
} }