]> code.delx.au - pulseaudio/blob - src/pulsecore/props.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / props.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulse/xmalloc.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/macro.h>
31
32 #include "props.h"
33
34 typedef struct pa_property {
35 char *name; /* Points to memory allocated by the property subsystem */
36 void *data; /* Points to memory maintained by the caller */
37 } pa_property;
38
39 /* Allocate a new property object */
40 static pa_property* property_new(const char *name, void *data) {
41 pa_property* p;
42
43 pa_assert(name);
44 pa_assert(data);
45
46 p = pa_xnew(pa_property, 1);
47 p->name = pa_xstrdup(name);
48 p->data = data;
49
50 return p;
51 }
52
53 /* Free a property object */
54 static void property_free(pa_property *p) {
55 pa_assert(p);
56
57 pa_xfree(p->name);
58 pa_xfree(p);
59 }
60
61 void* pa_property_get(pa_core *c, const char *name) {
62 pa_property *p;
63
64 pa_assert(c);
65 pa_assert(name);
66 pa_assert(c->properties);
67
68 if (!(p = pa_hashmap_get(c->properties, name)))
69 return NULL;
70
71 return p->data;
72 }
73
74 int pa_property_set(pa_core *c, const char *name, void *data) {
75 pa_property *p;
76
77 pa_assert(c);
78 pa_assert(name);
79 pa_assert(data);
80 pa_assert(c->properties);
81
82 if (pa_hashmap_get(c->properties, name))
83 return -1;
84
85 p = property_new(name, data);
86 pa_hashmap_put(c->properties, p->name, p);
87 return 0;
88 }
89
90 int pa_property_remove(pa_core *c, const char *name) {
91 pa_property *p;
92
93 pa_assert(c);
94 pa_assert(name);
95 pa_assert(c->properties);
96
97 if (!(p = pa_hashmap_remove(c->properties, name)))
98 return -1;
99
100 property_free(p);
101 return 0;
102 }
103
104 void pa_property_init(pa_core *c) {
105 pa_assert(c);
106
107 c->properties = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
108 }
109
110 void pa_property_cleanup(pa_core *c) {
111 pa_assert(c);
112
113 if (!c->properties)
114 return;
115
116 pa_assert(!pa_hashmap_size(c->properties));
117
118 pa_hashmap_free(c->properties, NULL, NULL);
119 c->properties = NULL;
120
121 }
122
123 void pa_property_dump(pa_core *c, pa_strbuf *s) {
124 void *state = NULL;
125 pa_property *p;
126
127 pa_assert(c);
128 pa_assert(s);
129
130 while ((p = pa_hashmap_iterate(c->properties, &state, NULL)))
131 pa_strbuf_printf(s, "[%s] -> [%p]\n", p->name, p->data);
132 }
133
134 int pa_property_replace(pa_core *c, const char *name, void *data) {
135 pa_assert(c);
136 pa_assert(name);
137
138 pa_property_remove(c, name);
139 return pa_property_set(c, name, data);
140 }