Mercurial > repos > blastem
diff util.c @ 866:69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 06 Nov 2015 12:19:39 -0800 |
parents | 3eced113081c |
children | 54ffba3768d6 |
line wrap: on
line diff
--- a/util.c Fri Nov 06 11:44:51 2015 -0800 +++ b/util.c Fri Nov 06 12:19:39 2015 -0800 @@ -11,6 +11,7 @@ #include "blastem.h" //for headless global #include "render.h" //for render_errorbox +#include "util.h" char * alloc_concat(char * first, char * second) { @@ -277,5 +278,46 @@ } return exe_dir; } +#include <dirent.h> + +dir_entry *get_dir_list(char *path, size_t *numret) +{ + DIR *d = opendir(path); + if (!d) { + if (numret) { + *numret = 0; + } + return NULL; + } + size_t storage = 64; + dir_entry *ret = malloc(sizeof(dir_entry) * storage); + size_t pos = 0; + struct dirent* entry; + while (entry = readdir(d)) + { + if (entry->d_type != DT_REG && entry->d_type != DT_LNK && entry->d_type != DT_DIR) { + continue; + } + if (pos == storage) { + storage = storage * 2; + ret = realloc(ret, sizeof(dir_entry) * storage); + } + ret[pos].name = strdup(entry->d_name); + ret[pos++].is_dir = entry->d_type == DT_DIR; + } + if (numret) { + *numret = pos; + } + return ret; +} + +void free_dir_list(dir_entry *list, size_t numentries) +{ + for (size_t i = 0; i < numentries; i++) + { + free(list[i].name); + } + free(list); +} #endif