DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / structures / name_table.c
1 /********************************************************************
3 name-table.c - the functions used for name table handling. The
4 name table is a binary tree. The node's left child has the name
5 alphabetically before the name in the node, and the right child
6 has the name which is alphabetically after the name in the node.
8 Niksa Orlic, 2004-04-25
10 ********************************************************************/
12 #include "../util/strings.h"
13 #include "../util/error.h"
14 //#include "../util/message.h"
15 #include "type_list.h"
16 #include "string_list.h"
17 #include "type.h"
18 #include "identifier.h"
19 #include "name_table.h"
20 #include "../classgen/bytecode.h"
21 #include "block.h"
22 #include "unit.h"
23 #include "../util/memory.h"
25 #include <string.h>
26 #include <stdlib.h>
29 /*
30 Create an empty name table.
31 */
32 name_table *name_table_create()
33 {
34 name_table *new_item;
35 new_item = (name_table*) mem_alloc(sizeof(name_table));
37 if (new_item == NULL)
38 die(1);
40 new_item->left_child = NULL;
41 new_item->right_child = NULL;
42 new_item->name = NULL;
43 new_item->descriptor = NULL;
45 new_item->last = new_item;
46 new_item->next = NULL;
48 return new_item;
49 }
52 /*
53 Destroy an allocated name table and free all memory.
54 */
55 void name_table_destroy(name_table *item)
56 {
57 if (item == NULL)
58 return;
60 if (item->name != NULL)
61 string_destroy(item->name);
63 if (item->descriptor != NULL)
64 identifier_destroy(item->descriptor);
66 if (item->left_child != NULL)
67 name_table_destroy(item->left_child);
69 if (item->right_child != NULL)
70 name_table_destroy(item->right_child);
71 }
73 /*
74 name_table *name_table_duplicate(name_table *item)
75 {
76 name_table *new_item = mem_alloc(sizeof(name_table));
78 new_item->descriptor = identifier_duplicate(item->descriptor);
79 new_item->name = string_duplicate(item->name);
81 if (item->left_child != NULL)
82 new_item->left_child = name_table_duplicate(item->left_child);
83 else
84 new_item->left_child = NULL;
86 if (item->right_child != NULL)
87 new_item->right_child = name_table_duplicate(item->right_child);
88 else
89 new_item->right_child = NULL;
91 return new_item;
92 }*/
95 /*
96 Inserts an entry into the name table. The string and the descriptor are
97 not copied, only pointers to the string and descriptor are copied.
98 */
99 void name_table_insert(name_table *root, string *name,
100 identifier *descriptor)
102 int string_compare;
103 name_table *item, *new_item;
105 if (root == NULL)
106 return;
108 if (root->name == NULL)
110 /* the root item is empty, insert the value
111 into the root item */
113 root->name = name;
114 root->descriptor = descriptor;
115 return;
119 /* go through the tree to the first free element */
120 item = root;
122 do
124 string_compare = STRING_COMPARE(string_get_cstr(name), string_get_cstr(item->name));
126 if (string_compare == 0)
128 /* an error occured, just return */
129 return;
132 if (string_compare < 0)
134 if (item->left_child == NULL)
135 break;
137 item = item->left_child;
140 if (string_compare > 0)
142 if (item->right_child == NULL)
143 break;
145 item = item->right_child;
147 } while (1); /* until the break is called */
149 new_item = name_table_create();
150 new_item->name = name;
151 new_item->descriptor = descriptor;
152 new_item->next = NULL;
154 root->last->next = new_item;
155 root->last = new_item;
157 if (string_compare < 0)
158 item->left_child = new_item;
160 if (string_compare > 0)
161 item->right_child = new_item;
166 /*
167 Searches name table for an element with a given name.
168 Return the identifier descriptor if found or NULL if
169 not found.
170 */
171 identifier* name_table_find(name_table *item, string *name)
173 int string_compare;
175 while ((item != NULL) && (item->name != NULL))
177 string_compare = STRING_COMPARE(string_get_cstr(name), string_get_cstr(item->name));
179 if (string_compare == 0)
180 return item->descriptor;
182 if (string_compare < 0)
183 item = item->left_child;
185 if (string_compare > 0)
186 item = item->right_child;
190 return NULL;
193 identifier *name_table_find_cstr(name_table *item, char *name)
195 int string_compare;
197 while ((item != NULL) && (item->name != NULL))
199 string_compare = STRING_COMPARE(name, string_get_cstr(item->name));
201 if (string_compare == 0)
202 return item->descriptor;
204 if (string_compare < 0)
205 item = item->left_child;
207 if (string_compare > 0)
208 item = item->right_child;
212 return NULL;