Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
dict.h File Reference

Go to the source code of this file.

Typedefs

typedef struct Dict Dict
 

Functions

Dictdict_create (long size_hint, void(*destroy_value)(void *))
 
void dict_destroy (Dict *dict)
 
void dict_put (Dict *dict, Octstr *key, void *value)
 
int dict_put_once (Dict *dict, Octstr *key, void *value)
 
void * dict_get (Dict *dict, Octstr *key)
 
void * dict_remove (Dict *dict, Octstr *key)
 
long dict_key_count (Dict *dict)
 
Listdict_keys (Dict *dict)
 
Dictdict_duplicate (Dict *dict, void *(*duplicate_value)(void *))
 
long dict_traverse (Dict *dict, void(*func)(Octstr *, void *, void *), void *data)
 
long dict_traverse_sorted (Dict *dict, int(*cmp)(const void *, const void *), void(*func)(Octstr *, void *, void *), void *data)
 

Typedef Documentation

typedef struct Dict Dict

Definition at line 70 of file dict.h.

Function Documentation

Dict* dict_create ( long  size_hint,
void(*)(void *)  destroy_value 
)

Definition at line 192 of file dict.c.

References Dict::destroy_value, Dict::key_count, Dict::lock, mutex_create, Dict::size, and Dict::tab.

Referenced by brunet_parse_body(), cfg_create(), clickatell_parse_body(), concat_handling_init(), conn_pool_init(), create_group(), dict_duplicate(), init_reroute(), main(), meta_data_merge(), meta_data_set_value(), meta_data_unpack(), parse_value_element(), port_init(), pushusers_create(), radius_acct_init(), run_smsbox(), server(), smasi_create(), smpp_create(), smpp_pdu_init(), smsbox_start(), store_file_init(), urltrans_create(), wap_map_add_user(), wap_push_ppg_init(), wap_push_ppg_pushuser_list_add(), wml_init(), and xmlrpc_create_struct_value().

193 {
194  Dict *dict;
195  long i;
196 
197  dict = gw_malloc(sizeof(*dict));
198 
199  /*
200  * Hash tables tend to work well until they are fill to about 50%.
201  */
202  dict->size = size_hint * 2;
203 
204  dict->tab = gw_malloc(sizeof(dict->tab[0]) * dict->size);
205  for (i = 0; i < dict->size; ++i)
206  dict->tab[i] = NULL;
207  dict->lock = mutex_create();
208  dict->destroy_value = destroy_value;
209  dict->key_count = 0;
210 
211  return dict;
212 }
Mutex * lock
Definition: dict.c:121
#define mutex_create()
Definition: thread.h:96
List ** tab
Definition: dict.c:117
long key_count
Definition: dict.c:119
Definition: dict.c:116
long size
Definition: dict.c:118
void(* destroy_value)(void *)
Definition: dict.c:120
void dict_destroy ( Dict dict)

Definition at line 215 of file dict.c.

References Dict::destroy_value, gwlist_destroy(), gwlist_extract_first(), item_destroy(), Dict::lock, mutex_destroy(), Dict::size, Dict::tab, and Item::value.

Referenced by brunet_parse_reply(), cfg_destroy(), clickatell_parse_reply(), concat_handling_cleanup(), conn_pool_shutdown(), destroy_group(), main(), meta_data_destroy(), meta_data_set_values(), msg_to_pdu(), port_shutdown(), radius_acct_shutdown(), run_smsbox(), smasi_destroy(), smpp_destroy(), smpp_pdu_init(), smpp_pdu_shutdown(), smsboxc_run(), smscconn_destroy(), smscconn_reconfig(), store_dumper(), urltrans_destroy(), wap_map_user_destroy(), wap_push_ppg_pushuser_list_destroy(), wap_push_ppg_shutdown(), wml_shutdown(), and xmlrpc_value_destroy().

