refactor(module): centralize path parsing and add OS-specific separator
- Define STK_PATH_SEP macro to handle Windows and Unix path separators. - Refactor extract_module_id to use STK_PATH_SEP. - Simplify stk_module_load by delegating module ID extraction to extract_module_id.
This commit is contained in:
+11
-27
@@ -5,6 +5,12 @@
|
|||||||
|
|
||||||
#define STK_MOD_FUNC_NAME_BUFFER 64
|
#define STK_MOD_FUNC_NAME_BUFFER 64
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define STK_PATH_SEP '\\'
|
||||||
|
#else
|
||||||
|
#define STK_PATH_SEP '/'
|
||||||
|
#endif
|
||||||
|
|
||||||
void *platform_load_library(const char *path);
|
void *platform_load_library(const char *path);
|
||||||
void platform_unload_library(void *handle);
|
void platform_unload_library(void *handle);
|
||||||
void *platform_get_symbol(void *handle, const char *symbol);
|
void *platform_get_symbol(void *handle, const char *symbol);
|
||||||
@@ -28,18 +34,10 @@ size_t stk_module_count(void) { return module_count; }
|
|||||||
|
|
||||||
void extract_module_id(const char *path, char *out_id)
|
void extract_module_id(const char *path, char *out_id)
|
||||||
{
|
{
|
||||||
const char *basename;
|
|
||||||
char *dot;
|
char *dot;
|
||||||
|
const char *basename = strrchr(path, STK_PATH_SEP);
|
||||||
|
|
||||||
basename = strrchr(path, '/');
|
basename = (basename) ? basename + 1 : path;
|
||||||
#ifdef _WIN32
|
|
||||||
if (!basename)
|
|
||||||
basename = strrchr(path, '\\');
|
|
||||||
#endif
|
|
||||||
if (!basename)
|
|
||||||
basename = path;
|
|
||||||
else
|
|
||||||
basename++;
|
|
||||||
|
|
||||||
strncpy(out_id, basename, STK_MOD_ID_BUFFER - 1);
|
strncpy(out_id, basename, STK_MOD_ID_BUFFER - 1);
|
||||||
out_id[STK_MOD_ID_BUFFER - 1] = '\0';
|
out_id[STK_MOD_ID_BUFFER - 1] = '\0';
|
||||||
@@ -81,10 +79,7 @@ int is_mod_loaded(const char *module_name)
|
|||||||
int stk_module_load(const char *path, int index)
|
int stk_module_load(const char *path, int index)
|
||||||
{
|
{
|
||||||
void *handle;
|
void *handle;
|
||||||
stk_module_func init_func;
|
stk_module_func init_func, shutdown_func;
|
||||||
stk_module_func shutdown_func;
|
|
||||||
const char *basename;
|
|
||||||
char *dot;
|
|
||||||
char module_id[STK_MOD_ID_BUFFER];
|
char module_id[STK_MOD_ID_BUFFER];
|
||||||
union {
|
union {
|
||||||
void *obj;
|
void *obj;
|
||||||
@@ -109,18 +104,7 @@ int stk_module_load(const char *path, int index)
|
|||||||
if (index == -1)
|
if (index == -1)
|
||||||
index = module_count;
|
index = module_count;
|
||||||
|
|
||||||
basename = strrchr(path, '/');
|
extract_module_id(path, module_id);
|
||||||
if (!basename)
|
|
||||||
basename = path;
|
|
||||||
else
|
|
||||||
basename++;
|
|
||||||
|
|
||||||
strncpy(module_id, basename, STK_MOD_ID_BUFFER - 1);
|
|
||||||
module_id[STK_MOD_ID_BUFFER - 1] = '\0';
|
|
||||||
|
|
||||||
dot = strrchr(module_id, '.');
|
|
||||||
if (dot)
|
|
||||||
*dot = '\0';
|
|
||||||
|
|
||||||
strncpy(stk_module_ids[index], module_id, STK_MOD_ID_BUFFER - 1);
|
strncpy(stk_module_ids[index], module_id, STK_MOD_ID_BUFFER - 1);
|
||||||
stk_module_ids[index][STK_MOD_ID_BUFFER - 1] = '\0';
|
stk_module_ids[index][STK_MOD_ID_BUFFER - 1] = '\0';
|
||||||
@@ -129,7 +113,7 @@ int stk_module_load(const char *path, int index)
|
|||||||
stk_inits[index] = init_func;
|
stk_inits[index] = init_func;
|
||||||
stk_shutdowns[index] = shutdown_func;
|
stk_shutdowns[index] = shutdown_func;
|
||||||
|
|
||||||
init_func(); /* TODO eventually, this should have some sort of check */
|
init_func(); /* TODO eventually return an int for success */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,7 +142,6 @@ size_t stk_poll(void)
|
|||||||
for (i = 0; i < file_count; ++i) {
|
for (i = 0; i < file_count; ++i) {
|
||||||
extract_module_id(file_list[i], mod_id);
|
extract_module_id(file_list[i], mod_id);
|
||||||
switch (events[i]) {
|
switch (events[i]) {
|
||||||
|
|
||||||
case STK_MOD_LOAD:
|
case STK_MOD_LOAD:
|
||||||
loaded_mod_indices[load_index++] = i;
|
loaded_mod_indices[load_index++] = i;
|
||||||
break;
|
break;
|
||||||
@@ -240,6 +239,7 @@ trim_arrays:
|
|||||||
++write_pos;
|
++write_pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module_count = write_pos;
|
module_count = write_pos;
|
||||||
stk_module_realloc_memory(module_count);
|
stk_module_realloc_memory(module_count);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user