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