216 {
217  long i;
218  Item *p;
219 
220  if (dict == NULL)
221  return;
222 
223  for (i = 0; i < dict->size; ++i) {
224  if (dict->tab[i] == NULL)
225  continue;
226 
227  while ((p = gwlist_extract_first(dict->tab[i])) != NULL) {
228  if (dict->destroy_value != NULL)
229  dict->destroy_value(p->value);
230  item_destroy(p);
231  }
232  gwlist_destroy(dict->tab[i], NULL);
233  }
234  mutex_destroy(dict->lock);
235  gw_free(dict->tab);
236  gw_free(dict);
237 }
Mutex * lock
Definition: dict.c:121
void * value
Definition: dict.c:77
List ** tab
Definition: dict.c:117
void * gwlist_extract_first(List *list)
Definition: list.c:305
long size
Definition: dict.c:118
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
static void item_destroy(void *item)
Definition: dict.c:91
void(* destroy_value)(void *)
Definition: dict.c:120
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
Dict* dict_duplicate ( Dict dict,
void *(*)(void *)  duplicate_value 
)

Definition at line 370 of file dict.c.

References Dict::destroy_value, dict_create(), dict_put(), gwlist_get(), gwlist_len(), Item::key, lock(), Dict::size, Dict::tab, unlock(), and Item::value.

371 {
372  Item *item;
373  long i, j;
374  Dict *dup;
375 
376  lock(dict);
377  dup = dict_create(dict->size, dict->destroy_value);
378  for (i = 0; i < dict->size; ++i) {
379  if (dict->tab[i] == NULL)
380  continue;
381  for (j = 0; j < gwlist_len(dict->tab[i]); ++j) {
382  item = gwlist_get(dict->tab[i], j);
383  dict_put(dup, item->key, duplicate_value(item->value));
384  }
385  }
386  unlock(dict);
387 
388  return dup;
389 }
Dict * dict_create(long size_hint, void(*destroy_value)(void *))
Definition: dict.c:192
void dict_put(Dict *dict, Octstr *key, void *value)
Definition: dict.c:240
Octstr * key
Definition: dict.c:76
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
static void unlock(Dict *dict)
Definition: dict.c:131
void * value
Definition: dict.c:77
List ** tab
Definition: dict.c:117
Definition: dict.c:116
long size
Definition: dict.c:118
static void lock(Dict *dict)
Definition: dict.c:125
void(* destroy_value)(void *)
Definition: dict.c:120
void* dict_get ( Dict dict,
Octstr key 
)

Definition at line 286 of file dict.c.

References gwlist_search(), item_has_key(), key_to_index(), lock(), Dict::tab, unlock(), and Item::value.

Referenced by add_group(), boxc_receiver(), brunet_parse_reply(), cfg_get_group_checksum(), cfg_get_multi_group(), cfg_get_real(), cfg_get_single_group(), clickatell_parse_reply(), concat_handling_check_and_handle(), concat_handling_clear_old_parts(), conn_pool_get(), do_dump(), do_queue_cleanup(), main(), meta_data_merge(), meta_data_pack(), meta_data_set_value(), meta_data_set_values(), oneuser_add(), parse_attribute(), parse_element(), port_add(), port_get_fdset(), port_get_request(), port_put_request(), port_set_timeout(), radius_acct_get_msisdn(), radius_get_attribute(), route_incoming_to_boxc(), route_incoming_to_smsc(), run_smsbox(), send_push_response(), smpp_pdu_init(), smpp_tlv_get_by_name(), smpp_tlv_get_by_tag(), store_file_for_each_message(), update_table(), update_tables(), urltrans_add_one(), urltrans_find_service(), user_find_by_username(), wap_map_user(), wap_push_ppg_pushuser_authenticate(), xmlrpc_get_member(), and xmlrpc_print_struct().

