server/src/callback.c

63 lines
1.2 KiB
C
Raw Normal View History

#include <config.h>
#include <platform.h>
2014-07-03 14:10:53 +02:00
#include "callback.h"
#include <stdlib.h>
#include <string.h>
static struct reg {
struct reg * next;
CALLBACK cb;
char *name;
2014-07-03 14:10:53 +02:00
} *registry;
CALLBACK create_callback(void (*cbv)(va_list va)) {
CALLBACK cb;
cb.cbv = cbv;
return cb;
}
void reset_callbacks(void) {
while(registry) {
struct reg *r = registry;
registry = r->next;
free(r->name);
free(r);
}
2014-07-03 14:10:53 +02:00
registry = 0;
}
CALLBACK register_callback(const char *name, void (*cbv)(va_list va))
{
struct reg * r = (struct reg *)malloc(sizeof(struct reg));
r->next = registry;
r->name = _strdup(name);
2014-07-03 14:10:53 +02:00
r->cb.cbv = cbv;
registry = r;
return r->cb;
}
int find_callback(const char *name, CALLBACK *result) {
if (result && name) {
struct reg *r;
for (r=registry;r;r=r->next) {
if (strcmp(r->name, name)==0) {
*result = r->cb;
return 0;
}
}
}
return -1;
}
int call_callback(CALLBACK cb, const char *name, ... ) {
va_list ap;
if (name) {
int err = find_callback(name, &cb);
if (err) return err;
}
va_start(ap, name);
cb.cbv(ap);
va_end(ap);
return 0;
}