* added compilers lcc and bcc (linux86)
[mascara-docs.git] / compilers / linux86-0.16.17 / cpp / hash.c
blob6013c54b2d9645a8beade8244c8d3c9a9ce82cb8
2 #include <stdio.h>
3 #ifdef __STDC__
4 #include <stdlib.h>
5 #include <string.h>
6 #else
7 #include <malloc.h>
8 #endif
9 #include "cc.h"
12 * Two functions:
13 * char * set_entry(int namespace, char * name, void * value);
14 * returns a pointer to the copy of the name;
16 * void * read_entry(int namespace, char * name);
17 * returns the value;
20 struct hashentry
22 struct hashentry * next;
23 void * value;
24 int namespace;
25 char word[1];
28 struct hashentry ** hashtable;
29 int hashsize = 0xFF; /* 2^X -1 */
30 int hashcount = 0;
31 static int hashvalue P((int namespace, char * word));
33 void *
34 read_entry(namespace, word)
35 int namespace;
36 char * word;
38 int hash_val;
39 struct hashentry * hashline;
40 if( hashtable == 0 ) return 0;
41 hash_val = hashvalue(namespace, word);
43 hashline = hashtable[hash_val];
45 for(; hashline; hashline = hashline->next)
47 if(namespace != hashline->namespace) continue;
48 if(word[0] != hashline->word[0]) continue;
49 if(strcmp(word, hashline->word) ) continue;
50 return hashline->value;
52 return 0;
55 char *
56 set_entry(namespace, word, value)
57 int namespace;
58 char * word;
59 void * value;
61 int hash_val, i;
62 struct hashentry * hashline, *prev;
63 hash_val = hashvalue(namespace, word);
65 if( hashtable )
67 hashline = hashtable[hash_val];
69 for(prev=0; hashline; prev=hashline, hashline = hashline->next)
71 if(namespace != hashline->namespace) continue;
72 if(word[0] != hashline->word[0]) continue;
73 if(strcmp(word, hashline->word) ) continue;
74 if( value ) hashline->value = value;
75 else
77 if( prev == 0 ) hashtable[hash_val] = hashline->next;
78 else prev->next = hashline->next;
79 free(hashline);
80 return 0;
82 return hashline->word;
85 if( value == 0 ) return 0;
86 if( hashtable == 0 )
88 hashtable = malloc((hashsize+1)*sizeof(char*));
89 if( hashtable == 0 ) cfatal("Out of memory");
90 for(i=0; i<=hashsize; i++) hashtable[i] = 0;
92 /* Add record */
93 hashline = malloc(sizeof(struct hashentry)+strlen(word));
94 if( hashline == 0 ) cfatal("Out of memory");
95 else
97 hashline->next = hashtable[hash_val];
98 hashline->namespace = namespace;
99 hashline->value = value;
100 strcpy(hashline->word, word);
101 hashtable[hash_val] = hashline;
103 return hashline->word;
106 static int hashvalue(namespace, word)
107 int namespace;
108 char * word;
110 int val = namespace;
111 char *p = word;
113 while(*p)
115 val = ((val<<4)^((val>>12)&0xF)^((*p++)&0xFF));
117 val &= hashsize;
118 return val;