12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #define HOOKS
- #ifndef LINKED_LIST
- #include "linkedList.h"
- #endif
- struct hook {
- LISTEL *start;
- int insertDirection;
- };
- struct hookFunction {
- void (*function)();
- };
- typedef struct hook HOOK;
- typedef struct hookFunction HOOKFUNC;
- HOOK * initializeHook(int direction)
- {
- HOOK * theReturn = malloc(sizeof(HOOK));
- theReturn->insertDirection = direction;
- return theReturn;
- }
- void runHook(HOOK * hook) {
- if (hook != NULL) {
- LISTEL * head = hook->start;
- while (head != NULL) {
- ((HOOKFUNC*)(head->data))->function();
- head = head->next;
- }
- }
- }
- void addToHook(HOOK * hook, void (*function)()) {
- if (hook == NULL) hook = initializeHook(1);
- HOOKFUNC *hookFunction = malloc(sizeof(HOOKFUNC));
- hookFunction->function = function;
- if (hook->start == NULL)
- {
- LISTEL *element = malloc(sizeof(LISTEL));
- element->data = (void*) hookFunction;
- hook->start = element;
- }
- else if (hook->insertDirection == 0)
- {
- LISTEL *newHead = insertBefore((void*) hookFunction, hook->start);
- hook->start = newHead;
- }
- else
- {
- LISTEL *addAfter = hook->start;
- while (addAfter->next != NULL)
- addAfter = addAfter->next;
- insertAfter((void*) hookFunction, addAfter);
- }
- }
|