annotate src/rovascope/libcalc/dict.c @ 925:3673bbab7372 trunk

[svn] - make madplug ignore crc error. closes #884.
author yaz
date Mon, 09 Apr 2007 03:51:00 -0700
parents 290588854a9d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
1 /* dict.c -- symbol dictionary structures
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
2 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
3 * Copyright (C) 2001 Janusz Gregorczyk <jgregor@kki.net.pl>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
4 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
5 * This file is part of xvs.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
6 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
10 * (at your option) any later version.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
11 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
15 * GNU General Public License for more details.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
16 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
20 */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
21
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
22 #define V_SPACE_INIT 8
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
23 #define V_SPACE_INCR 8
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
24
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
25 #include <glib.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
26 #include <string.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
27
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
28 #include "dict.h"
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
29
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
30 static void more_variables (symbol_dict_t *dict) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
31 var_t *new_var;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
32
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
33 dict->v_space += V_SPACE_INCR;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
34
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
35 new_var = (var_t *)g_malloc (dict->v_space * sizeof(var_t));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
36
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
37 memcpy (new_var, dict->variables, dict->v_count * sizeof(var_t));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
38 g_free (dict->variables);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
39
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
40 dict->variables = new_var;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
41 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
42
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
43 symbol_dict_t *dict_new (void) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
44 symbol_dict_t *dict;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
45
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
46 dict = (symbol_dict_t *) g_malloc (sizeof(symbol_dict_t));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
47
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
48 /* Allocate space for variables. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
49 dict->v_count = 0;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
50 dict->v_space = V_SPACE_INIT;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
51 dict->variables = (var_t *)g_malloc (dict->v_space * sizeof(var_t));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
52
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
53 return dict;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
54 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
55
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
56 void dict_free (symbol_dict_t *dict) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
57 int i;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
58
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
59 if (!dict)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
60 return;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
61
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
62 /* Free memory used by variables. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
63 for (i = 0; i < dict->v_count; i++)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
64 g_free (dict->variables[i].name);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
65 g_free (dict->variables);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
66
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
67 g_free (dict);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
68 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
69
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
70 static int dict_define_variable (symbol_dict_t *dict, const char *name) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
71 var_t *var;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
72
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
73 if (dict->v_count >= dict->v_space)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
74 more_variables (dict);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
75
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
76 var = &dict->variables[dict->v_count];
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
77
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
78 var->value = 0.0;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
79 var->name = g_strdup (name);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
80
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
81 return dict->v_count++;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
82 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
83
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
84 int dict_lookup (symbol_dict_t *dict, const char *name) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
85 int i;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
86
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
87 for (i = 0; i < dict->v_count; i++) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
88 if (strcmp (dict->variables[i].name, name) == 0)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
89 return i;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
90 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
91
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
92 /* Not defined -- define a new variable. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
93 return dict_define_variable (dict, name);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
94 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
95
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
96 double *dict_variable (symbol_dict_t *dict, const char *var_name) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
97 int id = dict_lookup (dict, var_name);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
98 return &dict->variables[id].value;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
99 }