#include <stdlib.h>
#include <ucx/map.h>
#include "../public/nsapi.h"
#include "../util/pblock.h"
#include "func.h"
UcxMap *function_map;
void func_init() {
function_map = ucx_map_new(
1337);
}
void add_function(FuncStruct *func) {
struct FuncStruct *f = malloc(
sizeof(FuncStruct));
*f = *func;
char *name = strdup(func->name);
for(
int i=
0;name[i]!=
'\0';i++) {
if(name[i] ==
'_') {
name[i] =
'-';
}
}
f->name = name;
log_ereport(
LOG_VERBOSE,
"add_function %s", f->name);
ucx_map_cstr_put(function_map, (
char*)f->name, f);
}
void add_functions(FuncStruct *funcs) {
int i =
0;
while(funcs[i].func !=
NULL) {
add_function(&funcs[i]);
i++;
}
}
FuncStruct* get_function(
char *name) {
for(
int i=
0;name[i]!=
'\0';i++) {
if(name[i] ==
'_') {
name[i] =
'-';
}
}
return ucx_map_cstr_get(function_map, name);
}
FuncStruct* func_resolve(pblock *pb, Session *sn, Request *rq) {
char *name = pblock_findkeyval(pb_key_fn, pb);
if(name ==
NULL) {
return NULL;
}
FuncStruct *func = get_function(name);
if(func ==
NULL) {
}
return func;
}
int func_exec(pblock *pb, Session *sn, Request *rq) {
FuncStruct *func = func_resolve(pb, sn, rq);
if(func ==
NULL) {
return REQ_ABORTED;
}
return func->func(pb, sn, rq);
}
struct FuncStruct func_insert(
char *name, FuncPtr fn) {
struct FuncStruct fc;
ZERO(&fc,
sizeof(FuncStruct));
fc.func = fn;
fc.name = strdup(name);
add_function(&fc);
return *get_function(name);
}