Handle streams separately in tree_add_track()
[cmus.git] / keyval.c
bloba6414af82f091b337ebff64d19686be185a09078
1 #include "keyval.h"
2 #include "xmalloc.h"
4 #include <string.h>
6 struct keyval *keyvals_dup(const struct keyval *keyvals)
8 struct keyval *c;
9 int i;
11 for (i = 0; keyvals[i].key; i++)
12 ; /* nothing */
13 c = xnew(struct keyval, i + 1);
14 for (i = 0; keyvals[i].key; i++) {
15 c[i].key = xstrdup(keyvals[i].key);
16 c[i].val = xstrdup(keyvals[i].val);
18 c[i].key = NULL;
19 c[i].val = NULL;
20 return c;
23 void keyvals_free(struct keyval *keyvals)
25 int i;
27 for (i = 0; keyvals[i].key; i++) {
28 free(keyvals[i].key);
29 free(keyvals[i].val);
31 free(keyvals);
34 const char *keyvals_get_val(const struct keyval *keyvals, const char *key)
36 int i;
38 for (i = 0; keyvals[i].key; i++) {
39 if (strcasecmp(keyvals[i].key, key) == 0)
40 return keyvals[i].val;
42 return NULL;
45 void keyvals_add(struct growing_keyvals *c, const char *key, char *val)
47 int n = c->count + 1;
49 if (n > c->alloc) {
50 n = (n + 3) & ~3;
51 c->keyvals = xrenew(struct keyval, c->keyvals, n);
52 c->alloc = n;
55 c->keyvals[c->count].key = xstrdup(key);
56 c->keyvals[c->count].val = val;
57 c->count++;
60 void keyvals_terminate(struct growing_keyvals *c)
62 int alloc = c->count + 1;
64 if (alloc > c->alloc) {
65 c->keyvals = xrenew(struct keyval, c->keyvals, alloc);
66 c->alloc = alloc;
68 c->keyvals[c->count].key = NULL;
69 c->keyvals[c->count].val = NULL;