property.c
6.62 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <property0.h>
static int property_find __P ((list_t, const char *, struct property_data **));
static int property_add __P ((property_t, const char *, const char *,
int (*_set_value)
__P ((property_t, const char *, const char *)),
int (*_get_value)
__P ((property_t, const char *, char *,
size_t, size_t *))));
static size_t property_hash __P ((const char *));
int
property_create (property_t *pp, void *owner)
{
property_t prop;
if (pp == NULL)
return EINVAL;
prop = calloc (1, sizeof (*prop));
if (prop == NULL)
return ENOMEM;
prop->owner = owner;
*pp = prop;
return 0;
}
void
property_destroy (property_t *pp, void *owner)
{
if (pp && *pp)
{
property_t prop = *pp;
if (prop->owner == owner)
{
/* Destroy the list and is properties. */
if (prop->list)
{
struct property_data *pd = NULL;
iterator_t iterator = NULL;
iterator_create (&iterator, prop->list);
for (iterator_first (iterator); !iterator_is_done (iterator);
iterator_next (iterator))
{
iterator_current (iterator, (void **)&pd);
if (pd)
{
if (pd->key)
free (pd->key);
if (pd->value)
free (pd->value);
free (pd);
}
}
iterator_destroy (&iterator);
list_destroy (&(prop->list));
}
free (prop);
}
*pp = NULL;
}
}
void *
property_get_owner (property_t prop)
{
return (prop == NULL) ? NULL : prop->owner;
}
int
property_add_defaults (property_t prop, const char *key, const char *value,
int (*_set_value) __P ((property_t, const char *,
const char *)),
int (*_get_value) __P ((property_t, const char *,
char *, size_t, size_t *)),
void *owner)
{
if (prop == NULL)
return EINVAL;
if (prop->owner != owner)
return EACCES;
return property_add (prop, key, value, _set_value, _get_value);
}
int
property_set_value (property_t prop, const char *key, const char *value)
{
if (prop == NULL)
return EINVAL;
return property_add (prop, key, value, NULL, NULL);
}
int
property_get_value (property_t prop, const char *key, char *buffer,
size_t buflen, size_t *n)
{
struct property_data *pd = NULL;
int status;
size_t len;
if (prop == NULL)
return EINVAL;
status = property_find (prop->list, key, &pd);
if (status != 0)
return status;
if (pd == NULL)
return ENOENT;
if (pd->_get_value)
return pd->_get_value (prop, key, buffer, buflen, n);
len = (pd->value) ? strlen (pd->value) : 0;
if (buffer && buflen != 0)
{
buflen--;
len = (buflen < len) ? buflen : len;
strncpy (buffer, pd->value, len)[len] = '\0';
}
if (n)
*n = len;
return 0;
}
#if 0
int
property_load (property_t prop, stream_t stream)
{
size_t n = 0;
off_t off = 0;
int status;
int buflen = 512;
char *buf = calloc (buflen, sizeof (*buf));
if (buf == NULL)
return ENOMEM;
while ((status = stream_readline (stream, buf, buflen, off, &n)) == 0
&& n > 0)
{
char *sep;
if (buf[n] != '\n')
{
char *tmp;
buflen *= 2;
tmp = realloc (buf, buflen);
if (tmp == NULL)
{
free (buf);
return ENOMEM;
}
buf = tmp;
continue;
}
sep = strchr (buf, '=');
if (sep)
{
*sep++ = '\0';
property_set_value (prop, buf, sep);
}
else
property_set (prop, buf);
}
return 0;
}
#endif
int
property_set (property_t prop, const char *k)
{
if (!property_is_set (prop, k))
return property_set_value (prop, k, "1");
return 0;
}
int
property_unset (property_t prop, const char *k)
{
if (property_is_set (prop, k))
return property_set_value (prop, k, NULL);
return 0;
}
int
property_is_set (property_t prop, const char *k)
{
size_t n = 0;
property_get_value (prop, k, NULL, 0, &n);
return (n != 0);
}
/* Taking from an article in Dr Dobbs. */
static size_t
property_hash (const char *s)
{
size_t hashval;
for (hashval = 0; *s != '\0' ; s++)
{
hashval += (unsigned)*s ;
hashval += (hashval << 10);
hashval ^= (hashval >> 6) ;
}
hashval += (hashval << 3);
hashval ^= (hashval >> 11);
hashval += (hashval << 15);
return hashval;
}
static int
property_find (list_t list, const char *key, struct property_data **p)
{
int status;
size_t h;
struct property_data *pd = NULL;
iterator_t iterator;
status = iterator_create (&iterator, list);
if (status != 0)
return status;
h = property_hash (key);
for (iterator_first (iterator); !iterator_is_done (iterator);
iterator_next (iterator))
{
iterator_current (iterator, (void **)&pd);
if (pd)
{
if (pd->hash == h)
if (pd->key && strcasecmp (pd->key, key) == 0)
break;
}
pd = NULL;
}
iterator_destroy (&iterator);
*p = pd;
return 0;
}
static int
property_add (property_t prop, const char *key, const char *value,
int (*_set_value) __P ((property_t, const char *, const char *)),
int (*_get_value) __P ((property_t, const char *, char *,
size_t, size_t *)))
{
struct property_data *pd = NULL;
int status;
if (key == NULL || *key == '\0')
return EINVAL;
if (prop->list == NULL)
{
status = list_create (&(prop->list));
if (status != 0)
return status;
}
status = property_find (prop->list, key, &pd);
if (status != 0)
return status;
/* None find create a new one. */
if (pd == NULL)
{
pd = calloc (1, sizeof (*pd));
if (pd == NULL)
return ENOMEM;
pd->hash = property_hash (key);
list_append (prop->list, (void *)pd);
}
if (pd->key == NULL)
pd->key = strdup (key);
if (pd->value)
free (pd->value);
pd->value = (value) ? strdup (value) : NULL;
pd->_set_value = _set_value;
pd->_get_value = _get_value;
return 0;
}