mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-14 00:25:11 +00:00
[ADD/WIP] Create and use basic command parser
Still need to write the router.
This commit is contained in:
parent
f434f7aa87
commit
c4b7d1b92a
6 changed files with 220 additions and 12 deletions
49
src/Command/Router.c
Normal file
49
src/Command/Router.c
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue