hook.c

Go to the documentation of this file.
00001 /*                          H O O K . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup bu_log */
00023 /*@{*/
00024 /** @file hook.c
00025  *
00026  * @brief
00027  * BRL-CAD support library's hook utility.
00028  *
00029  *  @author
00030  *  @n  Robert G. Parker
00031  *  @n  Michael John Muuss
00032  *  @n  Glenn Durfee
00033  *
00034  *  @par Source
00035  *  @n  The U. S. Army Research Laboratory
00036  *  @n  Aberdeen Proving Ground, Maryland  21005-5068  USA
00037  *
00038  *
00039  * @par Acknowledgements
00040  *      This builds on the work in libbu/log.c.
00041  *
00042  */
00043 /*@}*/
00044 
00045 
00046 #include "common.h"
00047 
00048 #include <stdlib.h>
00049 #include <stdio.h>
00050 
00051 #include "machine.h"
00052 #include "bu.h"
00053 
00054 
00055 void
00056 bu_hook_list_init(struct bu_hook_list *hlp)
00057 {
00058         BU_LIST_INIT(&hlp->l);
00059         hlp->hookfunc = BUHOOK_NULL;
00060         hlp->clientdata = GENPTR_NULL;
00061 }
00062 
00063 void
00064 bu_add_hook(struct bu_hook_list *hlp, bu_hook_t func, genptr_t clientdata)
00065 {
00066         struct bu_hook_list *new_hook;
00067 
00068         BU_GETSTRUCT(new_hook, bu_hook_list);
00069         new_hook->hookfunc = func;
00070         new_hook->clientdata = clientdata;
00071         new_hook->l.magic = BUHOOK_LIST_MAGIC;
00072         BU_LIST_APPEND(&hlp->l, &new_hook->l);
00073 }
00074 
00075 void
00076 bu_delete_hook(struct bu_hook_list *hlp, bu_hook_t func, genptr_t clientdata)
00077 {
00078         struct bu_hook_list *cur = hlp;
00079 
00080         for (BU_LIST_FOR(cur, bu_hook_list, &hlp->l)) {
00081                 if (cur->hookfunc == func && cur->clientdata == clientdata) {
00082                         struct bu_hook_list *old = BU_LIST_PLAST(bu_hook_list, cur);
00083                         BU_LIST_DEQUEUE(&(cur->l));
00084                         bu_free((genptr_t)cur, "bu_delete_hook");
00085                         cur = old;
00086                 }
00087         }
00088 }
00089 
00090 void
00091 bu_call_hook(struct bu_hook_list *hlp, genptr_t buf)
00092 {
00093         struct bu_hook_list     *call_hook;
00094 
00095         for (BU_LIST_FOR(call_hook, bu_hook_list, &hlp->l)) {
00096                 if( !(call_hook->hookfunc) ) {
00097                     exit(EXIT_FAILURE); /* don't call through 0! */
00098                 }
00099                 call_hook->hookfunc(call_hook->clientdata, buf);
00100         }
00101 }
00102 
00103 /*
00104  * Local Variables:
00105  * mode: C
00106  * tab-width: 8
00107  * c-basic-offset: 4
00108  * indent-tabs-mode: t
00109  * End:
00110  * ex: shiftwidth=4 tabstop=8
00111  */

Generated on Mon Sep 18 01:24:48 2006 for BRL-CAD by  doxygen 1.4.6