manager can query and list IKE_SA status (no layout yet)
[strongswan.git] / src / manager / lib / dict.c
1 /**
2 * @file dict.c
3 *
4 * @brief Implementation of dict_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "dict.h"
24
25 #include <utils/linked_list.h>
26
27
28 typedef struct private_dict_t private_dict_t;
29
30 /**
31 * private data of dict
32 */
33 struct private_dict_t {
34
35 /**
36 * public functions
37 */
38 dict_t public;
39
40 /**
41 * baaah, we really should have a hashtable for this
42 */
43 linked_list_t *list;
44
45 /**
46 * key comparator function
47 */
48 bool(*key_comparator)(void*,void*);
49
50 /**
51 * destructor function for key
52 */
53 void(*key_destructor)(void*);
54
55 /**
56 * destructor function for value
57 */
58 void(*value_destructor)(void*);
59 };
60
61 /**
62 * key value pair to store entries
63 */
64 typedef struct {
65 void *key;
66 void *value;
67 } key_value_t;
68
69 /**
70 * Implementation of dict_t.get.
71 */
72 static void* get(private_dict_t *this, void *key)
73 {
74 key_value_t *kv;
75 iterator_t *iterator;
76 void *value = NULL;
77
78 iterator = this->list->create_iterator(this->list, TRUE);
79 while (iterator->iterate(iterator, (void**)&kv))
80 {
81 if (this->key_comparator(kv->key, key))
82 {
83 value = kv->value;
84 break;
85 }
86 }
87 iterator->destroy(iterator);
88 return value;
89 }
90 /**
91 * Implementation of dict_t.set.
92 */
93 static void set(private_dict_t *this, void *key, void *value)
94 {
95 /* we don't overwrite, just prepend */
96 key_value_t *kv = malloc_thing(key_value_t);
97 kv->key = key;
98 kv->value = value;
99 this->list->insert_first(this->list, kv);
100 }
101
102
103 /**
104 * comparator for strings
105 */
106 bool dict_streq(void *a, void *b)
107 {
108 return streq(a, b);
109 }
110
111 /**
112 * Implementation of dict_t.destroy
113 */
114 static void destroy(private_dict_t *this)
115 {
116 key_value_t *kv;
117
118 while (this->list->remove_last(this->list, (void**)&kv) == SUCCESS)
119 {
120 if (this->key_destructor)
121 {
122 this->key_destructor(kv->key);
123 }
124 if (this->value_destructor)
125 {
126 this->value_destructor(kv->value);
127 }
128 free(kv);
129 }
130 this->list->destroy(this->list);
131 free(this);
132 }
133
134 /*
135 * see header file
136 */
137 dict_t *dict_create(bool(*key_comparator)(void*,void*),
138 void(*key_destructor)(void*),
139 void(*value_destructor)(void*))
140 {
141 private_dict_t *this = malloc_thing(private_dict_t);
142
143 this->public.set = (void(*)(dict_t*, void *key, void *value))set;
144 this->public.get = (void*(*)(dict_t*, void *key))get;
145 this->public.destroy = (void(*)(dict_t*))destroy;
146
147 this->list = linked_list_create();
148 this->key_comparator = key_comparator;
149 this->key_destructor = key_destructor;
150 this->value_destructor = value_destructor;
151
152 return &this->public;
153 }
154