#include <stdlib.h>
#include <cx/map.h>
#include <cx/hash_map.h>
#include "../public/nsapi.h"
#include "../util/pblock.h"
#include "func.h"
CxMap *function_map;
void func_init() {
function_map = cxHashMapCreate(cxDefaultAllocator,
CX_STORE_POINTERS,
256);
}
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);
cxMapPut(function_map, cx_hash_key_str(f->name), f);
}
void add_functions(FuncStruct *funcs) {
int i =
0;
while(funcs[i].func !=
NULL) {
add_function(&funcs[i]);
i++;
}
}
FuncStruct* get_function(
const char *name) {
char *name_dup = strdup(name);
for(
int i=
0;name_dup[i]!=
'\0';i++) {
if(name_dup[i] ==
'_') {
name_dup[i] =
'-';
}
}
void *ret = cxMapGet(function_map, cx_hash_key_str(name_dup));
free(name_dup);
return ret;
}
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);
}