summaryrefslogtreecommitdiffstats
path: root/src/proplist.c
blob: 0eb26db0cfe25370ad27b9e94a6e039e7db56cc6 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/***
  This file is part of libcanberra.

  Copyright 2008 Lennart Poettering

  libcanberra is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation, either version 2.1 of the
  License, or (at your option) any later version.

  libcanberra 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with libcanberra. If not, see
  <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdarg.h>

#include "canberra.h"
#include "proplist.h"
#include "macro.h"
#include "malloc.h"

static unsigned calc_hash(const char *c) {
    unsigned hash = 0;

    for (; *c; c++)
        hash = 31 * hash + (unsigned) *c;

    return hash;
}

/**
 * ca_proplist_create:
 * @p: A pointer where to fill in a pointer for the new property list.
 *
 * Allocate a new empty property list.
 *
 * Returns: 0 on success, negative error code on error.
 */
int ca_proplist_create(ca_proplist **_p) {
    ca_proplist *p;
    ca_return_val_if_fail(_p, CA_ERROR_INVALID);

    if (!(p = ca_new0(ca_proplist, 1)))
        return CA_ERROR_OOM;

    if (!(p->mutex = ca_mutex_new())) {
        ca_free(p);
        return CA_ERROR_OOM;
    }

    *_p = p;

    return CA_SUCCESS;
}

static int _unset(ca_proplist *p, const char *key) {
    ca_prop *prop, *nprop;
    unsigned i;

    ca_return_val_if_fail(p, CA_ERROR_INVALID);
    ca_return_val_if_fail(key, CA_ERROR_INVALID);

    i = calc_hash(key) % N_HASHTABLE;

    nprop = NULL;
    for (prop = p->prop_hashtable[i]; prop; nprop = prop, prop = prop->next_in_slot)
        if (strcmp(prop->key, key) == 0)
            break;

    if (prop) {
        if (nprop)
            nprop->next_in_slot = prop->next_in_slot;
        else
            p->prop_hashtable[i] = prop->next_in_slot;

        if (prop->prev_item)
            prop->prev_item->next_item = prop->next_item;
        else
            p->first_item = prop->next_item;

        if (prop->next_item)
            prop->next_item->prev_item = prop->prev_item;

        ca_free(prop->key);
        ca_free(prop);
    }

    return CA_SUCCESS;
}

/**
 * ca_proplist_sets:
 * @p: The property list to add this key/value pair to
 * @key: The key for this key/value pair
 * @value: The value for this key/value pair
 *
 * Add a new string key/value pair to the property list.
 *
 * Returns: 0 on success, negative error code on error.
 */

int ca_proplist_sets(ca_proplist *p, const char *key, const char *value) {
    ca_return_val_if_fail(p, CA_ERROR_INVALID);
    ca_return_val_if_fail(key, CA_ERROR_INVALID);
    ca_return_val_if_fail(value, CA_ERROR_INVALID);

    return ca_proplist_set(p, key, value, strlen(value)+1);
}

/**
 * ca_proplist_setf:
 * @p: The property list to add this key/value pair to
 * @key: The key for this key/value pair
 * @format: The format string for the value for this key/value pair
 * @...: The parameters for the format string
 *
 * Much like ca_proplist_sets(): add a new string key/value pair to
 * the property list. Takes a standard C format string plus arguments
 * and formats a string of it.
 *
 * Returns: 0 on success, negative error code on error.
 */

int ca_proplist_setf(ca_proplist *p, const char *key, const char *format, ...) {
    int ret;
    char *k;
    ca_prop *prop;
    size_t size = 100;
    unsigned h;

    ca_return_val_if_fail(p, CA_ERROR_INVALID);
    ca_return_val_if_fail(key, CA_ERROR_INVALID);
    ca_return_val_if_fail(format, CA_ERROR_INVALID);

    if (!(k = ca_strdup(key)))
        return CA_ERROR_OOM;

    for (;;) {
        va_list ap;
        int r;

        if (!(prop = ca_malloc(CA_ALIGN(sizeof(ca_prop)) + size))) {
            ca_free(k);
            return CA_ERROR_OOM;
        }


        va_start(ap, format);
        r = vsnprintf(CA_PROP_DATA(prop), size, format, ap);
        va_end(ap);

        ((char*) CA_PROP_DATA(prop))[size-1] = 0;

        if (r > -1 && (size_t) r < size) {
            prop->nbytes = (size_t) r+1;
            break;
        }

        if (r > -1)    /* glibc 2.1 */
            size = (size_t) r+1;
        else           /* glibc 2.0 */
            size *= 2;

        ca_free(prop);
    }

    prop->key = k;

    ca_mutex_lock(p->mutex);

    if ((ret = _unset(p, key)) < 0) {
        ca_free(prop);
        ca_free(k);
        goto finish;
    }

    h = calc_hash(key) % N_HASHTABLE;

    prop->next_in_slot = p->prop_hashtable[h];
    p->prop_hashtable[h] = prop;

    prop->prev_item = NULL;
    prop->next_item = p->first_item;
    p->first_item = prop;

finish:

    ca_mutex_unlock(p->mutex);

    return ret;
}

/**
 * ca_proplist_set:
 * @p: The property list to add this key/value pair to
 * @key: The key for this key/value pair
 * @data: The binary value for this key value pair
 * @nbytes: The size of thebinary value for this key value pair.
 *
 * Add a new binary key/value pair to the property list.
 *
 * Returns: 0 on success, negative error code on error.
 */

