Browse Source

Moved hook initialization up to the top, conditionalize hook execution
and initialize hooks on add

Samuel W. Flint 7 years ago
parent
commit
bc5cf40565
1 changed files with 14 additions and 11 deletions
  1. 14 11
      hooks.h

+ 14 - 11
hooks.h

@@ -16,15 +16,25 @@ struct hookFunction {
 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) {
-  LISTEL * head = hook->start;
-  while (head != NULL) {
-    ((HOOKFUNC*)(head->data))->function();
-    head = head->next;
+  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)
@@ -46,10 +56,3 @@ void addToHook(HOOK * hook, void (*function)()) {
       insertAfter((void*) hookFunction, addAfter);
     }
 }
-
-HOOK * initializeHook(int direction)
-{
-  HOOK * theReturn = malloc(sizeof(HOOK));
-  theReturn->insertDirection = direction;
-  return theReturn;
-}