2 Copyright (c) 2003 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 #define LOG2_NUMBUCKETS 10
31 #define NUMBUCKETS (1 << LOG2_NUMBUCKETS)
34 hash(const char *string)
38 for(i = 0; string[i] != '\0'; i++)
39 u = (u<<5) + (u >> (LOG2_NUMBUCKETS - 5)) + (unsigned char)string[i];
40 return (u & (NUMBUCKETS - 1));
44 strcpy_lwr(char *dst, const char *src)
58 return calloc(NUMBUCKETS, sizeof(HashBucketPtr));
62 destroyHashTable(HashTablePtr table)
67 for(i = 0; i < NUMBUCKETS; i++) {
70 table[i] = table[i]->next;
80 getHash(HashTablePtr table, const char *key)
84 for(bp = table[i]; bp; bp = bp->next) {
85 if(strcasecmp(bp->key, key) == 0)
92 putHash(HashTablePtr table, char *key, char *value, int prio)
95 char *keycopy = NULL, *valuecopy = NULL;
97 for(bp = table[i]; bp; bp = bp->next) {
98 if(strcasecmp(bp->key, key) == 0) {
100 keycopy = malloc(strlen(key) + 1);
101 if(keycopy == NULL) goto fail;
102 strcpy_lwr(keycopy, key);
103 valuecopy = malloc(strlen(value) + 1);
104 if(valuecopy == NULL) goto fail;
105 strcpy(valuecopy, value);
109 bp->value = valuecopy;
114 keycopy = malloc(strlen(key) + 1);
117 strcpy_lwr(keycopy, key);
118 valuecopy = malloc(strlen(value) + 1);
119 if(valuecopy == NULL)
121 strcpy(valuecopy, value);
122 bp = malloc(sizeof(HashBucketRec));
126 bp->value = valuecopy;
133 if(keycopy) free(keycopy);
134 if(valuecopy) free(valuecopy);
139 hashElements(HashTablePtr table)
145 for(i = 0; i < NUMBUCKETS; i++) {
146 for(bp = table[i]; bp; bp = bp->next) {
154 key_first_cmp(const void *v1, const void *v2)
156 const HashBucketPtr *b1 = v1, *b2 = v2;
157 int c1 = strcasecmp((*b1)->key, (*b2)->key);
158 if(c1 != 0) return c1;
159 return strcmp((*b1)->value, (*b2)->value);
163 value_first_cmp(const void *v1, const void *v2)
165 const HashBucketPtr *b1 = v1, *b2 = v2;
166 int c1 = strcmp((*b1)->value, (*b2)->value);
167 if(c1 != 0) return c1;
168 return strcasecmp((*b1)->key, (*b2)->key);
172 hashArray(HashTablePtr table, int value_first)
177 n = hashElements(table);
178 dst = malloc((n + 1) * sizeof(HashBucketPtr));
183 for(i = 0; i < NUMBUCKETS; i++) {
186 table[i] = table[i]->next;
189 qsort(dst, j, sizeof(HashBucketPtr),
190 value_first ? value_first_cmp : key_first_cmp);
198 destroyHashArray(HashBucketPtr *array)
203 free(array[i]->value);