int ca_proplist_set(ca_proplist *p, const char *key, const void *data, size_t nbytes) {
    int ret;
    char *k;
    ca_prop *prop;
    unsigned h;

    ca_return_val_if_fail(p, CA_ERROR_INVALID);
    ca_return_val_if_fail(key, CA_ERROR_INVALID);
    ca_return_val_if_fail(!nbytes || data, CA_ERROR_INVALID);

    if (!(k = ca_strdup(key)))
        return CA_ERROR_OOM;

    if (!(prop = ca_malloc(CA_ALIGN(sizeof(ca_prop)) + nbytes))) {
        ca_free(k);
        return CA_ERROR_OOM;
    }

    prop->key = k;
    prop->nbytes = nbytes;
    memcpy(CA_PROP_DATA(prop), data, nbytes);

    ca_mutex_lock(p->mutex);

    if ((ret = _unset(p, key)) < 0) {
        ca_free(prop);
        ca_free(k);
        goto finish;
    }

    h = calc_hash(key) % N_HASHTABLE;

    prop->next_in_slot = p->prop_hashtable[h];
    p->prop_hashtable[h] = prop;

    prop->prev_item = NULL;
    prop->next_item = p->first_item;
    p->first_item = prop;

finish:

    ca_mutex_unlock(p->mutex);

    return ret;
}

/* Not exported, not self-locking */
ca_prop* ca_proplist_get_unlocked(ca_proplist *p, const char *key) {
    ca_prop *prop;
    unsigned i;

    ca_return_val_if_fail(p, NULL);
    ca_return_val_if_fail(key, NULL);

    i = calc_hash(key) % N_HASHTABLE;

    for (prop = p->prop_hashtable[i]; prop; prop = prop->next_in_slot)
        if (strcmp(prop->key, key) == 0)
            return prop;

    return NULL;
}

/* Not exported, not self-locking */
const char* ca_proplist_gets_unlocked(ca_proplist *p, const char *key) {
    ca_prop *prop;

    ca_return_val_if_fail(p, NULL);
    ca_return_val_if_fail(key, NULL);

    if (!(prop = ca_proplist_get_unlocked(p, key)))
        return NULL;

    if (!memchr(CA_PROP_DATA(prop), 0, prop->nbytes))
        return NULL;

    return CA_PROP_DATA(prop);
}

/**
 * ca_proplist_destroy:
 * @p: The property list to destroy
 *
 * Destroys a property list that was created with ca_proplist_create() earlier.
 *
 * Returns: 0 on success, negative error code on error.
 */

int ca_proplist_destroy(ca_proplist *p) {
    ca_prop *prop, *nprop;

    ca_return_val_if_fail(p, CA_ERROR_INVALID);

    for (prop = p->first_item; prop; prop = nprop) {
        nprop = prop->next_item;
        ca_free(prop->key);
        ca_free(prop);
    }

    ca_mutex_free(p->mutex);

    ca_free(p);

    return CA_SUCCESS;
}

static int merge_into(ca_proplist *a, ca_proplist *b) {
    int ret = CA_SUCCESS;
    ca_prop *prop;

    ca_return_val_if_fail(a, CA_ERROR_INVALID);
    ca_return_val_if_fail(b, CA_ERROR_INVALID);

    ca_mutex_lock(b->mutex);

    for (prop = b->first_item; prop; prop = prop->next_item)
        if ((ret = ca_proplist_set(a, prop->key, CA_PROP_DATA(prop), prop->nbytes)) < 0)
            break;

    ca_mutex_unlock(b->mutex);

    return ret;
}

int ca_proplist_merge(ca_proplist **_a, ca_proplist *b, ca_proplist *c) {
    ca_proplist *a;
    int ret;

    ca_return_val_if_fail(_a, CA_ERROR_INVALID);
    ca_return_val_if_fail(b, CA_ERROR_INVALID);
    ca_return_val_if_fail(c, CA_ERROR_INVALID);

    if ((ret = ca_proplist_create(&a)) < 0)
        return ret;

    if ((ret = merge_into(a, b)) < 0 ||
        (ret = merge_into(a, c)) < 0) {
        ca_proplist_destroy(a);
        return ret;
    }

    *_a = a;
    return CA_SUCCESS;
}

ca_bool_t ca_proplist_contains(ca_proplist *p, const char *key) {
    ca_bool_t b;

    ca_return_val_if_fail(p, FALSE);
    ca_return_val_if_fail(key, FALSE);

    ca_mutex_lock(p->mutex);
    b = !!ca_proplist_get_unlocked(p, key);
    ca_mutex_unlock(p->mutex);

    return b;
}

int ca_proplist_merge_ap(ca_proplist *p, va_list ap) {
    int ret;

    ca_return_val_if_fail(p, CA_ERROR_INVALID);

    for (;;) {
        const char *key, *value;

        if (!(key = va_arg(ap, const char*)))
            break;

        if (!(value = va_arg(ap, const char*)))
            return CA_ERROR_INVALID;

        if ((ret = ca_proplist_sets(p, key, value)) < 0)
            return ret;
    }

    return CA_SUCCESS;
}

int ca_proplist_from_ap(ca_proplist **_p, va_list ap) {
    int ret;
    ca_proplist *p;

    ca_return_val_if_fail(_p, CA_ERROR_INVALID);

    if ((ret = ca_proplist_create(&p)) < 0)
        return ret;

    if ((ret = ca_proplist_merge_ap(p, ap)) < 0)
        goto fail;

    *_p = p;

    return CA_SUCCESS;

fail:
    ca_assert_se(ca_proplist_destroy(p) == CA_SUCCESS);

    return ret;
}