80 lines
1.6 KiB
C
80 lines
1.6 KiB
C
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <hexagram/sim.h>
|
||
|
|
||
|
struct _hexagram_sim {
|
||
|
hexagram_dict *can_ifs;
|
||
|
hexagram_dict *modules;
|
||
|
};
|
||
|
|
||
|
hexagram_sim *hexagram_sim_new() {
|
||
|
hexagram_sim *sim;
|
||
|
|
||
|
if ((sim = malloc(sizeof(*sim))) == NULL) {
|
||
|
goto error_malloc;
|
||
|
}
|
||
|
|
||
|
if ((sim->can_ifs = hexagram_dict_new()) == NULL) {
|
||
|
goto error_dict_new_can_ifs;
|
||
|
}
|
||
|
|
||
|
if ((sim->modules = hexagram_dict_new()) == NULL) {
|
||
|
goto error_dict_new_modules;
|
||
|
}
|
||
|
|
||
|
return sim;
|
||
|
|
||
|
error_dict_new_modules:
|
||
|
hexagram_dict_destroy(sim->can_ifs);
|
||
|
|
||
|
error_dict_new_can_ifs:
|
||
|
free(sim);
|
||
|
|
||
|
error_malloc:
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
void hexagram_sim_destroy(hexagram_sim *sim) {
|
||
|
hexagram_dict_destroy(sim->modules);
|
||
|
hexagram_dict_destroy(sim->can_ifs);
|
||
|
|
||
|
free(sim);
|
||
|
}
|
||
|
|
||
|
int hexagram_sim_add_can_if(hexagram_sim *sim,
|
||
|
const char *name,
|
||
|
hexagram_can_if *can_if) {
|
||
|
if (hexagram_dict_set(sim->can_ifs,
|
||
|
(void *)name, strlen(name), can_if) == NULL) {
|
||
|
goto error_dict_set;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
|
||
|
error_dict_set:
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int hexagram_sim_add_module(hexagram_sim *sim,
|
||
|
hexagram_module *module) {
|
||
|
const char *name;
|
||
|
|
||
|
if ((name = hexagram_module_name(module)) == NULL) {
|
||
|
goto error_module_name;
|
||
|
}
|
||
|
|
||
|
if (hexagram_dict_set(sim->modules,
|
||
|
(void *)name, strlen(name), module) == NULL) {
|
||
|
goto error_dict_set;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
|
||
|
error_dict_set:
|
||
|
error_module_name:
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int hexagram_sim_run(hexagram_sim *sim);
|