From e1e38edd5e84d126da2d54d96e5a07e0e3e88d24 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 17 Nov 2019 19:25:40 -0600 Subject: [PATCH] Feature: enable custom filter and sort in fl_directory_list() --- level_1/fl_directory/c/directory.c | 4 ++-- level_1/fl_directory/c/directory.h | 13 ++++++++++++- level_3/firewall/c/firewall.c | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/level_1/fl_directory/c/directory.c b/level_1/fl_directory/c/directory.c index f6d46d8..13a9fe7 100644 --- a/level_1/fl_directory/c/directory.c +++ b/level_1/fl_directory/c/directory.c @@ -5,7 +5,7 @@ extern "C" { #endif #ifndef _di_fl_directory_list_ - f_return_status fl_directory_list(const f_string directory_path, f_string_dynamics *names) { + f_return_status fl_directory_list(const f_string directory_path, int (*filter)(const struct dirent *), int (*sort)(const struct dirent **, const struct dirent **), f_string_dynamics *names) { #ifndef _di_level_1_parameter_checking_ if (names == 0) return f_status_set_error(f_invalid_parameter); #endif // _di_level_1_parameter_checking_ @@ -16,7 +16,7 @@ extern "C" { f_string_length size = 0; f_status status = f_none; - length = scandir(directory_path, &listing, 0, alphasort); + length = scandir(directory_path, &listing, filter, sort); for (; i < length; i++) { size = strnlen(listing[i]->d_name, fl_directory_name_max); diff --git a/level_1/fl_directory/c/directory.h b/level_1/fl_directory/c/directory.h index dcc0deb..bb73e73 100644 --- a/level_1/fl_directory/c/directory.h +++ b/level_1/fl_directory/c/directory.h @@ -37,8 +37,17 @@ extern "C" { /** * Print the names of each file and/or directory inside the given directory. * + * Allows specifying a custom filter and custom sort. + * * @param directory_path * Filesystem path to the directory. + * @param filter + * A filter function of the form: int xxx(const struct direct *). + * Set to 0 to not use (NULL). + * @param sort + * A sort function of the form: int xxx(const struct direct *, const struct direct *). + * Set to 0 to not use (NULL). + * There are two pre-made libc functions available for this: alphasort() and versionsort(). * @param names * Will be populated with the names of each file and/or directory inside the names parameter. * @@ -48,9 +57,11 @@ extern "C" { * f_failure (with error bit) if failed to read directory information. * f_invalid_parameter (with error bit) if a parameter is invalid. * f_reallocation_error (with error bit) on memory reallocation error. + * + * @see scandir() */ #ifndef _di_fl_directory_list_ - extern f_return_status fl_directory_list(const f_string directory_path, f_string_dynamics *names); + extern f_return_status fl_directory_list(const f_string directory_path, int (*filter)(const struct dirent *), int (*sort)(const struct dirent **, const struct dirent **), f_string_dynamics *names); #endif // _di_fl_directory_list_ #ifdef __cplusplus diff --git a/level_3/firewall/c/firewall.c b/level_3/firewall/c/firewall.c index 6eda8b7..309cfdd 100644 --- a/level_3/firewall/c/firewall.c +++ b/level_3/firewall/c/firewall.c @@ -316,7 +316,7 @@ extern "C" { } // load all network devices - status = fl_directory_list((f_string) network_devices, &data->devices); + status = fl_directory_list((f_string) network_devices, 0, alphasort, &data->devices); if (f_status_is_error(status)) { status = f_status_set_fine(status); -- 1.8.3.1