handler.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stddef.h>
  2. #include <grass/gis.h>
  3. struct handler {
  4. void (*func)(void *);
  5. void *closure;
  6. };
  7. static struct handler *handlers;
  8. static int num_handlers;
  9. static int max_handlers;
  10. static struct handler *alloc_handler(void)
  11. {
  12. int i;
  13. for (i = 0; i < num_handlers; i++) {
  14. struct handler *h = &handlers[i];
  15. if (!h->func)
  16. return h;
  17. }
  18. if (num_handlers >= max_handlers) {
  19. max_handlers += 10;
  20. handlers = G_realloc(handlers, max_handlers * sizeof(struct handler));
  21. }
  22. return &handlers[num_handlers++];
  23. }
  24. void G_add_error_handler(void (*func)(void *), void *closure)
  25. {
  26. struct handler *h = alloc_handler();
  27. h->func = func;
  28. h->closure = closure;
  29. }
  30. void G_remove_error_handler(void (*func)(void *), void *closure)
  31. {
  32. int i;
  33. for (i = 0; i < num_handlers; i++) {
  34. struct handler *h = &handlers[i];
  35. if (h->func == func && h->closure == closure) {
  36. h->func = NULL;
  37. h->closure = NULL;
  38. }
  39. }
  40. }
  41. void G__call_error_handlers(void)
  42. {
  43. int i;
  44. for (i = 0; i < num_handlers; i++) {
  45. struct handler *h = &handlers[i];
  46. if (h->func)
  47. (*h->func)(h->closure);
  48. }
  49. }