hooks.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. ((HOOKFUNC*)(head->data))->function();
  18. head = head->next;
  19. }
  20. }
  21. void addToHook(HOOK * hook, void (*function)()) {
  22. HOOKFUNC *hookFunction = malloc(sizeof(HOOKFUNC));
  23. hookFunction->function = function;
  24. if (hook->start == NULL)
  25. {
  26. LISTEL *element = malloc(sizeof(LISTEL));
  27. element->data = (void*) hookFunction;
  28. hook->start = element;
  29. }
  30. else if (hook->insertDirection == 0)
  31. {
  32. LISTEL *newHead = insertBefore((void*) hookFunction, hook->start);
  33. hook->start = newHead;
  34. }
  35. else
  36. {
  37. LISTEL *addAfter = hook->start;
  38. while (addAfter->next != NULL)
  39. addAfter = addAfter->next;
  40. insertAfter((void*) hookFunction, addAfter);
  41. }
  42. }
  43. HOOK * initializeHook(int direction)
  44. {
  45. HOOK * theReturn = malloc(sizeof(HOOK));
  46. theReturn->insertDirection = direction;
  47. return theReturn;
  48. }