287 {
288  long i;
289  Item *p;
290  void *value;
291 
292  lock(dict);
293  i = key_to_index(dict, key);
294  if (dict->tab[i] == NULL)
295  p = NULL;
296  else
297  p = gwlist_search(dict->tab[i], key, item_has_key);
298  if (p == NULL)
299  value = NULL;
300  else
301  value = p->value;
302  unlock(dict);
303  return value;
304 }
void * gwlist_search(List *list, void *pattern, int(*cmp)(void *, void *))
Definition: list.c:486
static void unlock(Dict *dict)
Definition: dict.c:131
void * value
Definition: dict.c:77
List ** tab
Definition: dict.c:117
static int item_has_key(void *item, void *key)
Definition: dict.c:101
static long key_to_index(Dict *dict, Octstr *key)
Definition: dict.c:137
static void lock(Dict *dict)
Definition: dict.c:125
long dict_key_count ( Dict dict)

Definition at line 335 of file dict.c.

References Dict::key_count, lock(), and unlock().

Referenced by boxc_status(), main(), proxy_thread(), run_smsbox(), server(), store_file_dump(), store_file_load(), store_file_messages(), xmlrpc_count_members(), and xmlrpc_print_struct().

336 {
337  long result;
338 
339  lock(dict);
340  result = dict->key_count;
341  unlock(dict);
342 
343  return result;
344 }
static void unlock(Dict *dict)
Definition: dict.c:131
long key_count
Definition: dict.c:119
static void lock(Dict *dict)
Definition: dict.c:125
List* dict_keys ( Dict dict)

Definition at line 347 of file dict.c.

References gwlist_append(), gwlist_create, gwlist_get(), gwlist_len(), Item::key, lock(), octstr_duplicate, Dict::size, Dict::tab, and unlock().

Referenced by add_group(), cfg_dump(), cfg_get_group_checksum(), concat_handling_clear_old_parts(), do_dump(), do_queue_cleanup(), grp_dump(), io_thread(), main(), meta_data_merge(), meta_data_pack(), meta_data_set_values(), run_smsbox(), store_file_for_each_message(), store_file_load(), and xmlrpc_print_struct().

