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