forked from sba1/simplemail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash.c
277 lines (235 loc) · 6.94 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/***************************************************************************
SimpleMail - Copyright (C) 2000 Hynek Schlawack and Sebastian Bauer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
/*
** hash.c
**
** A implementation of hash tables for strings and a single assiociated
** data field.
**
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "debug.h"
#include "support_indep.h"
#include "hash.h"
/**************************************************************************
The hash function. It's the one used in berkely db (sleepycat)
**************************************************************************/
unsigned long sdbm(const unsigned char *str)
{
unsigned long hash = 0;
int c;
while ((c = *str++))
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
/**************************************************************************
Initialize the given hash table with space for 2^bits entries. With
filename you can specify a filename (may be NULL) where the hash_table
is stored if calling hash_table_store().
**************************************************************************/
int hash_table_init(struct hash_table *ht, int bits, const char *filename)
{
FILE *fh;
int i;
unsigned int size;
unsigned int mem_size;
struct hash_bucket *table;
if (!bits) return 0;
size = 2;
for (i=1;i<bits;i++)
size <<= 1;
mem_size = size*sizeof(struct hash_bucket);
if (!(table = (struct hash_bucket*)malloc(mem_size)))
return 0;
memset(table,0,mem_size);
ht->bits = bits;
ht->mask = size - 1;
ht->size = size;
ht->table = table;
ht->filename = filename;
if (filename)
{
if ((fh = fopen(filename,"r")))
{
char buf[512];
fgets(buf,sizeof(buf),fh);
if (!strncmp(buf,"SMHASH1",7))
{
fgets(buf,sizeof(buf),fh);
ht->data = atoi(buf);
while (fgets(buf,sizeof(buf),fh))
{
int len = strlen(buf);
char *lc;
unsigned int data;
if (len && buf[len-1] == '\n') buf[len-1] = 0;
if ((len>1) && buf[len-2] == '\r') buf[len-2]=0;
data = strtoul(buf,&lc,10);
if (lc)
{
if (isspace((unsigned char)*lc))
{
char *string;
lc++;
if ((string = mystrdup(lc)))
hash_table_insert(ht, string, data);
}
}
}
}
fclose(fh);
}
}
return 1;
}
/**************************************************************************
"Makes" the hash table empty
**************************************************************************/
void hash_table_clear(struct hash_table *ht)
{
unsigned int i, size, mem_size;
if (ht->table)
{
for (i=0;i<ht->size;i++)
{
struct hash_bucket *hb = &ht->table[i];
if (hb->entry.string) free((void*)hb->entry.string);
hb = hb->next;
while (hb)
{
struct hash_bucket *thb = hb->next;
free((void*)hb->entry.string);
free(hb);
hb = thb;
}
}
ht->data = 0;
size = ht->size;
mem_size = size*sizeof(struct hash_bucket);
memset(ht->table,0,mem_size);
}
}
/**************************************************************************
Cleanup all memory allocated by the hash table (exluding the hash table
itself)
**************************************************************************/
void hash_table_clean(struct hash_table *ht)
{
unsigned int i;
if (ht->table)
{
for (i=0;i<ht->size;i++)
{
struct hash_bucket *hb = &ht->table[i];
if (hb->entry.string) free((void*)hb->entry.string);
hb = hb->next;
while (hb)
{
struct hash_bucket *thb = hb->next;
free((void*)hb->entry.string);
free(hb);
hb = thb;
}
}
free(ht->table);
ht->table = NULL;
ht->data = 0;
}
}
/**************************************************************************
Insert a new entry into the hash table
**************************************************************************/
struct hash_entry *hash_table_insert(struct hash_table *ht, const char *string, unsigned int data)
{
unsigned int index;
struct hash_bucket *hb,*nhb;
if (!string) return NULL;
index = sdbm((const unsigned char*)string) & ht->mask;
hb = &ht->table[index];
if (!hb->entry.string)
{
hb->entry.string = string;
hb->entry.data = data;
return &hb->entry;
}
nhb = malloc(sizeof(struct hash_bucket));
if (!nhb) return NULL;
*nhb = *hb;
hb->next = nhb;
hb->entry.string = string;
hb->entry.data = data;
return &hb->entry;
}
/**************************************************************************
Lookup an entry inside the hash table
**************************************************************************/
struct hash_entry *hash_table_lookup(struct hash_table *ht, const char *string)
{
unsigned int index;
struct hash_bucket *hb;
if (!string) return NULL;
index = sdbm((const unsigned char*)string) & ht->mask;
hb = &ht->table[index];
if (!hb->entry.string) return NULL;
while (hb)
{
if (!strcmp(hb->entry.string,string)) return &hb->entry;
hb = hb->next;
}
return NULL;
}
/**************************************************************************
Callback which is called for every entry. It stores the line
**************************************************************************/
static void hash_table_store_callback(struct hash_entry *entry, void *data)
{
fprintf((FILE*)data,"%d %s\n",entry->data,entry->string);
}
/**************************************************************************
Store the hash table under the name given at hash_table_init()
**************************************************************************/
void hash_table_store(struct hash_table *ht)
{
FILE *fh;
if (!ht->filename) return;
if ((fh = fopen(ht->filename,"w")))
{
fputs("SMHASH1\n",fh);
fprintf(fh,"%d\n",ht->data);
hash_table_call_for_every_entry(ht,hash_table_store_callback, fh);
fclose(fh);
}
}
/**************************************************************************
For every entry in the hash table call the given function
**************************************************************************/
void hash_table_call_for_every_entry(struct hash_table *ht, void (*func)(struct hash_entry *entry, void *data), void *data)
{
unsigned int i;
if (!func) return;
for (i=0;i<ht->size;i++)
{
struct hash_bucket *hb = &ht->table[i];
while (hb)
{
if (hb->entry.string)
func(&hb->entry,data);
hb = hb->next;
}
}
}