[ADD/WIP] Create and use basic command parser

Still need to write the router.
This commit is contained in:
LDA 2024-06-28 22:34:28 +02:00
commit c4b7d1b92a
6 changed files with 220 additions and 12 deletions

49
src/Command/Router.c Normal file
View file

@ -0,0 +1,49 @@
#include <Command.h>
#include <Cytoplasm/Memory.h>
struct CommandRouter {
HashMap *routes;
};
CommandRouter *
CommandCreateRouter(void)
{
CommandRouter *router = Malloc(sizeof(*router));
router->routes = HashMapCreate();
return router;
}
void
CommandAddCommand(CommandRouter *rter, char *c, CommandRoute rte)
{
if (!rter || !c || !rte)
{
return;
}
HashMapSet(rter->routes, c, rte);
}
void
RouteCommand(CommandRouter *rter, Command *cmd, void *d)
{
CommandRoute route;
if (!rter || !cmd)
{
return;
}
route = HashMapGet(rter->routes, cmd->command);
if (route)
{
route(cmd, d);
}
}
void
CommandFreeRouter(CommandRouter *rter)
{
if (!rter)
{
return;
}
HashMapFree(rter->routes);
Free(rter);
}