]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/func.c
Remove stdio while we're at it.
[vlc] / modules / visualization / galaktos / func.c
1 /* Function management */
2
3
4 #include "common.h"
5 #include "fatal.h"
6
7 #include "func_types.h"
8 #include "func.h"
9
10 #include "splaytree_types.h"
11 #include "splaytree.h"
12 #include "tree_types.h"
13
14 #include "builtin_funcs.h"
15
16 /* A splay tree of builtin functions */
17 splaytree_t * builtin_func_tree;
18
19 /* Private function prototypes */
20 int compare_func(char * name, char * name2);
21 int insert_func(func_t * name);
22 void * copy_func_key(char * string);
23
24
25 void * copy_func_key(char * string) {
26         
27         char * clone_string;
28         
29         if ((clone_string = malloc(MAX_TOKEN_SIZE)) == NULL)
30                 return NULL;
31         
32         strncpy(clone_string, string, MAX_TOKEN_SIZE-1);
33         
34         return (void*)clone_string;
35 }       
36
37
38 func_t * create_func (char * name, double (*func_ptr)(), int num_args) {
39
40   func_t * func;
41   func = (func_t*)malloc(sizeof(func_t));
42  
43   if (func == NULL)
44     return NULL;
45
46   
47   /* Clear name space */
48   memset(func->name, 0, MAX_TOKEN_SIZE);
49
50   /* Copy given name into function structure */
51   strncpy(func->name, name, MAX_TOKEN_SIZE); 
52
53   /* Assign value pointer */
54   func->func_ptr = func_ptr;
55   func->num_args = num_args;
56   /* Return instantiated function */
57   return func;
58
59 }
60
61 /* Initialize the builtin function database.
62    Should only be necessary once */
63 int init_builtin_func_db() {
64   int retval;
65
66   builtin_func_tree = create_splaytree(compare_string, copy_string, free_string);
67
68   if (builtin_func_tree == NULL)
69     return OUTOFMEM_ERROR;
70
71   retval = load_all_builtin_func();
72   return SUCCESS;
73 }
74
75
76 /* Destroy the builtin function database.
77    Generally, do this on projectm exit */
78 int destroy_builtin_func_db() {
79
80   splay_traverse(free_func, builtin_func_tree);
81   destroy_splaytree(builtin_func_tree);
82   return SUCCESS;
83
84 }
85
86 /* Insert a function into the database */
87 int insert_func(func_t * func) {
88
89   if (func == NULL)
90     return ERROR;
91
92   splay_insert(func, func->name, builtin_func_tree);
93
94   return SUCCESS;
95 }
96
97 /* Frees a function type, real complicated... */
98 void free_func(func_t * func) {
99   free(func);
100 }
101
102 /* Remove a function from the database */
103 int remove_func(func_t * func) {
104
105   if (func == NULL)
106     return ERROR;
107
108     splay_delete(func->name, builtin_func_tree);
109
110   return SUCCESS;
111 }
112
113 /* Find a function given its name */
114 func_t * find_func(char * name) {
115
116   func_t * func = NULL;
117
118   /* First look in the builtin database */
119   func = (func_t *)splay_find(name, builtin_func_tree);
120         
121   return func;
122
123 }
124
125 /* Compare string name with function name */
126 int compare_func(char * name, char * name2) {
127
128   int cmpval;
129
130   /* Uses string comparison function */
131   cmpval = strncmp(name, name2, MAX_TOKEN_SIZE-1);
132   
133   return cmpval;
134 }
135
136 /* Loads a builtin function */
137 int load_builtin_func(char * name,  double (*func_ptr)(), int num_args) {
138
139   func_t * func; 
140   int retval; 
141
142   /* Create new function */
143   func = create_func(name, func_ptr, num_args);
144
145   if (func == NULL)
146     return OUTOFMEM_ERROR;
147
148   retval = insert_func(func);
149
150   return retval;
151
152 }
153
154 /* Loads all builtin functions */
155 int load_all_builtin_func() {
156
157   
158   if (load_builtin_func("int", int_wrapper, 1) < 0)
159     return ERROR;
160   if (load_builtin_func("abs", abs_wrapper, 1) < 0)
161     return ERROR;
162   if (load_builtin_func("sin", sin_wrapper, 1) < 0)
163     return ERROR;
164   if (load_builtin_func("cos", cos_wrapper, 1) < 0)
165     return ERROR;
166   if (load_builtin_func("tan", tan_wrapper, 1) < 0)
167     return ERROR;
168   if (load_builtin_func("asin", asin_wrapper, 1) < 0)
169     return ERROR;
170   if (load_builtin_func("acos", acos_wrapper, 1) < 0)
171     return ERROR;
172   if (load_builtin_func("atan", atan_wrapper, 1) < 0)
173     return ERROR;
174   if (load_builtin_func("sqr", sqr_wrapper, 1) < 0)
175     return ERROR;
176   if (load_builtin_func("sqrt", sqrt_wrapper, 1) < 0)
177     return ERROR;
178   if (load_builtin_func("pow", pow_wrapper, 2) < 0)
179     return ERROR;
180   if (load_builtin_func("exp", exp_wrapper, 1) < 0)
181     return ERROR;
182   if (load_builtin_func("log", log_wrapper, 1) < 0)
183     return ERROR;
184   if (load_builtin_func("log10", log10_wrapper, 1) < 0)
185     return ERROR;
186   if (load_builtin_func("sign", sign_wrapper, 1) < 0)
187     return ERROR;
188   if (load_builtin_func("min", min_wrapper, 2) < 0)
189     return ERROR;
190   if (load_builtin_func("max", max_wrapper, 2) < 0)
191     return ERROR;
192   if (load_builtin_func("sigmoid", sigmoid_wrapper, 2) < 0)
193     return ERROR;
194   if (load_builtin_func("atan2", atan2_wrapper, 2) < 0)
195     return ERROR;
196   if (load_builtin_func("rand", rand_wrapper, 1) < 0)
197     return ERROR;
198   if (load_builtin_func("band", band_wrapper, 2) < 0)
199     return ERROR;
200   if (load_builtin_func("bor", bor_wrapper, 2) < 0)
201     return ERROR;
202   if (load_builtin_func("bnot", bnot_wrapper, 1) < 0)
203     return ERROR;
204   if (load_builtin_func("if", if_wrapper, 3) < 0)
205     return ERROR;
206   if (load_builtin_func("equal", equal_wrapper, 2) < 0)
207     return ERROR;
208   if (load_builtin_func("above", above_wrapper, 2) < 0)
209     return ERROR;
210   if (load_builtin_func("below", below_wrapper, 2) < 0)
211     return ERROR;
212   if (load_builtin_func("nchoosek", nchoosek_wrapper, 2) < 0)
213     return ERROR;
214   if (load_builtin_func("fact", fact_wrapper, 1) < 0)
215     return ERROR;
216
217
218   return SUCCESS;
219 }