From a9e1701e0c8caf7bc43d2ac706f585dbfe9dfd1c Mon Sep 17 00:00:00 2001 From: Ivo Timmermans Date: Tue, 16 Apr 2002 17:00:02 +0000 Subject: [PATCH] Hooks --- lib/hooks.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/hooks.h | 34 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 lib/hooks.c create mode 100644 lib/hooks.h diff --git a/lib/hooks.c b/lib/hooks.c new file mode 100644 index 00000000..121df2d3 --- /dev/null +++ b/lib/hooks.c @@ -0,0 +1,97 @@ +/* + hooks.c -- hooks management + Copyright (C) 2002 Guus Sliepen , + 2002 Ivo Timmermans + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + $Id: hooks.c,v 1.1.2.1 2002/04/16 17:00:02 zarq Exp $ +*/ + +#include "config.h" + +#include +#include + +#include +#include + +avl_tree_t *hooks_tree = NULL; + +static int hook_type_compare(const void *a, const void *b) +{ + return strcmp((const char*)a, (const char*)b); +} + +static int hook_dummy_compare(const void *a, const void *b) +{ + if(a < b) + return -1; + if(a > b) + return 1; + return 0; +} + +void run_hooks(const char *type, ...) +{ + avl_node_t *avlnode; + va_list args; + avl_tree_t *t; + + if(!hooks_tree) + return; + t = avl_search(hooks_tree, type); + if(!t) + return; + + va_start(args, fmt); + for(avlnode = t->head; avlnode; avlnode = avlnode->next) + { + assert(avlnode->data); + ((hook_function_t*)(avlnode->data))(type, args); + } + va_end(args); +} + +void add_hook(const char *type, hook_function_t *hook) +{ + avl_tree_t *t; + + if(!hooks_tree) + hooks_tree = avl_alloc_tree(hook_type_compare, NULL); + + t = avl_search(hooks_tree, type); + if(!t) + { + t = avl_alloc_tree(hook_dummy_compare, NULL); + avl_insert(log_hooks_tree, (void*)t); + } + + avl_insert(t, (void*)hook); +} + +void del_hook(const char *type, hook_function_t *hook) +{ + avl_tree_t *t; + + if(!hooks_tree) + return; + + t = avl_search(hooks_tree, type); + if(!t) + return; + + avl_delete(t, (void*)hook); +} diff --git a/lib/hooks.h b/lib/hooks.h new file mode 100644 index 00000000..d190a738 --- /dev/null +++ b/lib/hooks.h @@ -0,0 +1,34 @@ +/* + hooks.h -- header file for hooks.c + Copyright (C) 2002 Guus Sliepen , + 2002 Ivo Timmermans + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + $Id: hooks.h,v 1.1.2.1 2002/04/16 16:59:48 zarq Exp $ +*/ + +#ifndef __TINC_HOOKS_H__ +#define __TINC_HOOKS_H__ + +#include + +typedef void (hook_function_t)(const char*,va_list); + +void run_hooks(const char *type, ...); +void add_hook(const char *type, hook_function_t *hook); +void del_hook(const char *type, hook_function_t *hook); + +#endif /* __TINC_HOOKS_H__ */ -- 2.20.1