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

#define MAXNUM 500

struct FuncList{
        int num; /* id of the function handle */
        funcHandle handle;
};

/* to keep function handles in c space */
struct FuncList funcList[MAXNUM];  
static int index_c = 0;   /* index of funList */

int ptfun2_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: return value is %d\n", 
          funptr());

  return num;
}

funcHandle ptfun2_getHandle(int num)  {
  int i;

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