-
Notifications
You must be signed in to change notification settings - Fork 30
/
hash.c
108 lines (92 loc) · 2.48 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
/*
* Copyright (c) 2001-2002 Secure Software, Inc
*
* 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.
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "hash.h"
Hash HashInit()
{
return (Hash)hash_create(515, HashCompare, NULL);
}
int HashCompare(const void *firstitem, const void *seconditem)
{
return strcmp((char *)firstitem, (char *)seconditem);
}
int HashInsert(Hash myhash, void *item, char *name)
{
hnode_t * mynode = (hnode_t *)NULL;
hnode_t * hisnode = (hnode_t *)NULL;
if (myhash == NULL)
{
fprintf(stderr, "NULL Hash object passed to HashInsert\n");
return 0;
}
mynode = hnode_create(item);
if (mynode == NULL)
{
fprintf(stderr, "Node creation failed in HashInsert\n");
return 0;
}
if ((hisnode = hash_lookup(myhash, name)) != NULL)
{
fprintf(stderr, "Function %s has multiple entries in database. New entry will be used\n", name);
hash_delete(myhash, hisnode);
}
hash_insert(myhash, mynode, name);
return 1;
}
void *HashGet(Hash myhash, char *name)
{
hnode_t * mynode = (hnode_t *)NULL;
void * mydata = (void *)NULL;
mynode = hash_lookup(myhash, name);
if (mynode != (hnode_t *)NULL)
mydata = hnode_get(mynode);
return mydata;
}
long
HashCount(Hash myhash)
{
return (long)hash_count(myhash);
}
char **
HashKeys(Hash myhash)
{
hscan_t hs;
hnode_t *hn;
long nents = 0;
char **ret = NULL;
int i = 0;
nents = HashCount(myhash);
ret = malloc((nents+1)*(sizeof(char *)));
hash_scan_begin(&hs, myhash);
while ((hn = hash_scan_next(&hs)))
{
char *tmp = hnode_getkey(hn);
ret[i++] = tmp;
}
ret[i] = NULL;
return ret;
}
void
HashFreeKeys(Hash myhash , char **keys)
{
if (keys)
free(keys);
}