lib: implement tc_module_find_all_from_category

This commit is contained in:
jussi 2019-10-29 19:27:26 +02:00
parent 810b6097c9
commit 98bb929d7b

View File

@ -3,6 +3,7 @@
#include <string.h>
#include <tc_module.h>
#include <tc_filesystem.h>
// Local data structure that contains the handle of the module and the module data
typedef struct {
@ -53,10 +54,18 @@ static void close_lib_by_module(const tc_module_t* mod) {
static char *module_filename(const char *category, const char *mod_name) {
// TC_MODULE_PATH should be defined as an absolute path
// Posix
// Check if file by the exact name exists
char path[128];
snprintf(path, 128, "%s/%s/lib%s.so", TC_MODULE_PATH, category, mod_name);
return strdup(path);
snprintf(path, 128, "%s/%s/%s", TC_MODULE_PATH, category, mod_name);
if (tc_fs_file_exists(path)) {
return strdup(path);
}
// Form a filename of the form lib<mod_name>.so
// Posix
char affixed_path[128];
snprintf(affixed_path, 128, "%s/%s/lib%s.so", TC_MODULE_PATH, category, mod_name);
return strdup(affixed_path);
}
tc_module_t *tc_module_find(enum tc_module_category category, const char *name) {
@ -104,9 +113,41 @@ tc_module_t *tc_module_find(enum tc_module_category category, const char *name)
tc_module_t **tc_module_find_all_from_category(enum tc_module_category category, uint16_t *count) {
// Get the file name list of the category modules
uint16_t file_count = 0;
char mod_dir_name[512];
switch (category) {
case TC_CATEGORY_ASSIGNABLE:
snprintf(mod_dir_name, 512, "%s/%s", TC_MODULE_PATH, "assignable");
break;
default:
return NULL;
}
return NULL;
char **file_names = tc_fs_dir_filenames(mod_dir_name, &file_count);
if (file_names == NULL) {
return NULL;
}
// Open all modules using the file names
tc_module_t *mod_list[TC_MAX_LOADED_MODULES];
uint16_t mod_count = 0;
tc_module_t *mod;
for (uint16_t i = 0; i < file_count; i++) {
if ((mod = tc_module_find(category, file_names[i])) == NULL) {
continue;
}
mod_list[mod_count] = mod;
mod_count++;
}
// Allocate pointer array on the heap
tc_module_t **retval = calloc(mod_count, sizeof(tc_module_t*));
for (uint16_t i = 0; i < mod_count; i++) {
retval[i] = mod_list[i];
}
*count = mod_count;
return retval;
}
void tc_module_close(tc_module_t* module) {