forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
maphash.c
253 lines (206 loc) · 6.15 KB
/
maphash.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Implement hashTableObj class.
* Author: Sean Gillies, [email protected]
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include <ctype.h>
#include "mapserver.h"
#include "maphash.h"
static unsigned hash(const char *key)
{
unsigned hashval;
for(hashval=0; *key!='\0'; key++)
hashval = tolower(*key) + 31 * hashval;
return(hashval % MS_HASHSIZE);
}
hashTableObj *msCreateHashTable()
{
int i;
hashTableObj *table;
table = (hashTableObj *) msSmallMalloc(sizeof(hashTableObj));
table->items = (struct hashObj **) msSmallMalloc(sizeof(struct hashObj *)*MS_HASHSIZE);
for (i=0; i<MS_HASHSIZE; i++)
table->items[i] = NULL;
table->numitems = 0;
return table;
}
int initHashTable( hashTableObj *table )
{
int i;
table->items = (struct hashObj **) malloc(sizeof(struct hashObj *)*MS_HASHSIZE);
MS_CHECK_ALLOC(table->items, sizeof(struct hashObj *)*MS_HASHSIZE, MS_FAILURE);
for (i=0; i<MS_HASHSIZE; i++)
table->items[i] = NULL;
table->numitems = 0;
return MS_SUCCESS;
}
void msFreeHashTable( hashTableObj *table )
{
if( table != NULL ) {
msFreeHashItems(table);
free(table);
table = NULL;
}
}
int msHashIsEmpty( hashTableObj *table )
{
if (table->numitems == 0 )
return MS_TRUE;
else
return MS_FALSE;
}
void msFreeHashItems( hashTableObj *table )
{
int i;
struct hashObj *tp=NULL;
struct hashObj *prev_tp=NULL;
if (table) {
if(table->items) {
for (i=0; i<MS_HASHSIZE; i++) {
if (table->items[i] != NULL) {
for (tp=table->items[i]; tp!=NULL; prev_tp=tp,tp=tp->next,free(prev_tp)) {
msFree(tp->key);
msFree(tp->data);
}
}
}
free(table->items);
table->items = NULL;
} else {
msSetError(MS_HASHERR, "No items allocated.", "msFreeHashItems()");
}
} else {
msSetError(MS_HASHERR, "Can't free NULL table", "msFreeHashItems()");
}
}
struct hashObj *msInsertHashTable(hashTableObj *table,
const char *key, const char *value) {
struct hashObj *tp;
unsigned hashval;
if (!table || !key || !value) {
msSetError(MS_HASHERR, "Invalid hash table or key",
"msInsertHashTable");
return NULL;
}
for (tp=table->items[hash(key)]; tp!=NULL; tp=tp->next)
if(strcasecmp(key, tp->key) == 0)
break;
if (tp == NULL) { /* not found */
tp = (struct hashObj *) malloc(sizeof(*tp));
MS_CHECK_ALLOC(tp, sizeof(*tp), NULL);
tp->key = msStrdup(key);
hashval = hash(key);
tp->next = table->items[hashval];
table->items[hashval] = tp;
table->numitems++;
} else {
free(tp->data);
}
if ((tp->data = msStrdup(value)) == NULL)
return NULL;
return tp;
}
char *msLookupHashTable(hashTableObj *table, const char *key)
{
struct hashObj *tp;
if (!table || !key) {
return(NULL);
}
for (tp=table->items[hash(key)]; tp!=NULL; tp=tp->next)
if (strcasecmp(key, tp->key) == 0)
return(tp->data);
return NULL;
}
int msRemoveHashTable(hashTableObj *table, const char *key)
{
struct hashObj *tp;
struct hashObj *prev_tp=NULL;
int status = MS_FAILURE;
if (!table || !key) {
msSetError(MS_HASHERR, "No hash table", "msRemoveHashTable");
return MS_FAILURE;
}
tp=table->items[hash(key)];
if (!tp) {
msSetError(MS_HASHERR, "No such hash entry", "msRemoveHashTable");
return MS_FAILURE;
}
prev_tp = NULL;
while (tp != NULL) {
if (strcasecmp(key, tp->key) == 0) {
status = MS_SUCCESS;
if (prev_tp) {
prev_tp->next = tp->next;
free(tp);
break;
} else {
table->items[hash(key)] = tp->next;
free(tp);
break;
}
}
prev_tp = tp;
tp = tp->next;
}
if (status == MS_SUCCESS)
table->numitems--;
return status;
}
const char *msFirstKeyFromHashTable( hashTableObj *table )
{
int hash_index;
if (!table) {
msSetError(MS_HASHERR, "No hash table", "msFirstKeyFromHashTable");
return NULL;
}
for (hash_index = 0; hash_index < MS_HASHSIZE; hash_index++ ) {
if (table->items[hash_index] != NULL )
return table->items[hash_index]->key;
}
return NULL;
}
const char *msNextKeyFromHashTable( hashTableObj *table, const char *lastKey )
{
int hash_index;
struct hashObj *link;
if (!table) {
msSetError(MS_HASHERR, "No hash table", "msNextKeyFromHashTable");
return NULL;
}
if ( lastKey == NULL )
return msFirstKeyFromHashTable( table );
hash_index = hash(lastKey);
for ( link = table->items[hash_index];
link != NULL && strcasecmp(lastKey,link->key) != 0;
link = link->next ) {}
if ( link != NULL && link->next != NULL )
return link->next->key;
while ( ++hash_index < MS_HASHSIZE ) {
if ( table->items[hash_index] != NULL )
return table->items[hash_index]->key;
}
return NULL;
}