1 #include <stdlib.h>
2 #include <string.h>
3 #include <sys/types.h>
4
5 #ifdef DMALLOC
6 #include <dmalloc.h>
7 #endif
8
9 #include "hash.h"
10
hash_create(hash * table)11 void hash_create(hash *table) {
12 Tcl_InitHashTable(table,TCL_STRING_KEYS);
13 }
14
hash_destroy(hash * table)15 void hash_destroy(hash *table) {
16 Tcl_HashEntry *entry;
17 Tcl_HashSearch ptr;
18 if((entry=Tcl_FirstHashEntry(table,&ptr))) {
19 free(Tcl_GetHashValue(entry));
20 while((entry=Tcl_NextHashEntry(&ptr))) {
21 free(Tcl_GetHashValue(entry));
22 }
23 }
24 Tcl_DeleteHashTable(table);
25 }
26
hash_set(hash * table,const char * key,const char * value)27 int hash_set(hash *table, const char *key, const char *value) {
28 Tcl_HashEntry *entry;
29 char *data;
30 int newentry;
31 if(!(data=strdup(value))) {
32 return -1;
33 } else {
34 entry=Tcl_CreateHashEntry(table,key,&newentry);
35 if(!newentry) free(Tcl_GetHashValue(entry));
36 Tcl_SetHashValue(entry,data);
37 return 0;
38 }
39 }
40
hash_get(hash * table,const char * key)41 char *hash_get(hash *table, const char *key) {
42 Tcl_HashEntry *entry;
43 if(!(entry=Tcl_FindHashEntry(table,key))) {
44 return 0;
45 } else {
46 return((char *)Tcl_GetHashValue(entry));
47 }
48 }
49
hash_delete(hash * table,char * key)50 int hash_delete(hash *table, char *key) {
51 Tcl_HashEntry *entry;
52 if(!(entry=Tcl_FindHashEntry(table,key))) {
53 return -1;
54 } else {
55 free(Tcl_GetHashValue(entry));
56 Tcl_DeleteHashEntry(entry);
57 return 0;
58 }
59 }
60