hooks.h 1.2 KB

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