Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / sources / beh / beh / beh_dict.c
1
2 /* ###--------------------------------------------------------------### */
3 /* */
4 /* file : beh_dict.c */
5 /* date : Jun 15 1994 */
6 /* version : v107 */
7 /* authors : Pirouz BAZARGAN SABET */
8 /* content : low-level function */
9 /* */
10 /* ###--------------------------------------------------------------### */
11
12 #include <stdio.h>
13 #include MUT_H
14 #include BEH_H
15
16 #define BEH_ALODFN 64
17 #define BEH_HSZDFN 307
18
19 static struct beden *BEH_DCEHED = NULL; /* free entries list */
20 static struct bedrd *BEH_DCRHED = NULL; /* free records list */
21
22 /* ###--------------------------------------------------------------### */
23 /* function : beh_addent */
24 /* description : add a new entry point to the dictionary */
25 /* called func. : mbkalloc */
26 /* ###--------------------------------------------------------------### */
27
28 static struct beden *beh_addent (head, key)
29
30 struct beden *head;
31 char *key;
32
33 {
34 struct beden *entry;
35 int i ;
36
37 if (BEH_DCEHED == NULL)
38 {
39 BEH_DCEHED = (struct beden *)
40 mbkalloc (sizeof (struct beden) * BEH_ALODFN);
41
42 entry = BEH_DCEHED;
43 for (i=1 ; i<BEH_ALODFN ; i++)
44 {
45 entry->NEXT = entry + 1;
46 entry++;
47 }
48 entry->NEXT = NULL;
49 }
50
51 entry = BEH_DCEHED;
52 BEH_DCEHED = BEH_DCEHED->NEXT;
53
54 entry->NEXT = head;
55 entry->DATA = NULL;
56 entry->KEY = key;
57
58 return (entry);
59 }
60
61 /* ###--------------------------------------------------------------### */
62 /* function : beh_addrcd */
63 /* description : add a new record to the dictionary */
64 /* called func. : mbkalloc */
65 /* ###--------------------------------------------------------------### */
66
67 static struct bedrd *beh_addrcd (head, key)
68
69 struct bedrd *head;
70 char *key;
71
72 {
73 struct bedrd *recrd;
74 int i ;
75
76 if (BEH_DCRHED == NULL)
77 {
78 BEH_DCRHED = (struct bedrd *)
79 mbkalloc (sizeof (struct bedrd) * BEH_ALODFN);
80
81 recrd = BEH_DCRHED;
82 for (i=1 ; i<BEH_ALODFN ; i++)
83 {
84 recrd->NEXT = recrd + 1;
85 recrd++;
86 }
87 recrd->NEXT = NULL;
88 }
89
90 recrd = BEH_DCRHED;
91 BEH_DCRHED = BEH_DCRHED->NEXT;
92
93 recrd->NEXT = head;
94 recrd->FD0_VAL = 0;
95 recrd->FD1_VAL = 0;
96 recrd->FD2_VAL = 0;
97 recrd->FD3_VAL = 0;
98 recrd->FD4_VAL = 0;
99 recrd->FD5_VAL = 0;
100 recrd->FD6_VAL = 0;
101 recrd->PNT_VAL = 0l;
102 recrd->KEY = key;
103
104 return (recrd);
105 }
106
107 /* ###--------------------------------------------------------------### */
108 /* function : beh_initab */
109 /* description : create a new dictionary */
110 /* called func. : mbkalloc */
111 /* ###--------------------------------------------------------------### */
112
113 struct beden **beh_initab ()
114
115 {
116 struct beden **head;
117 int i;
118
119 head = (struct beden **)
120 mbkalloc (sizeof(struct beden *) * BEH_HSZDFN);
121
122 for (i=0 ; i<BEH_HSZDFN ; i++)
123 head[i] = NULL;
124
125 return (head);
126 }
127
128 /* ###--------------------------------------------------------------### */
129 /* function : beh_addtab */
130 /* description : save a data in a dictionary */
131 /* called func. : beh_addent, beh_addrcd */
132 /* ###--------------------------------------------------------------### */
133
134 void beh_addtab (head, key_str, ctx_str, field, valu)
135
136 struct beden **head;
137 char *key_str;
138 char *ctx_str;
139 int field;
140 long valu;
141
142 {
143 int found = 0;
144 unsigned long key ;
145 unsigned int index ;
146 struct beden *entry_pnt ;
147 struct bedrd *recrd_pnt ;
148
149 key = ((unsigned long) key_str) + ((unsigned long) ctx_str);
150
151 index = key % BEH_HSZDFN;
152 entry_pnt = head [index];
153
154 while (entry_pnt != NULL)
155 {
156 if (entry_pnt->KEY == key_str)
157 {
158 found = 1;
159 break;
160 }
161 entry_pnt = entry_pnt->NEXT;
162 }
163
164 if (found == 0)
165 {
166 head [index] = beh_addent (head [index], key_str);
167 entry_pnt = head [index];
168 }
169
170 found = 0;
171 recrd_pnt = entry_pnt->DATA;
172 while (recrd_pnt != NULL)
173 {
174 if (recrd_pnt->KEY == ctx_str)
175 {
176 found = 1;
177 break;
178 }
179 recrd_pnt = recrd_pnt->NEXT;
180 }
181
182 if (found == 0)
183 {
184 entry_pnt->DATA = beh_addrcd (entry_pnt->DATA, ctx_str);
185 recrd_pnt = entry_pnt->DATA ;
186 }
187
188 switch (field)
189 {
190 case 0 :
191 recrd_pnt->FD0_VAL = valu; break;
192 case 1 :
193 recrd_pnt->FD1_VAL = valu; break;
194 case 2 :
195 recrd_pnt->FD2_VAL = valu; break;
196 case 3 :
197 recrd_pnt->FD3_VAL = valu; break;
198 case 4 :
199 recrd_pnt->FD4_VAL = valu; break;
200 case 5 :
201 recrd_pnt->FD5_VAL = valu; break;
202 case 6 :
203 recrd_pnt->FD6_VAL = valu; break;
204 case 7 :
205 recrd_pnt->PNT_VAL = valu; break;
206 }
207
208 }
209
210 /* ###--------------------------------------------------------------### */
211 /* function : beh_chktab */
212 /* description : extract a data from a dictionary */
213 /* called func. : none */
214 /* ###--------------------------------------------------------------### */
215
216 long beh_chktab (head, key_str, ctx_str, field)
217
218 struct beden **head ;
219 char *key_str;
220 char *ctx_str;
221 int field ;
222
223 {
224 int found = 0;
225 long valu = 0;
226 unsigned long key ;
227 unsigned int index ;
228 struct beden *entry_pnt;
229 struct bedrd *recrd_pnt;
230
231 key = ((unsigned long) key_str) + ((unsigned long) ctx_str);
232
233 index = key % BEH_HSZDFN;
234 entry_pnt = head [index];
235
236 while (entry_pnt != NULL)
237 {
238 if (entry_pnt->KEY == key_str)
239 {
240 found = 1;
241 break;
242 }
243 entry_pnt = entry_pnt->NEXT;
244 }
245
246 if (found == 1)
247 {
248 found = 0;
249 recrd_pnt = entry_pnt->DATA;
250 while (recrd_pnt != NULL)
251 {
252 if (recrd_pnt->KEY == ctx_str)
253 {
254 found = 1;
255 break;
256 }
257 recrd_pnt = recrd_pnt->NEXT;
258 }
259 if (found == 1)
260 {
261 switch (field)
262 {
263 case 0 :
264 valu = recrd_pnt->FD0_VAL; break;
265 case 1 :
266 valu = recrd_pnt->FD1_VAL; break;
267 case 2 :
268 valu = recrd_pnt->FD2_VAL; break;
269 case 3 :
270 valu = recrd_pnt->FD3_VAL; break;
271 case 4 :
272 valu = recrd_pnt->FD4_VAL; break;
273 case 5 :
274 valu = recrd_pnt->FD5_VAL; break;
275 case 6 :
276 valu = recrd_pnt->FD6_VAL; break;
277 case 7 :
278 valu = recrd_pnt->PNT_VAL; break;
279 }
280 }
281 }
282
283 return (valu);
284 }
285
286 /* ###--------------------------------------------------------------### */
287 /* function : beh_fretab */
288 /* description : release a dictionary */
289 /* called func. : mbkfree */
290 /* ###--------------------------------------------------------------### */
291
292 void beh_fretab (pt_hash)
293
294 struct beden **pt_hash;
295
296 {
297 struct beden *pt_entry ;
298 struct beden *pt_nxtentry;
299 struct bedrd *pt_record ;
300 int i ;
301
302 if (pt_hash != NULL)
303 {
304 for (i=0 ; i<BEH_HSZDFN ; i++)
305 {
306 if ((pt_entry = pt_hash [i]) != NULL)
307 {
308 while (pt_entry != NULL)
309 {
310 pt_record = pt_entry->DATA;
311
312 while (pt_record->NEXT != NULL)
313 pt_record = pt_record->NEXT;
314
315 pt_record->NEXT = BEH_DCRHED;
316 BEH_DCRHED = pt_entry->DATA;
317
318 pt_nxtentry = pt_entry->NEXT;
319 pt_entry->NEXT = BEH_DCEHED;
320 BEH_DCEHED = pt_entry;
321 pt_entry = pt_nxtentry;
322 }
323 }
324 }
325 mbkfree (pt_hash);
326 }
327 }