348 {
349  List *list;
350  Item *item;
351  long i, j;
352 
353  list = gwlist_create();
354 
355  lock(dict);
356  for (i = 0; i < dict->size; ++i) {
357  if (dict->tab[i] == NULL)
358  continue;
359  for (j = 0; j < gwlist_len(dict->tab[i]); ++j) {
360  item = gwlist_get(dict->tab[i], j);
361  gwlist_append(list, octstr_duplicate(item->key));
362  }
363  }
364  unlock(dict);
365 
366  return list;
367 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
Octstr * key
Definition: dict.c:76
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
static void unlock(Dict *dict)
Definition: dict.c:131
List ** tab
Definition: dict.c:117
#define octstr_duplicate(ostr)
Definition: octstr.h:187
long size
Definition: dict.c:118
#define gwlist_create()
Definition: list.h:136
static void lock(Dict *dict)
Definition: dict.c:125
Definition: list.c:102
void dict_put ( Dict dict,
Octstr key,
void *  value 
)

Definition at line 240 of file dict.c.

References Dict::destroy_value, dict_remove(), gwlist_append(), gwlist_create, gwlist_search(), item_create(), item_has_key(), Dict::key_count, key_to_index(), lock(), Dict::tab, unlock(), and Item::value.

Referenced by add_group(), attr_dict_construct(), boxc_sent_push(), brunet_parse_body(), cfg_set(), clickatell_parse_body(), concat_handling_check_and_handle(), concat_handling_clear_old_parts(), dict_duplicate(), main(), meta_data_merge(), meta_data_set_value(), meta_data_unpack(), oneuser_add(), pap_request_thread(), port_add(), send_messages(), smpp_pdu_init(), smsbox_req_handle(), smsbox_req_sendota(), smsbox_sendota_post(), store_to_dict(), update_table(), update_tables(), urltrans_add_one(), wap_map_add_user(), wap_push_ppg_pushuser_authenticate(), and wml_init().

241 {
242  long i;
243  Item *p;
244 
245  if (value == NULL) {
246  value = dict_remove(dict, key);
247  if (dict->destroy_value != NULL)
248  dict->destroy_value(value);
249  return;
250  }
251 
252  lock(dict);
253  i = key_to_index(dict, key);
254  if (dict->tab[i] == NULL) {
255  dict->tab[i] = gwlist_create();
256  p = NULL;
257  } else
258  p = gwlist_search(dict->tab[i], key, item_has_key);
259  if (p == NULL) {
260  p = item_create(key, value);
261  gwlist_append(dict->tab[i], p);
262  dict->key_count++;
263  } else {
264  if (dict->destroy_value != NULL)
265  dict->destroy_value(p->value);
266  p->value = value;
267  }
268  unlock(dict);
269 }
void * gwlist_search(List *list, void *pattern, int(*cmp)(void *, void *))
Definition: list.c:486
static Item * item_create(Octstr *key, void *value)
Definition: dict.c:81
void gwlist_append(List *list, void *item)
Definition: list.c:179
static void unlock(Dict *dict)
Definition: dict.c:131
void * value
Definition: dict.c:77
List ** tab
Definition: dict.c:117
void * dict_remove(Dict *dict, Octstr *key)
Definition: dict.c:307
long key_count
Definition: dict.c:119
static int item_has_key(void *item, void *key)
Definition: dict.c:101
static long key_to_index(Dict *dict, Octstr *key)
Definition: dict.c:137
#define gwlist_create()
Definition: list.h:136
static void lock(Dict *dict)
Definition: dict.c:125
void(* destroy_value)(void *)
Definition: dict.c:120
int dict_put_once ( Dict dict,
Octstr key,
void *  value 
)

Definition at line 271 of file dict.c.

References dict_put_true(), and handle_null_value().

Referenced by boxc_receiver(), init_reroute(), init_smsbox_routes(), meta_data_merge(), meta_data_set_values(), pap_request_thread(), parse_struct_element(), smpp_pdu_init(), and xmlrpc_add_member().

272 {
273  int ret;
274 
275  ret = 1;
276  if (handle_null_value(dict, key, value))
277  return 1;
278  if (dict_put_true(dict, key, value)) {
279  ret = 1;
280  } else {
281  ret = 0;
282  }
283  return ret;
284 }
static int dict_put_true(Dict *dict, Octstr *key, void *value)
Definition: dict.c:154
static int handle_null_value(Dict *dict, Octstr *key, void *value)
Definition: dict.c:142
void* dict_remove ( Dict dict,
Octstr key 
)

Definition at line 307 of file dict.c.

References gw_assert(), gwlist_destroy(), gwlist_extract_matching(), gwlist_get(), gwlist_len(), item_destroy(), item_has_key(), Dict::key_count, key_to_index(), lock(), Dict::tab, unlock(), and Item::value.

Referenced by boxc_sent_pop(), concat_handling_clear_old_parts(), delayed_http_reply(), dict_put(), do_queue_cleanup(), handle_null_value(), handle_pdu(), io_thread(), meta_data_get_value(), port_remove(), run_smsbox(), send_push_response(), smsbox_req_handle(), smsbox_req_sendota(), smsbox_sendota_post(), store_file_load(), store_to_dict(), update_table(), update_tables(), and wap_push_ppg_pushuser_authenticate().

308 {
309  long i;
310  Item *p;
311  void *value;
312  List *list;
313 
314  lock(dict);
315  i = key_to_index(dict, key);
316  if (dict->tab[i] == NULL)
317  list = NULL;
318  else
319  list = gwlist_extract_matching(dict->tab[i], key, item_has_key);
320  gw_assert(list == NULL || gwlist_len(list) == 1);
321  if (list == NULL)
322  value = NULL;
323  else {
324  p = gwlist_get(list, 0);
325  gwlist_destroy(list, NULL);
326  value = p->value;
327  item_destroy(p);
328  dict->key_count--;
329  }
330  unlock(dict);
331  return value;
332 }
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
static void unlock(Dict *dict)
Definition: dict.c:131
List * gwlist_extract_matching(List *list, void *pat, gwlist_item_matches_t *cmp)
Definition: list.c:322
void * value
Definition: dict.c:77
List ** tab
Definition: dict.c:117
long key_count
Definition: dict.c:119
static int item_has_key(void *item, void *key)
Definition: dict.c:101
static long key_to_index(Dict *dict, Octstr *key)
Definition: dict.c:137
gw_assert(wtls_machine->packet_to_send!=NULL)
static void item_destroy(void *item)
Definition: dict.c:91
static void lock(Dict *dict)
Definition: dict.c:125
Definition: list.c:102
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
long dict_traverse ( Dict dict,
void(*)(Octstr *, void *, void *)  func,
void *  data 
)

Definition at line 392 of file dict.c.

References gwlist_get(), gwlist_len(), Item::key, lock(), Dict::size, Dict::tab, unlock(), and Item::value.

393 {
394  Item *item;
395  long i, j, r = 0;
396 
397  lock(dict);
398  for (i = 0; i < dict->size; ++i) {
399  if (dict->tab[i] == NULL)
400  continue;
401  for (j = 0; j < gwlist_len(dict->tab[i]); ++j) {
402  item = gwlist_get(dict->tab[i], j);
403  func(item->key, item->value, data);
404  r++;
405  }
406  }
407  unlock(dict);
408 
409  return r;
410 }
Octstr * key
Definition: dict.c:76
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
static void unlock(Dict *dict)
Definition: dict.c:131
void * value
Definition: dict.c:77
List ** tab
Definition: dict.c:117
long size
Definition: dict.c:118
static void lock(Dict *dict)
Definition: dict.c:125
long dict_traverse_sorted ( Dict dict,
int(*)(const void *, const void *)  cmp,
void(*)(Octstr *, void *, void *)  func,
void *  data 
)

Definition at line 413 of file dict.c.

References gwlist_append(), gwlist_consume(), gwlist_create, gwlist_destroy(), gwlist_get(), gwlist_len(), gwlist_sort(), Item::key, lock(), Dict::size, Dict::tab, unlock(), and Item::value.

415 {
416  Item *item;
417  long i, j, r = 0;
418  List *l;
419 
420  l = gwlist_create();
421  lock(dict);
422 
423  /* We need to aggregate a list of all item elements first. */
424  for (i = 0; i < dict->size; ++i) {
425  if (dict->tab[i] == NULL)
426  continue;
427  for (j = 0; j < gwlist_len(dict->tab[i]); ++j) {
428  gwlist_append(l, gwlist_get(dict->tab[i], j));
429  }
430  }
431 
432  /* Now we can sort the list. */
433  gwlist_sort(l, cmp);
434 
435  /* And traverse the list. */
436  r = gwlist_len(l);
437  while ((item = gwlist_consume(l)) != NULL) {
438  func(item->key, item->value, data);
439  }
440 
441  unlock(dict);
442  gwlist_destroy(l, NULL);
443 
444  return r;
445 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
Octstr * key
Definition: dict.c:76
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
void gwlist_sort(List *list, int(*cmp)(const void *, const void *))
Definition: list.c:576
static void unlock(Dict *dict)
Definition: dict.c:131
void * value
Definition: dict.c:77
List ** tab
Definition: dict.c:117
long size
Definition: dict.c:118
void * gwlist_consume(List *list)
Definition: list.c:427
#define gwlist_create()
Definition: list.h:136
static void lock(Dict *dict)
Definition: dict.c:125
Definition: list.c:102
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.