hooks.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #define HOOKS
  2. #ifndef LINKED_LIST
  3. #include "linkedList.h"
  4. #endif
  5. struct hook {
  6. LISTEL *start;
  7. int insertDirection;
  8. };
  9. struct hookFunction {
  10. void (*function)();
  11. };
  12. typedef struct hook HOOK;
  13. typedef struct hookFunction HOOKFUNC;
  14. void runHook(HOOK * hook) {
  15. LISTEL * head = hook->start;
  16. while (head != NULL) {
  17. /* void *function = head->data; */
  18. /* ((void*)function)(); */
  19. ((HOOKFUNC*)(head->data))->function();
  20. head = head->next;
  21. }
  22. }
  23. void addToHook(HOOK * hook, void (*function)()) {
  24. HOOKFUNC *hookFunction = malloc(sizeof(HOOKFUNC));
  25. hookFunction->function = function;
  26. if (hook->start == NULL)
  27. {
  28. LISTEL *element = malloc(sizeof(LISTEL));
  29. element->data = (void*) hookFunction;
  30. hook->start = element;
  31. }
  32. else if (hook->insertDirection == 0)
  33. {
  34. LISTEL *newHead = insertBefore((void*) hookFunction, hook->start);
  35. hook->start = newHead;
  36. }
  37. else
  38. {
  39. LISTEL *addAfter = hook->start;
  40. while (addAfter->next != NULL)
  41. addAfter = addAfter->next;
  42. insertAfter((void*) hookFunction, addAfter);
  43. }
  44. }
  45. HOOK * initializeHook(int direction)
  46. {
  47. HOOK * theReturn = malloc(sizeof(HOOK));
  48. theReturn->insertDirection = direction;
  49. return theReturn;
  50. }