1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@videolan.org>
8 * code from projectM http://xmms-projectm.sourceforge.net
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.
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.
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 *****************************************************************************/
27 /* Library functions to manipulate initial condition values */
36 #include "param_types.h"
37 #include "expr_types.h"
38 #include "init_cond_types.h"
39 #include "init_cond.h"
41 #include "splaytree_types.h"
42 #include "splaytree.h"
43 char init_cond_string_buffer[STRING_BUFFER_SIZE];
44 int init_cond_string_buffer_index = 0;
47 void init_cond_to_string(init_cond_t * init_cond);
49 /* Frees initial condition structure */
50 void free_init_cond(init_cond_t * init_cond) {
54 /* Evaluate an initial conditon */
55 void eval_init_cond(init_cond_t * init_cond) {
57 if (init_cond == NULL)
60 /* Parameter is of boolean type, either a 1 or 0 value integer */
62 /* Set matrix flag to zero. This ensures
63 its constant value will be used rather than a matrix value
65 init_cond->param->matrix_flag = 0;
66 if (init_cond->param->type == P_TYPE_BOOL) {
67 if (INIT_COND_DEBUG) printf("init_cond: %s = %d (TYPE BOOL)\n", init_cond->param->name, init_cond->init_val.bool_val);
68 *((int*)init_cond->param->engine_val) = init_cond->init_val.bool_val;
72 /* Parameter is an integer type, just like C */
74 if (init_cond->param->type == P_TYPE_INT) {
75 if (INIT_COND_DEBUG) printf("init_cond: %s = %d (TYPE INT)\n", init_cond->param->name, init_cond->init_val.int_val);
76 *((int*)init_cond->param->engine_val) = init_cond->init_val.int_val;
80 /* Parameter is of a double type, just like C */
82 if (init_cond->param->type == P_TYPE_DOUBLE) {
83 if (INIT_COND_DEBUG) printf("init_cond: %s = %f (TYPE DOUBLE)\n", init_cond->param->name, init_cond->init_val.double_val);
84 *((double*)init_cond->param->engine_val) = init_cond->init_val.double_val;
88 /* Unknown type of parameter */
92 /* Creates a new initial condition */
93 init_cond_t * new_init_cond(param_t * param, value_t init_val) {
95 init_cond_t * init_cond;
97 init_cond = (init_cond_t*)malloc(sizeof(init_cond_t));
99 if (init_cond == NULL)
102 init_cond->param = param;
103 init_cond->init_val = init_val;
108 void init_cond_to_string(init_cond_t * init_cond) {
111 char string[MAX_TOKEN_SIZE];
113 if (init_cond == NULL)
116 /* Create a string "param_name=val" */
117 switch (init_cond->param->type) {
120 sprintf(string, "%s=%d\n", init_cond->param->name, init_cond->init_val.bool_val);
123 sprintf(string, "%s=%d\n", init_cond->param->name, init_cond->init_val.int_val);
126 sprintf(string, "%s=%f\n", init_cond->param->name, init_cond->init_val.double_val);
132 /* Compute the length of the string */
133 string_length = strlen(string);
135 /* Buffer overflow check */
136 if ((init_cond_string_buffer_index + string_length + 1) > (STRING_BUFFER_SIZE - 1))
139 /* Copy the string into the initial condition string buffer */
141 strncpy(init_cond_string_buffer + init_cond_string_buffer_index, string, string_length);
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;
150 char * create_init_cond_string_buffer(splaytree_t * init_cond_tree) {
152 if (init_cond_tree == NULL)
155 init_cond_string_buffer_index = 0;
157 splay_traverse(init_cond_to_string, init_cond_tree);
159 return init_cond_string_buffer;