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

typedef int (*funcHandle)(float, int); /* function pointer type */

/* C functions to replace the Ch one */
static ChInterp_t interp;
static int setFunction4_chdl_funarg(float, int);

/* function pointers to save Ch function pointers */
static void *setFunction4_chdl_funptr;

EXPORTCH void setFunction4_chdl(void *varg){
  ChVaList_t ap;
  int arg1;
  funcHandle handle_ch, handle_c = NULL;

  Ch_VaStart(interp, ap, varg);
  arg1 = Ch_VaArg(interp, ap, int);
  handle_ch = Ch_VaArg(interp, ap, funcHandle); /* get ch function pointer */
  setFunction4_chdl_funptr = (void *)handle_ch;

  /* replace the Ch function pointer with the C one,
     the NULL pointer can't be replaced */
  if(handle_ch != NULL) {
    handle_c = (funcHandle)setFunction4_chdl_funarg;
  }

  setFunction4(arg1, handle_c);
  Ch_VaEnd(interp, ap);
}

static int setFunction4_chdl_funarg(float f, int i) {
  int retval;
  Ch_CallFuncByAddr(interp, setFunction4_chdl_funptr, &retval, f, i);
  return retval;
}
