282
|
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 void more_variables (symbol_dict_t *dict) {
|
|
31 var_t *new_var;
|
|
32
|
|
33 dict->v_space += V_SPACE_INCR;
|
|
34
|
|
35 new_var = (var_t *)g_malloc (dict->v_space * sizeof(var_t));
|
|
36
|
|
37 memcpy (new_var, dict->variables, dict->v_count * sizeof(var_t));
|
|
38 g_free (dict->variables);
|
|
39
|
|
40 dict->variables = new_var;
|
|
41 }
|
|
42
|
|
43 symbol_dict_t *dict_new (void) {
|
|
44 symbol_dict_t *dict;
|
|
45
|
|
46 dict = (symbol_dict_t *) g_malloc (sizeof(symbol_dict_t));
|
|
47
|
|
48 /* Allocate space for variables. */
|
|
49 dict->v_count = 0;
|
|
50 dict->v_space = V_SPACE_INIT;
|
|
51 dict->variables = (var_t *)g_malloc (dict->v_space * sizeof(var_t));
|
|
52
|
|
53 return dict;
|
|
54 }
|
|
55
|
|
56 void dict_free (symbol_dict_t *dict) {
|
|
57 int i;
|
|
58
|
|
59 if (!dict)
|
|
60 return;
|
|
61
|
|
62 /* Free memory used by variables. */
|
|
63 for (i = 0; i < dict->v_count; i++)
|
|
64 g_free (dict->variables[i].name);
|
|
65 g_free (dict->variables);
|
|
66
|
|
67 g_free (dict);
|
|
68 }
|
|
69
|
|
70 static int dict_define_variable (symbol_dict_t *dict, const char *name) {
|
|
71 var_t *var;
|
|
72
|
|
73 if (dict->v_count >= dict->v_space)
|
|
74 more_variables (dict);
|
|
75
|
|
76 var = &dict->variables[dict->v_count];
|
|
77
|
|
78 var->value = 0.0;
|
|
79 var->name = g_strdup (name);
|
|
80
|
|
81 return dict->v_count++;
|
|
82 }
|
|
83
|
|
84 int dict_lookup (symbol_dict_t *dict, const char *name) {
|
|
85 int i;
|
|
86
|
|
87 for (i = 0; i < dict->v_count; i++) {
|
|
88 if (strcmp (dict->variables[i].name, name) == 0)
|
|
89 return i;
|
|
90 }
|
|
91
|
|
92 /* Not defined -- define a new variable. */
|
|
93 return dict_define_variable (dict, name);
|
|
94 }
|
|
95
|
|
96 double *dict_variable (symbol_dict_t *dict, const char *var_name) {
|
|
97 int id = dict_lookup (dict, var_name);
|
|
98 return &dict->variables[id].value;
|
|
99 }
|