FTables.c

Go to the documentation of this file.
00001 /**************************************
00002  * Copyright Jean-Philippe Chancelier 
00003  * ENPC 
00004  **************************************/
00005 
00006 #include <string.h>
00007 #include "FTables.h"
00008 #define FTable_H   /* to prevent  a type conflict with GetFuncPtr */ 
00009 #include "stack-c.h"
00010 #include "stack2.h"
00011 #include "sciprint.h"
00012 #include "link.h"
00013 
00014 static char buf[MAXNAME];
00015 
00016 static int SearchComp  __PARAMS((FTAB *Ftab, char *op, void (**realop) ( )));
00017 static void Emptyfunc  __PARAMS((void)) {}
00018 
00019 voidf GetFuncPtr(char *name, int n, FTAB *Table, voidf scifun, int *ifunc, int *lhs, int *rhs);
00020 voidf SetFunction  __PARAMS((char *name, int *rep, FTAB *table));  
00021 
00022 /*******************************************
00023  * General functions 
00024  *******************************************/
00025 
00026 voidf SetFunction(char *name, int *rep, FTAB *table)
00027 {
00028   void (*loc)();
00029   char *s;
00030 
00031   strncpy(buf,name,MAXNAME);
00032   s=buf;
00033   while ( *s != ' ' && *s != '\0') { s++;};
00034   *s= '\0';
00035   if ( SearchComp(table,buf,&loc) == OK) 
00036     {
00037       *rep = 0;
00038       return(loc);
00039     }
00040   if ( SearchInDynLinks(buf,&loc) >= 0 )
00041     {
00042       *rep = 0;
00043       return(loc);
00044     }
00045   loc = Emptyfunc;
00046   *rep = 1;
00047   /* sciprint(" Function %s not found\r\n",name); */
00048   return(loc);
00049 }
00050 
00051 
00052 
00053 /*******************************************
00054  * Attention trier la table 
00055  * cherche un operateur dans une table : 
00056  * a ameliorer en utilisant bsearch 
00057  *******************************************/
00058 
00059 static int SearchComp(FTAB *Ftab, char *op, void (**realop) (/* ??? */))
00060 {
00061   int i=0;
00062   while ( Ftab[i].name != (char *) 0) 
00063      {
00064        int j;
00065        j = strcmp(op,Ftab[i].name);
00066        if ( j == 0 )
00067          {
00068            *realop = Ftab[i].f;
00069            return(OK);
00070          }
00071        else
00072          { 
00073            if ( j <= 0)
00074              {
00075                /* sciprint("\nUnknow function <%s>\r\n",op); */
00076                return(FAIL);
00077              }
00078            else i++;
00079          }
00080      }
00081   /* sciprint("\n Unknow function <%s>\r\n",op); */
00082   return(FAIL);
00083 }
00084 
00085 
00086 /*********************************************************
00087  * General function to get an external 
00088  * when writting an interface 
00089  * In 
00090  *   name : name of calling function for error message 
00091  *   n    : position of external in argument list 
00092  *   Table: table of stored function 
00093  *   scifun: a function 
00094  *   lhs,rhs : expected lhs,rhs values of the external 
00095  * 
00096  * if external argument is a 'string' : an associated function 
00097  * is searched in Table and in dynamically linked functions 
00098  * if found the function is returned 
00099  *
00100  * if external argument is a macro (lhs,rhs) are checked 
00101  * and scifun is returned 
00102  * 
00103  *********************************************************/
00104 
00105 #define a_chain 10
00106 #define a_function 13
00107 
00108 voidf GetFuncPtr(char *name, int n, FTAB *Table, voidf scifun, int *ifunc, int *lhs, int *rhs)
00109 {
00110   int type,rep,mm,nn,nlhs,nrhs;
00111   voidf f;
00112   type=C2F(vartype)(&n);
00113   switch ( type)
00114     {
00115     case a_chain :
00116       GetRhsVar(n, "c", &mm, &nn, ifunc);
00117       f = SetFunction(cstk(*ifunc),&rep,Table);
00118       if ( rep == 1 )
00119         {
00120           Scierror(999,"%s: external %s not found \r\n",name,cstk(*ifunc));
00121           return (voidf) 0;
00122         }  
00123       return f ;
00124     case  a_function :
00125       GetRhsVar(n, "f", &nlhs,&nrhs, ifunc);
00126       /*
00127       if ( nlhs != *lhs || nrhs != *rhs ) 
00128         {
00129           Scierror(999,"%s: given external has wrong (lhs,rhs) values (%d,%d), (%d,%d) required\r\n",name,
00130                    nlhs,nrhs,*lhs,*rhs);
00131           return (voidf) 0 ;
00132         }
00133       */
00134       return (voidf) scifun ;
00135     default:
00136       sciprint("Wrong parameter in %s ! (number %d)\r\n",name,n);
00137       Error(999);
00138       return (voidf) 0 ;
00139     }
00140 }

Generated on Sun Mar 4 15:03:46 2007 for Scilab [trunk] by  doxygen 1.5.1