Added `list` command w/ fnmatch support

This commit is contained in:
Casey 2024-04-30 12:58:01 +03:00
parent 4035b50128
commit 26fb7bc8a5
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 20 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include "common.h" #include "common.h"
#include "miniaudio.h" #include "miniaudio.h"
#include <errno.h> #include <errno.h>
#include <fnmatch.h>
#include <getopt.h> #include <getopt.h>
#include <limits.h> #include <limits.h>
#include <stdarg.h> #include <stdarg.h>
@ -81,6 +82,12 @@ struct global_counters {
} global_counters = { 0 }; } global_counters = { 0 };
#endif #endif
static int qsort_compare_index_of_key(const void *p1, const void *p2) {
struct sfx_pool_item *it1 = &sounds_pool.sounds[*(int *)p1];
struct sfx_pool_item *it2 = &sounds_pool.sounds[*(int *)p2];
return strcmp(it1->key, it2->key);
}
void usage(int argc, char **argv) { void usage(int argc, char **argv) {
// TODO // TODO
(void)argc; (void)argc;
@ -323,6 +330,19 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
send_txt(tgt, "%s\n", item.key); send_txt(tgt, "%s\n", item.key);
} }
} }
} else if (0 == strcmp(command, "list") || 0 == strcmp(command, "ls")) {
// TODO: find a better way to do this!
int *indices = calloc(sounds_pool.cap, sizeof(int));
for (int i = 0; i < sounds_pool.cap; i++) indices[i] = i;
qsort(indices, sounds_pool.cap, sizeof(int), qsort_compare_index_of_key);
for (int i = 0; i < sounds_pool.cap; i++) {
struct sfx_pool_item *item = &sounds_pool.sounds[indices[i]];
if (item->key[0] == '\0') continue;
if (0 == fnmatch(params, item->key, 0) || strlen(params) == 0) {
send_txt(tgt, "%s\n", item->key);
}
}
free(indices);
} else if (0 == strcmp(command, "source")) { } else if (0 == strcmp(command, "source")) {
if (strlen(params) == 0) { if (strlen(params) == 0) {
send_txt(tgt, "ERR: argument required: PATH\n"); send_txt(tgt, "ERR: argument required: PATH\n");