comparison src/paranormal-ng/libcalc/dict.c @ 2078:1fa3c8cd366a

paranormal-ng: a GL visualiser. lots to do, most stuff won't work, but hey, this will do cool stuff too soon
author William Pitcock <nenolod@atheme.org>
date Mon, 15 Oct 2007 06:20:13 -0500
parents
children f1b6f1b2cdb3
comparison
equal deleted inserted replaced
2077:e5b639ab62b0 2078:1fa3c8cd366a
1 /* dict.c -- symbol dictionary structures
2 *
3 * Copyright (C) 2001 Janusz Gregorczyk <jgregor@kki.net.pl>
4 *
5 * This file is part of xvs.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #define V_SPACE_INIT 8
23 #define V_SPACE_INCR 8
24
25 #include <glib.h>
26 #include <string.h>
27
28 #include "dict.h"
29
30 static int global_dict_initialized = 0;
31 static symbol_dict_t global_dict;
32
33 static void more_variables (symbol_dict_t *dict) {
34 var_t *new_var;
35
36 dict->v_space += V_SPACE_INCR;
37
38 new_var = g_new(var_t, dict->v_space + 1);
39 memcpy (new_var, dict->variables, dict->v_count * sizeof(var_t));
40 g_free (dict->variables);
41
42 dict->variables = new_var;
43 }
44
45 static int dict_define_variable (symbol_dict_t *dict, const char *name) {
46 var_t *var;
47
48 if (dict->v_count >= dict->v_space)
49 more_variables (dict);
50
51 var = &dict->variables[dict->v_count];
52
53 var->value = 0.0;
54 var->name = g_strdup (name);
55
56 return dict->v_count++;
57 }
58
59 symbol_dict_t *dict_new (void) {
60 symbol_dict_t *dict;
61
62 if (global_dict_initialized != 1) {
63 int i;
64
65 global_dict.v_count = 0;
66 global_dict.v_space = V_SPACE_INIT;
67 global_dict.variables = (var_t *) g_new(var_t, global_dict.v_space + 1);
68 global_dict_initialized = 1;
69
70 for (i = 0; i < 100; i++) {
71 gchar tmpbuf[40];
72 g_snprintf(tmpbuf, 40, "global_reg%d", i);
73 dict_define_variable(&global_dict, tmpbuf);
74 }
75 }
76
77 dict = g_new(symbol_dict_t, 1);
78
79 /* Allocate space for variables. */
80 dict->v_count = 0;
81 dict->v_space = V_SPACE_INIT;
82 dict->variables = (var_t *) g_new (var_t, dict->v_space + 1);
83
84 return dict;
85 }
86
87 void dict_free (symbol_dict_t *dict) {
88 int i;
89
90 if (!dict)
91 return;
92
93 /* Free memory used by variables. */
94 for (i = 0; i < dict->v_count; i++)
95 g_free (dict->variables[i].name);
96 g_free (dict->variables);
97
98 g_free (dict);
99 }
100
101 int dict_lookup (symbol_dict_t *dict, const char *name) {
102 int i;
103
104 for (i = 0; i < global_dict.v_count; i++) {
105 if (strcmp (global_dict.variables[i].name, name) == 0)
106 return -i;
107 }
108
109 for (i = 0; i < dict->v_count; i++) {
110 if (strcmp (dict->variables[i].name, name) == 0)
111 return i;
112 }
113
114 /* Not defined -- define a new variable. */
115 return dict_define_variable (dict, name);
116 }
117
118 double *dict_variable (symbol_dict_t *dict, const char *var_name) {
119 int id = dict_lookup (dict, var_name);
120
121 /* global variables are presented as negative offset. negating
122 * a negative number results in a positive offset. --nenolod
123 */
124 if (id < 0)
125 return &global_dict.variables[-id].value;
126
127 return &dict->variables[id].value;
128 }