fix possible crash on user deletion
[srvx.git] / src / dict.h
1 /* dict.h - Abstract dictionary type
2  * Copyright 2000-2004 srvx Development Team
3  *
4  * This file is part of srvx.
5  *
6  * srvx is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with srvx; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
19  */
20
21 #if !defined(DICT_H)
22 #define DICT_H
23
24 /* helper types */
25 typedef void (*free_f)(void*);
26 typedef int (*dict_iterator_f)(const char *key, void *data, void *extra);
27
28 /* exposed ONLY for the iteration macros; if you use these, DIE */
29 struct dict_node {
30     const char *key;
31     void *data;
32     struct dict_node *l, *r, *prev, *next;
33 };
34
35 struct dict {
36     free_f free_keys, free_data;
37     struct dict_node *root, *first, *last;
38     unsigned int count;
39 };
40
41 /* "published" API */
42 typedef struct dict *dict_t;
43 typedef struct dict_node *dict_iterator_t;
44
45 #define dict_first(DICT) ((DICT) ? (DICT)->first : NULL)
46 #define iter_key(ITER) ((ITER)->key)
47 #define iter_data(ITER) ((ITER)->data)
48 #define iter_next(ITER) ((ITER)->next)
49
50 dict_t dict_new(void);
51 /* dict_foreach returns key of node causing halt (non-zero return from
52  * iterator function) */
53 const char* dict_foreach(dict_t dict, dict_iterator_f it, void *extra);
54 void dict_insert(dict_t dict, const char *key, void *data);
55 void dict_set_free_keys(dict_t dict, free_f free_keys);
56 void dict_set_free_data(dict_t dict, free_f free_data);
57 unsigned int dict_size(dict_t dict);
58 /* if present!=NULL, then *present=1 iff node was found (if node is
59  * not found, return value is NULL, which may be a valid datum) */
60 void* dict_find(dict_t dict, const char *key, int *present);
61 int dict_remove2(dict_t dict, const char *key, int no_dispose);
62 #define dict_remove(DICT, KEY) dict_remove2(DICT, KEY, 0)
63 char *dict_sanity_check(dict_t dict);
64 void dict_delete(dict_t dict);
65
66 #endif /* !defined(DICT_H) */