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

#define MAXNUM 500

/* keep function handles in C space */
struct FuncList{
        int num; /* id of the function handle */
        funcHandle handle;
} funcList[MAXNUM];

static int index_c = 0;   /* index of funList */
int ptfun1_2_setHandle(int num, funcHandle funptr)  {
  int i;

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

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

  /* Call Ch function from C space */ 
  printf("Calling Ch function from C space:");
  funptr();

  return num;
}

funcHandle ptfun1_2_getHandle(int num)  {
  int i;

  for(i=0; i<index_c; i++)
    if(funcList[i].num == num)
      return funcList[i].handle;
  return NULL;
}
