dir2list

generate a list of files shuffled by directory
git clone anongit@rnpnr.xyz:dir2list.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 80dd69a19731fe1abd3fac5a40f74f76f006a4b0
Parent: d383915da566fe7bc5a9353144c1a4cbb0327b7a
Author: 0x766F6964
Date:   Sun,  3 May 2020 13:20:32 -0600

implement list generation

this includes the simple randomization "algorithm"

Diffstat:
Mmpvdir2list.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 85 insertions(+), 7 deletions(-)

diff --git a/mpvdir2list.c b/mpvdir2list.c @@ -4,6 +4,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> struct node { char path[PATH_MAX]; /* Path containing media files */ @@ -11,9 +12,13 @@ struct node { struct node *next; /* next node */ }; -const char *topdir = "/"; +struct list { + char *elem; + struct list *next; +}; -static struct node top; +static struct node *node_head; +static struct list *list_head; static void die(const char *fmt, ...) @@ -55,6 +60,41 @@ valid_dir(const char *dir) return 0; } +static int +valid_file(const char *file) +{ + const char *s, **types; + types = filetypes; + for (; *types; types++) { + s = *types; + if (strstr(file, s)) + return 1; + } + return 0; +} + +static struct list * +addfile(struct node *node, struct list *list, const char *file) +{ + char *s; + size_t len; + + len = strlen(node->path) + strlen(file) + 2; + s = xmalloc(len); + s[0] = 0; + + strcat(s, node->path); + strcat(s, "/"); + strcat(s, file); + + list->elem = s; + list->next = xmalloc(sizeof(struct list)); + list->next->elem = NULL; + list->next->next = NULL; + + return list->next; +} + /* Fill out the next field for all nodes */ static struct node * subdir(struct node *node) @@ -82,6 +122,7 @@ subdir(struct node *node) node->next = xmalloc(sizeof(struct node)); xstrcat(node->next->path, s, PATH_MAX); + node->next->next = NULL; /* recurse into subdir */ node = subdir(node->next); @@ -95,17 +136,54 @@ subdir(struct node *node) } static void +mklist(struct node *node, struct list *list) +{ + DIR *dir; + struct dirent *ent; + + if (!node) + return; + + /* allocated or "dumb" randomization */ + if (node->a || (rand() % 100) < 77) { + mklist(node->next, list); + return; + } + + if (!(dir = opendir(node->path))) + die("opendir(): failed to open: %s\n", node->path); + + while ((ent = readdir(dir))) + if (valid_file(ent->d_name)) + list = addfile(node, list, ent->d_name); + + node->a = 1; + mklist(node->next, list); +} + +static void recurse(struct node *node) { - subdir(node); + subdir(node); + mklist(node, list_head); } int main(void) { - xstrcat(top.path, topdir, PATH_MAX); - top.a = 0; - top.next = NULL; - recurse(&top); + node_head = xmalloc(sizeof(struct node)); + list_head = xmalloc(sizeof(struct list)); + + xstrcat(node_head->path, topdir, PATH_MAX); + node_head->a = 0; + node_head->next = NULL; + + list_head->elem = NULL; + list_head->next = NULL; + + srand(time(NULL)); + + recurse(node_head); + return 0; }