diff --git a/src/sfxd.c b/src/sfxd.c index a65d1d2..085fb09 100644 --- a/src/sfxd.c +++ b/src/sfxd.c @@ -1,6 +1,7 @@ #include "common.h" #include "miniaudio.h" #include +#include #include #include #include @@ -81,6 +82,12 @@ struct global_counters { } global_counters = { 0 }; #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) { // TODO (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); } } + } 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")) { if (strlen(params) == 0) { send_txt(tgt, "ERR: argument required: PATH\n");