]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/init_cond.c
c5ccc71a471cde4eafa24bf68068f8f3ed1edefd
[vlc] / modules / visualization / galaktos / init_cond.c
1 /*****************************************************************************
2  * init_cond.:
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          code from projectM http://xmms-projectm.sourceforge.net
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26
27 /* Library functions to manipulate initial condition values */
28
29
30 #include "common.h"
31 #include "fatal.h"
32
33 #include "param_types.h"
34 #include "expr_types.h"
35 #include "init_cond_types.h"
36 #include "init_cond.h"
37
38 #include "splaytree_types.h"
39 #include "splaytree.h"
40 char init_cond_string_buffer[STRING_BUFFER_SIZE];
41 int init_cond_string_buffer_index = 0;
42
43
44 void init_cond_to_string(init_cond_t * init_cond);
45
46 /* Frees initial condition structure */
47 void free_init_cond(init_cond_t * init_cond) {
48   free(init_cond);
49 }
50
51 /* Evaluate an initial conditon */
52 void eval_init_cond(init_cond_t * init_cond) {
53
54   if (init_cond == NULL)
55     return;
56  
57   /* Parameter is of boolean type, either a 1 or 0 value integer */
58
59   /* Set matrix flag to zero. This ensures
60      its constant value will be used rather than a matrix value 
61   */
62   init_cond->param->matrix_flag = 0;
63   if (init_cond->param->type == P_TYPE_BOOL) {
64          if (INIT_COND_DEBUG) printf("init_cond: %s = %d (TYPE BOOL)\n", init_cond->param->name, init_cond->init_val.bool_val); 
65          *((int*)init_cond->param->engine_val) = init_cond->init_val.bool_val;
66      return;
67   }
68   
69   /* Parameter is an integer type, just like C */
70   
71   if (init_cond->param->type == P_TYPE_INT) {
72          if (INIT_COND_DEBUG) printf("init_cond: %s = %d (TYPE INT)\n", init_cond->param->name, init_cond->init_val.int_val);
73          *((int*)init_cond->param->engine_val) = init_cond->init_val.int_val;
74      return;
75   }
76
77   /* Parameter is of a double type, just like C */
78
79   if (init_cond->param->type == P_TYPE_DOUBLE) {
80         if (INIT_COND_DEBUG) printf("init_cond: %s = %f (TYPE DOUBLE)\n", init_cond->param->name, init_cond->init_val.double_val);
81         *((double*)init_cond->param->engine_val) = init_cond->init_val.double_val;
82     return;
83   }
84
85   /* Unknown type of parameter */
86   return;
87 }
88
89 /* Creates a new initial condition */
90 init_cond_t * new_init_cond(param_t * param, value_t init_val) {
91
92   init_cond_t * init_cond;
93
94   init_cond = (init_cond_t*)malloc(sizeof(init_cond_t));
95    
96   if (init_cond == NULL)
97     return NULL;
98  
99   init_cond->param = param;
100   init_cond->init_val = init_val;
101   return init_cond;
102 }
103
104 /* WIP */
105 void init_cond_to_string(init_cond_t * init_cond) {
106         
107         int string_length;
108         char string[MAX_TOKEN_SIZE];
109         
110         if (init_cond == NULL)
111                 return;
112
113         /* Create a string "param_name=val" */
114         switch (init_cond->param->type) {
115                 lldiv_t div;
116                 
117                 case P_TYPE_BOOL:
118                         sprintf(string, "%s=%d\n", init_cond->param->name, init_cond->init_val.bool_val);
119                         break; 
120                 case P_TYPE_INT:
121                         sprintf(string, "%s=%d\n", init_cond->param->name, init_cond->init_val.int_val);
122                         break;
123                 case P_TYPE_DOUBLE:
124                         div = lldiv( init_cond->init_val.double_val * 1000000,
125                                      1000000 );
126                         sprintf(string, "%s="I64Fd".%06u\n", init_cond->param->name, div.quot, (unsigned int) div.rem );
127                         break;
128                 default:
129                         return;
130         }               
131                 
132         /* Compute the length of the string */
133         string_length = strlen(string);
134         
135         /* Buffer overflow check */
136         if ((init_cond_string_buffer_index + string_length + 1)  > (STRING_BUFFER_SIZE - 1))
137                 return;
138         
139         /* Copy the string into the initial condition string buffer */
140         
141         strncpy(init_cond_string_buffer + init_cond_string_buffer_index, string, string_length);
142         
143         /* Increment the string buffer, offset by one for the null terminator, which will be
144            overwritten by the next call to this function */
145         init_cond_string_buffer_index+= string_length + 1;
146                 
147 }
148
149
150 char * create_init_cond_string_buffer(splaytree_t * init_cond_tree) {
151
152         if (init_cond_tree == NULL)
153                 return NULL;
154         
155         init_cond_string_buffer_index = 0;
156         
157         splay_traverse(init_cond_to_string, init_cond_tree);
158         
159         return init_cond_string_buffer;
160                 
161 }