hook.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Hooks
  2. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  3. 2004, 2005, 2007, 2009, 2010 Free Software Foundation, Inc.
  4. Written 1994, 1995, 1996 by:
  5. Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
  6. Jakub Jelinek, Mauricio Plaza.
  7. The file_date routine is mostly from GNU's fileutils package,
  8. written by Richard Stallman and David MacKenzie.
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  20. /** \file
  21. * \brief Source: hooks
  22. */
  23. #include <config.h>
  24. #include "lib/global.h"
  25. #include "lib/hook.h"
  26. /*** global variables ****************************************************************************/
  27. /*** file scope macro definitions ****************************************************************/
  28. /*** file scope type declarations ****************************************************************/
  29. /*** file scope variables ************************************************************************/
  30. /*** file scope functions ************************************************************************/
  31. /* --------------------------------------------------------------------------------------------- */
  32. /*** public functions ****************************************************************************/
  33. /* --------------------------------------------------------------------------------------------- */
  34. void
  35. add_hook (hook_t ** hook_list, void (*hook_fn) (void *), void *data)
  36. {
  37. hook_t *new_hook = g_new (hook_t, 1);
  38. new_hook->hook_fn = hook_fn;
  39. new_hook->next = *hook_list;
  40. new_hook->hook_data = data;
  41. *hook_list = new_hook;
  42. }
  43. /* --------------------------------------------------------------------------------------------- */
  44. void
  45. execute_hooks (hook_t * hook_list)
  46. {
  47. hook_t *new_hook = NULL;
  48. hook_t *p;
  49. /* We copy the hook list first so tahat we let the hook
  50. * function call delete_hook
  51. */
  52. while (hook_list != NULL)
  53. {
  54. add_hook (&new_hook, hook_list->hook_fn, hook_list->hook_data);
  55. hook_list = hook_list->next;
  56. }
  57. p = new_hook;
  58. while (new_hook != NULL)
  59. {
  60. new_hook->hook_fn (new_hook->hook_data);
  61. new_hook = new_hook->next;
  62. }
  63. for (hook_list = p; hook_list != NULL;)
  64. {
  65. p = hook_list;
  66. hook_list = hook_list->next;
  67. g_free (p);
  68. }
  69. }
  70. /* --------------------------------------------------------------------------------------------- */
  71. void
  72. delete_hook (hook_t ** hook_list, void (*hook_fn) (void *))
  73. {
  74. hook_t *new_list = NULL;
  75. hook_t *current, *next;
  76. for (current = *hook_list; current != NULL; current = next)
  77. {
  78. next = current->next;
  79. if (current->hook_fn == hook_fn)
  80. g_free (current);
  81. else
  82. add_hook (&new_list, current->hook_fn, current->hook_data);
  83. }
  84. *hook_list = new_list;
  85. }
  86. /* --------------------------------------------------------------------------------------------- */
  87. gboolean
  88. hook_present (hook_t * hook_list, void (*hook_fn) (void *))
  89. {
  90. hook_t *p;
  91. for (p = hook_list; p != NULL; p = p->next)
  92. if (p->hook_fn == hook_fn)
  93. return TRUE;
  94. return FALSE;
  95. }
  96. /* --------------------------------------------------------------------------------------------- */