Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

func.c

00001 /* Function management */
00002 
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <stdlib.h>
00006 
00007 #include "common.h"
00008 #include "fatal.h"
00009 
00010 #include "func_types.h"
00011 #include "func.h"
00012 
00013 #include "splaytree_types.h"
00014 #include "splaytree.h"
00015 #include "tree_types.h"
00016 
00017 #include "builtin_funcs.h"
00018 
00019 /* A splay tree of builtin functions */
00020 splaytree_t * builtin_func_tree;
00021 
00022 /* Private function prototypes */
00023 int compare_func(char * name, char * name2);
00024 int insert_func(func_t * name);
00025 void * copy_func_key(char * string);
00026 
00027 
00028 void * copy_func_key(char * string) {
00029         
00030         char * clone_string;
00031         
00032         if ((clone_string = malloc(MAX_TOKEN_SIZE)) == NULL)
00033                 return NULL;
00034         
00035         strncpy(clone_string, string, MAX_TOKEN_SIZE-1);
00036         
00037         return (void*)clone_string;
00038 }       
00039 
00040 
00041 func_t * create_func (char * name, double (*func_ptr)(), int num_args) {
00042 
00043   func_t * func;
00044   func = (func_t*)malloc(sizeof(func_t));
00045  
00046   if (func == NULL)
00047     return NULL;
00048 
00049   
00050   /* Clear name space */
00051   memset(func->name, 0, MAX_TOKEN_SIZE);
00052 
00053   /* Copy given name into function structure */
00054   strncpy(func->name, name, MAX_TOKEN_SIZE); 
00055 
00056   /* Assign value pointer */
00057   func->func_ptr = func_ptr;
00058   func->num_args = num_args;
00059   /* Return instantiated function */
00060   return func;
00061 
00062 }
00063 
00064 /* Initialize the builtin function database.
00065    Should only be necessary once */
00066 int init_builtin_func_db() {
00067   int retval;
00068 
00069   builtin_func_tree = create_splaytree(compare_string, copy_string, free_string);
00070 
00071   if (builtin_func_tree == NULL)
00072     return OUTOFMEM_ERROR;
00073 
00074   retval = load_all_builtin_func();
00075   return SUCCESS;
00076 }
00077 
00078 
00079 /* Destroy the builtin function database.
00080    Generally, do this on projectm exit */
00081 int destroy_builtin_func_db() {
00082 
00083   splay_traverse(free_func, builtin_func_tree);
00084   destroy_splaytree(builtin_func_tree);
00085   return SUCCESS;
00086 
00087 }
00088 
00089 /* Insert a function into the database */
00090 int insert_func(func_t * func) {
00091 
00092   if (func == NULL)
00093     return ERROR;
00094 
00095   splay_insert(func, func->name, builtin_func_tree);
00096 
00097   return SUCCESS;
00098 }
00099 
00100 /* Frees a function type, real complicated... */
00101 void free_func(func_t * func) {
00102   free(func);
00103 }
00104 
00105 /* Remove a function from the database */
00106 int remove_func(func_t * func) {
00107 
00108   if (func == NULL)
00109     return ERROR;
00110 
00111     splay_delete(func->name, builtin_func_tree);
00112 
00113   return SUCCESS;
00114 }
00115 
00116 /* Find a function given its name */
00117 func_t * find_func(char * name) {
00118 
00119   func_t * func = NULL;
00120 
00121   /* First look in the builtin database */
00122   func = (func_t *)splay_find(name, builtin_func_tree);
00123         
00124   return func;
00125 
00126 }
00127 
00128 /* Compare string name with function name */
00129 int compare_func(char * name, char * name2) {
00130 
00131   int cmpval;
00132 
00133   /* Uses string comparison function */
00134   cmpval = strncmp(name, name2, MAX_TOKEN_SIZE-1);
00135   
00136   return cmpval;
00137 }
00138 
00139 /* Loads a builtin function */
00140 int load_builtin_func(char * name,  double (*func_ptr)(), int num_args) {
00141 
00142   func_t * func; 
00143   int retval; 
00144 
00145   /* Create new function */
00146   func = create_func(name, func_ptr, num_args);
00147 
00148   if (func == NULL)
00149     return OUTOFMEM_ERROR;
00150 
00151   retval = insert_func(func);
00152 
00153   return retval;
00154 
00155 }
00156 
00157 /* Loads all builtin functions */
00158 int load_all_builtin_func() {
00159 
00160   
00161   if (load_builtin_func("int", int_wrapper, 1) < 0)
00162     return ERROR;
00163   if (load_builtin_func("abs", abs_wrapper, 1) < 0)
00164     return ERROR;
00165   if (load_builtin_func("sin", sin_wrapper, 1) < 0)
00166     return ERROR;
00167   if (load_builtin_func("cos", cos_wrapper, 1) < 0)
00168     return ERROR;
00169   if (load_builtin_func("tan", tan_wrapper, 1) < 0)
00170     return ERROR;
00171   if (load_builtin_func("asin", asin_wrapper, 1) < 0)
00172     return ERROR;
00173   if (load_builtin_func("acos", acos_wrapper, 1) < 0)
00174     return ERROR;
00175   if (load_builtin_func("atan", atan_wrapper, 1) < 0)
00176     return ERROR;
00177   if (load_builtin_func("sqr", sqr_wrapper, 1) < 0)
00178     return ERROR;
00179   if (load_builtin_func("sqrt", sqrt_wrapper, 1) < 0)
00180     return ERROR;
00181   if (load_builtin_func("pow", pow_wrapper, 2) < 0)
00182     return ERROR;
00183   if (load_builtin_func("exp", exp_wrapper, 1) < 0)
00184     return ERROR;
00185   if (load_builtin_func("log", log_wrapper, 1) < 0)
00186     return ERROR;
00187   if (load_builtin_func("log10", log10_wrapper, 1) < 0)
00188     return ERROR;
00189   if (load_builtin_func("sign", sign_wrapper, 1) < 0)
00190     return ERROR;
00191   if (load_builtin_func("min", min_wrapper, 2) < 0)
00192     return ERROR;
00193   if (load_builtin_func("max", max_wrapper, 2) < 0)
00194     return ERROR;
00195   if (load_builtin_func("sigmoid", sigmoid_wrapper, 2) < 0)
00196     return ERROR;
00197   if (load_builtin_func("atan2", atan2_wrapper, 2) < 0)
00198     return ERROR;
00199   if (load_builtin_func("rand", rand_wrapper, 1) < 0)
00200     return ERROR;
00201   if (load_builtin_func("band", band_wrapper, 2) < 0)
00202     return ERROR;
00203   if (load_builtin_func("bor", bor_wrapper, 2) < 0)
00204     return ERROR;
00205   if (load_builtin_func("bnot", bnot_wrapper, 1) < 0)
00206     return ERROR;
00207   if (load_builtin_func("if", if_wrapper, 3) < 0)
00208     return ERROR;
00209   if (load_builtin_func("equal", equal_wrapper, 2) < 0)
00210     return ERROR;
00211   if (load_builtin_func("above", above_wrapper, 2) < 0)
00212     return ERROR;
00213   if (load_builtin_func("below", below_wrapper, 2) < 0)
00214     return ERROR;
00215   if (load_builtin_func("nchoosek", nchoosek_wrapper, 2) < 0)
00216     return ERROR;
00217   if (load_builtin_func("fact", fact_wrapper, 1) < 0)
00218     return ERROR;
00219 
00220 
00221   return SUCCESS;
00222 }

Generated on Tue Dec 20 10:14:57 2005 for vlc-0.8.4a by  doxygen 1.4.2