Pārlūkot izejas kodu

Added in the Hooks portion of the library

Samuel W. Flint 7 gadi atpakaļ
vecāks
revīzija
bbb379524a
1 mainītis faili ar 57 papildinājumiem un 0 dzēšanām
  1. 57 0
      hooks.h

+ 57 - 0
hooks.h

@@ -0,0 +1,57 @@
+#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;
+
+void runHook(HOOK * hook) {
+  LISTEL * head = hook->start;
+  while (head != NULL) {
+    /* void *function = head->data; */
+    /* ((void*)function)(); */
+    ((HOOKFUNC*)(head->data))->function();
+    head = head->next;
+  }
+}
+
+void addToHook(HOOK * hook, void (*function)()) {
+  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);
+    }
+}
+
+HOOK * initializeHook(int direction)
+{
+  HOOK * theReturn = malloc(sizeof(HOOK));
+  theReturn->insertDirection = direction;
+  return theReturn;
+}