home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06048b < prev    next >
Text File  |  1991-04-16  |  4KB  |  123 lines

  1.  
  2. /* -------------------------------------------------------------------- */
  3. /* Listing 2 - parse source and call the user function */
  4.  
  5.  /* user function table */
  6.  /* this static data structure is compiled into the report writer */
  7.    struct func_list ufunc_tab[] = {
  8.         {"myfunc", myfunc},
  9.         {0, 0} };
  10.  
  11. /* define array to hold parameters to be passed to the user function */
  12. #define MAX_PARAMS 10
  13.  struct param uparams[MAX_PARAMS];
  14.  /* define structure to hold return value from user function */
  15.  struct param return_param;
  16.  
  17. main()
  18. {
  19.   int ier = 0;
  20.   struct param *retp;            /* return value from user function */
  21.  
  22. /* set values for example */
  23. /* as if parsing report source line:
  24.   Call myfunc("hello world")
  25. */
  26.  char *ufunc_name = "myfunc";
  27.  int num_params = 1;
  28.  uparams[0].datatype = STRING;
  29.  uparams[0].value.sparam = "hello world";
  30.  
  31.  /* call user function */
  32.  retp = call_ufunc(ufunc_name, uparams, num_params);
  33.  
  34.  /* test return */
  35.  if (retp) {
  36.    fputs("(main)", stdout);
  37.    switch (retp->datatype) {
  38.      case CHAR:
  39.        printf(" return(%d)=%c\n", retp->datatype, retp->value.cparam);
  40.        break;
  41.      case STRING:
  42.        printf(" return(%d)=%s\n", retp->datatype, retp->value.sparam);
  43.        break;
  44.      case INTEGER:
  45.        printf(" return(%d)=%d\n", retp->datatype, retp->value.iparam);
  46.        break;
  47.      /* etc., for each data type */
  48.      default:
  49.        printf(" unknown return type=%d\n", retp->datatype);
  50.        ier = 1;
  51.        break;
  52.      } /* end switch */
  53.    }
  54.  else {
  55.    fputs("(main) NULL return\n", stdout);
  56.    ier = 2;           /* NULL return from call_ufunc */
  57.    }
  58.  
  59.  return (ier);
  60. } /* end main */
  61.  
  62.  
  63. /* This function sets up for and calls the user function */
  64. struct param * call_ufunc(
  65.     char *ufuncname,        /* name of user function */
  66.     struct param uparams[], /* array of parameters for user function */
  67.     int num_params)         /* number of parameters */
  68. {
  69.   struct param *(*ufunc)();           /* pointer to user function */
  70.   struct param *retp;                 /* return value from user function */
  71.  
  72.   /* find user function in table */
  73.   if ((ufunc = lookup(ufuncname, ufunc_tab)) == NULL) {
  74.     fprintf(stderr, " ERROR - function not in table: \"%s\"\n", ufuncname);
  75.     return (NULL);
  76.     }
  77.  
  78.   /* call user function via function pointer (dummy call) */
  79.   switch (num_params) {
  80.     case 0:
  81.       retp = (*ufunc)();
  82.       break;
  83.     case 1:
  84.       retp = (*ufunc)(&uparams[0]);
  85.       break;
  86.     /* etc., 1 case for each number of parameters */
  87.     }; /* end switch */
  88. /*
  89.   alternative call to user function, see Note 3 
  90.   retp = ufunc(uparams[0], uparams[1], uparams[2], ...);
  91. */
  92.  
  93.   return (retp);      /* return pointer to parameter struct */
  94. }
  95.  
  96. /* function to search the function table */
  97. /* returns pointer to function */
  98. struct param *(*lookup(
  99.     char *ufunc_name,              /* name of function */
  100.     struct func_list ufunc_tab[]   /* function table */
  101.     ))()
  102. {
  103.   /* search function table for name of the CALLed function */
  104.   for (;ufunc_tab->funcname;++ufunc_tab) {
  105.     if (! strcmp(ufunc_name, ufunc_tab->funcname))
  106.       return (ufunc_tab->func);
  107.     }
  108.  
  109.   return (NULL); /* user function not found in table */
  110. } /* end lookup */
  111.  
  112. /* user functions */
  113. struct param *myfunc(struct param *sparm)
  114. {
  115.   printf("function %s\n %s\n", "myfunc", sparm->value.sparam);
  116.  
  117.   /* set up return value */
  118.   return_param.datatype = INTEGER;
  119.   return_param.value.iparam = 0;
  120.   return (&return_param);
  121. } /* end myfunc */
  122.  
  123.