/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
#include<stdio.h>
#include "ptfun6_c.h"

#define MAXNUM 500
struct FuncList{
        /* id of the function handle, treated as the event id */
        int num;
        /* function handle, regarded as a pointer to the callback function*/  
        funcHandle handle;
        /* assume usrMsg is a pointer used by callback function */
        void* usrMsg;
};

/* internal data to keep function handles */
struct FuncList functList[MAXNUM];  
static int index_c = 0;   /* index of funList */

int setHandle(int index, funcHandle funptr, void * usrMsg)  {
  int i;

  if(index_c == MAXNUM) {
    printf("Error: too many functions");
    exit(1);
    }

  /* make sure that one id is mapped to only one handle */
  for(i=0; i<index_c; i++) 
    if(functList[i].num == index) {
      functList[i].handle = funptr;
      functList[i].usrMsg = usrMsg;
      return index;
    }
  functList[index_c].num = index;
  functList[index_c].handle = funptr;
  functList[index_c++].usrMsg = usrMsg;

  return index;
}

void callFun(int index)  {
  int i;

  for(i=0; i<index_c; i++) {
    if(functList[i].num == index)
       functList[i].handle(functList[i].usrMsg);
  }
  return;
}
