From 539f35bdae578037908fe8a01f8af14383bfaa40 Mon Sep 17 00:00:00 2001 From: Christian C Date: Tue, 4 Mar 2025 17:04:02 -0800 Subject: Directory file listings --- src/main.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 246fae0..f9cc292 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include @@ -17,6 +19,37 @@ #define FALSE 0 #define TRUE 1 +//----------------------------------------------- +// List directory +char** lsdir(char* dirname) { + DIR *d; + struct dirent *dir; + d = opendir(dirname); + char** file_names = (char**)malloc(sizeof(char*)); + if (!file_names) { + return NULL; + } + file_names[0] = NULL; + size_t file_count = 0; + if (d) { + while ((dir = readdir(d)) != NULL) { + if (dir->d_type == DT_REG) { + // When a regular file is reached + /// Create space for it in the list + file_names = realloc(file_names, (file_count+1+1)*sizeof(char*)); + /// Create space for the name + file_names[file_count] = calloc(strlen(dir->d_name)+1, sizeof(char)); + /// Copy the name + strcpy(file_names[file_count], dir->d_name); + /// Mark the end of the file list with a NULL entry + file_names[++file_count] = NULL; + } + } + return file_names; + } + return NULL; +} + //----------------------------------------------- // Difference in Time // Compute the difference between timespec structs @@ -95,6 +128,24 @@ bool_t flood(uint8_t* image, uint16_t* mask, size_t width, size_t height, size_t int main() { + //----------------------------------------------- + //-LIST-FILES-IN-DIRECTORY----------------------- + //----------------------------------------------- + char** file_list = lsdir("data/"); + if (file_list) { + size_t index = 0; + while (1) { + char* fname = file_list[index]; + if (fname == NULL) { + break; + } + printf("%s\n", fname); + free(file_list[index++]); + } + free(file_list); + } + //----------------------------------------------- + //----------------------------------------------- //-RAYLIB-INIT //----------------------------------------------- -- cgit v1.2.1