Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wshash.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wshash.c
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * A mapping from null-terminated strings to `void *' pointers.
67  *
68  */
69 
70 #include "wsint.h"
71 #include "wshash.h"
72 
73 /********************* Types and definitions ****************************/
74 
75 /* The size of the hash table. */
76 #define WS_HASH_TABLE_SIZE 256
77 
78 /* A hash item. */
80 {
82  char *name;
83  void *data;
84 };
85 
86 typedef struct WsHashItemRec WsHashItem;
87 
88 /* The hash object. */
89 struct WsHashRec
90 {
94 };
95 
96 /********************* Prototypes for static functions ******************/
97 
98 /* Hash function to count the hash value of string `string'. */
99 static size_t count_hash(const char *string);
100 
101 /********************* Global functions *********************************/
102 
104 {
105  WsHashPtr hash = ws_calloc(1, sizeof(*hash));
106 
107  if (hash) {
108  hash->destructor = destructor;
109  hash->destructor_context = context;
110  }
111 
112  return hash;
113 }
114 
115 
117 {
118  if (hash == NULL)
119  return;
120 
121  ws_hash_clear(hash);
122  ws_free(hash);
123 }
124 
125 
126 WsBool ws_hash_put(WsHashPtr hash, const char *name, void *data)
127 {
128  WsHashItem *i;
129  size_t h = count_hash(name);
130 
131  for (i = hash->items[h]; i; i = i->next) {
132  if (strcmp(i->name, name) == 0) {
133  /* Found it. */
134 
135  /* Destroy the old item */
136  if (hash->destructor)
137  (*hash->destructor)(i->data, hash->destructor_context);
138 
139  i->data = data;
140 
141  return WS_FALSE;
142  }
143  }
144 
145  /* Must create a new mapping. */
146  i = ws_calloc(1, sizeof(*i));
147 
148  if (i == NULL)
149  return WS_FALSE;
150 
151  i->name = ws_strdup(name);
152  if (i->name == NULL) {
153  ws_free(i);
154  return WS_FALSE;
155  }
156 
157  i->data = data;
158 
159  /* Link it to our hash. */
160  i->next = hash->items[h];
161  hash->items[h] = i;
162 
163  return WS_TRUE;
164 }
165 
166 
167 void *ws_hash_get(WsHashPtr hash, const char *name)
168 {
169  WsHashItem *i;
170  size_t h = count_hash(name);
171 
172  for (i = hash->items[h]; i; i = i->next)
173  if (strcmp(i->name, name) == 0)
174  return i->data;
175 
176  return NULL;
177 }
178 
179 
181 {
182  WsHashItem *i, *n;
183  size_t j;
184 
185  for (j = 0; j < WS_HASH_TABLE_SIZE; j++) {
186  for (i = hash->items[j]; i; i = n) {
187  n = i->next;
188  if (hash->destructor)
189  (*hash->destructor)(i->data, hash->destructor_context);
190 
191  ws_free(i->name);
192  ws_free(i);
193  }
194  hash->items[j] = NULL;
195  }
196 }
197 
198 /********************* Static functions *********************************/
199 
200 static size_t count_hash(const char *string)
201 {
202  size_t val = 0;
203  int i;
204 
205  for (i = 0; string[i]; i++) {
206  val <<= 3;
207  val ^= string[i];
208  val ^= (val & 0xff00) >> 5;
209  val ^= (val & 0xff0000) >> 16;
210  }
211 
212  return val % WS_HASH_TABLE_SIZE;
213 }
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83
Definition: wsint.h:131
void ws_hash_clear(WsHashPtr hash)
Definition: wshash.c:180
Definition: parse.c:65
void ws_free(void *ptr)
Definition: wsalloc.c:139
void ws_hash_destroy(WsHashPtr hash)
Definition: wshash.c:116
#define WS_HASH_TABLE_SIZE
Definition: wshash.c:76
WsHashItemDestructor destructor
Definition: wshash.c:92
struct WsHashItemRec * next
Definition: wshash.c:81
char * name
Definition: smsc_cimd2.c:212
void(* WsHashItemDestructor)(void *item, void *context)
Definition: wshash.h:82
WsBool
Definition: wsint.h:128
WsHashItem * items[WS_HASH_TABLE_SIZE]
Definition: wshash.c:91
void * ws_strdup(const char *str)
Definition: wsalloc.c:119
void * data
Definition: wshash.c:83
void * destructor_context
Definition: wshash.c:93
static size_t count_hash(const char *string)
Definition: wshash.c:200
char * name
Definition: wshash.c:82
WsBool ws_hash_put(WsHashPtr hash, const char *name, void *data)
Definition: wshash.c:126
void * ws_hash_get(WsHashPtr hash, const char *name)
Definition: wshash.c:167
WsHashPtr ws_hash_create(WsHashItemDestructor destructor, void *context)
Definition: wshash.c:103
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.